blob: 3036d17b47febad5eb573a901ca4d45c1b07afdb [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 Peskinea0c06552019-05-21 15:54:54 +0200207/** \def PSA_KEY_ATTRIBUTES_INIT
208 *
209 * This macro returns a suitable initializer for a key attribute structure
210 * of type #psa_key_attributes_t.
211 */
212#ifdef __DOXYGEN_ONLY__
213/* This is an example definition for documentation purposes.
214 * Implementations should define a suitable value in `crypto_struct.h`.
215 */
216#define PSA_KEY_ATTRIBUTES_INIT {0}
217#endif
218
219/** Return an initial value for a key attributes structure.
220 */
221static psa_key_attributes_t psa_key_attributes_init(void);
222
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200223/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200224 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200225 * If the attribute structure currently declares the key as volatile (which
226 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200227 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200228 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200229 * This function does not access storage, it merely stores the given
230 * value in the structure.
231 * The persistent key will be written to storage when the attribute
232 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200233 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200234 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200235 *
Gilles Peskine20628592019-04-19 19:29:50 +0200236 * This function may be declared as `static` (i.e. without external
237 * linkage). This function may be provided as a function-like macro,
238 * but in this case it must evaluate each of its arguments exactly once.
239 *
240 * \param[out] attributes The attribute structure to write to.
241 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200242 */
243static void psa_set_key_id(psa_key_attributes_t *attributes,
244 psa_key_id_t id);
245
246/** Set the location of a persistent key.
247 *
248 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200249 * with psa_set_key_id(). By default, a key that has a persistent identifier
250 * is stored in the default storage area identifier by
251 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
252 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200253 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200254 * This function does not access storage, it merely stores the given
255 * value in the structure.
256 * The persistent key will be written to storage when the attribute
257 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200258 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200259 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200260 *
261 * This function may be declared as `static` (i.e. without external
262 * linkage). This function may be provided as a function-like macro,
263 * but in this case it must evaluate each of its arguments exactly once.
264 *
265 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200266 * \param lifetime The lifetime for the key.
267 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200268 * key will be volatile, and the key identifier
269 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200270 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200271static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
272 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200273
Gilles Peskine20628592019-04-19 19:29:50 +0200274/** Retrieve the key identifier from key attributes.
275 *
276 * This function may be declared as `static` (i.e. without external
277 * linkage). This function may be provided as a function-like macro,
278 * but in this case it must evaluate its argument exactly once.
279 *
280 * \param[in] attributes The key attribute structure to query.
281 *
282 * \return The persistent identifier stored in the attribute structure.
283 * This value is unspecified if the attribute structure declares
284 * the key as volatile.
285 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200286static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
287
Gilles Peskine20628592019-04-19 19:29:50 +0200288/** Retrieve the lifetime from key attributes.
289 *
290 * This function may be declared as `static` (i.e. without external
291 * linkage). This function may be provided as a function-like macro,
292 * but in this case it must evaluate its argument exactly once.
293 *
294 * \param[in] attributes The key attribute structure to query.
295 *
296 * \return The lifetime value stored in the attribute structure.
297 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200298static psa_key_lifetime_t psa_get_key_lifetime(
299 const psa_key_attributes_t *attributes);
300
Gilles Peskine20628592019-04-19 19:29:50 +0200301/** Declare usage flags for a key.
302 *
303 * Usage flags are part of a key's usage policy. They encode what
304 * kind of operations are permitted on the key. For more details,
305 * refer to the documentation of the type #psa_key_usage_t.
306 *
307 * This function overwrites any usage flags
308 * previously set in \p attributes.
309 *
310 * This function may be declared as `static` (i.e. without external
311 * linkage). This function may be provided as a function-like macro,
312 * but in this case it must evaluate each of its arguments exactly once.
313 *
314 * \param[out] attributes The attribute structure to write to.
315 * \param usage_flags The usage flags to write.
316 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200317static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
318 psa_key_usage_t usage_flags);
319
Gilles Peskine20628592019-04-19 19:29:50 +0200320/** Retrieve the usage flags from key attributes.
321 *
322 * This function may be declared as `static` (i.e. without external
323 * linkage). This function may be provided as a function-like macro,
324 * but in this case it must evaluate its argument exactly once.
325 *
326 * \param[in] attributes The key attribute structure to query.
327 *
328 * \return The usage flags stored in the attribute structure.
329 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200330static psa_key_usage_t psa_get_key_usage_flags(
331 const psa_key_attributes_t *attributes);
332
Gilles Peskine20628592019-04-19 19:29:50 +0200333/** Declare the permitted algorithm policy for a key.
334 *
335 * The permitted algorithm policy of a key encodes which algorithm or
336 * algorithms are permitted to be used with this key.
337 *
338 * This function overwrites any algorithm policy
339 * previously set in \p attributes.
340 *
341 * This function may be declared as `static` (i.e. without external
342 * linkage). This function may be provided as a function-like macro,
343 * but in this case it must evaluate each of its arguments exactly once.
344 *
345 * \param[out] attributes The attribute structure to write to.
346 * \param alg The permitted algorithm policy to write.
347 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200348static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
349 psa_algorithm_t alg);
350
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100351
Gilles Peskine20628592019-04-19 19:29:50 +0200352/** Retrieve the algorithm policy from key 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 its argument exactly once.
357 *
358 * \param[in] attributes The key attribute structure to query.
359 *
360 * \return The algorithm stored in the attribute structure.
361 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200362static psa_algorithm_t psa_get_key_algorithm(
363 const psa_key_attributes_t *attributes);
364
Gilles Peskine20628592019-04-19 19:29:50 +0200365/** Declare the type of a key.
366 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200367 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200368 * previously set in \p attributes.
369 *
370 * This function may be declared as `static` (i.e. without external
371 * linkage). This function may be provided as a function-like macro,
372 * but in this case it must evaluate each of its arguments exactly once.
373 *
374 * \param[out] attributes The attribute structure to write to.
375 * \param type The key type to write.
376 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200377static void psa_set_key_type(psa_key_attributes_t *attributes,
378 psa_key_type_t type);
379
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100380
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200381/** Declare the size of a key.
382 *
383 * This function overwrites any key size previously set in \p attributes.
384 *
385 * This function may be declared as `static` (i.e. without external
386 * linkage). This function may be provided as a function-like macro,
387 * but in this case it must evaluate each of its arguments exactly once.
388 *
389 * \param[out] attributes The attribute structure to write to.
390 * \param bits The key size in bits.
391 */
392static void psa_set_key_bits(psa_key_attributes_t *attributes,
393 size_t bits);
394
Gilles Peskine20628592019-04-19 19:29:50 +0200395/** Retrieve the key type from key attributes.
396 *
397 * This function may be declared as `static` (i.e. without external
398 * linkage). This function may be provided as a function-like macro,
399 * but in this case it must evaluate its argument exactly once.
400 *
401 * \param[in] attributes The key attribute structure to query.
402 *
403 * \return The key type stored in the attribute structure.
404 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200405static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
406
Gilles Peskine20628592019-04-19 19:29:50 +0200407/** Retrieve the key size from key attributes.
408 *
409 * This function may be declared as `static` (i.e. without external
410 * linkage). This function may be provided as a function-like macro,
411 * but in this case it must evaluate its argument exactly once.
412 *
413 * \param[in] attributes The key attribute structure to query.
414 *
415 * \return The key size stored in the attribute structure, in bits.
416 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200417static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
418
Gilles Peskine20628592019-04-19 19:29:50 +0200419/** Retrieve the attributes of a key.
420 *
421 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200422 * psa_reset_key_attributes(). It then copies the attributes of
423 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200424 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200425 * \note This function may allocate memory or other resources.
426 * Once you have called this function on an attribute structure,
427 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200428 *
Gilles Peskine20628592019-04-19 19:29:50 +0200429 * \param[in] handle Handle to the key to query.
430 * \param[in,out] attributes On success, the attributes of the key.
431 * On failure, equivalent to a
432 * freshly-initialized structure.
433 *
434 * \retval #PSA_SUCCESS
435 * \retval #PSA_ERROR_INVALID_HANDLE
436 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
437 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
438 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200439psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
440 psa_key_attributes_t *attributes);
441
Gilles Peskine20628592019-04-19 19:29:50 +0200442/** Reset a key attribute structure to a freshly initialized state.
443 *
444 * You must initialize the attribute structure as described in the
445 * documentation of the type #psa_key_attributes_t before calling this
446 * function. Once the structure has been initialized, you may call this
447 * function at any time.
448 *
449 * This function frees any auxiliary resources that the structure
450 * may contain.
451 *
452 * \param[in,out] attributes The attribute structure to reset.
453 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200454void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200455
Gilles Peskine87a5e562019-04-17 12:28:25 +0200456/**@}*/
457
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100458/** \defgroup key_management Key management
459 * @{
460 */
461
Gilles Peskinef535eb22018-11-30 14:08:36 +0100462/** Open a handle to an existing persistent key.
463 *
Gilles Peskine4754cde2019-05-21 15:56:29 +0200464 * Open a handle to a persistent key. A key is persistent if it was created
465 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
466 * always has a nonzero key identifier, set with psa_set_key_id() when
467 * creating the key. Implementations may provide additional pre-provisioned
468 * keys with identifiers in the range
469 * #PSA_KEY_ID_VENDOR_MIN&ndash;#PSA_KEY_ID_VENDOR_MAX.
470 *
471 * The application must eventually close the handle with psa_close_key()
472 * to release associated resources. If the application dies without calling
473 * psa_close_key(), the implementation should perform the equivalent of a
474 * call to psa_close_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100475 *
Gilles Peskine4a231b82019-05-06 18:56:14 +0200476 * Implementations may provide additional keys that can be opened with
477 * psa_open_key(). Such keys have a key identifier in the vendor range,
478 * as documented in the description of #psa_key_id_t.
479 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100480 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100481 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100482 *
483 * \retval #PSA_SUCCESS
484 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100485 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100486 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200487 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100488 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine225010f2019-05-06 18:44:55 +0200489 * \p id is invalid.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100490 * \retval #PSA_ERROR_NOT_PERMITTED
491 * The specified key exists, but the application does not have the
492 * permission to access it. Note that this specification does not
493 * define any way to create such a key, but it may be possible
494 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200495 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
496 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100497 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200498psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100499 psa_key_handle_t *handle);
500
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100501
Gilles Peskinef535eb22018-11-30 14:08:36 +0100502/** Close a key handle.
503 *
504 * If the handle designates a volatile key, destroy the key material and
505 * free all associated resources, just like psa_destroy_key().
506 *
507 * If the handle designates a persistent key, free all resources associated
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100508 * with the key in volatile memory. The key in persistent storage is
Gilles Peskinef535eb22018-11-30 14:08:36 +0100509 * not affected and can be opened again later with psa_open_key().
510 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100511 * If the key is currently in use in a multipart operation,
512 * the multipart operation is aborted.
513 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100514 * \param handle The key handle to close.
515 *
516 * \retval #PSA_SUCCESS
517 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100518 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100519 */
520psa_status_t psa_close_key(psa_key_handle_t handle);
521
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100522/**@}*/
523
524/** \defgroup import_export Key import and export
525 * @{
526 */
527
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100528/**
529 * \brief Import a key in binary format.
530 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100531 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100532 * documentation of psa_export_public_key() for the format of public keys
533 * and to the documentation of psa_export_key() for the format for
534 * other key types.
535 *
536 * This specification supports a single format for each key type.
537 * Implementations may support other formats as long as the standard
538 * format is supported. Implementations that support other formats
539 * should ensure that the formats are clearly unambiguous so as to
540 * minimize the risk that an invalid input is accidentally interpreted
541 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100543
Gilles Peskine20628592019-04-19 19:29:50 +0200544 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200545 * The key size is always determined from the
546 * \p data buffer.
547 * If the key size in \p attributes is nonzero,
548 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200549 * \param[out] handle On success, a handle to the newly created key.
550 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100551 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200552 * buffer is interpreted according to the type declared
553 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200554 * All implementations must support at least the format
555 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100556 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200557 * the chosen type. Implementations may allow other
558 * formats, but should be conservative: implementations
559 * should err on the side of rejecting content if it
560 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200561 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100562 *
Gilles Peskine28538492018-07-11 17:34:00 +0200563 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100564 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100565 * If the key is persistent, the key material and the key's metadata
566 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200567 * \retval #PSA_ERROR_ALREADY_EXISTS
568 * This is an attempt to create a persistent key, and there is
569 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200570 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200571 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200572 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200573 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200574 * The key attributes, as a whole, are invalid.
575 * \retval #PSA_ERROR_INVALID_ARGUMENT
576 * The key data is not correctly formatted.
577 * \retval #PSA_ERROR_INVALID_ARGUMENT
578 * The size in \p attributes is nonzero and does not match the size
579 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200580 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
581 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
582 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100583 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200584 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200585 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300586 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300587 * The library has not been previously initialized by psa_crypto_init().
588 * It is implementation-dependent whether a failure to initialize
589 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100590 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200591psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100592 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200593 size_t data_length,
594 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100595
596/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100597 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200598 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100599 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200600 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100601 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200602 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100603 * This function also erases any metadata such as policies and frees all
604 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200605 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100606 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607 *
Gilles Peskine28538492018-07-11 17:34:00 +0200608 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100609 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200610 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100611 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200612 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100613 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200614 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200615 * There was an failure in communication with the cryptoprocessor.
616 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200617 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200618 * The storage is corrupted. Implementations shall make a best effort
619 * to erase key material even in this stage, however applications
620 * should be aware that it may be impossible to guarantee that the
621 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200622 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200623 * An unexpected condition which is not a storage corruption or
624 * a communication failure occurred. The cryptoprocessor may have
625 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300626 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300627 * The library has not been previously initialized by psa_crypto_init().
628 * It is implementation-dependent whether a failure to initialize
629 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100630 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100631psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100632
633/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100634 * \brief Export a key in binary format.
635 *
636 * The output of this function can be passed to psa_import_key() to
637 * create an equivalent object.
638 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100639 * If the implementation of psa_import_key() supports other formats
640 * beyond the format specified here, the output from psa_export_key()
641 * must use the representation specified here, not the original
642 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100643 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100644 * For standard key types, the output format is as follows:
645 *
646 * - For symmetric keys (including MAC keys), the format is the
647 * raw bytes of the key.
648 * - For DES, the key data consists of 8 bytes. The parity bits must be
649 * correct.
650 * - For Triple-DES, the format is the concatenation of the
651 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200652 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200653 * is the non-encrypted DER encoding of the representation defined by
654 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
655 * ```
656 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200657 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200658 * modulus INTEGER, -- n
659 * publicExponent INTEGER, -- e
660 * privateExponent INTEGER, -- d
661 * prime1 INTEGER, -- p
662 * prime2 INTEGER, -- q
663 * exponent1 INTEGER, -- d mod (p-1)
664 * exponent2 INTEGER, -- d mod (q-1)
665 * coefficient INTEGER, -- (inverse of q) mod p
666 * }
667 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200668 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200669 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100670 * a representation of the private value as a `ceiling(m/8)`-byte string
671 * where `m` is the bit size associated with the curve, i.e. the bit size
672 * of the order of the curve's coordinate field. This byte string is
673 * in little-endian order for Montgomery curves (curve types
674 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
675 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
676 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100677 * This is the content of the `privateKey` field of the `ECPrivateKey`
678 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200679 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200680 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000681 * format is the representation of the private key `x` as a big-endian byte
682 * string. The length of the byte string is the private key size in bytes
683 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200684 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
685 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100686 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200687 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
688 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100689 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200690 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200691 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200692 * \param[out] data_length On success, the number of bytes
693 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100694 *
Gilles Peskine28538492018-07-11 17:34:00 +0200695 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100696 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200697 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200698 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200699 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100700 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200701 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
702 * The size of the \p data buffer is too small. You can determine a
703 * sufficient buffer size by calling
704 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
705 * where \c type is the key type
706 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200707 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
708 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200709 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300710 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300711 * The library has not been previously initialized by psa_crypto_init().
712 * It is implementation-dependent whether a failure to initialize
713 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100714 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100715psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100716 uint8_t *data,
717 size_t data_size,
718 size_t *data_length);
719
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100720/**
721 * \brief Export a public key or the public part of a key pair in binary format.
722 *
723 * The output of this function can be passed to psa_import_key() to
724 * create an object that is equivalent to the public key.
725 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000726 * This specification supports a single format for each key type.
727 * Implementations may support other formats as long as the standard
728 * format is supported. Implementations that support other formats
729 * should ensure that the formats are clearly unambiguous so as to
730 * minimize the risk that an invalid input is accidentally interpreted
731 * according to a different format.
732 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000733 * For standard key types, the output format is as follows:
734 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
735 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
736 * ```
737 * RSAPublicKey ::= SEQUENCE {
738 * modulus INTEGER, -- n
739 * publicExponent INTEGER } -- e
740 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000741 * - For elliptic curve public keys (key types for which
742 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
743 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
744 * Let `m` be the bit size associated with the curve, i.e. the bit size of
745 * `q` for a curve over `F_q`. The representation consists of:
746 * - The byte 0x04;
747 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
748 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200749 * - For Diffie-Hellman key exchange public keys (key types for which
750 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000751 * the format is the representation of the public key `y = g^x mod p` as a
752 * big-endian byte string. The length of the byte string is the length of the
753 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100754 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200755 * Exporting a public key object or the public part of a key pair is
756 * always permitted, regardless of the key's usage flags.
757 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100758 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200759 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200760 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200761 * \param[out] data_length On success, the number of bytes
762 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100763 *
Gilles Peskine28538492018-07-11 17:34:00 +0200764 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100765 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200766 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200767 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200768 * The key is neither a public key nor a key pair.
769 * \retval #PSA_ERROR_NOT_SUPPORTED
770 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
771 * The size of the \p data buffer is too small. You can determine a
772 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200773 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200774 * where \c type is the key type
775 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200776 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
777 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200778 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300779 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300780 * The library has not been previously initialized by psa_crypto_init().
781 * It is implementation-dependent whether a failure to initialize
782 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100783 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100784psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100785 uint8_t *data,
786 size_t data_size,
787 size_t *data_length);
788
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100789/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100790 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100791 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000792 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100793 * This function is primarily useful to copy a key from one location
794 * to another, since it populates a key using the material from
795 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200796 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100797 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100798 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100799 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200800 * The policy on the source key must have the usage flag
801 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200802 * This flag is sufficient to permit the copy if the key has the lifetime
803 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
804 * Some secure elements do not provide a way to copy a key without
805 * making it extractable from the secure element. If a key is located
806 * in such a secure element, then the key must have both usage flags
807 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
808 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200809 *
Gilles Peskine20628592019-04-19 19:29:50 +0200810 * The resulting key may only be used in a way that conforms to
811 * both the policy of the original key and the policy specified in
812 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100813 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200814 * usage flags on the source policy and the usage flags in \p attributes.
815 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100816 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200817 * - If either of the policies allows an algorithm and the other policy
818 * allows a wildcard-based algorithm policy that includes this algorithm,
819 * the resulting key allows the same algorithm.
820 * - If the policies do not allow any algorithm in common, this function
821 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200822 *
Gilles Peskine20628592019-04-19 19:29:50 +0200823 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100824 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200825 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100826 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200827 * \param[in] attributes The attributes for the new key.
828 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200829 * - The key type and size may be 0. If either is
830 * nonzero, it must match the corresponding
831 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200832 * - The key location (the lifetime and, for
833 * persistent keys, the key identifier) is
834 * used directly.
835 * - The policy constraints (usage flags and
836 * algorithm policy) are combined from
837 * the source key and \p attributes so that
838 * both sets of restrictions apply, as
839 * described in the documentation of this function.
840 * \param[out] target_handle On success, a handle to the newly created key.
841 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200842 *
843 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100844 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200845 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200846 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200847 * This is an attempt to create a persistent key, and there is
848 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200849 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200850 * The lifetime or identifier in \p attributes are invalid.
851 * \retval #PSA_ERROR_INVALID_ARGUMENT
852 * The policy constraints on the source and specified in
853 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200854 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200855 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200856 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100857 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200858 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
859 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100860 * The source key is not exportable and its lifetime does not
861 * allow copying it to the target's lifetime.
862 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
863 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200864 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
865 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200866 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100867 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100868psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200869 const psa_key_attributes_t *attributes,
870 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100871
872/**@}*/
873
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100874/** \defgroup hash Message digests
875 * @{
876 */
877
Gilles Peskine69647a42019-01-14 20:18:12 +0100878/** Calculate the hash (digest) of a message.
879 *
880 * \note To verify the hash of a message against an
881 * expected value, use psa_hash_compare() instead.
882 *
883 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
884 * such that #PSA_ALG_IS_HASH(\p alg) is true).
885 * \param[in] input Buffer containing the message to hash.
886 * \param input_length Size of the \p input buffer in bytes.
887 * \param[out] hash Buffer where the hash is to be written.
888 * \param hash_size Size of the \p hash buffer in bytes.
889 * \param[out] hash_length On success, the number of bytes
890 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100891 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100892 *
893 * \retval #PSA_SUCCESS
894 * Success.
895 * \retval #PSA_ERROR_NOT_SUPPORTED
896 * \p alg is not supported or is not a hash algorithm.
897 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
898 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
899 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200900 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100901 */
902psa_status_t psa_hash_compute(psa_algorithm_t alg,
903 const uint8_t *input,
904 size_t input_length,
905 uint8_t *hash,
906 size_t hash_size,
907 size_t *hash_length);
908
909/** Calculate the hash (digest) of a message and compare it with a
910 * reference value.
911 *
912 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
913 * such that #PSA_ALG_IS_HASH(\p alg) is true).
914 * \param[in] input Buffer containing the message to hash.
915 * \param input_length Size of the \p input buffer in bytes.
916 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100917 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100918 *
919 * \retval #PSA_SUCCESS
920 * The expected hash is identical to the actual hash of the input.
921 * \retval #PSA_ERROR_INVALID_SIGNATURE
922 * The hash of the message was calculated successfully, but it
923 * differs from the expected hash.
924 * \retval #PSA_ERROR_NOT_SUPPORTED
925 * \p alg is not supported or is not a hash algorithm.
926 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
927 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
928 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200929 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100930 */
931psa_status_t psa_hash_compare(psa_algorithm_t alg,
932 const uint8_t *input,
933 size_t input_length,
934 const uint8_t *hash,
935 const size_t hash_length);
936
Gilles Peskine308b91d2018-02-08 09:47:44 +0100937/** The type of the state data structure for multipart hash operations.
938 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000939 * Before calling any function on a hash operation object, the application must
940 * initialize it by any of the following means:
941 * - Set the structure to all-bits-zero, for example:
942 * \code
943 * psa_hash_operation_t operation;
944 * memset(&operation, 0, sizeof(operation));
945 * \endcode
946 * - Initialize the structure to logical zero values, for example:
947 * \code
948 * psa_hash_operation_t operation = {0};
949 * \endcode
950 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
951 * for example:
952 * \code
953 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
954 * \endcode
955 * - Assign the result of the function psa_hash_operation_init()
956 * to the structure, for example:
957 * \code
958 * psa_hash_operation_t operation;
959 * operation = psa_hash_operation_init();
960 * \endcode
961 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100962 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100963 * make any assumptions about the content of this structure except
964 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100965typedef struct psa_hash_operation_s psa_hash_operation_t;
966
Jaeden Amero6a25b412019-01-04 11:47:44 +0000967/** \def PSA_HASH_OPERATION_INIT
968 *
969 * This macro returns a suitable initializer for a hash operation object
970 * of type #psa_hash_operation_t.
971 */
972#ifdef __DOXYGEN_ONLY__
973/* This is an example definition for documentation purposes.
974 * Implementations should define a suitable value in `crypto_struct.h`.
975 */
976#define PSA_HASH_OPERATION_INIT {0}
977#endif
978
979/** Return an initial value for a hash operation object.
980 */
981static psa_hash_operation_t psa_hash_operation_init(void);
982
Gilles Peskinef45adda2019-01-14 18:29:18 +0100983/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100984 *
985 * The sequence of operations to calculate a hash (message digest)
986 * is as follows:
987 * -# Allocate an operation object which will be passed to all the functions
988 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000989 * -# Initialize the operation object with one of the methods described in the
990 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200991 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100992 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100993 * of the message each time. The hash that is calculated is the hash
994 * of the concatenation of these messages in order.
995 * -# To calculate the hash, call psa_hash_finish().
996 * To compare the hash with an expected value, call psa_hash_verify().
997 *
998 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000999 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001000 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001001 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001002 * eventually terminate the operation. The following events terminate an
1003 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001004 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001005 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001006 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001007 * \param[in,out] operation The operation object to set up. It must have
1008 * been initialized as per the documentation for
1009 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001010 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1011 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001012 *
Gilles Peskine28538492018-07-11 17:34:00 +02001013 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001014 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001015 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001016 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001017 * \retval #PSA_ERROR_BAD_STATE
1018 * The operation state is not valid (already set up and not
1019 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001020 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1021 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1022 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001023 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001024 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001025psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001026 psa_algorithm_t alg);
1027
Gilles Peskine308b91d2018-02-08 09:47:44 +01001028/** Add a message fragment to a multipart hash operation.
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 *
1032 * If this function returns an error status, the operation becomes inactive.
1033 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001034 * \param[in,out] operation Active hash operation.
1035 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001036 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001037 *
Gilles Peskine28538492018-07-11 17:34:00 +02001038 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001039 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001040 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001041 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001042 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1043 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1044 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001045 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001046 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001047psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1048 const uint8_t *input,
1049 size_t input_length);
1050
Gilles Peskine308b91d2018-02-08 09:47:44 +01001051/** Finish the calculation of the hash of a message.
1052 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001053 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001054 * This function calculates the hash of the message formed by concatenating
1055 * the inputs passed to preceding calls to psa_hash_update().
1056 *
1057 * When this function returns, the operation becomes inactive.
1058 *
1059 * \warning Applications should not call this function if they expect
1060 * a specific value for the hash. Call psa_hash_verify() instead.
1061 * Beware that comparing integrity or authenticity data such as
1062 * hash values with a function such as \c memcmp is risky
1063 * because the time taken by the comparison may leak information
1064 * about the hashed data which could allow an attacker to guess
1065 * a valid hash and thereby bypass security controls.
1066 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001067 * \param[in,out] operation Active hash operation.
1068 * \param[out] hash Buffer where the hash is to be written.
1069 * \param hash_size Size of the \p hash buffer in bytes.
1070 * \param[out] hash_length On success, the number of bytes
1071 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001072 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001073 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 *
Gilles Peskine28538492018-07-11 17:34:00 +02001075 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001076 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001077 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001078 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001079 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001080 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001081 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001083 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1084 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1085 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001086 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001087 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001088psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1089 uint8_t *hash,
1090 size_t hash_size,
1091 size_t *hash_length);
1092
Gilles Peskine308b91d2018-02-08 09:47:44 +01001093/** Finish the calculation of the hash of a message and compare it with
1094 * an expected value.
1095 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001096 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001097 * This function calculates the hash of the message formed by concatenating
1098 * the inputs passed to preceding calls to psa_hash_update(). It then
1099 * compares the calculated hash with the expected hash passed as a
1100 * parameter to this function.
1101 *
1102 * When this function returns, the operation becomes inactive.
1103 *
Gilles Peskine19067982018-03-20 17:54:53 +01001104 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001105 * comparison between the actual hash and the expected hash is performed
1106 * in constant time.
1107 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001108 * \param[in,out] operation Active hash operation.
1109 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001110 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001111 *
Gilles Peskine28538492018-07-11 17:34:00 +02001112 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001113 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001114 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001115 * The hash of the message was calculated successfully, but it
1116 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001117 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001118 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001119 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1120 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1121 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001122 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001123 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001124psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1125 const uint8_t *hash,
1126 size_t hash_length);
1127
Gilles Peskine308b91d2018-02-08 09:47:44 +01001128/** Abort a hash operation.
1129 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001130 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001131 * \p operation structure itself. Once aborted, the operation object
1132 * can be reused for another operation by calling
1133 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001134 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001135 * You may call this function any time after the operation object has
1136 * been initialized by any of the following methods:
1137 * - A call to psa_hash_setup(), whether it succeeds or not.
1138 * - Initializing the \c struct to all-bits-zero.
1139 * - Initializing the \c struct to logical zeros, e.g.
1140 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001141 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001142 * In particular, calling psa_hash_abort() after the operation has been
1143 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1144 * psa_hash_verify() is safe and has no effect.
1145 *
1146 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001147 *
Gilles Peskine28538492018-07-11 17:34:00 +02001148 * \retval #PSA_SUCCESS
1149 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001150 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001151 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1152 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001153 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001154 */
1155psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001156
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001157/** Clone a hash operation.
1158 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001159 * This function copies the state of an ongoing hash operation to
1160 * a new operation object. In other words, this function is equivalent
1161 * to calling psa_hash_setup() on \p target_operation with the same
1162 * algorithm that \p source_operation was set up for, then
1163 * psa_hash_update() on \p target_operation with the same input that
1164 * that was passed to \p source_operation. After this function returns, the
1165 * two objects are independent, i.e. subsequent calls involving one of
1166 * the objects do not affect the other object.
1167 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001168 * \param[in] source_operation The active hash operation to clone.
1169 * \param[in,out] target_operation The operation object to set up.
1170 * It must be initialized but not active.
1171 *
1172 * \retval #PSA_SUCCESS
1173 * \retval #PSA_ERROR_BAD_STATE
1174 * \p source_operation is not an active hash operation.
1175 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001176 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001177 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1178 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001179 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001180 */
1181psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1182 psa_hash_operation_t *target_operation);
1183
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001184/**@}*/
1185
Gilles Peskine8c9def32018-02-08 10:02:12 +01001186/** \defgroup MAC Message authentication codes
1187 * @{
1188 */
1189
Gilles Peskine69647a42019-01-14 20:18:12 +01001190/** Calculate the MAC (message authentication code) of a message.
1191 *
1192 * \note To verify the MAC of a message against an
1193 * expected value, use psa_mac_verify() instead.
1194 * Beware that comparing integrity or authenticity data such as
1195 * MAC values with a function such as \c memcmp is risky
1196 * because the time taken by the comparison may leak information
1197 * about the MAC value which could allow an attacker to guess
1198 * a valid MAC and thereby bypass security controls.
1199 *
1200 * \param handle Handle to the key to use for the operation.
1201 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001202 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001203 * \param[in] input Buffer containing the input message.
1204 * \param input_length Size of the \p input buffer in bytes.
1205 * \param[out] mac Buffer where the MAC value is to be written.
1206 * \param mac_size Size of the \p mac buffer in bytes.
1207 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001208 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001209 *
1210 * \retval #PSA_SUCCESS
1211 * Success.
1212 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001213 * \retval #PSA_ERROR_NOT_PERMITTED
1214 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001215 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001216 * \retval #PSA_ERROR_NOT_SUPPORTED
1217 * \p alg is not supported or is not a MAC algorithm.
1218 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1219 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1220 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001221 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001222 * \retval #PSA_ERROR_BAD_STATE
1223 * The library has not been previously initialized by psa_crypto_init().
1224 * It is implementation-dependent whether a failure to initialize
1225 * results in this error code.
1226 */
1227psa_status_t psa_mac_compute(psa_key_handle_t handle,
1228 psa_algorithm_t alg,
1229 const uint8_t *input,
1230 size_t input_length,
1231 uint8_t *mac,
1232 size_t mac_size,
1233 size_t *mac_length);
1234
1235/** Calculate the MAC of a message and compare it with a reference value.
1236 *
1237 * \param handle Handle to the key to use for the operation.
1238 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001239 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001240 * \param[in] input Buffer containing the input message.
1241 * \param input_length Size of the \p input buffer in bytes.
1242 * \param[out] mac Buffer containing the expected MAC value.
1243 * \param mac_length Size of the \p mac buffer in bytes.
1244 *
1245 * \retval #PSA_SUCCESS
1246 * The expected MAC is identical to the actual MAC of the input.
1247 * \retval #PSA_ERROR_INVALID_SIGNATURE
1248 * The MAC of the message was calculated successfully, but it
1249 * differs from the expected value.
1250 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001251 * \retval #PSA_ERROR_NOT_PERMITTED
1252 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001253 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001254 * \retval #PSA_ERROR_NOT_SUPPORTED
1255 * \p alg is not supported or is not a MAC algorithm.
1256 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1257 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1258 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001259 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001260 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001261psa_status_t psa_mac_verify(psa_key_handle_t handle,
1262 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001263 const uint8_t *input,
1264 size_t input_length,
1265 const uint8_t *mac,
1266 const size_t mac_length);
1267
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001268/** The type of the state data structure for multipart MAC operations.
1269 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001270 * Before calling any function on a MAC operation object, the application must
1271 * initialize it by any of the following means:
1272 * - Set the structure to all-bits-zero, for example:
1273 * \code
1274 * psa_mac_operation_t operation;
1275 * memset(&operation, 0, sizeof(operation));
1276 * \endcode
1277 * - Initialize the structure to logical zero values, for example:
1278 * \code
1279 * psa_mac_operation_t operation = {0};
1280 * \endcode
1281 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1282 * for example:
1283 * \code
1284 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1285 * \endcode
1286 * - Assign the result of the function psa_mac_operation_init()
1287 * to the structure, for example:
1288 * \code
1289 * psa_mac_operation_t operation;
1290 * operation = psa_mac_operation_init();
1291 * \endcode
1292 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001293 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001294 * make any assumptions about the content of this structure except
1295 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001296typedef struct psa_mac_operation_s psa_mac_operation_t;
1297
Jaeden Amero769ce272019-01-04 11:48:03 +00001298/** \def PSA_MAC_OPERATION_INIT
1299 *
1300 * This macro returns a suitable initializer for a MAC operation object of type
1301 * #psa_mac_operation_t.
1302 */
1303#ifdef __DOXYGEN_ONLY__
1304/* This is an example definition for documentation purposes.
1305 * Implementations should define a suitable value in `crypto_struct.h`.
1306 */
1307#define PSA_MAC_OPERATION_INIT {0}
1308#endif
1309
1310/** Return an initial value for a MAC operation object.
1311 */
1312static psa_mac_operation_t psa_mac_operation_init(void);
1313
Gilles Peskinef45adda2019-01-14 18:29:18 +01001314/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001315 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001316 * This function sets up the calculation of the MAC
1317 * (message authentication code) of a byte string.
1318 * To verify the MAC of a message against an
1319 * expected value, use psa_mac_verify_setup() instead.
1320 *
1321 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001322 * -# Allocate an operation object which will be passed to all the functions
1323 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001324 * -# Initialize the operation object with one of the methods described in the
1325 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001326 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001327 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1328 * of the message each time. The MAC that is calculated is the MAC
1329 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001330 * -# At the end of the message, call psa_mac_sign_finish() to finish
1331 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001332 *
1333 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001334 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001335 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001336 * After a successful call to psa_mac_sign_setup(), the application must
1337 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001338 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001339 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001340 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001341 * \param[in,out] operation The operation object to set up. It must have
1342 * been initialized as per the documentation for
1343 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001344 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001345 * It must remain valid until the operation
1346 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001347 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001348 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001349 *
Gilles Peskine28538492018-07-11 17:34:00 +02001350 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001351 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001352 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001353 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001354 * \retval #PSA_ERROR_NOT_PERMITTED
1355 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001356 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001357 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001358 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001359 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1360 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1361 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001362 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001363 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001364 * The operation state is not valid (already set up and not
1365 * subsequently completed).
1366 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001367 * The library has not been previously initialized by psa_crypto_init().
1368 * It is implementation-dependent whether a failure to initialize
1369 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001370 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001371psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001372 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001373 psa_algorithm_t alg);
1374
Gilles Peskinef45adda2019-01-14 18:29:18 +01001375/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001376 *
1377 * This function sets up the verification of the MAC
1378 * (message authentication code) of a byte string against an expected value.
1379 *
1380 * The sequence of operations to verify a MAC is as follows:
1381 * -# Allocate an operation object which will be passed to all the functions
1382 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001383 * -# Initialize the operation object with one of the methods described in the
1384 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001385 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001386 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1387 * of the message each time. The MAC that is calculated is the MAC
1388 * of the concatenation of these messages in order.
1389 * -# At the end of the message, call psa_mac_verify_finish() to finish
1390 * calculating the actual MAC of the message and verify it against
1391 * the expected value.
1392 *
1393 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001394 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001395 *
1396 * After a successful call to psa_mac_verify_setup(), the application must
1397 * eventually terminate the operation through one of the following methods:
1398 * - A failed call to psa_mac_update().
1399 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1400 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001401 * \param[in,out] operation The operation object to set up. It must have
1402 * been initialized as per the documentation for
1403 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001404 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001405 * It must remain valid until the operation
1406 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001407 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1408 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001409 *
Gilles Peskine28538492018-07-11 17:34:00 +02001410 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001411 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001412 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001413 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001414 * \retval #PSA_ERROR_NOT_PERMITTED
1415 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001416 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001417 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001418 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001419 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1420 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1421 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001422 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001423 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001424 * The operation state is not valid (already set up and not
1425 * subsequently completed).
1426 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001427 * The library has not been previously initialized by psa_crypto_init().
1428 * It is implementation-dependent whether a failure to initialize
1429 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001430 */
1431psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001432 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001433 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001434
Gilles Peskinedcd14942018-07-12 00:30:52 +02001435/** Add a message fragment to a multipart MAC operation.
1436 *
1437 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1438 * before calling this function.
1439 *
1440 * If this function returns an error status, the operation becomes inactive.
1441 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001442 * \param[in,out] operation Active MAC operation.
1443 * \param[in] input Buffer containing the message fragment to add to
1444 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001445 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001446 *
1447 * \retval #PSA_SUCCESS
1448 * Success.
1449 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001450 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001451 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1452 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1453 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001454 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001455 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001456psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1457 const uint8_t *input,
1458 size_t input_length);
1459
Gilles Peskinedcd14942018-07-12 00:30:52 +02001460/** Finish the calculation of the MAC of a message.
1461 *
1462 * The application must call psa_mac_sign_setup() before calling this function.
1463 * This function calculates the MAC of the message formed by concatenating
1464 * the inputs passed to preceding calls to psa_mac_update().
1465 *
1466 * When this function returns, the operation becomes inactive.
1467 *
1468 * \warning Applications should not call this function if they expect
1469 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1470 * Beware that comparing integrity or authenticity data such as
1471 * MAC values with a function such as \c memcmp is risky
1472 * because the time taken by the comparison may leak information
1473 * about the MAC value which could allow an attacker to guess
1474 * a valid MAC and thereby bypass security controls.
1475 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001476 * \param[in,out] operation Active MAC operation.
1477 * \param[out] mac Buffer where the MAC value is to be written.
1478 * \param mac_size Size of the \p mac buffer in bytes.
1479 * \param[out] mac_length On success, the number of bytes
1480 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001481 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001482 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001483 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001484 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001485 *
1486 * \retval #PSA_SUCCESS
1487 * Success.
1488 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001489 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001490 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001491 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001492 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1493 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1494 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1495 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001496 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001497 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001498psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1499 uint8_t *mac,
1500 size_t mac_size,
1501 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001502
Gilles Peskinedcd14942018-07-12 00:30:52 +02001503/** Finish the calculation of the MAC of a message and compare it with
1504 * an expected value.
1505 *
1506 * The application must call psa_mac_verify_setup() before calling this function.
1507 * This function calculates the MAC of the message formed by concatenating
1508 * the inputs passed to preceding calls to psa_mac_update(). It then
1509 * compares the calculated MAC with the expected MAC passed as a
1510 * parameter to this function.
1511 *
1512 * When this function returns, the operation becomes inactive.
1513 *
1514 * \note Implementations shall make the best effort to ensure that the
1515 * comparison between the actual MAC and the expected MAC is performed
1516 * in constant time.
1517 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001518 * \param[in,out] operation Active MAC operation.
1519 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001520 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001521 *
1522 * \retval #PSA_SUCCESS
1523 * The expected MAC is identical to the actual MAC of the message.
1524 * \retval #PSA_ERROR_INVALID_SIGNATURE
1525 * The MAC of the message was calculated successfully, but it
1526 * differs from the expected MAC.
1527 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001528 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001529 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1530 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1531 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001532 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001533 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001534psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1535 const uint8_t *mac,
1536 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001537
Gilles Peskinedcd14942018-07-12 00:30:52 +02001538/** Abort a MAC operation.
1539 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001540 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001541 * \p operation structure itself. Once aborted, the operation object
1542 * can be reused for another operation by calling
1543 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001544 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001545 * You may call this function any time after the operation object has
1546 * been initialized by any of the following methods:
1547 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1548 * it succeeds or not.
1549 * - Initializing the \c struct to all-bits-zero.
1550 * - Initializing the \c struct to logical zeros, e.g.
1551 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001552 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001553 * In particular, calling psa_mac_abort() after the operation has been
1554 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1555 * psa_mac_verify_finish() is safe and has no effect.
1556 *
1557 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001558 *
1559 * \retval #PSA_SUCCESS
1560 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001561 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1563 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001564 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001565 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001566psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1567
1568/**@}*/
1569
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001570/** \defgroup cipher Symmetric ciphers
1571 * @{
1572 */
1573
Gilles Peskine69647a42019-01-14 20:18:12 +01001574/** Encrypt a message using a symmetric cipher.
1575 *
1576 * This function encrypts a message with a random IV (initialization
1577 * vector).
1578 *
1579 * \param handle Handle to the key to use for the operation.
1580 * It must remain valid until the operation
1581 * terminates.
1582 * \param alg The cipher algorithm to compute
1583 * (\c PSA_ALG_XXX value such that
1584 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1585 * \param[in] input Buffer containing the message to encrypt.
1586 * \param input_length Size of the \p input buffer in bytes.
1587 * \param[out] output Buffer where the output is to be written.
1588 * The output contains the IV followed by
1589 * the ciphertext proper.
1590 * \param output_size Size of the \p output buffer in bytes.
1591 * \param[out] output_length On success, the number of bytes
1592 * that make up the output.
1593 *
1594 * \retval #PSA_SUCCESS
1595 * Success.
1596 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001597 * \retval #PSA_ERROR_NOT_PERMITTED
1598 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001599 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001600 * \retval #PSA_ERROR_NOT_SUPPORTED
1601 * \p alg is not supported or is not a cipher algorithm.
1602 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1603 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1604 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1605 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001606 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001607 */
1608psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1609 psa_algorithm_t alg,
1610 const uint8_t *input,
1611 size_t input_length,
1612 uint8_t *output,
1613 size_t output_size,
1614 size_t *output_length);
1615
1616/** Decrypt a message using a symmetric cipher.
1617 *
1618 * This function decrypts a message encrypted with a symmetric cipher.
1619 *
1620 * \param handle Handle to the key to use for the operation.
1621 * It must remain valid until the operation
1622 * terminates.
1623 * \param alg The cipher algorithm to compute
1624 * (\c PSA_ALG_XXX value such that
1625 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1626 * \param[in] input Buffer containing the message to decrypt.
1627 * This consists of the IV followed by the
1628 * ciphertext proper.
1629 * \param input_length Size of the \p input buffer in bytes.
1630 * \param[out] output Buffer where the plaintext is to be written.
1631 * \param output_size Size of the \p output buffer in bytes.
1632 * \param[out] output_length On success, the number of bytes
1633 * that make up the output.
1634 *
1635 * \retval #PSA_SUCCESS
1636 * Success.
1637 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001638 * \retval #PSA_ERROR_NOT_PERMITTED
1639 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001640 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001641 * \retval #PSA_ERROR_NOT_SUPPORTED
1642 * \p alg is not supported or is not a cipher algorithm.
1643 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1644 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1645 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1646 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001647 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001648 */
1649psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1650 psa_algorithm_t alg,
1651 const uint8_t *input,
1652 size_t input_length,
1653 uint8_t *output,
1654 size_t output_size,
1655 size_t *output_length);
1656
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001657/** The type of the state data structure for multipart cipher operations.
1658 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001659 * Before calling any function on a cipher operation object, the application
1660 * must initialize it by any of the following means:
1661 * - Set the structure to all-bits-zero, for example:
1662 * \code
1663 * psa_cipher_operation_t operation;
1664 * memset(&operation, 0, sizeof(operation));
1665 * \endcode
1666 * - Initialize the structure to logical zero values, for example:
1667 * \code
1668 * psa_cipher_operation_t operation = {0};
1669 * \endcode
1670 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1671 * for example:
1672 * \code
1673 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1674 * \endcode
1675 * - Assign the result of the function psa_cipher_operation_init()
1676 * to the structure, for example:
1677 * \code
1678 * psa_cipher_operation_t operation;
1679 * operation = psa_cipher_operation_init();
1680 * \endcode
1681 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001682 * This is an implementation-defined \c struct. Applications should not
1683 * make any assumptions about the content of this structure except
1684 * as directed by the documentation of a specific implementation. */
1685typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1686
Jaeden Amero5bae2272019-01-04 11:48:27 +00001687/** \def PSA_CIPHER_OPERATION_INIT
1688 *
1689 * This macro returns a suitable initializer for a cipher operation object of
1690 * type #psa_cipher_operation_t.
1691 */
1692#ifdef __DOXYGEN_ONLY__
1693/* This is an example definition for documentation purposes.
1694 * Implementations should define a suitable value in `crypto_struct.h`.
1695 */
1696#define PSA_CIPHER_OPERATION_INIT {0}
1697#endif
1698
1699/** Return an initial value for a cipher operation object.
1700 */
1701static psa_cipher_operation_t psa_cipher_operation_init(void);
1702
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001703/** Set the key for a multipart symmetric encryption operation.
1704 *
1705 * The sequence of operations to encrypt a message with a symmetric cipher
1706 * is as follows:
1707 * -# Allocate an operation object which will be passed to all the functions
1708 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001709 * -# Initialize the operation object with one of the methods described in the
1710 * documentation for #psa_cipher_operation_t, e.g.
1711 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001712 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001713 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001714 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001715 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001716 * requires a specific IV value.
1717 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1718 * of the message each time.
1719 * -# Call psa_cipher_finish().
1720 *
1721 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001722 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001723 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001724 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001725 * eventually terminate the operation. The following events terminate an
1726 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001727 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001728 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001729 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001730 * \param[in,out] operation The operation object to set up. It must have
1731 * been initialized as per the documentation for
1732 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001733 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001734 * It must remain valid until the operation
1735 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001736 * \param alg The cipher algorithm to compute
1737 * (\c PSA_ALG_XXX value such that
1738 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001739 *
Gilles Peskine28538492018-07-11 17:34:00 +02001740 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001741 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001742 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001743 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001744 * \retval #PSA_ERROR_NOT_PERMITTED
1745 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001746 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001747 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001748 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001749 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1750 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1751 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001752 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001753 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001754 * The operation state is not valid (already set up and not
1755 * subsequently completed).
1756 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001757 * The library has not been previously initialized by psa_crypto_init().
1758 * It is implementation-dependent whether a failure to initialize
1759 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001760 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001761psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001762 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001763 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001764
1765/** Set the key for a multipart symmetric decryption operation.
1766 *
1767 * The sequence of operations to decrypt a message with a symmetric cipher
1768 * is as follows:
1769 * -# Allocate an operation object which will be passed to all the functions
1770 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001771 * -# Initialize the operation object with one of the methods described in the
1772 * documentation for #psa_cipher_operation_t, e.g.
1773 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001774 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001775 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001776 * decryption. If the IV is prepended to the ciphertext, you can call
1777 * psa_cipher_update() on a buffer containing the IV followed by the
1778 * beginning of the message.
1779 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1780 * of the message each time.
1781 * -# Call psa_cipher_finish().
1782 *
1783 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001784 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001785 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001786 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001787 * eventually terminate the operation. The following events terminate an
1788 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001789 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001790 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001791 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001792 * \param[in,out] operation The operation object to set up. It must have
1793 * been initialized as per the documentation for
1794 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001795 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001796 * It must remain valid until the operation
1797 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001798 * \param alg The cipher algorithm to compute
1799 * (\c PSA_ALG_XXX value such that
1800 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001801 *
Gilles Peskine28538492018-07-11 17:34:00 +02001802 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001803 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001804 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001805 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001806 * \retval #PSA_ERROR_NOT_PERMITTED
1807 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001808 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001809 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001810 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001811 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1812 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1813 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001814 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001815 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001816 * The operation state is not valid (already set up and not
1817 * subsequently completed).
1818 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001819 * The library has not been previously initialized by psa_crypto_init().
1820 * It is implementation-dependent whether a failure to initialize
1821 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001822 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001823psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001824 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001825 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001826
Gilles Peskinedcd14942018-07-12 00:30:52 +02001827/** Generate an IV for a symmetric encryption operation.
1828 *
1829 * This function generates a random IV (initialization vector), nonce
1830 * or initial counter value for the encryption operation as appropriate
1831 * for the chosen algorithm, key type and key size.
1832 *
1833 * The application must call psa_cipher_encrypt_setup() before
1834 * calling this function.
1835 *
1836 * If this function returns an error status, the operation becomes inactive.
1837 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001838 * \param[in,out] operation Active cipher operation.
1839 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001840 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001841 * \param[out] iv_length On success, the number of bytes of the
1842 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001843 *
1844 * \retval #PSA_SUCCESS
1845 * Success.
1846 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001847 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001848 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001849 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001850 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1851 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1852 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001853 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001854 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001855psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1856 unsigned char *iv,
1857 size_t iv_size,
1858 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001859
Gilles Peskinedcd14942018-07-12 00:30:52 +02001860/** Set the IV for a symmetric encryption or decryption operation.
1861 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001862 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001863 * or initial counter value for the encryption or decryption operation.
1864 *
1865 * The application must call psa_cipher_encrypt_setup() before
1866 * calling this function.
1867 *
1868 * If this function returns an error status, the operation becomes inactive.
1869 *
1870 * \note When encrypting, applications should use psa_cipher_generate_iv()
1871 * instead of this function, unless implementing a protocol that requires
1872 * a non-random IV.
1873 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001874 * \param[in,out] operation Active cipher operation.
1875 * \param[in] iv Buffer containing the IV to use.
1876 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001877 *
1878 * \retval #PSA_SUCCESS
1879 * Success.
1880 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001881 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001882 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001883 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001884 * or the chosen algorithm does not use an IV.
1885 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1886 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1887 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001888 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001889 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001890psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1891 const unsigned char *iv,
1892 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001893
Gilles Peskinedcd14942018-07-12 00:30:52 +02001894/** Encrypt or decrypt a message fragment in an active cipher operation.
1895 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001896 * Before calling this function, you must:
1897 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1898 * The choice of setup function determines whether this function
1899 * encrypts or decrypts its input.
1900 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1901 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001902 *
1903 * If this function returns an error status, the operation becomes inactive.
1904 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001905 * \param[in,out] operation Active cipher operation.
1906 * \param[in] input Buffer containing the message fragment to
1907 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001908 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001909 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001910 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001911 * \param[out] output_length On success, the number of bytes
1912 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001913 *
1914 * \retval #PSA_SUCCESS
1915 * Success.
1916 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001917 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001918 * not set, or already completed).
1919 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1920 * The size of the \p output buffer is too small.
1921 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1922 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1923 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001924 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001925 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001926psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1927 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001928 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001929 unsigned char *output,
1930 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001931 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001932
Gilles Peskinedcd14942018-07-12 00:30:52 +02001933/** Finish encrypting or decrypting a message in a cipher operation.
1934 *
1935 * The application must call psa_cipher_encrypt_setup() or
1936 * psa_cipher_decrypt_setup() before calling this function. The choice
1937 * of setup function determines whether this function encrypts or
1938 * decrypts its input.
1939 *
1940 * This function finishes the encryption or decryption of the message
1941 * formed by concatenating the inputs passed to preceding calls to
1942 * psa_cipher_update().
1943 *
1944 * When this function returns, the operation becomes inactive.
1945 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001946 * \param[in,out] operation Active cipher operation.
1947 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001948 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001949 * \param[out] output_length On success, the number of bytes
1950 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001951 *
1952 * \retval #PSA_SUCCESS
1953 * Success.
1954 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001955 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001956 * not set, or already completed).
1957 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1958 * The size of the \p output buffer is too small.
1959 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1960 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1961 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001962 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001963 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001964psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001965 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001966 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001967 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001968
Gilles Peskinedcd14942018-07-12 00:30:52 +02001969/** Abort a cipher operation.
1970 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001971 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001972 * \p operation structure itself. Once aborted, the operation object
1973 * can be reused for another operation by calling
1974 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001975 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001976 * You may call this function any time after the operation object has
1977 * been initialized by any of the following methods:
1978 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1979 * whether it succeeds or not.
1980 * - Initializing the \c struct to all-bits-zero.
1981 * - Initializing the \c struct to logical zeros, e.g.
1982 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001983 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001984 * In particular, calling psa_cipher_abort() after the operation has been
1985 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1986 * is safe and has no effect.
1987 *
1988 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001989 *
1990 * \retval #PSA_SUCCESS
1991 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001992 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001993 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1994 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001995 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001996 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001997psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1998
1999/**@}*/
2000
Gilles Peskine3b555712018-03-03 21:27:57 +01002001/** \defgroup aead Authenticated encryption with associated data (AEAD)
2002 * @{
2003 */
2004
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002005/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002006 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002007 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002008 * \param alg The AEAD algorithm to compute
2009 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002010 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002011 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002012 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002013 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002014 * but not encrypted.
2015 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002016 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002017 * encrypted.
2018 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002019 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002020 * encrypted data. The additional data is not
2021 * part of this output. For algorithms where the
2022 * encrypted data and the authentication tag
2023 * are defined as separate outputs, the
2024 * authentication tag is appended to the
2025 * encrypted data.
2026 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2027 * This must be at least
2028 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2029 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002030 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002031 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002032 *
Gilles Peskine28538492018-07-11 17:34:00 +02002033 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002034 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002035 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002036 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002037 * \retval #PSA_ERROR_NOT_PERMITTED
2038 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002039 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002040 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002041 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002042 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2043 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2044 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002045 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002046 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002047 * The library has not been previously initialized by psa_crypto_init().
2048 * It is implementation-dependent whether a failure to initialize
2049 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002050 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002051psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002052 psa_algorithm_t alg,
2053 const uint8_t *nonce,
2054 size_t nonce_length,
2055 const uint8_t *additional_data,
2056 size_t additional_data_length,
2057 const uint8_t *plaintext,
2058 size_t plaintext_length,
2059 uint8_t *ciphertext,
2060 size_t ciphertext_size,
2061 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002062
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002063/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002064 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002065 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002066 * \param alg The AEAD algorithm to compute
2067 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002068 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002069 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002070 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002071 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002072 * but not encrypted.
2073 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002074 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002075 * encrypted. For algorithms where the
2076 * encrypted data and the authentication tag
2077 * are defined as separate inputs, the buffer
2078 * must contain the encrypted data followed
2079 * by the authentication tag.
2080 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002081 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002082 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2083 * This must be at least
2084 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2085 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002086 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002087 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002088 *
Gilles Peskine28538492018-07-11 17:34:00 +02002089 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002090 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002091 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002092 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002093 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002094 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002095 * \retval #PSA_ERROR_NOT_PERMITTED
2096 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002097 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002098 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002099 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002100 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2101 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2102 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002103 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002104 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002105 * The library has not been previously initialized by psa_crypto_init().
2106 * It is implementation-dependent whether a failure to initialize
2107 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002108 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002109psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002110 psa_algorithm_t alg,
2111 const uint8_t *nonce,
2112 size_t nonce_length,
2113 const uint8_t *additional_data,
2114 size_t additional_data_length,
2115 const uint8_t *ciphertext,
2116 size_t ciphertext_length,
2117 uint8_t *plaintext,
2118 size_t plaintext_size,
2119 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002120
Gilles Peskine30a9e412019-01-14 18:36:12 +01002121/** The type of the state data structure for multipart AEAD operations.
2122 *
2123 * Before calling any function on an AEAD operation object, the application
2124 * must initialize it by any of the following means:
2125 * - Set the structure to all-bits-zero, for example:
2126 * \code
2127 * psa_aead_operation_t operation;
2128 * memset(&operation, 0, sizeof(operation));
2129 * \endcode
2130 * - Initialize the structure to logical zero values, for example:
2131 * \code
2132 * psa_aead_operation_t operation = {0};
2133 * \endcode
2134 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2135 * for example:
2136 * \code
2137 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2138 * \endcode
2139 * - Assign the result of the function psa_aead_operation_init()
2140 * to the structure, for example:
2141 * \code
2142 * psa_aead_operation_t operation;
2143 * operation = psa_aead_operation_init();
2144 * \endcode
2145 *
2146 * This is an implementation-defined \c struct. Applications should not
2147 * make any assumptions about the content of this structure except
2148 * as directed by the documentation of a specific implementation. */
2149typedef struct psa_aead_operation_s psa_aead_operation_t;
2150
2151/** \def PSA_AEAD_OPERATION_INIT
2152 *
2153 * This macro returns a suitable initializer for an AEAD operation object of
2154 * type #psa_aead_operation_t.
2155 */
2156#ifdef __DOXYGEN_ONLY__
2157/* This is an example definition for documentation purposes.
2158 * Implementations should define a suitable value in `crypto_struct.h`.
2159 */
2160#define PSA_AEAD_OPERATION_INIT {0}
2161#endif
2162
2163/** Return an initial value for an AEAD operation object.
2164 */
2165static psa_aead_operation_t psa_aead_operation_init(void);
2166
2167/** Set the key for a multipart authenticated encryption operation.
2168 *
2169 * The sequence of operations to encrypt a message with authentication
2170 * is as follows:
2171 * -# Allocate an operation object which will be passed to all the functions
2172 * listed here.
2173 * -# Initialize the operation object with one of the methods described in the
2174 * documentation for #psa_aead_operation_t, e.g.
2175 * PSA_AEAD_OPERATION_INIT.
2176 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002177 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2178 * inputs to the subsequent calls to psa_aead_update_ad() and
2179 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2180 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002181 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2182 * generate or set the nonce. You should use
2183 * psa_aead_generate_nonce() unless the protocol you are implementing
2184 * requires a specific nonce value.
2185 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2186 * of the non-encrypted additional authenticated data each time.
2187 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002188 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002189 * -# Call psa_aead_finish().
2190 *
2191 * The application may call psa_aead_abort() at any time after the operation
2192 * has been initialized.
2193 *
2194 * After a successful call to psa_aead_encrypt_setup(), the application must
2195 * eventually terminate the operation. The following events terminate an
2196 * operation:
2197 * - A failed call to any of the \c psa_aead_xxx functions.
2198 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2199 *
2200 * \param[in,out] operation The operation object to set up. It must have
2201 * been initialized as per the documentation for
2202 * #psa_aead_operation_t and not yet in use.
2203 * \param handle Handle to the key to use for the operation.
2204 * It must remain valid until the operation
2205 * terminates.
2206 * \param alg The AEAD algorithm to compute
2207 * (\c PSA_ALG_XXX value such that
2208 * #PSA_ALG_IS_AEAD(\p alg) is true).
2209 *
2210 * \retval #PSA_SUCCESS
2211 * Success.
2212 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002213 * \retval #PSA_ERROR_NOT_PERMITTED
2214 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002215 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002216 * \retval #PSA_ERROR_NOT_SUPPORTED
2217 * \p alg is not supported or is not an AEAD algorithm.
2218 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2219 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2220 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002221 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002222 * \retval #PSA_ERROR_BAD_STATE
2223 * The library has not been previously initialized by psa_crypto_init().
2224 * It is implementation-dependent whether a failure to initialize
2225 * results in this error code.
2226 */
2227psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2228 psa_key_handle_t handle,
2229 psa_algorithm_t alg);
2230
2231/** Set the key for a multipart authenticated decryption operation.
2232 *
2233 * The sequence of operations to decrypt a message with authentication
2234 * is as follows:
2235 * -# Allocate an operation object which will be passed to all the functions
2236 * listed here.
2237 * -# Initialize the operation object with one of the methods described in the
2238 * documentation for #psa_aead_operation_t, e.g.
2239 * PSA_AEAD_OPERATION_INIT.
2240 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002241 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2242 * inputs to the subsequent calls to psa_aead_update_ad() and
2243 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2244 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002245 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2246 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2247 * of the non-encrypted additional authenticated data each time.
2248 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002249 * of the ciphertext to decrypt each time.
2250 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002251 *
2252 * The application may call psa_aead_abort() at any time after the operation
2253 * has been initialized.
2254 *
2255 * After a successful call to psa_aead_decrypt_setup(), the application must
2256 * eventually terminate the operation. The following events terminate an
2257 * operation:
2258 * - A failed call to any of the \c psa_aead_xxx functions.
2259 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2260 *
2261 * \param[in,out] operation The operation object to set up. It must have
2262 * been initialized as per the documentation for
2263 * #psa_aead_operation_t and not yet in use.
2264 * \param handle Handle to the key to use for the operation.
2265 * It must remain valid until the operation
2266 * terminates.
2267 * \param alg The AEAD algorithm to compute
2268 * (\c PSA_ALG_XXX value such that
2269 * #PSA_ALG_IS_AEAD(\p alg) is true).
2270 *
2271 * \retval #PSA_SUCCESS
2272 * Success.
2273 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002274 * \retval #PSA_ERROR_NOT_PERMITTED
2275 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002276 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002277 * \retval #PSA_ERROR_NOT_SUPPORTED
2278 * \p alg is not supported or is not an AEAD algorithm.
2279 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2280 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2281 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002282 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002283 * \retval #PSA_ERROR_BAD_STATE
2284 * The library has not been previously initialized by psa_crypto_init().
2285 * It is implementation-dependent whether a failure to initialize
2286 * results in this error code.
2287 */
2288psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2289 psa_key_handle_t handle,
2290 psa_algorithm_t alg);
2291
2292/** Generate a random nonce for an authenticated encryption operation.
2293 *
2294 * This function generates a random nonce for the authenticated encryption
2295 * operation with an appropriate size for the chosen algorithm, key type
2296 * and key size.
2297 *
2298 * The application must call psa_aead_encrypt_setup() before
2299 * calling this function.
2300 *
2301 * If this function returns an error status, the operation becomes inactive.
2302 *
2303 * \param[in,out] operation Active AEAD operation.
2304 * \param[out] nonce Buffer where the generated nonce is to be
2305 * written.
2306 * \param nonce_size Size of the \p nonce buffer in bytes.
2307 * \param[out] nonce_length On success, the number of bytes of the
2308 * generated nonce.
2309 *
2310 * \retval #PSA_SUCCESS
2311 * Success.
2312 * \retval #PSA_ERROR_BAD_STATE
2313 * The operation state is not valid (not set up, or nonce already set).
2314 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2315 * The size of the \p nonce buffer is too small.
2316 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2317 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2318 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002319 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002320 */
2321psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2322 unsigned char *nonce,
2323 size_t nonce_size,
2324 size_t *nonce_length);
2325
2326/** Set the nonce for an authenticated encryption or decryption operation.
2327 *
2328 * This function sets the nonce for the authenticated
2329 * encryption or decryption operation.
2330 *
2331 * The application must call psa_aead_encrypt_setup() before
2332 * calling this function.
2333 *
2334 * If this function returns an error status, the operation becomes inactive.
2335 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002336 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002337 * instead of this function, unless implementing a protocol that requires
2338 * a non-random IV.
2339 *
2340 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002341 * \param[in] nonce Buffer containing the nonce to use.
2342 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002343 *
2344 * \retval #PSA_SUCCESS
2345 * Success.
2346 * \retval #PSA_ERROR_BAD_STATE
2347 * The operation state is not valid (not set up, or nonce already set).
2348 * \retval #PSA_ERROR_INVALID_ARGUMENT
2349 * The size of \p nonce is not acceptable for the chosen algorithm.
2350 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2351 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2352 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002353 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002354 */
2355psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2356 const unsigned char *nonce,
2357 size_t nonce_length);
2358
Gilles Peskinebc59c852019-01-17 15:26:08 +01002359/** Declare the lengths of the message and additional data for AEAD.
2360 *
2361 * The application must call this function before calling
2362 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2363 * the operation requires it. If the algorithm does not require it,
2364 * calling this function is optional, but if this function is called
2365 * then the implementation must enforce the lengths.
2366 *
2367 * You may call this function before or after setting the nonce with
2368 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2369 *
2370 * - For #PSA_ALG_CCM, calling this function is required.
2371 * - For the other AEAD algorithms defined in this specification, calling
2372 * this function is not required.
2373 * - For vendor-defined algorithm, refer to the vendor documentation.
2374 *
2375 * \param[in,out] operation Active AEAD operation.
2376 * \param ad_length Size of the non-encrypted additional
2377 * authenticated data in bytes.
2378 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2379 *
2380 * \retval #PSA_SUCCESS
2381 * Success.
2382 * \retval #PSA_ERROR_BAD_STATE
2383 * The operation state is not valid (not set up, already completed,
2384 * or psa_aead_update_ad() or psa_aead_update() already called).
2385 * \retval #PSA_ERROR_INVALID_ARGUMENT
2386 * At least one of the lengths is not acceptable for the chosen
2387 * algorithm.
2388 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2389 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2390 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002391 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinebc59c852019-01-17 15:26:08 +01002392 */
2393psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2394 size_t ad_length,
2395 size_t plaintext_length);
2396
Gilles Peskine30a9e412019-01-14 18:36:12 +01002397/** Pass additional data to an active AEAD operation.
2398 *
2399 * Additional data is authenticated, but not encrypted.
2400 *
2401 * You may call this function multiple times to pass successive fragments
2402 * of the additional data. You may not call this function after passing
2403 * data to encrypt or decrypt with psa_aead_update().
2404 *
2405 * Before calling this function, you must:
2406 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2407 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2408 *
2409 * If this function returns an error status, the operation becomes inactive.
2410 *
2411 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2412 * there is no guarantee that the input is valid. Therefore, until
2413 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2414 * treat the input as untrusted and prepare to undo any action that
2415 * depends on the input if psa_aead_verify() returns an error status.
2416 *
2417 * \param[in,out] operation Active AEAD operation.
2418 * \param[in] input Buffer containing the fragment of
2419 * additional data.
2420 * \param input_length Size of the \p input buffer in bytes.
2421 *
2422 * \retval #PSA_SUCCESS
2423 * Success.
2424 * \retval #PSA_ERROR_BAD_STATE
2425 * The operation state is not valid (not set up, nonce not set,
2426 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002427 * \retval #PSA_ERROR_INVALID_ARGUMENT
2428 * The total input length overflows the additional data length that
2429 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002430 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2431 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2432 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002433 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002434 */
2435psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2436 const uint8_t *input,
2437 size_t input_length);
2438
2439/** Encrypt or decrypt a message fragment in an active AEAD operation.
2440 *
2441 * Before calling this function, you must:
2442 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2443 * The choice of setup function determines whether this function
2444 * encrypts or decrypts its input.
2445 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2446 * 3. Call psa_aead_update_ad() to pass all the additional data.
2447 *
2448 * If this function returns an error status, the operation becomes inactive.
2449 *
2450 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2451 * there is no guarantee that the input is valid. Therefore, until
2452 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2453 * - Do not use the output in any way other than storing it in a
2454 * confidential location. If you take any action that depends
2455 * on the tentative decrypted data, this action will need to be
2456 * undone if the input turns out not to be valid. Furthermore,
2457 * if an adversary can observe that this action took place
2458 * (for example through timing), they may be able to use this
2459 * fact as an oracle to decrypt any message encrypted with the
2460 * same key.
2461 * - In particular, do not copy the output anywhere but to a
2462 * memory or storage space that you have exclusive access to.
2463 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002464 * This function does not require the input to be aligned to any
2465 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002466 * a whole block at a time, it must consume all the input provided, but
2467 * it may delay the end of the corresponding output until a subsequent
2468 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2469 * provides sufficient input. The amount of data that can be delayed
2470 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002471 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002472 * \param[in,out] operation Active AEAD operation.
2473 * \param[in] input Buffer containing the message fragment to
2474 * encrypt or decrypt.
2475 * \param input_length Size of the \p input buffer in bytes.
2476 * \param[out] output Buffer where the output is to be written.
2477 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002478 * This must be at least
2479 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2480 * \p input_length) where \c alg is the
2481 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002482 * \param[out] output_length On success, the number of bytes
2483 * that make up the returned output.
2484 *
2485 * \retval #PSA_SUCCESS
2486 * Success.
2487 * \retval #PSA_ERROR_BAD_STATE
2488 * The operation state is not valid (not set up, nonce not set
2489 * or already completed).
2490 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2491 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002492 * You can determine a sufficient buffer size by calling
2493 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2494 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002495 * \retval #PSA_ERROR_INVALID_ARGUMENT
2496 * The total length of input to psa_aead_update_ad() so far is
2497 * less than the additional data length that was previously
2498 * specified with psa_aead_set_lengths().
2499 * \retval #PSA_ERROR_INVALID_ARGUMENT
2500 * The total input length overflows the plaintext length that
2501 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002502 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2503 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2504 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002505 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002506 */
2507psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2508 const uint8_t *input,
2509 size_t input_length,
2510 unsigned char *output,
2511 size_t output_size,
2512 size_t *output_length);
2513
2514/** Finish encrypting a message in an AEAD operation.
2515 *
2516 * The operation must have been set up with psa_aead_encrypt_setup().
2517 *
2518 * This function finishes the authentication of the additional data
2519 * formed by concatenating the inputs passed to preceding calls to
2520 * psa_aead_update_ad() with the plaintext formed by concatenating the
2521 * inputs passed to preceding calls to psa_aead_update().
2522 *
2523 * This function has two output buffers:
2524 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002525 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002526 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002527 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002528 * that the operation performs.
2529 *
2530 * When this function returns, the operation becomes inactive.
2531 *
2532 * \param[in,out] operation Active AEAD operation.
2533 * \param[out] ciphertext Buffer where the last part of the ciphertext
2534 * is to be written.
2535 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002536 * This must be at least
2537 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2538 * \c alg is the algorithm that is being
2539 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002540 * \param[out] ciphertext_length On success, the number of bytes of
2541 * returned ciphertext.
2542 * \param[out] tag Buffer where the authentication tag is
2543 * to be written.
2544 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002545 * This must be at least
2546 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2547 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002548 * \param[out] tag_length On success, the number of bytes
2549 * that make up the returned tag.
2550 *
2551 * \retval #PSA_SUCCESS
2552 * Success.
2553 * \retval #PSA_ERROR_BAD_STATE
2554 * The operation state is not valid (not set up, nonce not set,
2555 * decryption, or already completed).
2556 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002557 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002558 * You can determine a sufficient buffer size for \p ciphertext by
2559 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2560 * where \c alg is the algorithm that is being calculated.
2561 * You can determine a sufficient buffer size for \p tag by
2562 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002563 * \retval #PSA_ERROR_INVALID_ARGUMENT
2564 * The total length of input to psa_aead_update_ad() so far is
2565 * less than the additional data length that was previously
2566 * specified with psa_aead_set_lengths().
2567 * \retval #PSA_ERROR_INVALID_ARGUMENT
2568 * The total length of input to psa_aead_update() so far is
2569 * less than the plaintext length that was previously
2570 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002571 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2572 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2573 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002574 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002575 */
2576psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002577 uint8_t *ciphertext,
2578 size_t ciphertext_size,
2579 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002580 uint8_t *tag,
2581 size_t tag_size,
2582 size_t *tag_length);
2583
2584/** Finish authenticating and decrypting a message in an AEAD operation.
2585 *
2586 * The operation must have been set up with psa_aead_decrypt_setup().
2587 *
2588 * This function finishes the authentication of the additional data
2589 * formed by concatenating the inputs passed to preceding calls to
2590 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2591 * inputs passed to preceding calls to psa_aead_update().
2592 *
2593 * When this function returns, the operation becomes inactive.
2594 *
2595 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002596 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002597 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002598 * from previous calls to psa_aead_update()
2599 * that could not be processed until the end
2600 * of the input.
2601 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002602 * This must be at least
2603 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2604 * \c alg is the algorithm that is being
2605 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002606 * \param[out] plaintext_length On success, the number of bytes of
2607 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002608 * \param[in] tag Buffer containing the authentication tag.
2609 * \param tag_length Size of the \p tag buffer in bytes.
2610 *
2611 * \retval #PSA_SUCCESS
2612 * Success.
2613 * \retval #PSA_ERROR_BAD_STATE
2614 * The operation state is not valid (not set up, nonce not set,
2615 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002616 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2617 * The size of the \p plaintext buffer is too small.
2618 * You can determine a sufficient buffer size for \p plaintext by
2619 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2620 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002621 * \retval #PSA_ERROR_INVALID_ARGUMENT
2622 * The total length of input to psa_aead_update_ad() so far is
2623 * less than the additional data length that was previously
2624 * specified with psa_aead_set_lengths().
2625 * \retval #PSA_ERROR_INVALID_ARGUMENT
2626 * The total length of input to psa_aead_update() so far is
2627 * less than the plaintext length that was previously
2628 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002629 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2630 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2631 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002632 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002633 */
2634psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002635 uint8_t *plaintext,
2636 size_t plaintext_size,
2637 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002638 const uint8_t *tag,
2639 size_t tag_length);
2640
2641/** Abort an AEAD operation.
2642 *
2643 * Aborting an operation frees all associated resources except for the
2644 * \p operation structure itself. Once aborted, the operation object
2645 * can be reused for another operation by calling
2646 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2647 *
2648 * You may call this function any time after the operation object has
2649 * been initialized by any of the following methods:
2650 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2651 * whether it succeeds or not.
2652 * - Initializing the \c struct to all-bits-zero.
2653 * - Initializing the \c struct to logical zeros, e.g.
2654 * `psa_aead_operation_t operation = {0}`.
2655 *
2656 * In particular, calling psa_aead_abort() after the operation has been
2657 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2658 * is safe and has no effect.
2659 *
2660 * \param[in,out] operation Initialized AEAD operation.
2661 *
2662 * \retval #PSA_SUCCESS
2663 * \retval #PSA_ERROR_BAD_STATE
2664 * \p operation is not an active AEAD operation.
2665 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2666 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002667 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002668 */
2669psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2670
Gilles Peskine3b555712018-03-03 21:27:57 +01002671/**@}*/
2672
Gilles Peskine20035e32018-02-03 22:44:14 +01002673/** \defgroup asymmetric Asymmetric cryptography
2674 * @{
2675 */
2676
2677/**
2678 * \brief Sign a hash or short message with a private key.
2679 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002680 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002681 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002682 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2683 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2684 * to determine the hash algorithm to use.
2685 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002686 * \param handle Handle to the key to use for the operation.
2687 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002688 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002689 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002690 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002691 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002692 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002693 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002694 * \param[out] signature_length On success, the number of bytes
2695 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002696 *
Gilles Peskine28538492018-07-11 17:34:00 +02002697 * \retval #PSA_SUCCESS
2698 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002699 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002700 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002701 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002702 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002703 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002704 * \retval #PSA_ERROR_NOT_SUPPORTED
2705 * \retval #PSA_ERROR_INVALID_ARGUMENT
2706 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2707 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2708 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002709 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002710 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002711 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002712 * The library has not been previously initialized by psa_crypto_init().
2713 * It is implementation-dependent whether a failure to initialize
2714 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002715 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002716psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002717 psa_algorithm_t alg,
2718 const uint8_t *hash,
2719 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002720 uint8_t *signature,
2721 size_t signature_size,
2722 size_t *signature_length);
2723
2724/**
2725 * \brief Verify the signature a hash or short message using a public key.
2726 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002727 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002728 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002729 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2730 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2731 * to determine the hash algorithm to use.
2732 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002733 * \param handle Handle to the key to use for the operation.
2734 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002735 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002736 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002737 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002738 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002739 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002740 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002741 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002742 *
Gilles Peskine28538492018-07-11 17:34:00 +02002743 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002744 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002745 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002746 * The calculation was perfomed successfully, but the passed
2747 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002748 * \retval #PSA_ERROR_NOT_SUPPORTED
2749 * \retval #PSA_ERROR_INVALID_ARGUMENT
2750 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2751 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2752 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002753 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002754 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002755 * The library has not been previously initialized by psa_crypto_init().
2756 * It is implementation-dependent whether a failure to initialize
2757 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002758 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002759psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002760 psa_algorithm_t alg,
2761 const uint8_t *hash,
2762 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002763 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002764 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002765
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002766/**
2767 * \brief Encrypt a short message with a public key.
2768 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002769 * \param handle Handle to the key to use for the operation.
2770 * It must be a public key or an asymmetric
2771 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002772 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002773 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002774 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002775 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002776 * \param[in] salt A salt or label, if supported by the
2777 * encryption algorithm.
2778 * If the algorithm does not support a
2779 * salt, pass \c NULL.
2780 * If the algorithm supports an optional
2781 * salt and you do not want to pass a salt,
2782 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002783 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002784 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2785 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002786 * \param salt_length Size of the \p salt buffer in bytes.
2787 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002788 * \param[out] output Buffer where the encrypted message is to
2789 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002790 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002791 * \param[out] output_length On success, the number of bytes
2792 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002793 *
Gilles Peskine28538492018-07-11 17:34:00 +02002794 * \retval #PSA_SUCCESS
2795 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002796 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002797 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002798 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002799 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002800 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002801 * \retval #PSA_ERROR_NOT_SUPPORTED
2802 * \retval #PSA_ERROR_INVALID_ARGUMENT
2803 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2804 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2805 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002806 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002807 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002808 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002809 * The library has not been previously initialized by psa_crypto_init().
2810 * It is implementation-dependent whether a failure to initialize
2811 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002812 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002813psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002814 psa_algorithm_t alg,
2815 const uint8_t *input,
2816 size_t input_length,
2817 const uint8_t *salt,
2818 size_t salt_length,
2819 uint8_t *output,
2820 size_t output_size,
2821 size_t *output_length);
2822
2823/**
2824 * \brief Decrypt a short message with a private key.
2825 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002826 * \param handle Handle to the key to use for the operation.
2827 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002828 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002829 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002830 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002831 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002832 * \param[in] salt A salt or label, if supported by the
2833 * encryption algorithm.
2834 * If the algorithm does not support a
2835 * salt, pass \c NULL.
2836 * If the algorithm supports an optional
2837 * salt and you do not want to pass a salt,
2838 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002839 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002840 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2841 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002842 * \param salt_length Size of the \p salt buffer in bytes.
2843 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002844 * \param[out] output Buffer where the decrypted message is to
2845 * be written.
2846 * \param output_size Size of the \c output buffer in bytes.
2847 * \param[out] output_length On success, the number of bytes
2848 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002849 *
Gilles Peskine28538492018-07-11 17:34:00 +02002850 * \retval #PSA_SUCCESS
2851 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002852 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002853 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002854 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002855 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002856 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002857 * \retval #PSA_ERROR_NOT_SUPPORTED
2858 * \retval #PSA_ERROR_INVALID_ARGUMENT
2859 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2860 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2861 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002862 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002863 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2864 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002865 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002866 * The library has not been previously initialized by psa_crypto_init().
2867 * It is implementation-dependent whether a failure to initialize
2868 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002869 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002870psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002871 psa_algorithm_t alg,
2872 const uint8_t *input,
2873 size_t input_length,
2874 const uint8_t *salt,
2875 size_t salt_length,
2876 uint8_t *output,
2877 size_t output_size,
2878 size_t *output_length);
2879
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002880/**@}*/
2881
Gilles Peskine35675b62019-05-16 17:26:11 +02002882/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002883 * @{
2884 */
2885
Gilles Peskine35675b62019-05-16 17:26:11 +02002886/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002887 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002888 * Before calling any function on a key derivation operation object, the
2889 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002890 * - Set the structure to all-bits-zero, for example:
2891 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002892 * psa_key_derivation_operation_t operation;
2893 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002894 * \endcode
2895 * - Initialize the structure to logical zero values, for example:
2896 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002897 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002898 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002899 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002900 * for example:
2901 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002902 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002903 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002904 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002905 * to the structure, for example:
2906 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002907 * psa_key_derivation_operation_t operation;
2908 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002909 * \endcode
2910 *
2911 * This is an implementation-defined \c struct. Applications should not
2912 * make any assumptions about the content of this structure except
2913 * as directed by the documentation of a specific implementation.
2914 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002915typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002916
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002917/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002918 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002919 * This macro returns a suitable initializer for a key derivation operation
2920 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002921 */
2922#ifdef __DOXYGEN_ONLY__
2923/* This is an example definition for documentation purposes.
2924 * Implementations should define a suitable value in `crypto_struct.h`.
2925 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002926#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02002927#endif
2928
Gilles Peskine35675b62019-05-16 17:26:11 +02002929/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002930 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002931static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002932
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002933/** Set up a key derivation operation.
2934 *
2935 * A key derivation algorithm takes some inputs and uses them to generate
2936 * a byte stream in a deterministic way.
2937 * This byte stream can be used to produce keys and other
2938 * cryptographic material.
2939 *
2940 * To derive a key:
2941 * - Start with an initialized object of type #psa_key_derivation_operation_t.
2942 * - Call psa_key_derivation_setup() to select the algorithm.
2943 * - Provide the inputs for the key derivation by calling
2944 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2945 * as appropriate. Which inputs are needed, in what order, and whether
2946 * they may be keys and if so of what type depends on the algorithm.
2947 * - Optionally set the operation's maximum capacity with
2948 * psa_key_derivation_set_capacity(). You may do this before, in the middle
2949 * of or after providing inputs. For some algorithms, this step is mandatory
2950 * because the output depends on the maximum capacity.
2951 * - To derive a key, call psa_key_derivation_output_key().
2952 * To derive a byte string for a different purpose, call
2953 * - psa_key_derivation_output_bytes().
2954 * Successive calls to these functions use successive output bytes
2955 * calculated by the key derivation algorithm.
2956 * - Clean up the key derivation operation object with
2957 * psa_key_derivation_abort().
2958 *
2959 * \param[in,out] operation The key derivation operation object
2960 * to set up. It must
2961 * have been initialized but not set up yet.
2962 * \param alg The key derivation algorithm to compute
2963 * (\c PSA_ALG_XXX value such that
2964 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2965 *
2966 * \retval #PSA_SUCCESS
2967 * Success.
2968 * \retval #PSA_ERROR_INVALID_ARGUMENT
2969 * \c alg is not a key derivation algorithm.
2970 * \retval #PSA_ERROR_NOT_SUPPORTED
2971 * \c alg is not supported or is not a key derivation algorithm.
2972 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2973 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2974 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002975 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002976 * \retval #PSA_ERROR_BAD_STATE
2977 */
2978psa_status_t psa_key_derivation_setup(
2979 psa_key_derivation_operation_t *operation,
2980 psa_algorithm_t alg);
2981
Gilles Peskine35675b62019-05-16 17:26:11 +02002982/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002983 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002984 * The capacity of a key derivation is the maximum number of bytes that it can
2985 * return. When you get *N* bytes of output from a key derivation operation,
2986 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002987 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002988 * \param[in] operation The operation to query.
2989 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002990 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002991 * \retval #PSA_SUCCESS
2992 * \retval #PSA_ERROR_BAD_STATE
2993 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002994 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002995psa_status_t psa_key_derivation_get_capacity(
2996 const psa_key_derivation_operation_t *operation,
2997 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002998
Gilles Peskine35675b62019-05-16 17:26:11 +02002999/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003000 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003001 * The capacity of a key derivation operation is the maximum number of bytes
3002 * that the key derivation operation can return from this point onwards.
3003 *
3004 * \param[in,out] operation The key derivation operation object to modify.
3005 * \param capacity The new capacity of the operation.
3006 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003007 * current capacity.
3008 *
3009 * \retval #PSA_SUCCESS
3010 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003011 * \p capacity is larger than the operation's current capacity.
3012 * In this case, the operation object remains valid and its capacity
3013 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003014 * \retval #PSA_ERROR_BAD_STATE
3015 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3016 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003017psa_status_t psa_key_derivation_set_capacity(
3018 psa_key_derivation_operation_t *operation,
3019 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003020
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003021/** Use the maximum possible capacity for a key derivation operation.
3022 *
3023 * Use this value as the capacity argument when setting up a key derivation
3024 * to indicate that the operation should have the maximum possible capacity.
3025 * The value of the maximum possible capacity depends on the key derivation
3026 * algorithm.
3027 */
3028#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3029
3030/** Provide an input for key derivation or key agreement.
3031 *
3032 * Which inputs are required and in what order depends on the algorithm.
3033 * Refer to the documentation of each key derivation or key agreement
3034 * algorithm for information.
3035 *
3036 * This function passes direct inputs. Some inputs must be passed as keys
3037 * using psa_key_derivation_input_key() instead of this function. Refer to
3038 * the documentation of individual step types for information.
3039 *
3040 * \param[in,out] operation The key derivation operation object to use.
3041 * It must have been set up with
3042 * psa_key_derivation_setup() and must not
3043 * have produced any output yet.
3044 * \param step Which step the input data is for.
3045 * \param[in] data Input data to use.
3046 * \param data_length Size of the \p data buffer in bytes.
3047 *
3048 * \retval #PSA_SUCCESS
3049 * Success.
3050 * \retval #PSA_ERROR_INVALID_ARGUMENT
3051 * \c step is not compatible with the operation's algorithm.
3052 * \retval #PSA_ERROR_INVALID_ARGUMENT
3053 * \c step does not allow direct inputs.
3054 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3055 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3056 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003057 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003058 * \retval #PSA_ERROR_BAD_STATE
3059 * The value of \p step is not valid given the state of \p operation.
3060 * \retval #PSA_ERROR_BAD_STATE
3061 * The library has not been previously initialized by psa_crypto_init().
3062 * It is implementation-dependent whether a failure to initialize
3063 * results in this error code.
3064 */
3065psa_status_t psa_key_derivation_input_bytes(
3066 psa_key_derivation_operation_t *operation,
3067 psa_key_derivation_step_t step,
3068 const uint8_t *data,
3069 size_t data_length);
3070
3071/** Provide an input for key derivation in the form of a key.
3072 *
3073 * Which inputs are required and in what order depends on the algorithm.
3074 * Refer to the documentation of each key derivation or key agreement
3075 * algorithm for information.
3076 *
3077 * This function passes key inputs. Some inputs must be passed as keys
3078 * of the appropriate type using this function, while others must be
3079 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3080 * the documentation of individual step types for information.
3081 *
3082 * \param[in,out] operation The key derivation operation object to use.
3083 * It must have been set up with
3084 * psa_key_derivation_setup() and must not
3085 * have produced any output yet.
3086 * \param step Which step the input data is for.
3087 * \param handle Handle to the key. It must have an
3088 * appropriate type for \p step and must
3089 * allow the usage #PSA_KEY_USAGE_DERIVE.
3090 *
3091 * \retval #PSA_SUCCESS
3092 * Success.
3093 * \retval #PSA_ERROR_INVALID_HANDLE
3094 * \retval #PSA_ERROR_DOES_NOT_EXIST
3095 * \retval #PSA_ERROR_NOT_PERMITTED
3096 * \retval #PSA_ERROR_INVALID_ARGUMENT
3097 * \c step is not compatible with the operation's algorithm.
3098 * \retval #PSA_ERROR_INVALID_ARGUMENT
3099 * \c step does not allow key inputs.
3100 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3101 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3102 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003103 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003104 * \retval #PSA_ERROR_BAD_STATE
3105 * The value of \p step is not valid given the state of \p operation.
3106 * \retval #PSA_ERROR_BAD_STATE
3107 * The library has not been previously initialized by psa_crypto_init().
3108 * It is implementation-dependent whether a failure to initialize
3109 * results in this error code.
3110 */
3111psa_status_t psa_key_derivation_input_key(
3112 psa_key_derivation_operation_t *operation,
3113 psa_key_derivation_step_t step,
3114 psa_key_handle_t handle);
3115
3116/** Perform a key agreement and use the shared secret as input to a key
3117 * derivation.
3118 *
3119 * A key agreement algorithm takes two inputs: a private key \p private_key
3120 * a public key \p peer_key.
3121 * The result of this function is passed as input to a key derivation.
3122 * The output of this key derivation can be extracted by reading from the
3123 * resulting operation to produce keys and other cryptographic material.
3124 *
3125 * \param[in,out] operation The key derivation operation object to use.
3126 * It must have been set up with
3127 * psa_key_derivation_setup() with a
3128 * key agreement and derivation algorithm
3129 * \c alg (\c PSA_ALG_XXX value such that
3130 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3131 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3132 * is false).
3133 * The operation must be ready for an
3134 * input of the type given by \p step.
3135 * \param step Which step the input data is for.
3136 * \param private_key Handle to the private key to use.
3137 * \param[in] peer_key Public key of the peer. The peer key must be in the
3138 * same format that psa_import_key() accepts for the
3139 * public key type corresponding to the type of
3140 * private_key. That is, this function performs the
3141 * equivalent of
3142 * #psa_import_key(...,
3143 * `peer_key`, `peer_key_length`) where
3144 * with key attributes indicating the public key
3145 * type corresponding to the type of `private_key`.
3146 * For example, for EC keys, this means that peer_key
3147 * is interpreted as a point on the curve that the
3148 * private key is on. The standard formats for public
3149 * keys are documented in the documentation of
3150 * psa_export_public_key().
3151 * \param peer_key_length Size of \p peer_key in bytes.
3152 *
3153 * \retval #PSA_SUCCESS
3154 * Success.
3155 * \retval #PSA_ERROR_INVALID_HANDLE
3156 * \retval #PSA_ERROR_DOES_NOT_EXIST
3157 * \retval #PSA_ERROR_NOT_PERMITTED
3158 * \retval #PSA_ERROR_INVALID_ARGUMENT
3159 * \c private_key is not compatible with \c alg,
3160 * or \p peer_key is not valid for \c alg or not compatible with
3161 * \c private_key.
3162 * \retval #PSA_ERROR_NOT_SUPPORTED
3163 * \c alg is not supported or is not a key derivation algorithm.
3164 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3165 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3166 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003167 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003168 */
3169psa_status_t psa_key_derivation_key_agreement(
3170 psa_key_derivation_operation_t *operation,
3171 psa_key_derivation_step_t step,
3172 psa_key_handle_t private_key,
3173 const uint8_t *peer_key,
3174 size_t peer_key_length);
3175
Gilles Peskine35675b62019-05-16 17:26:11 +02003176/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003177 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003178 * This function calculates output bytes from a key derivation algorithm and
3179 * return those bytes.
3180 * If you view the key derivation's output as a stream of bytes, this
3181 * function destructively reads the requested number of bytes from the
3182 * stream.
3183 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003184 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003185 * \param[in,out] operation The key derivation operation object to read from.
3186 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003187 * \param output_length Number of bytes to output.
3188 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003189 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003190 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003191 * The operation's capacity was less than
3192 * \p output_length bytes. Note that in this case,
3193 * no output is written to the output buffer.
3194 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003195 * subsequent calls to this function will not
3196 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003197 * \retval #PSA_ERROR_BAD_STATE
3198 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3199 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3200 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003201 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003202 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003203psa_status_t psa_key_derivation_output_bytes(
3204 psa_key_derivation_operation_t *operation,
3205 uint8_t *output,
3206 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003207
Gilles Peskine35675b62019-05-16 17:26:11 +02003208/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003209 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003210 * This function calculates output bytes from a key derivation algorithm
3211 * and uses those bytes to generate a key deterministically.
3212 * If you view the key derivation's output as a stream of bytes, this
3213 * function destructively reads as many bytes as required from the
3214 * stream.
3215 * The operation's capacity decreases by the number of bytes read.
3216 *
3217 * How much output is produced and consumed from the operation, and how
3218 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003219 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003220 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003221 * of a given size, this function is functionally equivalent to
3222 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003223 * and passing the resulting output to #psa_import_key.
3224 * However, this function has a security benefit:
3225 * if the implementation provides an isolation boundary then
3226 * the key material is not exposed outside the isolation boundary.
3227 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003228 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003229 * The following key types defined in this specification follow this scheme:
3230 *
3231 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003232 * - #PSA_KEY_TYPE_ARC4;
3233 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003234 * - #PSA_KEY_TYPE_DERIVE;
3235 * - #PSA_KEY_TYPE_HMAC.
3236 *
3237 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003238 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003239 * Montgomery curve), this function always draws a byte string whose
3240 * length is determined by the curve, and sets the mandatory bits
3241 * accordingly. That is:
3242 *
3243 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3244 * and process it as specified in RFC 7748 &sect;5.
3245 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3246 * and process it as specified in RFC 7748 &sect;5.
3247 *
3248 * - For key types for which the key is represented by a single sequence of
3249 * \p bits bits with constraints as to which bit sequences are acceptable,
3250 * this function draws a byte string of length (\p bits / 8) bytes rounded
3251 * up to the nearest whole number of bytes. If the resulting byte string
3252 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3253 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003254 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003255 * for the output produced by psa_export_key().
3256 * The following key types defined in this specification follow this scheme:
3257 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003258 * - #PSA_KEY_TYPE_DES.
3259 * Force-set the parity bits, but discard forbidden weak keys.
3260 * For 2-key and 3-key triple-DES, the three keys are generated
3261 * successively (for example, for 3-key triple-DES,
3262 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3263 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003264 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003265 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003266 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003267 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003268 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003269 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003270 * Weierstrass curve).
3271 * For these key types, interpret the byte string as integer
3272 * in big-endian order. Discard it if it is not in the range
3273 * [0, *N* - 2] where *N* is the boundary of the private key domain
3274 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003275 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003276 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003277 * This method allows compliance to NIST standards, specifically
3278 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003279 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3280 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3281 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3282 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003283 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003284 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003285 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003286 * implementation-defined.
3287 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003288 * In all cases, the data that is read is discarded from the operation.
3289 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003290 *
Gilles Peskine20628592019-04-19 19:29:50 +02003291 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003292 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003293 * \param[out] handle On success, a handle to the newly created key.
3294 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003295 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003296 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003297 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003298 * If the key is persistent, the key material and the key's metadata
3299 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003300 * \retval #PSA_ERROR_ALREADY_EXISTS
3301 * This is an attempt to create a persistent key, and there is
3302 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003303 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003304 * There was not enough data to create the desired key.
3305 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003306 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003307 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003308 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003309 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003310 * implementation in general or in this particular location.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003311 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003312 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3313 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3314 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3315 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003316 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003317 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003318 * The library has not been previously initialized by psa_crypto_init().
3319 * It is implementation-dependent whether a failure to initialize
3320 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003321 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003322psa_status_t psa_key_derivation_output_key(
3323 const psa_key_attributes_t *attributes,
3324 psa_key_derivation_operation_t *operation,
3325 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003326
Gilles Peskine35675b62019-05-16 17:26:11 +02003327/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003328 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003329 * Once a key derivation operation has been aborted, its capacity is zero.
3330 * Aborting an operation frees all associated resources except for the
3331 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003332 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003333 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003334 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003335 * psa_key_derivation_operation_init() or a zero value. In particular,
3336 * it is valid to call psa_key_derivation_abort() twice, or to call
3337 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003338 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003339 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003340 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003341 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003342 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003343 * \retval #PSA_SUCCESS
3344 * \retval #PSA_ERROR_BAD_STATE
3345 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3346 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003347 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003348 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003349psa_status_t psa_key_derivation_abort(
3350 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003351
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003352/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003353 *
3354 * \warning The raw result of a key agreement algorithm such as finite-field
3355 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3356 * not be used directly as key material. It should instead be passed as
3357 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003358 * a key derivation, use psa_key_derivation_key_agreement() and other
3359 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003360 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003361 * \param alg The key agreement algorithm to compute
3362 * (\c PSA_ALG_XXX value such that
3363 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3364 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003365 * \param private_key Handle to the private key to use.
3366 * \param[in] peer_key Public key of the peer. It must be
3367 * in the same format that psa_import_key()
3368 * accepts. The standard formats for public
3369 * keys are documented in the documentation
3370 * of psa_export_public_key().
3371 * \param peer_key_length Size of \p peer_key in bytes.
3372 * \param[out] output Buffer where the decrypted message is to
3373 * be written.
3374 * \param output_size Size of the \c output buffer in bytes.
3375 * \param[out] output_length On success, the number of bytes
3376 * that make up the returned output.
3377 *
3378 * \retval #PSA_SUCCESS
3379 * Success.
3380 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003381 * \retval #PSA_ERROR_NOT_PERMITTED
3382 * \retval #PSA_ERROR_INVALID_ARGUMENT
3383 * \p alg is not a key agreement algorithm
3384 * \retval #PSA_ERROR_INVALID_ARGUMENT
3385 * \p private_key is not compatible with \p alg,
3386 * or \p peer_key is not valid for \p alg or not compatible with
3387 * \p private_key.
3388 * \retval #PSA_ERROR_NOT_SUPPORTED
3389 * \p alg is not a supported key agreement algorithm.
3390 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3391 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3392 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003393 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine769c7a62019-01-18 16:42:29 +01003394 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003395psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3396 psa_key_handle_t private_key,
3397 const uint8_t *peer_key,
3398 size_t peer_key_length,
3399 uint8_t *output,
3400 size_t output_size,
3401 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003402
Gilles Peskineea0fb492018-07-12 17:17:20 +02003403/**@}*/
3404
Gilles Peskineedd76872018-07-20 17:42:05 +02003405/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003406 * @{
3407 */
3408
3409/**
3410 * \brief Generate random bytes.
3411 *
3412 * \warning This function **can** fail! Callers MUST check the return status
3413 * and MUST NOT use the content of the output buffer if the return
3414 * status is not #PSA_SUCCESS.
3415 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003416 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003417 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003418 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003419 * \param output_size Number of bytes to generate and output.
3420 *
Gilles Peskine28538492018-07-11 17:34:00 +02003421 * \retval #PSA_SUCCESS
3422 * \retval #PSA_ERROR_NOT_SUPPORTED
3423 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3424 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3425 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003426 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003427 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003428 * The library has not been previously initialized by psa_crypto_init().
3429 * It is implementation-dependent whether a failure to initialize
3430 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003431 */
3432psa_status_t psa_generate_random(uint8_t *output,
3433 size_t output_size);
3434
3435/**
3436 * \brief Generate a key or key pair.
3437 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003438 * The key is generated randomly.
3439 * Its location, policy, type and size are taken from \p attributes.
3440 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003441 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003442 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003443 * the public exponent is 65537.
3444 * The modulus is a product of two probabilistic primes
3445 * between 2^{n-1} and 2^n where n is the bit size specified in the
3446 * attributes.
3447 *
Gilles Peskine20628592019-04-19 19:29:50 +02003448 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003449 * \param[out] handle On success, a handle to the newly created key.
3450 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003451 *
Gilles Peskine28538492018-07-11 17:34:00 +02003452 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003453 * Success.
3454 * If the key is persistent, the key material and the key's metadata
3455 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003456 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003457 * This is an attempt to create a persistent key, and there is
3458 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003459 * \retval #PSA_ERROR_NOT_SUPPORTED
3460 * \retval #PSA_ERROR_INVALID_ARGUMENT
3461 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3462 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3463 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3464 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003465 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003466 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003467 * The library has not been previously initialized by psa_crypto_init().
3468 * It is implementation-dependent whether a failure to initialize
3469 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003470 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003471psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003472 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003473
3474/**@}*/
3475
Gilles Peskinee59236f2018-01-27 23:32:46 +01003476#ifdef __cplusplus
3477}
3478#endif
3479
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003480/* The file "crypto_sizes.h" contains definitions for size calculation
3481 * macros whose definitions are implementation-specific. */
3482#include "crypto_sizes.h"
3483
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003484/* The file "crypto_struct.h" contains definitions for
3485 * implementation-specific structs that are declared above. */
3486#include "crypto_struct.h"
3487
3488/* The file "crypto_extra.h" contains vendor-specific definitions. This
3489 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003490#include "crypto_extra.h"
3491
3492#endif /* PSA_CRYPTO_H */