blob: f099967a08027124dd04c2e6c4962b3d65d351c3 [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
1213 * \retval #PSA_ERROR_EMPTY_SLOT
1214 * \retval #PSA_ERROR_NOT_PERMITTED
1215 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001216 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001217 * \retval #PSA_ERROR_NOT_SUPPORTED
1218 * \p alg is not supported or is not a MAC algorithm.
1219 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1220 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1221 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001222 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001223 * \retval #PSA_ERROR_BAD_STATE
1224 * The library has not been previously initialized by psa_crypto_init().
1225 * It is implementation-dependent whether a failure to initialize
1226 * results in this error code.
1227 */
1228psa_status_t psa_mac_compute(psa_key_handle_t handle,
1229 psa_algorithm_t alg,
1230 const uint8_t *input,
1231 size_t input_length,
1232 uint8_t *mac,
1233 size_t mac_size,
1234 size_t *mac_length);
1235
1236/** Calculate the MAC of a message and compare it with a reference value.
1237 *
1238 * \param handle Handle to the key to use for the operation.
1239 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001240 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001241 * \param[in] input Buffer containing the input message.
1242 * \param input_length Size of the \p input buffer in bytes.
1243 * \param[out] mac Buffer containing the expected MAC value.
1244 * \param mac_length Size of the \p mac buffer in bytes.
1245 *
1246 * \retval #PSA_SUCCESS
1247 * The expected MAC is identical to the actual MAC of the input.
1248 * \retval #PSA_ERROR_INVALID_SIGNATURE
1249 * The MAC of the message was calculated successfully, but it
1250 * differs from the expected value.
1251 * \retval #PSA_ERROR_INVALID_HANDLE
1252 * \retval #PSA_ERROR_EMPTY_SLOT
1253 * \retval #PSA_ERROR_NOT_PERMITTED
1254 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001255 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001256 * \retval #PSA_ERROR_NOT_SUPPORTED
1257 * \p alg is not supported or is not a MAC algorithm.
1258 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1259 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1260 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001261 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001262 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001263psa_status_t psa_mac_verify(psa_key_handle_t handle,
1264 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001265 const uint8_t *input,
1266 size_t input_length,
1267 const uint8_t *mac,
1268 const size_t mac_length);
1269
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001270/** The type of the state data structure for multipart MAC operations.
1271 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001272 * Before calling any function on a MAC operation object, the application must
1273 * initialize it by any of the following means:
1274 * - Set the structure to all-bits-zero, for example:
1275 * \code
1276 * psa_mac_operation_t operation;
1277 * memset(&operation, 0, sizeof(operation));
1278 * \endcode
1279 * - Initialize the structure to logical zero values, for example:
1280 * \code
1281 * psa_mac_operation_t operation = {0};
1282 * \endcode
1283 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1284 * for example:
1285 * \code
1286 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1287 * \endcode
1288 * - Assign the result of the function psa_mac_operation_init()
1289 * to the structure, for example:
1290 * \code
1291 * psa_mac_operation_t operation;
1292 * operation = psa_mac_operation_init();
1293 * \endcode
1294 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001295 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001296 * make any assumptions about the content of this structure except
1297 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001298typedef struct psa_mac_operation_s psa_mac_operation_t;
1299
Jaeden Amero769ce272019-01-04 11:48:03 +00001300/** \def PSA_MAC_OPERATION_INIT
1301 *
1302 * This macro returns a suitable initializer for a MAC operation object of type
1303 * #psa_mac_operation_t.
1304 */
1305#ifdef __DOXYGEN_ONLY__
1306/* This is an example definition for documentation purposes.
1307 * Implementations should define a suitable value in `crypto_struct.h`.
1308 */
1309#define PSA_MAC_OPERATION_INIT {0}
1310#endif
1311
1312/** Return an initial value for a MAC operation object.
1313 */
1314static psa_mac_operation_t psa_mac_operation_init(void);
1315
Gilles Peskinef45adda2019-01-14 18:29:18 +01001316/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001317 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001318 * This function sets up the calculation of the MAC
1319 * (message authentication code) of a byte string.
1320 * To verify the MAC of a message against an
1321 * expected value, use psa_mac_verify_setup() instead.
1322 *
1323 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001324 * -# Allocate an operation object which will be passed to all the functions
1325 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001326 * -# Initialize the operation object with one of the methods described in the
1327 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001328 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001329 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1330 * of the message each time. The MAC that is calculated is the MAC
1331 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001332 * -# At the end of the message, call psa_mac_sign_finish() to finish
1333 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001334 *
1335 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001336 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001337 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001338 * After a successful call to psa_mac_sign_setup(), the application must
1339 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001340 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001341 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001342 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001343 * \param[in,out] operation The operation object to set up. It must have
1344 * been initialized as per the documentation for
1345 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001346 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001347 * It must remain valid until the operation
1348 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001349 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001350 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001351 *
Gilles Peskine28538492018-07-11 17:34:00 +02001352 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001353 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001354 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001355 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001356 * \retval #PSA_ERROR_NOT_PERMITTED
1357 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001358 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001359 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001360 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001361 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1362 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1363 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001364 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001365 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001366 * The operation state is not valid (already set up and not
1367 * subsequently completed).
1368 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001369 * The library has not been previously initialized by psa_crypto_init().
1370 * It is implementation-dependent whether a failure to initialize
1371 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001372 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001373psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001374 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001375 psa_algorithm_t alg);
1376
Gilles Peskinef45adda2019-01-14 18:29:18 +01001377/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001378 *
1379 * This function sets up the verification of the MAC
1380 * (message authentication code) of a byte string against an expected value.
1381 *
1382 * The sequence of operations to verify a MAC is as follows:
1383 * -# Allocate an operation object which will be passed to all the functions
1384 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001385 * -# Initialize the operation object with one of the methods described in the
1386 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001387 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001388 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1389 * of the message each time. The MAC that is calculated is the MAC
1390 * of the concatenation of these messages in order.
1391 * -# At the end of the message, call psa_mac_verify_finish() to finish
1392 * calculating the actual MAC of the message and verify it against
1393 * the expected value.
1394 *
1395 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001396 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001397 *
1398 * After a successful call to psa_mac_verify_setup(), the application must
1399 * eventually terminate the operation through one of the following methods:
1400 * - A failed call to psa_mac_update().
1401 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1402 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001403 * \param[in,out] operation The operation object to set up. It must have
1404 * been initialized as per the documentation for
1405 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001406 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001407 * It must remain valid until the operation
1408 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001409 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1410 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001411 *
Gilles Peskine28538492018-07-11 17:34:00 +02001412 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001413 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001414 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001415 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001416 * \retval #PSA_ERROR_NOT_PERMITTED
1417 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001418 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001419 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001420 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001421 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1422 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1423 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001424 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001425 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001426 * The operation state is not valid (already set up and not
1427 * subsequently completed).
1428 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001429 * The library has not been previously initialized by psa_crypto_init().
1430 * It is implementation-dependent whether a failure to initialize
1431 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001432 */
1433psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001434 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001435 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001436
Gilles Peskinedcd14942018-07-12 00:30:52 +02001437/** Add a message fragment to a multipart MAC operation.
1438 *
1439 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1440 * before calling this function.
1441 *
1442 * If this function returns an error status, the operation becomes inactive.
1443 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001444 * \param[in,out] operation Active MAC operation.
1445 * \param[in] input Buffer containing the message fragment to add to
1446 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001447 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001448 *
1449 * \retval #PSA_SUCCESS
1450 * Success.
1451 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001452 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001453 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1454 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1455 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001456 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001457 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001458psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1459 const uint8_t *input,
1460 size_t input_length);
1461
Gilles Peskinedcd14942018-07-12 00:30:52 +02001462/** Finish the calculation of the MAC of a message.
1463 *
1464 * The application must call psa_mac_sign_setup() before calling this function.
1465 * This function calculates the MAC of the message formed by concatenating
1466 * the inputs passed to preceding calls to psa_mac_update().
1467 *
1468 * When this function returns, the operation becomes inactive.
1469 *
1470 * \warning Applications should not call this function if they expect
1471 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1472 * Beware that comparing integrity or authenticity data such as
1473 * MAC values with a function such as \c memcmp is risky
1474 * because the time taken by the comparison may leak information
1475 * about the MAC value which could allow an attacker to guess
1476 * a valid MAC and thereby bypass security controls.
1477 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001478 * \param[in,out] operation Active MAC operation.
1479 * \param[out] mac Buffer where the MAC value is to be written.
1480 * \param mac_size Size of the \p mac buffer in bytes.
1481 * \param[out] mac_length On success, the number of bytes
1482 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001483 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001484 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001485 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001486 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001487 *
1488 * \retval #PSA_SUCCESS
1489 * Success.
1490 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001491 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001492 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001493 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001494 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1495 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1496 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1497 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001498 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001499 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001500psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1501 uint8_t *mac,
1502 size_t mac_size,
1503 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001504
Gilles Peskinedcd14942018-07-12 00:30:52 +02001505/** Finish the calculation of the MAC of a message and compare it with
1506 * an expected value.
1507 *
1508 * The application must call psa_mac_verify_setup() before calling this function.
1509 * This function calculates the MAC of the message formed by concatenating
1510 * the inputs passed to preceding calls to psa_mac_update(). It then
1511 * compares the calculated MAC with the expected MAC passed as a
1512 * parameter to this function.
1513 *
1514 * When this function returns, the operation becomes inactive.
1515 *
1516 * \note Implementations shall make the best effort to ensure that the
1517 * comparison between the actual MAC and the expected MAC is performed
1518 * in constant time.
1519 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001520 * \param[in,out] operation Active MAC operation.
1521 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001522 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001523 *
1524 * \retval #PSA_SUCCESS
1525 * The expected MAC is identical to the actual MAC of the message.
1526 * \retval #PSA_ERROR_INVALID_SIGNATURE
1527 * The MAC of the message was calculated successfully, but it
1528 * differs from the expected MAC.
1529 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001530 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001531 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1532 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1533 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001534 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001535 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001536psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1537 const uint8_t *mac,
1538 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001539
Gilles Peskinedcd14942018-07-12 00:30:52 +02001540/** Abort a MAC operation.
1541 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001542 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001543 * \p operation structure itself. Once aborted, the operation object
1544 * can be reused for another operation by calling
1545 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001546 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001547 * You may call this function any time after the operation object has
1548 * been initialized by any of the following methods:
1549 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1550 * it succeeds or not.
1551 * - Initializing the \c struct to all-bits-zero.
1552 * - Initializing the \c struct to logical zeros, e.g.
1553 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001554 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001555 * In particular, calling psa_mac_abort() after the operation has been
1556 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1557 * psa_mac_verify_finish() is safe and has no effect.
1558 *
1559 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001560 *
1561 * \retval #PSA_SUCCESS
1562 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001563 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001564 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1565 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001566 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001567 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001568psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1569
1570/**@}*/
1571
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001572/** \defgroup cipher Symmetric ciphers
1573 * @{
1574 */
1575
Gilles Peskine69647a42019-01-14 20:18:12 +01001576/** Encrypt a message using a symmetric cipher.
1577 *
1578 * This function encrypts a message with a random IV (initialization
1579 * vector).
1580 *
1581 * \param handle Handle to the key to use for the operation.
1582 * It must remain valid until the operation
1583 * terminates.
1584 * \param alg The cipher algorithm to compute
1585 * (\c PSA_ALG_XXX value such that
1586 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1587 * \param[in] input Buffer containing the message to encrypt.
1588 * \param input_length Size of the \p input buffer in bytes.
1589 * \param[out] output Buffer where the output is to be written.
1590 * The output contains the IV followed by
1591 * the ciphertext proper.
1592 * \param output_size Size of the \p output buffer in bytes.
1593 * \param[out] output_length On success, the number of bytes
1594 * that make up the output.
1595 *
1596 * \retval #PSA_SUCCESS
1597 * Success.
1598 * \retval #PSA_ERROR_INVALID_HANDLE
1599 * \retval #PSA_ERROR_EMPTY_SLOT
1600 * \retval #PSA_ERROR_NOT_PERMITTED
1601 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001602 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001603 * \retval #PSA_ERROR_NOT_SUPPORTED
1604 * \p alg is not supported or is not a cipher algorithm.
1605 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1606 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1607 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1608 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001609 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001610 */
1611psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1612 psa_algorithm_t alg,
1613 const uint8_t *input,
1614 size_t input_length,
1615 uint8_t *output,
1616 size_t output_size,
1617 size_t *output_length);
1618
1619/** Decrypt a message using a symmetric cipher.
1620 *
1621 * This function decrypts a message encrypted with a symmetric cipher.
1622 *
1623 * \param handle Handle to the key to use for the operation.
1624 * It must remain valid until the operation
1625 * terminates.
1626 * \param alg The cipher algorithm to compute
1627 * (\c PSA_ALG_XXX value such that
1628 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1629 * \param[in] input Buffer containing the message to decrypt.
1630 * This consists of the IV followed by the
1631 * ciphertext proper.
1632 * \param input_length Size of the \p input buffer in bytes.
1633 * \param[out] output Buffer where the plaintext is to be written.
1634 * \param output_size Size of the \p output buffer in bytes.
1635 * \param[out] output_length On success, the number of bytes
1636 * that make up the output.
1637 *
1638 * \retval #PSA_SUCCESS
1639 * Success.
1640 * \retval #PSA_ERROR_INVALID_HANDLE
1641 * \retval #PSA_ERROR_EMPTY_SLOT
1642 * \retval #PSA_ERROR_NOT_PERMITTED
1643 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001644 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001645 * \retval #PSA_ERROR_NOT_SUPPORTED
1646 * \p alg is not supported or is not a cipher algorithm.
1647 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1648 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1649 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1650 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001651 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001652 */
1653psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1654 psa_algorithm_t alg,
1655 const uint8_t *input,
1656 size_t input_length,
1657 uint8_t *output,
1658 size_t output_size,
1659 size_t *output_length);
1660
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001661/** The type of the state data structure for multipart cipher operations.
1662 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001663 * Before calling any function on a cipher operation object, the application
1664 * must initialize it by any of the following means:
1665 * - Set the structure to all-bits-zero, for example:
1666 * \code
1667 * psa_cipher_operation_t operation;
1668 * memset(&operation, 0, sizeof(operation));
1669 * \endcode
1670 * - Initialize the structure to logical zero values, for example:
1671 * \code
1672 * psa_cipher_operation_t operation = {0};
1673 * \endcode
1674 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1675 * for example:
1676 * \code
1677 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1678 * \endcode
1679 * - Assign the result of the function psa_cipher_operation_init()
1680 * to the structure, for example:
1681 * \code
1682 * psa_cipher_operation_t operation;
1683 * operation = psa_cipher_operation_init();
1684 * \endcode
1685 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001686 * This is an implementation-defined \c struct. Applications should not
1687 * make any assumptions about the content of this structure except
1688 * as directed by the documentation of a specific implementation. */
1689typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1690
Jaeden Amero5bae2272019-01-04 11:48:27 +00001691/** \def PSA_CIPHER_OPERATION_INIT
1692 *
1693 * This macro returns a suitable initializer for a cipher operation object of
1694 * type #psa_cipher_operation_t.
1695 */
1696#ifdef __DOXYGEN_ONLY__
1697/* This is an example definition for documentation purposes.
1698 * Implementations should define a suitable value in `crypto_struct.h`.
1699 */
1700#define PSA_CIPHER_OPERATION_INIT {0}
1701#endif
1702
1703/** Return an initial value for a cipher operation object.
1704 */
1705static psa_cipher_operation_t psa_cipher_operation_init(void);
1706
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001707/** Set the key for a multipart symmetric encryption operation.
1708 *
1709 * The sequence of operations to encrypt a message with a symmetric cipher
1710 * is as follows:
1711 * -# Allocate an operation object which will be passed to all the functions
1712 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001713 * -# Initialize the operation object with one of the methods described in the
1714 * documentation for #psa_cipher_operation_t, e.g.
1715 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001716 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001717 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001718 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001719 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001720 * requires a specific IV value.
1721 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1722 * of the message each time.
1723 * -# Call psa_cipher_finish().
1724 *
1725 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001726 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001727 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001728 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001729 * eventually terminate the operation. The following events terminate an
1730 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001731 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001732 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001733 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001734 * \param[in,out] operation The operation object to set up. It must have
1735 * been initialized as per the documentation for
1736 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001737 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001738 * It must remain valid until the operation
1739 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001740 * \param alg The cipher algorithm to compute
1741 * (\c PSA_ALG_XXX value such that
1742 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001743 *
Gilles Peskine28538492018-07-11 17:34:00 +02001744 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001745 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001746 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001747 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001748 * \retval #PSA_ERROR_NOT_PERMITTED
1749 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001750 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001751 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001752 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001753 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1754 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1755 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001756 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001757 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001758 * The operation state is not valid (already set up and not
1759 * subsequently completed).
1760 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001761 * The library has not been previously initialized by psa_crypto_init().
1762 * It is implementation-dependent whether a failure to initialize
1763 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001764 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001765psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001766 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001767 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001768
1769/** Set the key for a multipart symmetric decryption operation.
1770 *
1771 * The sequence of operations to decrypt a message with a symmetric cipher
1772 * is as follows:
1773 * -# Allocate an operation object which will be passed to all the functions
1774 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001775 * -# Initialize the operation object with one of the methods described in the
1776 * documentation for #psa_cipher_operation_t, e.g.
1777 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001778 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001779 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001780 * decryption. If the IV is prepended to the ciphertext, you can call
1781 * psa_cipher_update() on a buffer containing the IV followed by the
1782 * beginning of the message.
1783 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1784 * of the message each time.
1785 * -# Call psa_cipher_finish().
1786 *
1787 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001788 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001789 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001790 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001791 * eventually terminate the operation. The following events terminate an
1792 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001793 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001794 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001795 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001796 * \param[in,out] operation The operation object to set up. It must have
1797 * been initialized as per the documentation for
1798 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001799 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001800 * It must remain valid until the operation
1801 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001802 * \param alg The cipher algorithm to compute
1803 * (\c PSA_ALG_XXX value such that
1804 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001805 *
Gilles Peskine28538492018-07-11 17:34:00 +02001806 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001807 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001808 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001809 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001810 * \retval #PSA_ERROR_NOT_PERMITTED
1811 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001812 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001813 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001814 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001815 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1816 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1817 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001818 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001819 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001820 * The operation state is not valid (already set up and not
1821 * subsequently completed).
1822 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001823 * The library has not been previously initialized by psa_crypto_init().
1824 * It is implementation-dependent whether a failure to initialize
1825 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001826 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001827psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001828 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001829 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001830
Gilles Peskinedcd14942018-07-12 00:30:52 +02001831/** Generate an IV for a symmetric encryption operation.
1832 *
1833 * This function generates a random IV (initialization vector), nonce
1834 * or initial counter value for the encryption operation as appropriate
1835 * for the chosen algorithm, key type and key size.
1836 *
1837 * The application must call psa_cipher_encrypt_setup() before
1838 * calling this function.
1839 *
1840 * If this function returns an error status, the operation becomes inactive.
1841 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001842 * \param[in,out] operation Active cipher operation.
1843 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001844 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001845 * \param[out] iv_length On success, the number of bytes of the
1846 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001847 *
1848 * \retval #PSA_SUCCESS
1849 * Success.
1850 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001851 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001852 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001853 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001854 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1855 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1856 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001857 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001858 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001859psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1860 unsigned char *iv,
1861 size_t iv_size,
1862 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001863
Gilles Peskinedcd14942018-07-12 00:30:52 +02001864/** Set the IV for a symmetric encryption or decryption operation.
1865 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001866 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001867 * or initial counter value for the encryption or decryption operation.
1868 *
1869 * The application must call psa_cipher_encrypt_setup() before
1870 * calling this function.
1871 *
1872 * If this function returns an error status, the operation becomes inactive.
1873 *
1874 * \note When encrypting, applications should use psa_cipher_generate_iv()
1875 * instead of this function, unless implementing a protocol that requires
1876 * a non-random IV.
1877 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001878 * \param[in,out] operation Active cipher operation.
1879 * \param[in] iv Buffer containing the IV to use.
1880 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001881 *
1882 * \retval #PSA_SUCCESS
1883 * Success.
1884 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001885 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001886 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001887 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001888 * or the chosen algorithm does not use an IV.
1889 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1890 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1891 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001892 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001893 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001894psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1895 const unsigned char *iv,
1896 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001897
Gilles Peskinedcd14942018-07-12 00:30:52 +02001898/** Encrypt or decrypt a message fragment in an active cipher operation.
1899 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001900 * Before calling this function, you must:
1901 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1902 * The choice of setup function determines whether this function
1903 * encrypts or decrypts its input.
1904 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1905 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001906 *
1907 * If this function returns an error status, the operation becomes inactive.
1908 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001909 * \param[in,out] operation Active cipher operation.
1910 * \param[in] input Buffer containing the message fragment to
1911 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001912 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001913 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001914 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001915 * \param[out] output_length On success, the number of bytes
1916 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001917 *
1918 * \retval #PSA_SUCCESS
1919 * Success.
1920 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001921 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001922 * not set, or already completed).
1923 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1924 * The size of the \p output buffer is too small.
1925 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1926 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1927 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001928 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001929 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001930psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1931 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001932 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001933 unsigned char *output,
1934 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001935 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001936
Gilles Peskinedcd14942018-07-12 00:30:52 +02001937/** Finish encrypting or decrypting a message in a cipher operation.
1938 *
1939 * The application must call psa_cipher_encrypt_setup() or
1940 * psa_cipher_decrypt_setup() before calling this function. The choice
1941 * of setup function determines whether this function encrypts or
1942 * decrypts its input.
1943 *
1944 * This function finishes the encryption or decryption of the message
1945 * formed by concatenating the inputs passed to preceding calls to
1946 * psa_cipher_update().
1947 *
1948 * When this function returns, the operation becomes inactive.
1949 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001950 * \param[in,out] operation Active cipher operation.
1951 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001952 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001953 * \param[out] output_length On success, the number of bytes
1954 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001955 *
1956 * \retval #PSA_SUCCESS
1957 * Success.
1958 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001959 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001960 * not set, or already completed).
1961 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1962 * The size of the \p output buffer is too small.
1963 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1964 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1965 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001966 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001967 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001968psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001969 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001970 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001971 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001972
Gilles Peskinedcd14942018-07-12 00:30:52 +02001973/** Abort a cipher operation.
1974 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001975 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001976 * \p operation structure itself. Once aborted, the operation object
1977 * can be reused for another operation by calling
1978 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001979 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001980 * You may call this function any time after the operation object has
1981 * been initialized by any of the following methods:
1982 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1983 * whether it succeeds or not.
1984 * - Initializing the \c struct to all-bits-zero.
1985 * - Initializing the \c struct to logical zeros, e.g.
1986 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001987 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001988 * In particular, calling psa_cipher_abort() after the operation has been
1989 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1990 * is safe and has no effect.
1991 *
1992 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001993 *
1994 * \retval #PSA_SUCCESS
1995 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001996 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001997 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1998 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001999 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02002000 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002001psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2002
2003/**@}*/
2004
Gilles Peskine3b555712018-03-03 21:27:57 +01002005/** \defgroup aead Authenticated encryption with associated data (AEAD)
2006 * @{
2007 */
2008
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002009/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002010 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002011 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002012 * \param alg The AEAD algorithm to compute
2013 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002014 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002015 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002016 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002017 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002018 * but not encrypted.
2019 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002020 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002021 * encrypted.
2022 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002023 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002024 * encrypted data. The additional data is not
2025 * part of this output. For algorithms where the
2026 * encrypted data and the authentication tag
2027 * are defined as separate outputs, the
2028 * authentication tag is appended to the
2029 * encrypted data.
2030 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2031 * This must be at least
2032 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2033 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002034 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002035 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002036 *
Gilles Peskine28538492018-07-11 17:34:00 +02002037 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002038 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002039 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002040 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002041 * \retval #PSA_ERROR_NOT_PERMITTED
2042 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002043 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002044 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002045 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002046 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2047 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2048 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002049 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002050 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002051 * The library has not been previously initialized by psa_crypto_init().
2052 * It is implementation-dependent whether a failure to initialize
2053 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002054 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002055psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002056 psa_algorithm_t alg,
2057 const uint8_t *nonce,
2058 size_t nonce_length,
2059 const uint8_t *additional_data,
2060 size_t additional_data_length,
2061 const uint8_t *plaintext,
2062 size_t plaintext_length,
2063 uint8_t *ciphertext,
2064 size_t ciphertext_size,
2065 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002066
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002067/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002068 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002069 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002070 * \param alg The AEAD algorithm to compute
2071 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002072 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002073 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002074 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002075 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002076 * but not encrypted.
2077 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002078 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002079 * encrypted. For algorithms where the
2080 * encrypted data and the authentication tag
2081 * are defined as separate inputs, the buffer
2082 * must contain the encrypted data followed
2083 * by the authentication tag.
2084 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002085 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002086 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2087 * This must be at least
2088 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2089 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002090 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002091 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002092 *
Gilles Peskine28538492018-07-11 17:34:00 +02002093 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002094 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002095 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002096 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002097 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002098 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002099 * \retval #PSA_ERROR_NOT_PERMITTED
2100 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002101 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002102 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002103 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002104 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2105 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2106 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002107 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002108 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002109 * The library has not been previously initialized by psa_crypto_init().
2110 * It is implementation-dependent whether a failure to initialize
2111 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002112 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002113psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002114 psa_algorithm_t alg,
2115 const uint8_t *nonce,
2116 size_t nonce_length,
2117 const uint8_t *additional_data,
2118 size_t additional_data_length,
2119 const uint8_t *ciphertext,
2120 size_t ciphertext_length,
2121 uint8_t *plaintext,
2122 size_t plaintext_size,
2123 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002124
Gilles Peskine30a9e412019-01-14 18:36:12 +01002125/** The type of the state data structure for multipart AEAD operations.
2126 *
2127 * Before calling any function on an AEAD operation object, the application
2128 * must initialize it by any of the following means:
2129 * - Set the structure to all-bits-zero, for example:
2130 * \code
2131 * psa_aead_operation_t operation;
2132 * memset(&operation, 0, sizeof(operation));
2133 * \endcode
2134 * - Initialize the structure to logical zero values, for example:
2135 * \code
2136 * psa_aead_operation_t operation = {0};
2137 * \endcode
2138 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2139 * for example:
2140 * \code
2141 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2142 * \endcode
2143 * - Assign the result of the function psa_aead_operation_init()
2144 * to the structure, for example:
2145 * \code
2146 * psa_aead_operation_t operation;
2147 * operation = psa_aead_operation_init();
2148 * \endcode
2149 *
2150 * This is an implementation-defined \c struct. Applications should not
2151 * make any assumptions about the content of this structure except
2152 * as directed by the documentation of a specific implementation. */
2153typedef struct psa_aead_operation_s psa_aead_operation_t;
2154
2155/** \def PSA_AEAD_OPERATION_INIT
2156 *
2157 * This macro returns a suitable initializer for an AEAD operation object of
2158 * type #psa_aead_operation_t.
2159 */
2160#ifdef __DOXYGEN_ONLY__
2161/* This is an example definition for documentation purposes.
2162 * Implementations should define a suitable value in `crypto_struct.h`.
2163 */
2164#define PSA_AEAD_OPERATION_INIT {0}
2165#endif
2166
2167/** Return an initial value for an AEAD operation object.
2168 */
2169static psa_aead_operation_t psa_aead_operation_init(void);
2170
2171/** Set the key for a multipart authenticated encryption operation.
2172 *
2173 * The sequence of operations to encrypt a message with authentication
2174 * is as follows:
2175 * -# Allocate an operation object which will be passed to all the functions
2176 * listed here.
2177 * -# Initialize the operation object with one of the methods described in the
2178 * documentation for #psa_aead_operation_t, e.g.
2179 * PSA_AEAD_OPERATION_INIT.
2180 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002181 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2182 * inputs to the subsequent calls to psa_aead_update_ad() and
2183 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2184 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002185 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2186 * generate or set the nonce. You should use
2187 * psa_aead_generate_nonce() unless the protocol you are implementing
2188 * requires a specific nonce value.
2189 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2190 * of the non-encrypted additional authenticated data each time.
2191 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002192 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002193 * -# Call psa_aead_finish().
2194 *
2195 * The application may call psa_aead_abort() at any time after the operation
2196 * has been initialized.
2197 *
2198 * After a successful call to psa_aead_encrypt_setup(), the application must
2199 * eventually terminate the operation. The following events terminate an
2200 * operation:
2201 * - A failed call to any of the \c psa_aead_xxx functions.
2202 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2203 *
2204 * \param[in,out] operation The operation object to set up. It must have
2205 * been initialized as per the documentation for
2206 * #psa_aead_operation_t and not yet in use.
2207 * \param handle Handle to the key to use for the operation.
2208 * It must remain valid until the operation
2209 * terminates.
2210 * \param alg The AEAD algorithm to compute
2211 * (\c PSA_ALG_XXX value such that
2212 * #PSA_ALG_IS_AEAD(\p alg) is true).
2213 *
2214 * \retval #PSA_SUCCESS
2215 * Success.
2216 * \retval #PSA_ERROR_INVALID_HANDLE
2217 * \retval #PSA_ERROR_EMPTY_SLOT
2218 * \retval #PSA_ERROR_NOT_PERMITTED
2219 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002220 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002221 * \retval #PSA_ERROR_NOT_SUPPORTED
2222 * \p alg is not supported or is not an AEAD algorithm.
2223 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2224 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2225 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002226 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002227 * \retval #PSA_ERROR_BAD_STATE
2228 * The library has not been previously initialized by psa_crypto_init().
2229 * It is implementation-dependent whether a failure to initialize
2230 * results in this error code.
2231 */
2232psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2233 psa_key_handle_t handle,
2234 psa_algorithm_t alg);
2235
2236/** Set the key for a multipart authenticated decryption operation.
2237 *
2238 * The sequence of operations to decrypt a message with authentication
2239 * is as follows:
2240 * -# Allocate an operation object which will be passed to all the functions
2241 * listed here.
2242 * -# Initialize the operation object with one of the methods described in the
2243 * documentation for #psa_aead_operation_t, e.g.
2244 * PSA_AEAD_OPERATION_INIT.
2245 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002246 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2247 * inputs to the subsequent calls to psa_aead_update_ad() and
2248 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2249 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002250 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2251 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2252 * of the non-encrypted additional authenticated data each time.
2253 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002254 * of the ciphertext to decrypt each time.
2255 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002256 *
2257 * The application may call psa_aead_abort() at any time after the operation
2258 * has been initialized.
2259 *
2260 * After a successful call to psa_aead_decrypt_setup(), the application must
2261 * eventually terminate the operation. The following events terminate an
2262 * operation:
2263 * - A failed call to any of the \c psa_aead_xxx functions.
2264 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2265 *
2266 * \param[in,out] operation The operation object to set up. It must have
2267 * been initialized as per the documentation for
2268 * #psa_aead_operation_t and not yet in use.
2269 * \param handle Handle to the key to use for the operation.
2270 * It must remain valid until the operation
2271 * terminates.
2272 * \param alg The AEAD algorithm to compute
2273 * (\c PSA_ALG_XXX value such that
2274 * #PSA_ALG_IS_AEAD(\p alg) is true).
2275 *
2276 * \retval #PSA_SUCCESS
2277 * Success.
2278 * \retval #PSA_ERROR_INVALID_HANDLE
2279 * \retval #PSA_ERROR_EMPTY_SLOT
2280 * \retval #PSA_ERROR_NOT_PERMITTED
2281 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002282 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002283 * \retval #PSA_ERROR_NOT_SUPPORTED
2284 * \p alg is not supported or is not an AEAD algorithm.
2285 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2286 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2287 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002288 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002289 * \retval #PSA_ERROR_BAD_STATE
2290 * The library has not been previously initialized by psa_crypto_init().
2291 * It is implementation-dependent whether a failure to initialize
2292 * results in this error code.
2293 */
2294psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2295 psa_key_handle_t handle,
2296 psa_algorithm_t alg);
2297
2298/** Generate a random nonce for an authenticated encryption operation.
2299 *
2300 * This function generates a random nonce for the authenticated encryption
2301 * operation with an appropriate size for the chosen algorithm, key type
2302 * and key size.
2303 *
2304 * The application must call psa_aead_encrypt_setup() before
2305 * calling this function.
2306 *
2307 * If this function returns an error status, the operation becomes inactive.
2308 *
2309 * \param[in,out] operation Active AEAD operation.
2310 * \param[out] nonce Buffer where the generated nonce is to be
2311 * written.
2312 * \param nonce_size Size of the \p nonce buffer in bytes.
2313 * \param[out] nonce_length On success, the number of bytes of the
2314 * generated nonce.
2315 *
2316 * \retval #PSA_SUCCESS
2317 * Success.
2318 * \retval #PSA_ERROR_BAD_STATE
2319 * The operation state is not valid (not set up, or nonce already set).
2320 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2321 * The size of the \p nonce buffer is too small.
2322 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2323 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2324 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002325 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002326 */
2327psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2328 unsigned char *nonce,
2329 size_t nonce_size,
2330 size_t *nonce_length);
2331
2332/** Set the nonce for an authenticated encryption or decryption operation.
2333 *
2334 * This function sets the nonce for the authenticated
2335 * encryption or decryption operation.
2336 *
2337 * The application must call psa_aead_encrypt_setup() before
2338 * calling this function.
2339 *
2340 * If this function returns an error status, the operation becomes inactive.
2341 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002342 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002343 * instead of this function, unless implementing a protocol that requires
2344 * a non-random IV.
2345 *
2346 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002347 * \param[in] nonce Buffer containing the nonce to use.
2348 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002349 *
2350 * \retval #PSA_SUCCESS
2351 * Success.
2352 * \retval #PSA_ERROR_BAD_STATE
2353 * The operation state is not valid (not set up, or nonce already set).
2354 * \retval #PSA_ERROR_INVALID_ARGUMENT
2355 * The size of \p nonce is not acceptable for the chosen algorithm.
2356 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2357 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2358 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002359 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002360 */
2361psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2362 const unsigned char *nonce,
2363 size_t nonce_length);
2364
Gilles Peskinebc59c852019-01-17 15:26:08 +01002365/** Declare the lengths of the message and additional data for AEAD.
2366 *
2367 * The application must call this function before calling
2368 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2369 * the operation requires it. If the algorithm does not require it,
2370 * calling this function is optional, but if this function is called
2371 * then the implementation must enforce the lengths.
2372 *
2373 * You may call this function before or after setting the nonce with
2374 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2375 *
2376 * - For #PSA_ALG_CCM, calling this function is required.
2377 * - For the other AEAD algorithms defined in this specification, calling
2378 * this function is not required.
2379 * - For vendor-defined algorithm, refer to the vendor documentation.
2380 *
2381 * \param[in,out] operation Active AEAD operation.
2382 * \param ad_length Size of the non-encrypted additional
2383 * authenticated data in bytes.
2384 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2385 *
2386 * \retval #PSA_SUCCESS
2387 * Success.
2388 * \retval #PSA_ERROR_BAD_STATE
2389 * The operation state is not valid (not set up, already completed,
2390 * or psa_aead_update_ad() or psa_aead_update() already called).
2391 * \retval #PSA_ERROR_INVALID_ARGUMENT
2392 * At least one of the lengths is not acceptable for the chosen
2393 * algorithm.
2394 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2395 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2396 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002397 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinebc59c852019-01-17 15:26:08 +01002398 */
2399psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2400 size_t ad_length,
2401 size_t plaintext_length);
2402
Gilles Peskine30a9e412019-01-14 18:36:12 +01002403/** Pass additional data to an active AEAD operation.
2404 *
2405 * Additional data is authenticated, but not encrypted.
2406 *
2407 * You may call this function multiple times to pass successive fragments
2408 * of the additional data. You may not call this function after passing
2409 * data to encrypt or decrypt with psa_aead_update().
2410 *
2411 * Before calling this function, you must:
2412 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2413 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2414 *
2415 * If this function returns an error status, the operation becomes inactive.
2416 *
2417 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2418 * there is no guarantee that the input is valid. Therefore, until
2419 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2420 * treat the input as untrusted and prepare to undo any action that
2421 * depends on the input if psa_aead_verify() returns an error status.
2422 *
2423 * \param[in,out] operation Active AEAD operation.
2424 * \param[in] input Buffer containing the fragment of
2425 * additional data.
2426 * \param input_length Size of the \p input buffer in bytes.
2427 *
2428 * \retval #PSA_SUCCESS
2429 * Success.
2430 * \retval #PSA_ERROR_BAD_STATE
2431 * The operation state is not valid (not set up, nonce not set,
2432 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002433 * \retval #PSA_ERROR_INVALID_ARGUMENT
2434 * The total input length overflows the additional data length that
2435 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002436 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2437 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2438 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002439 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002440 */
2441psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2442 const uint8_t *input,
2443 size_t input_length);
2444
2445/** Encrypt or decrypt a message fragment in an active AEAD operation.
2446 *
2447 * Before calling this function, you must:
2448 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2449 * The choice of setup function determines whether this function
2450 * encrypts or decrypts its input.
2451 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2452 * 3. Call psa_aead_update_ad() to pass all the additional data.
2453 *
2454 * If this function returns an error status, the operation becomes inactive.
2455 *
2456 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2457 * there is no guarantee that the input is valid. Therefore, until
2458 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2459 * - Do not use the output in any way other than storing it in a
2460 * confidential location. If you take any action that depends
2461 * on the tentative decrypted data, this action will need to be
2462 * undone if the input turns out not to be valid. Furthermore,
2463 * if an adversary can observe that this action took place
2464 * (for example through timing), they may be able to use this
2465 * fact as an oracle to decrypt any message encrypted with the
2466 * same key.
2467 * - In particular, do not copy the output anywhere but to a
2468 * memory or storage space that you have exclusive access to.
2469 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002470 * This function does not require the input to be aligned to any
2471 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002472 * a whole block at a time, it must consume all the input provided, but
2473 * it may delay the end of the corresponding output until a subsequent
2474 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2475 * provides sufficient input. The amount of data that can be delayed
2476 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002477 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002478 * \param[in,out] operation Active AEAD operation.
2479 * \param[in] input Buffer containing the message fragment to
2480 * encrypt or decrypt.
2481 * \param input_length Size of the \p input buffer in bytes.
2482 * \param[out] output Buffer where the output is to be written.
2483 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002484 * This must be at least
2485 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2486 * \p input_length) where \c alg is the
2487 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002488 * \param[out] output_length On success, the number of bytes
2489 * that make up the returned output.
2490 *
2491 * \retval #PSA_SUCCESS
2492 * Success.
2493 * \retval #PSA_ERROR_BAD_STATE
2494 * The operation state is not valid (not set up, nonce not set
2495 * or already completed).
2496 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2497 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002498 * You can determine a sufficient buffer size by calling
2499 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2500 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002501 * \retval #PSA_ERROR_INVALID_ARGUMENT
2502 * The total length of input to psa_aead_update_ad() so far is
2503 * less than the additional data length that was previously
2504 * specified with psa_aead_set_lengths().
2505 * \retval #PSA_ERROR_INVALID_ARGUMENT
2506 * The total input length overflows the plaintext length that
2507 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002508 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2509 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2510 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002511 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002512 */
2513psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2514 const uint8_t *input,
2515 size_t input_length,
2516 unsigned char *output,
2517 size_t output_size,
2518 size_t *output_length);
2519
2520/** Finish encrypting a message in an AEAD operation.
2521 *
2522 * The operation must have been set up with psa_aead_encrypt_setup().
2523 *
2524 * This function finishes the authentication of the additional data
2525 * formed by concatenating the inputs passed to preceding calls to
2526 * psa_aead_update_ad() with the plaintext formed by concatenating the
2527 * inputs passed to preceding calls to psa_aead_update().
2528 *
2529 * This function has two output buffers:
2530 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002531 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002532 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002533 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002534 * that the operation performs.
2535 *
2536 * When this function returns, the operation becomes inactive.
2537 *
2538 * \param[in,out] operation Active AEAD operation.
2539 * \param[out] ciphertext Buffer where the last part of the ciphertext
2540 * is to be written.
2541 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002542 * This must be at least
2543 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2544 * \c alg is the algorithm that is being
2545 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002546 * \param[out] ciphertext_length On success, the number of bytes of
2547 * returned ciphertext.
2548 * \param[out] tag Buffer where the authentication tag is
2549 * to be written.
2550 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002551 * This must be at least
2552 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2553 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002554 * \param[out] tag_length On success, the number of bytes
2555 * that make up the returned tag.
2556 *
2557 * \retval #PSA_SUCCESS
2558 * Success.
2559 * \retval #PSA_ERROR_BAD_STATE
2560 * The operation state is not valid (not set up, nonce not set,
2561 * decryption, or already completed).
2562 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002563 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002564 * You can determine a sufficient buffer size for \p ciphertext by
2565 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2566 * where \c alg is the algorithm that is being calculated.
2567 * You can determine a sufficient buffer size for \p tag by
2568 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002569 * \retval #PSA_ERROR_INVALID_ARGUMENT
2570 * The total length of input to psa_aead_update_ad() so far is
2571 * less than the additional data length that was previously
2572 * specified with psa_aead_set_lengths().
2573 * \retval #PSA_ERROR_INVALID_ARGUMENT
2574 * The total length of input to psa_aead_update() so far is
2575 * less than the plaintext length that was previously
2576 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002577 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2578 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2579 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002580 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002581 */
2582psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002583 uint8_t *ciphertext,
2584 size_t ciphertext_size,
2585 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002586 uint8_t *tag,
2587 size_t tag_size,
2588 size_t *tag_length);
2589
2590/** Finish authenticating and decrypting a message in an AEAD operation.
2591 *
2592 * The operation must have been set up with psa_aead_decrypt_setup().
2593 *
2594 * This function finishes the authentication of the additional data
2595 * formed by concatenating the inputs passed to preceding calls to
2596 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2597 * inputs passed to preceding calls to psa_aead_update().
2598 *
2599 * When this function returns, the operation becomes inactive.
2600 *
2601 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002602 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002603 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002604 * from previous calls to psa_aead_update()
2605 * that could not be processed until the end
2606 * of the input.
2607 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002608 * This must be at least
2609 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2610 * \c alg is the algorithm that is being
2611 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002612 * \param[out] plaintext_length On success, the number of bytes of
2613 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002614 * \param[in] tag Buffer containing the authentication tag.
2615 * \param tag_length Size of the \p tag buffer in bytes.
2616 *
2617 * \retval #PSA_SUCCESS
2618 * Success.
2619 * \retval #PSA_ERROR_BAD_STATE
2620 * The operation state is not valid (not set up, nonce not set,
2621 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002622 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2623 * The size of the \p plaintext buffer is too small.
2624 * You can determine a sufficient buffer size for \p plaintext by
2625 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2626 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002627 * \retval #PSA_ERROR_INVALID_ARGUMENT
2628 * The total length of input to psa_aead_update_ad() so far is
2629 * less than the additional data length that was previously
2630 * specified with psa_aead_set_lengths().
2631 * \retval #PSA_ERROR_INVALID_ARGUMENT
2632 * The total length of input to psa_aead_update() so far is
2633 * less than the plaintext length that was previously
2634 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002635 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2636 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2637 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002638 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002639 */
2640psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002641 uint8_t *plaintext,
2642 size_t plaintext_size,
2643 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002644 const uint8_t *tag,
2645 size_t tag_length);
2646
2647/** Abort an AEAD operation.
2648 *
2649 * Aborting an operation frees all associated resources except for the
2650 * \p operation structure itself. Once aborted, the operation object
2651 * can be reused for another operation by calling
2652 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2653 *
2654 * You may call this function any time after the operation object has
2655 * been initialized by any of the following methods:
2656 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2657 * whether it succeeds or not.
2658 * - Initializing the \c struct to all-bits-zero.
2659 * - Initializing the \c struct to logical zeros, e.g.
2660 * `psa_aead_operation_t operation = {0}`.
2661 *
2662 * In particular, calling psa_aead_abort() after the operation has been
2663 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2664 * is safe and has no effect.
2665 *
2666 * \param[in,out] operation Initialized AEAD operation.
2667 *
2668 * \retval #PSA_SUCCESS
2669 * \retval #PSA_ERROR_BAD_STATE
2670 * \p operation is not an active AEAD operation.
2671 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2672 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002673 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002674 */
2675psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2676
Gilles Peskine3b555712018-03-03 21:27:57 +01002677/**@}*/
2678
Gilles Peskine20035e32018-02-03 22:44:14 +01002679/** \defgroup asymmetric Asymmetric cryptography
2680 * @{
2681 */
2682
2683/**
2684 * \brief Sign a hash or short message with a private key.
2685 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002686 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002687 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002688 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2689 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2690 * to determine the hash algorithm to use.
2691 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002692 * \param handle Handle to the key to use for the operation.
2693 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002694 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002695 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002696 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002697 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002698 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002699 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002700 * \param[out] signature_length On success, the number of bytes
2701 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002702 *
Gilles Peskine28538492018-07-11 17:34:00 +02002703 * \retval #PSA_SUCCESS
2704 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002705 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002706 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002707 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002708 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002709 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002710 * \retval #PSA_ERROR_NOT_SUPPORTED
2711 * \retval #PSA_ERROR_INVALID_ARGUMENT
2712 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2713 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2714 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002715 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002716 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002717 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002718 * The library has not been previously initialized by psa_crypto_init().
2719 * It is implementation-dependent whether a failure to initialize
2720 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002721 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002722psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002723 psa_algorithm_t alg,
2724 const uint8_t *hash,
2725 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002726 uint8_t *signature,
2727 size_t signature_size,
2728 size_t *signature_length);
2729
2730/**
2731 * \brief Verify the signature a hash or short message using a public key.
2732 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002733 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002734 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002735 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2736 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2737 * to determine the hash algorithm to use.
2738 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002739 * \param handle Handle to the key to use for the operation.
2740 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002741 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002742 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002743 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002744 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002745 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002746 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002747 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002748 *
Gilles Peskine28538492018-07-11 17:34:00 +02002749 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002750 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002751 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002752 * The calculation was perfomed successfully, but the passed
2753 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002754 * \retval #PSA_ERROR_NOT_SUPPORTED
2755 * \retval #PSA_ERROR_INVALID_ARGUMENT
2756 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2757 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2758 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002759 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002760 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002761 * The library has not been previously initialized by psa_crypto_init().
2762 * It is implementation-dependent whether a failure to initialize
2763 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002764 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002765psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002766 psa_algorithm_t alg,
2767 const uint8_t *hash,
2768 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002769 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002770 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002771
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002772/**
2773 * \brief Encrypt a short message with a public key.
2774 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002775 * \param handle Handle to the key to use for the operation.
2776 * It must be a public key or an asymmetric
2777 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002778 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002779 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002780 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002781 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002782 * \param[in] salt A salt or label, if supported by the
2783 * encryption algorithm.
2784 * If the algorithm does not support a
2785 * salt, pass \c NULL.
2786 * If the algorithm supports an optional
2787 * salt and you do not want to pass a salt,
2788 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002789 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002790 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2791 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002792 * \param salt_length Size of the \p salt buffer in bytes.
2793 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002794 * \param[out] output Buffer where the encrypted message is to
2795 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002796 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002797 * \param[out] output_length On success, the number of bytes
2798 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002799 *
Gilles Peskine28538492018-07-11 17:34:00 +02002800 * \retval #PSA_SUCCESS
2801 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002802 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002803 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002804 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002805 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002806 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002807 * \retval #PSA_ERROR_NOT_SUPPORTED
2808 * \retval #PSA_ERROR_INVALID_ARGUMENT
2809 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2810 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2811 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002812 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002813 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002814 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002815 * The library has not been previously initialized by psa_crypto_init().
2816 * It is implementation-dependent whether a failure to initialize
2817 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002818 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002819psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002820 psa_algorithm_t alg,
2821 const uint8_t *input,
2822 size_t input_length,
2823 const uint8_t *salt,
2824 size_t salt_length,
2825 uint8_t *output,
2826 size_t output_size,
2827 size_t *output_length);
2828
2829/**
2830 * \brief Decrypt a short message with a private key.
2831 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002832 * \param handle Handle to the key to use for the operation.
2833 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002834 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002835 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002836 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002837 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002838 * \param[in] salt A salt or label, if supported by the
2839 * encryption algorithm.
2840 * If the algorithm does not support a
2841 * salt, pass \c NULL.
2842 * If the algorithm supports an optional
2843 * salt and you do not want to pass a salt,
2844 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002845 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002846 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2847 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002848 * \param salt_length Size of the \p salt buffer in bytes.
2849 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002850 * \param[out] output Buffer where the decrypted message is to
2851 * be written.
2852 * \param output_size Size of the \c output buffer in bytes.
2853 * \param[out] output_length On success, the number of bytes
2854 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002855 *
Gilles Peskine28538492018-07-11 17:34:00 +02002856 * \retval #PSA_SUCCESS
2857 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002858 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002859 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002860 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002861 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002862 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002863 * \retval #PSA_ERROR_NOT_SUPPORTED
2864 * \retval #PSA_ERROR_INVALID_ARGUMENT
2865 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2866 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2867 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002868 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002869 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2870 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002871 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002872 * The library has not been previously initialized by psa_crypto_init().
2873 * It is implementation-dependent whether a failure to initialize
2874 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002875 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002876psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002877 psa_algorithm_t alg,
2878 const uint8_t *input,
2879 size_t input_length,
2880 const uint8_t *salt,
2881 size_t salt_length,
2882 uint8_t *output,
2883 size_t output_size,
2884 size_t *output_length);
2885
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002886/**@}*/
2887
Gilles Peskine35675b62019-05-16 17:26:11 +02002888/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002889 * @{
2890 */
2891
Gilles Peskine35675b62019-05-16 17:26:11 +02002892/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002893 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002894 * Before calling any function on a key derivation operation object, the
2895 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002896 * - Set the structure to all-bits-zero, for example:
2897 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002898 * psa_key_derivation_operation_t operation;
2899 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002900 * \endcode
2901 * - Initialize the structure to logical zero values, for example:
2902 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002903 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002904 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002905 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002906 * for example:
2907 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002908 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002909 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002910 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002911 * to the structure, for example:
2912 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002913 * psa_key_derivation_operation_t operation;
2914 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002915 * \endcode
2916 *
2917 * This is an implementation-defined \c struct. Applications should not
2918 * make any assumptions about the content of this structure except
2919 * as directed by the documentation of a specific implementation.
2920 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002921typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002922
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002923/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002924 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002925 * This macro returns a suitable initializer for a key derivation operation
2926 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002927 */
2928#ifdef __DOXYGEN_ONLY__
2929/* This is an example definition for documentation purposes.
2930 * Implementations should define a suitable value in `crypto_struct.h`.
2931 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002932#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02002933#endif
2934
Gilles Peskine35675b62019-05-16 17:26:11 +02002935/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002936 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002937static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002938
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002939/** Set up a key derivation operation.
2940 *
2941 * A key derivation algorithm takes some inputs and uses them to generate
2942 * a byte stream in a deterministic way.
2943 * This byte stream can be used to produce keys and other
2944 * cryptographic material.
2945 *
2946 * To derive a key:
2947 * - Start with an initialized object of type #psa_key_derivation_operation_t.
2948 * - Call psa_key_derivation_setup() to select the algorithm.
2949 * - Provide the inputs for the key derivation by calling
2950 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2951 * as appropriate. Which inputs are needed, in what order, and whether
2952 * they may be keys and if so of what type depends on the algorithm.
2953 * - Optionally set the operation's maximum capacity with
2954 * psa_key_derivation_set_capacity(). You may do this before, in the middle
2955 * of or after providing inputs. For some algorithms, this step is mandatory
2956 * because the output depends on the maximum capacity.
2957 * - To derive a key, call psa_key_derivation_output_key().
2958 * To derive a byte string for a different purpose, call
2959 * - psa_key_derivation_output_bytes().
2960 * Successive calls to these functions use successive output bytes
2961 * calculated by the key derivation algorithm.
2962 * - Clean up the key derivation operation object with
2963 * psa_key_derivation_abort().
2964 *
2965 * \param[in,out] operation The key derivation operation object
2966 * to set up. It must
2967 * have been initialized but not set up yet.
2968 * \param alg The key derivation algorithm to compute
2969 * (\c PSA_ALG_XXX value such that
2970 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2971 *
2972 * \retval #PSA_SUCCESS
2973 * Success.
2974 * \retval #PSA_ERROR_INVALID_ARGUMENT
2975 * \c alg is not a key derivation algorithm.
2976 * \retval #PSA_ERROR_NOT_SUPPORTED
2977 * \c alg is not supported or is not a key derivation algorithm.
2978 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2979 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2980 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002981 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002982 * \retval #PSA_ERROR_BAD_STATE
2983 */
2984psa_status_t psa_key_derivation_setup(
2985 psa_key_derivation_operation_t *operation,
2986 psa_algorithm_t alg);
2987
Gilles Peskine35675b62019-05-16 17:26:11 +02002988/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002989 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002990 * The capacity of a key derivation is the maximum number of bytes that it can
2991 * return. When you get *N* bytes of output from a key derivation operation,
2992 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002993 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002994 * \param[in] operation The operation to query.
2995 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002996 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002997 * \retval #PSA_SUCCESS
2998 * \retval #PSA_ERROR_BAD_STATE
2999 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02003000 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003001psa_status_t psa_key_derivation_get_capacity(
3002 const psa_key_derivation_operation_t *operation,
3003 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003004
Gilles Peskine35675b62019-05-16 17:26:11 +02003005/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003006 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003007 * The capacity of a key derivation operation is the maximum number of bytes
3008 * that the key derivation operation can return from this point onwards.
3009 *
3010 * \param[in,out] operation The key derivation operation object to modify.
3011 * \param capacity The new capacity of the operation.
3012 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003013 * current capacity.
3014 *
3015 * \retval #PSA_SUCCESS
3016 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003017 * \p capacity is larger than the operation's current capacity.
3018 * In this case, the operation object remains valid and its capacity
3019 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003020 * \retval #PSA_ERROR_BAD_STATE
3021 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3022 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003023psa_status_t psa_key_derivation_set_capacity(
3024 psa_key_derivation_operation_t *operation,
3025 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003026
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003027/** Use the maximum possible capacity for a key derivation operation.
3028 *
3029 * Use this value as the capacity argument when setting up a key derivation
3030 * to indicate that the operation should have the maximum possible capacity.
3031 * The value of the maximum possible capacity depends on the key derivation
3032 * algorithm.
3033 */
3034#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3035
3036/** Provide an input for key derivation or key agreement.
3037 *
3038 * Which inputs are required and in what order depends on the algorithm.
3039 * Refer to the documentation of each key derivation or key agreement
3040 * algorithm for information.
3041 *
3042 * This function passes direct inputs. Some inputs must be passed as keys
3043 * using psa_key_derivation_input_key() instead of this function. Refer to
3044 * the documentation of individual step types for information.
3045 *
3046 * \param[in,out] operation The key derivation operation object to use.
3047 * It must have been set up with
3048 * psa_key_derivation_setup() and must not
3049 * have produced any output yet.
3050 * \param step Which step the input data is for.
3051 * \param[in] data Input data to use.
3052 * \param data_length Size of the \p data buffer in bytes.
3053 *
3054 * \retval #PSA_SUCCESS
3055 * Success.
3056 * \retval #PSA_ERROR_INVALID_ARGUMENT
3057 * \c step is not compatible with the operation's algorithm.
3058 * \retval #PSA_ERROR_INVALID_ARGUMENT
3059 * \c step does not allow direct inputs.
3060 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3061 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3062 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003063 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003064 * \retval #PSA_ERROR_BAD_STATE
3065 * The value of \p step is not valid given the state of \p operation.
3066 * \retval #PSA_ERROR_BAD_STATE
3067 * The library has not been previously initialized by psa_crypto_init().
3068 * It is implementation-dependent whether a failure to initialize
3069 * results in this error code.
3070 */
3071psa_status_t psa_key_derivation_input_bytes(
3072 psa_key_derivation_operation_t *operation,
3073 psa_key_derivation_step_t step,
3074 const uint8_t *data,
3075 size_t data_length);
3076
3077/** Provide an input for key derivation in the form of a key.
3078 *
3079 * Which inputs are required and in what order depends on the algorithm.
3080 * Refer to the documentation of each key derivation or key agreement
3081 * algorithm for information.
3082 *
3083 * This function passes key inputs. Some inputs must be passed as keys
3084 * of the appropriate type using this function, while others must be
3085 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3086 * the documentation of individual step types for information.
3087 *
3088 * \param[in,out] operation The key derivation operation object to use.
3089 * It must have been set up with
3090 * psa_key_derivation_setup() and must not
3091 * have produced any output yet.
3092 * \param step Which step the input data is for.
3093 * \param handle Handle to the key. It must have an
3094 * appropriate type for \p step and must
3095 * allow the usage #PSA_KEY_USAGE_DERIVE.
3096 *
3097 * \retval #PSA_SUCCESS
3098 * Success.
3099 * \retval #PSA_ERROR_INVALID_HANDLE
3100 * \retval #PSA_ERROR_DOES_NOT_EXIST
3101 * \retval #PSA_ERROR_NOT_PERMITTED
3102 * \retval #PSA_ERROR_INVALID_ARGUMENT
3103 * \c step is not compatible with the operation's algorithm.
3104 * \retval #PSA_ERROR_INVALID_ARGUMENT
3105 * \c step does not allow key inputs.
3106 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3107 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3108 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003109 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003110 * \retval #PSA_ERROR_BAD_STATE
3111 * The value of \p step is not valid given the state of \p operation.
3112 * \retval #PSA_ERROR_BAD_STATE
3113 * The library has not been previously initialized by psa_crypto_init().
3114 * It is implementation-dependent whether a failure to initialize
3115 * results in this error code.
3116 */
3117psa_status_t psa_key_derivation_input_key(
3118 psa_key_derivation_operation_t *operation,
3119 psa_key_derivation_step_t step,
3120 psa_key_handle_t handle);
3121
3122/** Perform a key agreement and use the shared secret as input to a key
3123 * derivation.
3124 *
3125 * A key agreement algorithm takes two inputs: a private key \p private_key
3126 * a public key \p peer_key.
3127 * The result of this function is passed as input to a key derivation.
3128 * The output of this key derivation can be extracted by reading from the
3129 * resulting operation to produce keys and other cryptographic material.
3130 *
3131 * \param[in,out] operation The key derivation operation object to use.
3132 * It must have been set up with
3133 * psa_key_derivation_setup() with a
3134 * key agreement and derivation algorithm
3135 * \c alg (\c PSA_ALG_XXX value such that
3136 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3137 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3138 * is false).
3139 * The operation must be ready for an
3140 * input of the type given by \p step.
3141 * \param step Which step the input data is for.
3142 * \param private_key Handle to the private key to use.
3143 * \param[in] peer_key Public key of the peer. The peer key must be in the
3144 * same format that psa_import_key() accepts for the
3145 * public key type corresponding to the type of
3146 * private_key. That is, this function performs the
3147 * equivalent of
3148 * #psa_import_key(...,
3149 * `peer_key`, `peer_key_length`) where
3150 * with key attributes indicating the public key
3151 * type corresponding to the type of `private_key`.
3152 * For example, for EC keys, this means that peer_key
3153 * is interpreted as a point on the curve that the
3154 * private key is on. The standard formats for public
3155 * keys are documented in the documentation of
3156 * psa_export_public_key().
3157 * \param peer_key_length Size of \p peer_key in bytes.
3158 *
3159 * \retval #PSA_SUCCESS
3160 * Success.
3161 * \retval #PSA_ERROR_INVALID_HANDLE
3162 * \retval #PSA_ERROR_DOES_NOT_EXIST
3163 * \retval #PSA_ERROR_NOT_PERMITTED
3164 * \retval #PSA_ERROR_INVALID_ARGUMENT
3165 * \c private_key is not compatible with \c alg,
3166 * or \p peer_key is not valid for \c alg or not compatible with
3167 * \c private_key.
3168 * \retval #PSA_ERROR_NOT_SUPPORTED
3169 * \c alg is not supported or is not a key derivation algorithm.
3170 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3171 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3172 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003173 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003174 */
3175psa_status_t psa_key_derivation_key_agreement(
3176 psa_key_derivation_operation_t *operation,
3177 psa_key_derivation_step_t step,
3178 psa_key_handle_t private_key,
3179 const uint8_t *peer_key,
3180 size_t peer_key_length);
3181
Gilles Peskine35675b62019-05-16 17:26:11 +02003182/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003183 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003184 * This function calculates output bytes from a key derivation algorithm and
3185 * return those bytes.
3186 * If you view the key derivation's output as a stream of bytes, this
3187 * function destructively reads the requested number of bytes from the
3188 * stream.
3189 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003190 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003191 * \param[in,out] operation The key derivation operation object to read from.
3192 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003193 * \param output_length Number of bytes to output.
3194 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003195 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003196 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003197 * The operation's capacity was less than
3198 * \p output_length bytes. Note that in this case,
3199 * no output is written to the output buffer.
3200 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003201 * subsequent calls to this function will not
3202 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003203 * \retval #PSA_ERROR_BAD_STATE
3204 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3205 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3206 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003207 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003208 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003209psa_status_t psa_key_derivation_output_bytes(
3210 psa_key_derivation_operation_t *operation,
3211 uint8_t *output,
3212 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003213
Gilles Peskine35675b62019-05-16 17:26:11 +02003214/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003215 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003216 * This function calculates output bytes from a key derivation algorithm
3217 * and uses those bytes to generate a key deterministically.
3218 * If you view the key derivation's output as a stream of bytes, this
3219 * function destructively reads as many bytes as required from the
3220 * stream.
3221 * The operation's capacity decreases by the number of bytes read.
3222 *
3223 * How much output is produced and consumed from the operation, and how
3224 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003225 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003226 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003227 * of a given size, this function is functionally equivalent to
3228 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003229 * and passing the resulting output to #psa_import_key.
3230 * However, this function has a security benefit:
3231 * if the implementation provides an isolation boundary then
3232 * the key material is not exposed outside the isolation boundary.
3233 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003234 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003235 * The following key types defined in this specification follow this scheme:
3236 *
3237 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003238 * - #PSA_KEY_TYPE_ARC4;
3239 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003240 * - #PSA_KEY_TYPE_DERIVE;
3241 * - #PSA_KEY_TYPE_HMAC.
3242 *
3243 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003244 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003245 * Montgomery curve), this function always draws a byte string whose
3246 * length is determined by the curve, and sets the mandatory bits
3247 * accordingly. That is:
3248 *
3249 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3250 * and process it as specified in RFC 7748 &sect;5.
3251 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3252 * and process it as specified in RFC 7748 &sect;5.
3253 *
3254 * - For key types for which the key is represented by a single sequence of
3255 * \p bits bits with constraints as to which bit sequences are acceptable,
3256 * this function draws a byte string of length (\p bits / 8) bytes rounded
3257 * up to the nearest whole number of bytes. If the resulting byte string
3258 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3259 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003260 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003261 * for the output produced by psa_export_key().
3262 * The following key types defined in this specification follow this scheme:
3263 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003264 * - #PSA_KEY_TYPE_DES.
3265 * Force-set the parity bits, but discard forbidden weak keys.
3266 * For 2-key and 3-key triple-DES, the three keys are generated
3267 * successively (for example, for 3-key triple-DES,
3268 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3269 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003270 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003271 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003272 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003273 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003274 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003275 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003276 * Weierstrass curve).
3277 * For these key types, interpret the byte string as integer
3278 * in big-endian order. Discard it if it is not in the range
3279 * [0, *N* - 2] where *N* is the boundary of the private key domain
3280 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003281 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003282 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003283 * This method allows compliance to NIST standards, specifically
3284 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003285 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3286 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3287 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3288 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003289 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003290 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003291 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003292 * implementation-defined.
3293 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003294 * In all cases, the data that is read is discarded from the operation.
3295 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003296 *
Gilles Peskine20628592019-04-19 19:29:50 +02003297 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003298 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003299 * \param[out] handle On success, a handle to the newly created key.
3300 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003301 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003302 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003303 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003304 * If the key is persistent, the key material and the key's metadata
3305 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003306 * \retval #PSA_ERROR_ALREADY_EXISTS
3307 * This is an attempt to create a persistent key, and there is
3308 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003309 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003310 * There was not enough data to create the desired key.
3311 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003312 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003313 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003314 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003315 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003316 * implementation in general or in this particular location.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003317 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003318 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3319 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3320 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3321 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003322 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003323 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003324 * The library has not been previously initialized by psa_crypto_init().
3325 * It is implementation-dependent whether a failure to initialize
3326 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003327 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003328psa_status_t psa_key_derivation_output_key(
3329 const psa_key_attributes_t *attributes,
3330 psa_key_derivation_operation_t *operation,
3331 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003332
Gilles Peskine35675b62019-05-16 17:26:11 +02003333/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003334 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003335 * Once a key derivation operation has been aborted, its capacity is zero.
3336 * Aborting an operation frees all associated resources except for the
3337 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003338 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003339 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003340 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003341 * psa_key_derivation_operation_init() or a zero value. In particular,
3342 * it is valid to call psa_key_derivation_abort() twice, or to call
3343 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003344 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003345 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003346 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003347 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003348 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003349 * \retval #PSA_SUCCESS
3350 * \retval #PSA_ERROR_BAD_STATE
3351 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3352 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003353 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003354 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003355psa_status_t psa_key_derivation_abort(
3356 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003357
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003358/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003359 *
3360 * \warning The raw result of a key agreement algorithm such as finite-field
3361 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3362 * not be used directly as key material. It should instead be passed as
3363 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003364 * a key derivation, use psa_key_derivation_key_agreement() and other
3365 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003366 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003367 * \param alg The key agreement algorithm to compute
3368 * (\c PSA_ALG_XXX value such that
3369 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3370 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003371 * \param private_key Handle to the private key to use.
3372 * \param[in] peer_key Public key of the peer. It must be
3373 * in the same format that psa_import_key()
3374 * accepts. The standard formats for public
3375 * keys are documented in the documentation
3376 * of psa_export_public_key().
3377 * \param peer_key_length Size of \p peer_key in bytes.
3378 * \param[out] output Buffer where the decrypted message is to
3379 * be written.
3380 * \param output_size Size of the \c output buffer in bytes.
3381 * \param[out] output_length On success, the number of bytes
3382 * that make up the returned output.
3383 *
3384 * \retval #PSA_SUCCESS
3385 * Success.
3386 * \retval #PSA_ERROR_INVALID_HANDLE
3387 * \retval #PSA_ERROR_EMPTY_SLOT
3388 * \retval #PSA_ERROR_NOT_PERMITTED
3389 * \retval #PSA_ERROR_INVALID_ARGUMENT
3390 * \p alg is not a key agreement algorithm
3391 * \retval #PSA_ERROR_INVALID_ARGUMENT
3392 * \p private_key is not compatible with \p alg,
3393 * or \p peer_key is not valid for \p alg or not compatible with
3394 * \p private_key.
3395 * \retval #PSA_ERROR_NOT_SUPPORTED
3396 * \p alg is not a supported key agreement algorithm.
3397 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3398 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3399 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003400 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine769c7a62019-01-18 16:42:29 +01003401 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003402psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3403 psa_key_handle_t private_key,
3404 const uint8_t *peer_key,
3405 size_t peer_key_length,
3406 uint8_t *output,
3407 size_t output_size,
3408 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003409
Gilles Peskineea0fb492018-07-12 17:17:20 +02003410/**@}*/
3411
Gilles Peskineedd76872018-07-20 17:42:05 +02003412/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003413 * @{
3414 */
3415
3416/**
3417 * \brief Generate random bytes.
3418 *
3419 * \warning This function **can** fail! Callers MUST check the return status
3420 * and MUST NOT use the content of the output buffer if the return
3421 * status is not #PSA_SUCCESS.
3422 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003423 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003424 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003425 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003426 * \param output_size Number of bytes to generate and output.
3427 *
Gilles Peskine28538492018-07-11 17:34:00 +02003428 * \retval #PSA_SUCCESS
3429 * \retval #PSA_ERROR_NOT_SUPPORTED
3430 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3431 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3432 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003433 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003434 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003435 * The library has not been previously initialized by psa_crypto_init().
3436 * It is implementation-dependent whether a failure to initialize
3437 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003438 */
3439psa_status_t psa_generate_random(uint8_t *output,
3440 size_t output_size);
3441
3442/**
3443 * \brief Generate a key or key pair.
3444 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003445 * The key is generated randomly.
3446 * Its location, policy, type and size are taken from \p attributes.
3447 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003448 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003449 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003450 * the public exponent is 65537.
3451 * The modulus is a product of two probabilistic primes
3452 * between 2^{n-1} and 2^n where n is the bit size specified in the
3453 * attributes.
3454 *
Gilles Peskine20628592019-04-19 19:29:50 +02003455 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003456 * \param[out] handle On success, a handle to the newly created key.
3457 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003458 *
Gilles Peskine28538492018-07-11 17:34:00 +02003459 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003460 * Success.
3461 * If the key is persistent, the key material and the key's metadata
3462 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003463 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003464 * This is an attempt to create a persistent key, and there is
3465 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003466 * \retval #PSA_ERROR_NOT_SUPPORTED
3467 * \retval #PSA_ERROR_INVALID_ARGUMENT
3468 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3469 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3470 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3471 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003472 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003473 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003474 * The library has not been previously initialized by psa_crypto_init().
3475 * It is implementation-dependent whether a failure to initialize
3476 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003477 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003478psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003479 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003480
3481/**@}*/
3482
Gilles Peskinee59236f2018-01-27 23:32:46 +01003483#ifdef __cplusplus
3484}
3485#endif
3486
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003487/* The file "crypto_sizes.h" contains definitions for size calculation
3488 * macros whose definitions are implementation-specific. */
3489#include "crypto_sizes.h"
3490
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003491/* The file "crypto_struct.h" contains definitions for
3492 * implementation-specific structs that are declared above. */
3493#include "crypto_struct.h"
3494
3495/* The file "crypto_extra.h" contains vendor-specific definitions. This
3496 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003497#include "crypto_extra.h"
3498
3499#endif /* PSA_CRYPTO_H */