blob: f1a290d7b3710cf3fb930e9f0d435a9a2bfcf8be [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 *
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100464 * Open a handle to a key which was previously created with
465 * psa_make_key_persistent() when setting its attributes.
Adrian L. Shaw52d83da2019-05-15 11:39:06 +0100466 * The handle should eventually be closed with psa_close_key()
467 * to release associated resources.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100468 *
Gilles Peskine4a231b82019-05-06 18:56:14 +0200469 * Implementations may provide additional keys that can be opened with
470 * psa_open_key(). Such keys have a key identifier in the vendor range,
471 * as documented in the description of #psa_key_id_t.
472 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100473 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100474 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100475 *
476 * \retval #PSA_SUCCESS
477 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100478 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100479 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200480 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100481 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine225010f2019-05-06 18:44:55 +0200482 * \p id is invalid.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100483 * \retval #PSA_ERROR_NOT_PERMITTED
484 * The specified key exists, but the application does not have the
485 * permission to access it. Note that this specification does not
486 * define any way to create such a key, but it may be possible
487 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200488 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
489 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100490 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200491psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100492 psa_key_handle_t *handle);
493
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100494
Gilles Peskinef535eb22018-11-30 14:08:36 +0100495/** Close a key handle.
496 *
497 * If the handle designates a volatile key, destroy the key material and
498 * free all associated resources, just like psa_destroy_key().
499 *
500 * If the handle designates a persistent key, free all resources associated
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100501 * with the key in volatile memory. The key in persistent storage is
Gilles Peskinef535eb22018-11-30 14:08:36 +0100502 * not affected and can be opened again later with psa_open_key().
503 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100504 * If the key is currently in use in a multipart operation,
505 * the multipart operation is aborted.
506 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100507 * \param handle The key handle to close.
508 *
509 * \retval #PSA_SUCCESS
510 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100511 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100512 */
513psa_status_t psa_close_key(psa_key_handle_t handle);
514
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100515/**@}*/
516
517/** \defgroup import_export Key import and export
518 * @{
519 */
520
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100521/**
522 * \brief Import a key in binary format.
523 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100524 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100525 * documentation of psa_export_public_key() for the format of public keys
526 * and to the documentation of psa_export_key() for the format for
527 * other key types.
528 *
529 * This specification supports a single format for each key type.
530 * Implementations may support other formats as long as the standard
531 * format is supported. Implementations that support other formats
532 * should ensure that the formats are clearly unambiguous so as to
533 * minimize the risk that an invalid input is accidentally interpreted
534 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100535 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100536
Gilles Peskine20628592019-04-19 19:29:50 +0200537 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200538 * The key size is always determined from the
539 * \p data buffer.
540 * If the key size in \p attributes is nonzero,
541 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200542 * \param[out] handle On success, a handle to the newly created key.
543 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100544 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200545 * buffer is interpreted according to the type declared
546 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200547 * All implementations must support at least the format
548 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100549 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200550 * the chosen type. Implementations may allow other
551 * formats, but should be conservative: implementations
552 * should err on the side of rejecting content if it
553 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200554 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100555 *
Gilles Peskine28538492018-07-11 17:34:00 +0200556 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100557 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100558 * If the key is persistent, the key material and the key's metadata
559 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200560 * \retval #PSA_ERROR_ALREADY_EXISTS
561 * This is an attempt to create a persistent key, and there is
562 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200563 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200564 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200565 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200566 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200567 * The key attributes, as a whole, are invalid.
568 * \retval #PSA_ERROR_INVALID_ARGUMENT
569 * The key data is not correctly formatted.
570 * \retval #PSA_ERROR_INVALID_ARGUMENT
571 * The size in \p attributes is nonzero and does not match the size
572 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200573 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
574 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
575 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100576 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200577 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200578 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300579 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300580 * The library has not been previously initialized by psa_crypto_init().
581 * It is implementation-dependent whether a failure to initialize
582 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100583 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200584psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100585 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200586 size_t data_length,
587 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100588
589/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100590 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200591 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100592 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200593 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100594 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200595 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100596 * This function also erases any metadata such as policies and frees all
597 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200598 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100599 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100600 *
Gilles Peskine28538492018-07-11 17:34:00 +0200601 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100602 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200603 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100604 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200605 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100606 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200607 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200608 * There was an failure in communication with the cryptoprocessor.
609 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200610 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200611 * The storage is corrupted. Implementations shall make a best effort
612 * to erase key material even in this stage, however applications
613 * should be aware that it may be impossible to guarantee that the
614 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200615 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200616 * An unexpected condition which is not a storage corruption or
617 * a communication failure occurred. The cryptoprocessor may have
618 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300619 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300620 * The library has not been previously initialized by psa_crypto_init().
621 * It is implementation-dependent whether a failure to initialize
622 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100623 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100624psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100625
626/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100627 * \brief Export a key in binary format.
628 *
629 * The output of this function can be passed to psa_import_key() to
630 * create an equivalent object.
631 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100632 * If the implementation of psa_import_key() supports other formats
633 * beyond the format specified here, the output from psa_export_key()
634 * must use the representation specified here, not the original
635 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100636 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100637 * For standard key types, the output format is as follows:
638 *
639 * - For symmetric keys (including MAC keys), the format is the
640 * raw bytes of the key.
641 * - For DES, the key data consists of 8 bytes. The parity bits must be
642 * correct.
643 * - For Triple-DES, the format is the concatenation of the
644 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200645 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200646 * is the non-encrypted DER encoding of the representation defined by
647 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
648 * ```
649 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200650 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200651 * modulus INTEGER, -- n
652 * publicExponent INTEGER, -- e
653 * privateExponent INTEGER, -- d
654 * prime1 INTEGER, -- p
655 * prime2 INTEGER, -- q
656 * exponent1 INTEGER, -- d mod (p-1)
657 * exponent2 INTEGER, -- d mod (q-1)
658 * coefficient INTEGER, -- (inverse of q) mod p
659 * }
660 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200661 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200662 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100663 * a representation of the private value as a `ceiling(m/8)`-byte string
664 * where `m` is the bit size associated with the curve, i.e. the bit size
665 * of the order of the curve's coordinate field. This byte string is
666 * in little-endian order for Montgomery curves (curve types
667 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
668 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
669 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100670 * This is the content of the `privateKey` field of the `ECPrivateKey`
671 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200672 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200673 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000674 * format is the representation of the private key `x` as a big-endian byte
675 * string. The length of the byte string is the private key size in bytes
676 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200677 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
678 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100679 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200680 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
681 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100682 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200683 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200684 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200685 * \param[out] data_length On success, the number of bytes
686 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100687 *
Gilles Peskine28538492018-07-11 17:34:00 +0200688 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100689 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200690 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200691 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200692 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100693 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200694 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
695 * The size of the \p data buffer is too small. You can determine a
696 * sufficient buffer size by calling
697 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
698 * where \c type is the key type
699 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200700 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
701 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200702 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300703 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300704 * The library has not been previously initialized by psa_crypto_init().
705 * It is implementation-dependent whether a failure to initialize
706 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100707 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100708psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100709 uint8_t *data,
710 size_t data_size,
711 size_t *data_length);
712
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100713/**
714 * \brief Export a public key or the public part of a key pair in binary format.
715 *
716 * The output of this function can be passed to psa_import_key() to
717 * create an object that is equivalent to the public key.
718 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000719 * This specification supports a single format for each key type.
720 * Implementations may support other formats as long as the standard
721 * format is supported. Implementations that support other formats
722 * should ensure that the formats are clearly unambiguous so as to
723 * minimize the risk that an invalid input is accidentally interpreted
724 * according to a different format.
725 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000726 * For standard key types, the output format is as follows:
727 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
728 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
729 * ```
730 * RSAPublicKey ::= SEQUENCE {
731 * modulus INTEGER, -- n
732 * publicExponent INTEGER } -- e
733 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000734 * - For elliptic curve public keys (key types for which
735 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
736 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
737 * Let `m` be the bit size associated with the curve, i.e. the bit size of
738 * `q` for a curve over `F_q`. The representation consists of:
739 * - The byte 0x04;
740 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
741 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200742 * - For Diffie-Hellman key exchange public keys (key types for which
743 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000744 * the format is the representation of the public key `y = g^x mod p` as a
745 * big-endian byte string. The length of the byte string is the length of the
746 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100747 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200748 * Exporting a public key object or the public part of a key pair is
749 * always permitted, regardless of the key's usage flags.
750 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100751 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200752 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200753 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200754 * \param[out] data_length On success, the number of bytes
755 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100756 *
Gilles Peskine28538492018-07-11 17:34:00 +0200757 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100758 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200759 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200760 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200761 * The key is neither a public key nor a key pair.
762 * \retval #PSA_ERROR_NOT_SUPPORTED
763 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
764 * The size of the \p data buffer is too small. You can determine a
765 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200766 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200767 * where \c type is the key type
768 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200769 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
770 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200771 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300772 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300773 * The library has not been previously initialized by psa_crypto_init().
774 * It is implementation-dependent whether a failure to initialize
775 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100776 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100777psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100778 uint8_t *data,
779 size_t data_size,
780 size_t *data_length);
781
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100782/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100783 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100784 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000785 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100786 * This function is primarily useful to copy a key from one location
787 * to another, since it populates a key using the material from
788 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200789 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100790 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100791 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100792 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200793 * The policy on the source key must have the usage flag
794 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200795 * This flag is sufficient to permit the copy if the key has the lifetime
796 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
797 * Some secure elements do not provide a way to copy a key without
798 * making it extractable from the secure element. If a key is located
799 * in such a secure element, then the key must have both usage flags
800 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
801 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200802 *
Gilles Peskine20628592019-04-19 19:29:50 +0200803 * The resulting key may only be used in a way that conforms to
804 * both the policy of the original key and the policy specified in
805 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100806 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200807 * usage flags on the source policy and the usage flags in \p attributes.
808 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100809 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200810 * - If either of the policies allows an algorithm and the other policy
811 * allows a wildcard-based algorithm policy that includes this algorithm,
812 * the resulting key allows the same algorithm.
813 * - If the policies do not allow any algorithm in common, this function
814 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200815 *
Gilles Peskine20628592019-04-19 19:29:50 +0200816 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100817 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200818 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100819 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200820 * \param[in] attributes The attributes for the new key.
821 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200822 * - The key type and size may be 0. If either is
823 * nonzero, it must match the corresponding
824 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200825 * - The key location (the lifetime and, for
826 * persistent keys, the key identifier) is
827 * used directly.
828 * - The policy constraints (usage flags and
829 * algorithm policy) are combined from
830 * the source key and \p attributes so that
831 * both sets of restrictions apply, as
832 * described in the documentation of this function.
833 * \param[out] target_handle On success, a handle to the newly created key.
834 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200835 *
836 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100837 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200838 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200839 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200840 * This is an attempt to create a persistent key, and there is
841 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200842 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200843 * The lifetime or identifier in \p attributes are invalid.
844 * \retval #PSA_ERROR_INVALID_ARGUMENT
845 * The policy constraints on the source and specified in
846 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200847 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200848 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200849 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100850 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200851 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
852 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100853 * The source key is not exportable and its lifetime does not
854 * allow copying it to the target's lifetime.
855 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
856 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200857 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
858 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200859 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100860 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100861psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200862 const psa_key_attributes_t *attributes,
863 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100864
865/**@}*/
866
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100867/** \defgroup hash Message digests
868 * @{
869 */
870
Gilles Peskine69647a42019-01-14 20:18:12 +0100871/** Calculate the hash (digest) of a message.
872 *
873 * \note To verify the hash of a message against an
874 * expected value, use psa_hash_compare() instead.
875 *
876 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
877 * such that #PSA_ALG_IS_HASH(\p alg) is true).
878 * \param[in] input Buffer containing the message to hash.
879 * \param input_length Size of the \p input buffer in bytes.
880 * \param[out] hash Buffer where the hash is to be written.
881 * \param hash_size Size of the \p hash buffer in bytes.
882 * \param[out] hash_length On success, the number of bytes
883 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100884 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100885 *
886 * \retval #PSA_SUCCESS
887 * Success.
888 * \retval #PSA_ERROR_NOT_SUPPORTED
889 * \p alg is not supported or is not a hash algorithm.
890 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
891 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
892 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200893 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100894 */
895psa_status_t psa_hash_compute(psa_algorithm_t alg,
896 const uint8_t *input,
897 size_t input_length,
898 uint8_t *hash,
899 size_t hash_size,
900 size_t *hash_length);
901
902/** Calculate the hash (digest) of a message and compare it with a
903 * reference value.
904 *
905 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
906 * such that #PSA_ALG_IS_HASH(\p alg) is true).
907 * \param[in] input Buffer containing the message to hash.
908 * \param input_length Size of the \p input buffer in bytes.
909 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100910 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100911 *
912 * \retval #PSA_SUCCESS
913 * The expected hash is identical to the actual hash of the input.
914 * \retval #PSA_ERROR_INVALID_SIGNATURE
915 * The hash of the message was calculated successfully, but it
916 * differs from the expected hash.
917 * \retval #PSA_ERROR_NOT_SUPPORTED
918 * \p alg is not supported or is not a hash algorithm.
919 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
920 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
921 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200922 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100923 */
924psa_status_t psa_hash_compare(psa_algorithm_t alg,
925 const uint8_t *input,
926 size_t input_length,
927 const uint8_t *hash,
928 const size_t hash_length);
929
Gilles Peskine308b91d2018-02-08 09:47:44 +0100930/** The type of the state data structure for multipart hash operations.
931 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000932 * Before calling any function on a hash operation object, the application must
933 * initialize it by any of the following means:
934 * - Set the structure to all-bits-zero, for example:
935 * \code
936 * psa_hash_operation_t operation;
937 * memset(&operation, 0, sizeof(operation));
938 * \endcode
939 * - Initialize the structure to logical zero values, for example:
940 * \code
941 * psa_hash_operation_t operation = {0};
942 * \endcode
943 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
944 * for example:
945 * \code
946 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
947 * \endcode
948 * - Assign the result of the function psa_hash_operation_init()
949 * to the structure, for example:
950 * \code
951 * psa_hash_operation_t operation;
952 * operation = psa_hash_operation_init();
953 * \endcode
954 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100955 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100956 * make any assumptions about the content of this structure except
957 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100958typedef struct psa_hash_operation_s psa_hash_operation_t;
959
Jaeden Amero6a25b412019-01-04 11:47:44 +0000960/** \def PSA_HASH_OPERATION_INIT
961 *
962 * This macro returns a suitable initializer for a hash operation object
963 * of type #psa_hash_operation_t.
964 */
965#ifdef __DOXYGEN_ONLY__
966/* This is an example definition for documentation purposes.
967 * Implementations should define a suitable value in `crypto_struct.h`.
968 */
969#define PSA_HASH_OPERATION_INIT {0}
970#endif
971
972/** Return an initial value for a hash operation object.
973 */
974static psa_hash_operation_t psa_hash_operation_init(void);
975
Gilles Peskinef45adda2019-01-14 18:29:18 +0100976/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100977 *
978 * The sequence of operations to calculate a hash (message digest)
979 * is as follows:
980 * -# Allocate an operation object which will be passed to all the functions
981 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000982 * -# Initialize the operation object with one of the methods described in the
983 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200984 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100985 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100986 * of the message each time. The hash that is calculated is the hash
987 * of the concatenation of these messages in order.
988 * -# To calculate the hash, call psa_hash_finish().
989 * To compare the hash with an expected value, call psa_hash_verify().
990 *
991 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000992 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100993 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200994 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100995 * eventually terminate the operation. The following events terminate an
996 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100997 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100998 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100999 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001000 * \param[in,out] operation The operation object to set up. It must have
1001 * been initialized as per the documentation for
1002 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001003 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1004 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001005 *
Gilles Peskine28538492018-07-11 17:34:00 +02001006 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001007 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001008 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001009 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001010 * \retval #PSA_ERROR_BAD_STATE
1011 * The operation state is not valid (already set up and not
1012 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001013 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1014 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1015 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001016 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001017 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001018psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001019 psa_algorithm_t alg);
1020
Gilles Peskine308b91d2018-02-08 09:47:44 +01001021/** Add a message fragment to a multipart hash operation.
1022 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001023 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001024 *
1025 * If this function returns an error status, the operation becomes inactive.
1026 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001027 * \param[in,out] operation Active hash operation.
1028 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001029 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001030 *
Gilles Peskine28538492018-07-11 17:34:00 +02001031 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001032 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001033 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001034 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001035 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1036 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1037 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001038 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001039 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001040psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1041 const uint8_t *input,
1042 size_t input_length);
1043
Gilles Peskine308b91d2018-02-08 09:47:44 +01001044/** Finish the calculation of the hash of a message.
1045 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001046 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 * This function calculates the hash of the message formed by concatenating
1048 * the inputs passed to preceding calls to psa_hash_update().
1049 *
1050 * When this function returns, the operation becomes inactive.
1051 *
1052 * \warning Applications should not call this function if they expect
1053 * a specific value for the hash. Call psa_hash_verify() instead.
1054 * Beware that comparing integrity or authenticity data such as
1055 * hash values with a function such as \c memcmp is risky
1056 * because the time taken by the comparison may leak information
1057 * about the hashed data which could allow an attacker to guess
1058 * a valid hash and thereby bypass security controls.
1059 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001060 * \param[in,out] operation Active hash operation.
1061 * \param[out] hash Buffer where the hash is to be written.
1062 * \param hash_size Size of the \p hash buffer in bytes.
1063 * \param[out] hash_length On success, the number of bytes
1064 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001065 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001066 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001067 *
Gilles Peskine28538492018-07-11 17:34:00 +02001068 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001069 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001070 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001071 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001072 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001073 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001074 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001075 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001076 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1077 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1078 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001079 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001080 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001081psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1082 uint8_t *hash,
1083 size_t hash_size,
1084 size_t *hash_length);
1085
Gilles Peskine308b91d2018-02-08 09:47:44 +01001086/** Finish the calculation of the hash of a message and compare it with
1087 * an expected value.
1088 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001089 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090 * This function calculates the hash of the message formed by concatenating
1091 * the inputs passed to preceding calls to psa_hash_update(). It then
1092 * compares the calculated hash with the expected hash passed as a
1093 * parameter to this function.
1094 *
1095 * When this function returns, the operation becomes inactive.
1096 *
Gilles Peskine19067982018-03-20 17:54:53 +01001097 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001098 * comparison between the actual hash and the expected hash is performed
1099 * in constant time.
1100 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001101 * \param[in,out] operation Active hash operation.
1102 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001103 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001104 *
Gilles Peskine28538492018-07-11 17:34:00 +02001105 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001106 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001107 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001108 * The hash of the message was calculated successfully, but it
1109 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001110 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001111 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001112 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1113 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1114 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001115 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001116 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001117psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1118 const uint8_t *hash,
1119 size_t hash_length);
1120
Gilles Peskine308b91d2018-02-08 09:47:44 +01001121/** Abort a hash operation.
1122 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001123 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001124 * \p operation structure itself. Once aborted, the operation object
1125 * can be reused for another operation by calling
1126 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001127 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001128 * You may call this function any time after the operation object has
1129 * been initialized by any of the following methods:
1130 * - A call to psa_hash_setup(), whether it succeeds or not.
1131 * - Initializing the \c struct to all-bits-zero.
1132 * - Initializing the \c struct to logical zeros, e.g.
1133 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001134 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001135 * In particular, calling psa_hash_abort() after the operation has been
1136 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1137 * psa_hash_verify() is safe and has no effect.
1138 *
1139 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001140 *
Gilles Peskine28538492018-07-11 17:34:00 +02001141 * \retval #PSA_SUCCESS
1142 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001143 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001144 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1145 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001146 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001147 */
1148psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001149
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001150/** Clone a hash operation.
1151 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001152 * This function copies the state of an ongoing hash operation to
1153 * a new operation object. In other words, this function is equivalent
1154 * to calling psa_hash_setup() on \p target_operation with the same
1155 * algorithm that \p source_operation was set up for, then
1156 * psa_hash_update() on \p target_operation with the same input that
1157 * that was passed to \p source_operation. After this function returns, the
1158 * two objects are independent, i.e. subsequent calls involving one of
1159 * the objects do not affect the other object.
1160 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001161 * \param[in] source_operation The active hash operation to clone.
1162 * \param[in,out] target_operation The operation object to set up.
1163 * It must be initialized but not active.
1164 *
1165 * \retval #PSA_SUCCESS
1166 * \retval #PSA_ERROR_BAD_STATE
1167 * \p source_operation is not an active hash operation.
1168 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001169 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001170 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1171 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001172 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001173 */
1174psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1175 psa_hash_operation_t *target_operation);
1176
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001177/**@}*/
1178
Gilles Peskine8c9def32018-02-08 10:02:12 +01001179/** \defgroup MAC Message authentication codes
1180 * @{
1181 */
1182
Gilles Peskine69647a42019-01-14 20:18:12 +01001183/** Calculate the MAC (message authentication code) of a message.
1184 *
1185 * \note To verify the MAC of a message against an
1186 * expected value, use psa_mac_verify() instead.
1187 * Beware that comparing integrity or authenticity data such as
1188 * MAC values with a function such as \c memcmp is risky
1189 * because the time taken by the comparison may leak information
1190 * about the MAC value which could allow an attacker to guess
1191 * a valid MAC and thereby bypass security controls.
1192 *
1193 * \param handle Handle to the key to use for the operation.
1194 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001195 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001196 * \param[in] input Buffer containing the input message.
1197 * \param input_length Size of the \p input buffer in bytes.
1198 * \param[out] mac Buffer where the MAC value is to be written.
1199 * \param mac_size Size of the \p mac buffer in bytes.
1200 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001201 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001202 *
1203 * \retval #PSA_SUCCESS
1204 * Success.
1205 * \retval #PSA_ERROR_INVALID_HANDLE
1206 * \retval #PSA_ERROR_EMPTY_SLOT
1207 * \retval #PSA_ERROR_NOT_PERMITTED
1208 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001209 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001210 * \retval #PSA_ERROR_NOT_SUPPORTED
1211 * \p alg is not supported or is not a MAC algorithm.
1212 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1213 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1214 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001215 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001216 * \retval #PSA_ERROR_BAD_STATE
1217 * The library has not been previously initialized by psa_crypto_init().
1218 * It is implementation-dependent whether a failure to initialize
1219 * results in this error code.
1220 */
1221psa_status_t psa_mac_compute(psa_key_handle_t handle,
1222 psa_algorithm_t alg,
1223 const uint8_t *input,
1224 size_t input_length,
1225 uint8_t *mac,
1226 size_t mac_size,
1227 size_t *mac_length);
1228
1229/** Calculate the MAC of a message and compare it with a reference value.
1230 *
1231 * \param handle Handle to the key to use for the operation.
1232 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001233 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001234 * \param[in] input Buffer containing the input message.
1235 * \param input_length Size of the \p input buffer in bytes.
1236 * \param[out] mac Buffer containing the expected MAC value.
1237 * \param mac_length Size of the \p mac buffer in bytes.
1238 *
1239 * \retval #PSA_SUCCESS
1240 * The expected MAC is identical to the actual MAC of the input.
1241 * \retval #PSA_ERROR_INVALID_SIGNATURE
1242 * The MAC of the message was calculated successfully, but it
1243 * differs from the expected value.
1244 * \retval #PSA_ERROR_INVALID_HANDLE
1245 * \retval #PSA_ERROR_EMPTY_SLOT
1246 * \retval #PSA_ERROR_NOT_PERMITTED
1247 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001248 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001249 * \retval #PSA_ERROR_NOT_SUPPORTED
1250 * \p alg is not supported or is not a MAC algorithm.
1251 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1252 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1253 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001254 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001255 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001256psa_status_t psa_mac_verify(psa_key_handle_t handle,
1257 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001258 const uint8_t *input,
1259 size_t input_length,
1260 const uint8_t *mac,
1261 const size_t mac_length);
1262
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001263/** The type of the state data structure for multipart MAC operations.
1264 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001265 * Before calling any function on a MAC operation object, the application must
1266 * initialize it by any of the following means:
1267 * - Set the structure to all-bits-zero, for example:
1268 * \code
1269 * psa_mac_operation_t operation;
1270 * memset(&operation, 0, sizeof(operation));
1271 * \endcode
1272 * - Initialize the structure to logical zero values, for example:
1273 * \code
1274 * psa_mac_operation_t operation = {0};
1275 * \endcode
1276 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1277 * for example:
1278 * \code
1279 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1280 * \endcode
1281 * - Assign the result of the function psa_mac_operation_init()
1282 * to the structure, for example:
1283 * \code
1284 * psa_mac_operation_t operation;
1285 * operation = psa_mac_operation_init();
1286 * \endcode
1287 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001288 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001289 * make any assumptions about the content of this structure except
1290 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001291typedef struct psa_mac_operation_s psa_mac_operation_t;
1292
Jaeden Amero769ce272019-01-04 11:48:03 +00001293/** \def PSA_MAC_OPERATION_INIT
1294 *
1295 * This macro returns a suitable initializer for a MAC operation object of type
1296 * #psa_mac_operation_t.
1297 */
1298#ifdef __DOXYGEN_ONLY__
1299/* This is an example definition for documentation purposes.
1300 * Implementations should define a suitable value in `crypto_struct.h`.
1301 */
1302#define PSA_MAC_OPERATION_INIT {0}
1303#endif
1304
1305/** Return an initial value for a MAC operation object.
1306 */
1307static psa_mac_operation_t psa_mac_operation_init(void);
1308
Gilles Peskinef45adda2019-01-14 18:29:18 +01001309/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001310 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001311 * This function sets up the calculation of the MAC
1312 * (message authentication code) of a byte string.
1313 * To verify the MAC of a message against an
1314 * expected value, use psa_mac_verify_setup() instead.
1315 *
1316 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001317 * -# Allocate an operation object which will be passed to all the functions
1318 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001319 * -# Initialize the operation object with one of the methods described in the
1320 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001321 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001322 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1323 * of the message each time. The MAC that is calculated is the MAC
1324 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001325 * -# At the end of the message, call psa_mac_sign_finish() to finish
1326 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001327 *
1328 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001329 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001330 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001331 * After a successful call to psa_mac_sign_setup(), the application must
1332 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001333 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001334 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001335 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001336 * \param[in,out] operation The operation object to set up. It must have
1337 * been initialized as per the documentation for
1338 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001339 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001340 * It must remain valid until the operation
1341 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001342 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001343 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001344 *
Gilles Peskine28538492018-07-11 17:34:00 +02001345 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001346 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001347 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001348 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001349 * \retval #PSA_ERROR_NOT_PERMITTED
1350 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001351 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001352 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001353 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001354 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1355 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1356 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001357 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001358 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001359 * The operation state is not valid (already set up and not
1360 * subsequently completed).
1361 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001362 * The library has not been previously initialized by psa_crypto_init().
1363 * It is implementation-dependent whether a failure to initialize
1364 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001365 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001366psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001367 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001368 psa_algorithm_t alg);
1369
Gilles Peskinef45adda2019-01-14 18:29:18 +01001370/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001371 *
1372 * This function sets up the verification of the MAC
1373 * (message authentication code) of a byte string against an expected value.
1374 *
1375 * The sequence of operations to verify a MAC is as follows:
1376 * -# Allocate an operation object which will be passed to all the functions
1377 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001378 * -# Initialize the operation object with one of the methods described in the
1379 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001380 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001381 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1382 * of the message each time. The MAC that is calculated is the MAC
1383 * of the concatenation of these messages in order.
1384 * -# At the end of the message, call psa_mac_verify_finish() to finish
1385 * calculating the actual MAC of the message and verify it against
1386 * the expected value.
1387 *
1388 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001389 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001390 *
1391 * After a successful call to psa_mac_verify_setup(), the application must
1392 * eventually terminate the operation through one of the following methods:
1393 * - A failed call to psa_mac_update().
1394 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1395 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001396 * \param[in,out] operation The operation object to set up. It must have
1397 * been initialized as per the documentation for
1398 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001399 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001400 * It must remain valid until the operation
1401 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001402 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1403 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001404 *
Gilles Peskine28538492018-07-11 17:34:00 +02001405 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001406 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001407 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001408 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001409 * \retval #PSA_ERROR_NOT_PERMITTED
1410 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001411 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001412 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001413 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001414 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1415 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1416 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001417 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001418 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001419 * The operation state is not valid (already set up and not
1420 * subsequently completed).
1421 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001422 * The library has not been previously initialized by psa_crypto_init().
1423 * It is implementation-dependent whether a failure to initialize
1424 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001425 */
1426psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001427 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001428 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001429
Gilles Peskinedcd14942018-07-12 00:30:52 +02001430/** Add a message fragment to a multipart MAC operation.
1431 *
1432 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1433 * before calling this function.
1434 *
1435 * If this function returns an error status, the operation becomes inactive.
1436 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001437 * \param[in,out] operation Active MAC operation.
1438 * \param[in] input Buffer containing the message fragment to add to
1439 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001440 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001441 *
1442 * \retval #PSA_SUCCESS
1443 * Success.
1444 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001445 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001446 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1447 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1448 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001449 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001450 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001451psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1452 const uint8_t *input,
1453 size_t input_length);
1454
Gilles Peskinedcd14942018-07-12 00:30:52 +02001455/** Finish the calculation of the MAC of a message.
1456 *
1457 * The application must call psa_mac_sign_setup() before calling this function.
1458 * This function calculates the MAC of the message formed by concatenating
1459 * the inputs passed to preceding calls to psa_mac_update().
1460 *
1461 * When this function returns, the operation becomes inactive.
1462 *
1463 * \warning Applications should not call this function if they expect
1464 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1465 * Beware that comparing integrity or authenticity data such as
1466 * MAC values with a function such as \c memcmp is risky
1467 * because the time taken by the comparison may leak information
1468 * about the MAC value which could allow an attacker to guess
1469 * a valid MAC and thereby bypass security controls.
1470 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001471 * \param[in,out] operation Active MAC operation.
1472 * \param[out] mac Buffer where the MAC value is to be written.
1473 * \param mac_size Size of the \p mac buffer in bytes.
1474 * \param[out] mac_length On success, the number of bytes
1475 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001476 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001477 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001478 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001479 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001480 *
1481 * \retval #PSA_SUCCESS
1482 * Success.
1483 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001484 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001485 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001486 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001487 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1488 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1489 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1490 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001491 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001492 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001493psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1494 uint8_t *mac,
1495 size_t mac_size,
1496 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001497
Gilles Peskinedcd14942018-07-12 00:30:52 +02001498/** Finish the calculation of the MAC of a message and compare it with
1499 * an expected value.
1500 *
1501 * The application must call psa_mac_verify_setup() before calling this function.
1502 * This function calculates the MAC of the message formed by concatenating
1503 * the inputs passed to preceding calls to psa_mac_update(). It then
1504 * compares the calculated MAC with the expected MAC passed as a
1505 * parameter to this function.
1506 *
1507 * When this function returns, the operation becomes inactive.
1508 *
1509 * \note Implementations shall make the best effort to ensure that the
1510 * comparison between the actual MAC and the expected MAC is performed
1511 * in constant time.
1512 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001513 * \param[in,out] operation Active MAC operation.
1514 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001515 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001516 *
1517 * \retval #PSA_SUCCESS
1518 * The expected MAC is identical to the actual MAC of the message.
1519 * \retval #PSA_ERROR_INVALID_SIGNATURE
1520 * The MAC of the message was calculated successfully, but it
1521 * differs from the expected MAC.
1522 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001523 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001524 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1525 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1526 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001527 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001528 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001529psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1530 const uint8_t *mac,
1531 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001532
Gilles Peskinedcd14942018-07-12 00:30:52 +02001533/** Abort a MAC operation.
1534 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001535 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001536 * \p operation structure itself. Once aborted, the operation object
1537 * can be reused for another operation by calling
1538 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001539 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001540 * You may call this function any time after the operation object has
1541 * been initialized by any of the following methods:
1542 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1543 * it succeeds or not.
1544 * - Initializing the \c struct to all-bits-zero.
1545 * - Initializing the \c struct to logical zeros, e.g.
1546 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001547 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001548 * In particular, calling psa_mac_abort() after the operation has been
1549 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1550 * psa_mac_verify_finish() is safe and has no effect.
1551 *
1552 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001553 *
1554 * \retval #PSA_SUCCESS
1555 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001556 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001557 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1558 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001559 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001560 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001561psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1562
1563/**@}*/
1564
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001565/** \defgroup cipher Symmetric ciphers
1566 * @{
1567 */
1568
Gilles Peskine69647a42019-01-14 20:18:12 +01001569/** Encrypt a message using a symmetric cipher.
1570 *
1571 * This function encrypts a message with a random IV (initialization
1572 * vector).
1573 *
1574 * \param handle Handle to the key to use for the operation.
1575 * It must remain valid until the operation
1576 * terminates.
1577 * \param alg The cipher algorithm to compute
1578 * (\c PSA_ALG_XXX value such that
1579 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1580 * \param[in] input Buffer containing the message to encrypt.
1581 * \param input_length Size of the \p input buffer in bytes.
1582 * \param[out] output Buffer where the output is to be written.
1583 * The output contains the IV followed by
1584 * the ciphertext proper.
1585 * \param output_size Size of the \p output buffer in bytes.
1586 * \param[out] output_length On success, the number of bytes
1587 * that make up the output.
1588 *
1589 * \retval #PSA_SUCCESS
1590 * Success.
1591 * \retval #PSA_ERROR_INVALID_HANDLE
1592 * \retval #PSA_ERROR_EMPTY_SLOT
1593 * \retval #PSA_ERROR_NOT_PERMITTED
1594 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001595 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001596 * \retval #PSA_ERROR_NOT_SUPPORTED
1597 * \p alg is not supported or is not a cipher algorithm.
1598 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1599 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1600 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1601 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001602 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001603 */
1604psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1605 psa_algorithm_t alg,
1606 const uint8_t *input,
1607 size_t input_length,
1608 uint8_t *output,
1609 size_t output_size,
1610 size_t *output_length);
1611
1612/** Decrypt a message using a symmetric cipher.
1613 *
1614 * This function decrypts a message encrypted with a symmetric cipher.
1615 *
1616 * \param handle Handle to the key to use for the operation.
1617 * It must remain valid until the operation
1618 * terminates.
1619 * \param alg The cipher algorithm to compute
1620 * (\c PSA_ALG_XXX value such that
1621 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1622 * \param[in] input Buffer containing the message to decrypt.
1623 * This consists of the IV followed by the
1624 * ciphertext proper.
1625 * \param input_length Size of the \p input buffer in bytes.
1626 * \param[out] output Buffer where the plaintext is to be written.
1627 * \param output_size Size of the \p output buffer in bytes.
1628 * \param[out] output_length On success, the number of bytes
1629 * that make up the output.
1630 *
1631 * \retval #PSA_SUCCESS
1632 * Success.
1633 * \retval #PSA_ERROR_INVALID_HANDLE
1634 * \retval #PSA_ERROR_EMPTY_SLOT
1635 * \retval #PSA_ERROR_NOT_PERMITTED
1636 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001637 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001638 * \retval #PSA_ERROR_NOT_SUPPORTED
1639 * \p alg is not supported or is not a cipher algorithm.
1640 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1641 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1642 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1643 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001644 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001645 */
1646psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1647 psa_algorithm_t alg,
1648 const uint8_t *input,
1649 size_t input_length,
1650 uint8_t *output,
1651 size_t output_size,
1652 size_t *output_length);
1653
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001654/** The type of the state data structure for multipart cipher operations.
1655 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001656 * Before calling any function on a cipher operation object, the application
1657 * must initialize it by any of the following means:
1658 * - Set the structure to all-bits-zero, for example:
1659 * \code
1660 * psa_cipher_operation_t operation;
1661 * memset(&operation, 0, sizeof(operation));
1662 * \endcode
1663 * - Initialize the structure to logical zero values, for example:
1664 * \code
1665 * psa_cipher_operation_t operation = {0};
1666 * \endcode
1667 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1668 * for example:
1669 * \code
1670 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1671 * \endcode
1672 * - Assign the result of the function psa_cipher_operation_init()
1673 * to the structure, for example:
1674 * \code
1675 * psa_cipher_operation_t operation;
1676 * operation = psa_cipher_operation_init();
1677 * \endcode
1678 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001679 * This is an implementation-defined \c struct. Applications should not
1680 * make any assumptions about the content of this structure except
1681 * as directed by the documentation of a specific implementation. */
1682typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1683
Jaeden Amero5bae2272019-01-04 11:48:27 +00001684/** \def PSA_CIPHER_OPERATION_INIT
1685 *
1686 * This macro returns a suitable initializer for a cipher operation object of
1687 * type #psa_cipher_operation_t.
1688 */
1689#ifdef __DOXYGEN_ONLY__
1690/* This is an example definition for documentation purposes.
1691 * Implementations should define a suitable value in `crypto_struct.h`.
1692 */
1693#define PSA_CIPHER_OPERATION_INIT {0}
1694#endif
1695
1696/** Return an initial value for a cipher operation object.
1697 */
1698static psa_cipher_operation_t psa_cipher_operation_init(void);
1699
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001700/** Set the key for a multipart symmetric encryption operation.
1701 *
1702 * The sequence of operations to encrypt a message with a symmetric cipher
1703 * is as follows:
1704 * -# Allocate an operation object which will be passed to all the functions
1705 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001706 * -# Initialize the operation object with one of the methods described in the
1707 * documentation for #psa_cipher_operation_t, e.g.
1708 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001709 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001710 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001711 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001712 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001713 * requires a specific IV value.
1714 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1715 * of the message each time.
1716 * -# Call psa_cipher_finish().
1717 *
1718 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001719 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001720 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001721 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001722 * eventually terminate the operation. The following events terminate an
1723 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001724 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001725 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001726 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001727 * \param[in,out] operation The operation object to set up. It must have
1728 * been initialized as per the documentation for
1729 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001730 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001731 * It must remain valid until the operation
1732 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001733 * \param alg The cipher algorithm to compute
1734 * (\c PSA_ALG_XXX value such that
1735 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001736 *
Gilles Peskine28538492018-07-11 17:34:00 +02001737 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001738 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001739 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001740 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001741 * \retval #PSA_ERROR_NOT_PERMITTED
1742 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001743 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001744 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001745 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001746 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1747 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1748 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001749 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001750 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001751 * The operation state is not valid (already set up and not
1752 * subsequently completed).
1753 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001754 * The library has not been previously initialized by psa_crypto_init().
1755 * It is implementation-dependent whether a failure to initialize
1756 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001757 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001758psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001759 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001760 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001761
1762/** Set the key for a multipart symmetric decryption operation.
1763 *
1764 * The sequence of operations to decrypt a message with a symmetric cipher
1765 * is as follows:
1766 * -# Allocate an operation object which will be passed to all the functions
1767 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001768 * -# Initialize the operation object with one of the methods described in the
1769 * documentation for #psa_cipher_operation_t, e.g.
1770 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001771 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001772 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001773 * decryption. If the IV is prepended to the ciphertext, you can call
1774 * psa_cipher_update() on a buffer containing the IV followed by the
1775 * beginning of the message.
1776 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1777 * of the message each time.
1778 * -# Call psa_cipher_finish().
1779 *
1780 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001781 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001782 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001783 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001784 * eventually terminate the operation. The following events terminate an
1785 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001786 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001787 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001788 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001789 * \param[in,out] operation The operation object to set up. It must have
1790 * been initialized as per the documentation for
1791 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001792 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001793 * It must remain valid until the operation
1794 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001795 * \param alg The cipher algorithm to compute
1796 * (\c PSA_ALG_XXX value such that
1797 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001798 *
Gilles Peskine28538492018-07-11 17:34:00 +02001799 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001800 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001801 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001802 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001803 * \retval #PSA_ERROR_NOT_PERMITTED
1804 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001805 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001806 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001807 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001808 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1809 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1810 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001811 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001812 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001813 * The operation state is not valid (already set up and not
1814 * subsequently completed).
1815 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001816 * The library has not been previously initialized by psa_crypto_init().
1817 * It is implementation-dependent whether a failure to initialize
1818 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001819 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001820psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001821 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001822 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001823
Gilles Peskinedcd14942018-07-12 00:30:52 +02001824/** Generate an IV for a symmetric encryption operation.
1825 *
1826 * This function generates a random IV (initialization vector), nonce
1827 * or initial counter value for the encryption operation as appropriate
1828 * for the chosen algorithm, key type and key size.
1829 *
1830 * The application must call psa_cipher_encrypt_setup() before
1831 * calling this function.
1832 *
1833 * If this function returns an error status, the operation becomes inactive.
1834 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001835 * \param[in,out] operation Active cipher operation.
1836 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001837 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001838 * \param[out] iv_length On success, the number of bytes of the
1839 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001840 *
1841 * \retval #PSA_SUCCESS
1842 * Success.
1843 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001844 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001845 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001846 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001847 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1848 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1849 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001850 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001851 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001852psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1853 unsigned char *iv,
1854 size_t iv_size,
1855 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001856
Gilles Peskinedcd14942018-07-12 00:30:52 +02001857/** Set the IV for a symmetric encryption or decryption operation.
1858 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001859 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001860 * or initial counter value for the encryption or decryption operation.
1861 *
1862 * The application must call psa_cipher_encrypt_setup() before
1863 * calling this function.
1864 *
1865 * If this function returns an error status, the operation becomes inactive.
1866 *
1867 * \note When encrypting, applications should use psa_cipher_generate_iv()
1868 * instead of this function, unless implementing a protocol that requires
1869 * a non-random IV.
1870 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001871 * \param[in,out] operation Active cipher operation.
1872 * \param[in] iv Buffer containing the IV to use.
1873 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001874 *
1875 * \retval #PSA_SUCCESS
1876 * Success.
1877 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001878 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001879 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001880 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001881 * or the chosen algorithm does not use an IV.
1882 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1883 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1884 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001885 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001886 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001887psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1888 const unsigned char *iv,
1889 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001890
Gilles Peskinedcd14942018-07-12 00:30:52 +02001891/** Encrypt or decrypt a message fragment in an active cipher operation.
1892 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001893 * Before calling this function, you must:
1894 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1895 * The choice of setup function determines whether this function
1896 * encrypts or decrypts its input.
1897 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1898 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001899 *
1900 * If this function returns an error status, the operation becomes inactive.
1901 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001902 * \param[in,out] operation Active cipher operation.
1903 * \param[in] input Buffer containing the message fragment to
1904 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001905 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001906 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001907 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001908 * \param[out] output_length On success, the number of bytes
1909 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001910 *
1911 * \retval #PSA_SUCCESS
1912 * Success.
1913 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001914 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001915 * not set, or already completed).
1916 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1917 * The size of the \p output buffer is too small.
1918 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1919 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1920 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001921 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001922 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001923psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1924 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001925 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001926 unsigned char *output,
1927 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001928 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001929
Gilles Peskinedcd14942018-07-12 00:30:52 +02001930/** Finish encrypting or decrypting a message in a cipher operation.
1931 *
1932 * The application must call psa_cipher_encrypt_setup() or
1933 * psa_cipher_decrypt_setup() before calling this function. The choice
1934 * of setup function determines whether this function encrypts or
1935 * decrypts its input.
1936 *
1937 * This function finishes the encryption or decryption of the message
1938 * formed by concatenating the inputs passed to preceding calls to
1939 * psa_cipher_update().
1940 *
1941 * When this function returns, the operation becomes inactive.
1942 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001943 * \param[in,out] operation Active cipher operation.
1944 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001945 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001946 * \param[out] output_length On success, the number of bytes
1947 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001948 *
1949 * \retval #PSA_SUCCESS
1950 * Success.
1951 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001952 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001953 * not set, or already completed).
1954 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1955 * The size of the \p output buffer is too small.
1956 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1957 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1958 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001959 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001960 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001961psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001962 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001963 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001964 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001965
Gilles Peskinedcd14942018-07-12 00:30:52 +02001966/** Abort a cipher operation.
1967 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001968 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001969 * \p operation structure itself. Once aborted, the operation object
1970 * can be reused for another operation by calling
1971 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001972 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001973 * You may call this function any time after the operation object has
1974 * been initialized by any of the following methods:
1975 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1976 * whether it succeeds or not.
1977 * - Initializing the \c struct to all-bits-zero.
1978 * - Initializing the \c struct to logical zeros, e.g.
1979 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001980 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001981 * In particular, calling psa_cipher_abort() after the operation has been
1982 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1983 * is safe and has no effect.
1984 *
1985 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001986 *
1987 * \retval #PSA_SUCCESS
1988 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001989 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001990 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1991 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001992 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001993 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001994psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1995
1996/**@}*/
1997
Gilles Peskine3b555712018-03-03 21:27:57 +01001998/** \defgroup aead Authenticated encryption with associated data (AEAD)
1999 * @{
2000 */
2001
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002002/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002003 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002004 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002005 * \param alg The AEAD algorithm to compute
2006 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002007 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002008 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002009 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002010 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002011 * but not encrypted.
2012 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002013 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002014 * encrypted.
2015 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002016 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002017 * encrypted data. The additional data is not
2018 * part of this output. For algorithms where the
2019 * encrypted data and the authentication tag
2020 * are defined as separate outputs, the
2021 * authentication tag is appended to the
2022 * encrypted data.
2023 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2024 * This must be at least
2025 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2026 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002027 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002028 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002029 *
Gilles Peskine28538492018-07-11 17:34:00 +02002030 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002031 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002032 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002033 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002034 * \retval #PSA_ERROR_NOT_PERMITTED
2035 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002036 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002037 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002038 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002039 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2040 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2041 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002042 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002043 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002044 * The library has not been previously initialized by psa_crypto_init().
2045 * It is implementation-dependent whether a failure to initialize
2046 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002047 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002048psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002049 psa_algorithm_t alg,
2050 const uint8_t *nonce,
2051 size_t nonce_length,
2052 const uint8_t *additional_data,
2053 size_t additional_data_length,
2054 const uint8_t *plaintext,
2055 size_t plaintext_length,
2056 uint8_t *ciphertext,
2057 size_t ciphertext_size,
2058 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002059
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002060/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002061 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002062 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002063 * \param alg The AEAD algorithm to compute
2064 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002065 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002066 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002067 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002068 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002069 * but not encrypted.
2070 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002071 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002072 * encrypted. For algorithms where the
2073 * encrypted data and the authentication tag
2074 * are defined as separate inputs, the buffer
2075 * must contain the encrypted data followed
2076 * by the authentication tag.
2077 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002078 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002079 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2080 * This must be at least
2081 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2082 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002083 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002084 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002085 *
Gilles Peskine28538492018-07-11 17:34:00 +02002086 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002087 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002088 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002089 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002090 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002091 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002092 * \retval #PSA_ERROR_NOT_PERMITTED
2093 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002094 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002095 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002096 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002097 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2098 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2099 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002100 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002101 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002102 * The library has not been previously initialized by psa_crypto_init().
2103 * It is implementation-dependent whether a failure to initialize
2104 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002105 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002106psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002107 psa_algorithm_t alg,
2108 const uint8_t *nonce,
2109 size_t nonce_length,
2110 const uint8_t *additional_data,
2111 size_t additional_data_length,
2112 const uint8_t *ciphertext,
2113 size_t ciphertext_length,
2114 uint8_t *plaintext,
2115 size_t plaintext_size,
2116 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002117
Gilles Peskine30a9e412019-01-14 18:36:12 +01002118/** The type of the state data structure for multipart AEAD operations.
2119 *
2120 * Before calling any function on an AEAD operation object, the application
2121 * must initialize it by any of the following means:
2122 * - Set the structure to all-bits-zero, for example:
2123 * \code
2124 * psa_aead_operation_t operation;
2125 * memset(&operation, 0, sizeof(operation));
2126 * \endcode
2127 * - Initialize the structure to logical zero values, for example:
2128 * \code
2129 * psa_aead_operation_t operation = {0};
2130 * \endcode
2131 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2132 * for example:
2133 * \code
2134 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2135 * \endcode
2136 * - Assign the result of the function psa_aead_operation_init()
2137 * to the structure, for example:
2138 * \code
2139 * psa_aead_operation_t operation;
2140 * operation = psa_aead_operation_init();
2141 * \endcode
2142 *
2143 * This is an implementation-defined \c struct. Applications should not
2144 * make any assumptions about the content of this structure except
2145 * as directed by the documentation of a specific implementation. */
2146typedef struct psa_aead_operation_s psa_aead_operation_t;
2147
2148/** \def PSA_AEAD_OPERATION_INIT
2149 *
2150 * This macro returns a suitable initializer for an AEAD operation object of
2151 * type #psa_aead_operation_t.
2152 */
2153#ifdef __DOXYGEN_ONLY__
2154/* This is an example definition for documentation purposes.
2155 * Implementations should define a suitable value in `crypto_struct.h`.
2156 */
2157#define PSA_AEAD_OPERATION_INIT {0}
2158#endif
2159
2160/** Return an initial value for an AEAD operation object.
2161 */
2162static psa_aead_operation_t psa_aead_operation_init(void);
2163
2164/** Set the key for a multipart authenticated encryption operation.
2165 *
2166 * The sequence of operations to encrypt a message with authentication
2167 * is as follows:
2168 * -# Allocate an operation object which will be passed to all the functions
2169 * listed here.
2170 * -# Initialize the operation object with one of the methods described in the
2171 * documentation for #psa_aead_operation_t, e.g.
2172 * PSA_AEAD_OPERATION_INIT.
2173 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002174 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2175 * inputs to the subsequent calls to psa_aead_update_ad() and
2176 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2177 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002178 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2179 * generate or set the nonce. You should use
2180 * psa_aead_generate_nonce() unless the protocol you are implementing
2181 * requires a specific nonce value.
2182 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2183 * of the non-encrypted additional authenticated data each time.
2184 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002185 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002186 * -# Call psa_aead_finish().
2187 *
2188 * The application may call psa_aead_abort() at any time after the operation
2189 * has been initialized.
2190 *
2191 * After a successful call to psa_aead_encrypt_setup(), the application must
2192 * eventually terminate the operation. The following events terminate an
2193 * operation:
2194 * - A failed call to any of the \c psa_aead_xxx functions.
2195 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2196 *
2197 * \param[in,out] operation The operation object to set up. It must have
2198 * been initialized as per the documentation for
2199 * #psa_aead_operation_t and not yet in use.
2200 * \param handle Handle to the key to use for the operation.
2201 * It must remain valid until the operation
2202 * terminates.
2203 * \param alg The AEAD algorithm to compute
2204 * (\c PSA_ALG_XXX value such that
2205 * #PSA_ALG_IS_AEAD(\p alg) is true).
2206 *
2207 * \retval #PSA_SUCCESS
2208 * Success.
2209 * \retval #PSA_ERROR_INVALID_HANDLE
2210 * \retval #PSA_ERROR_EMPTY_SLOT
2211 * \retval #PSA_ERROR_NOT_PERMITTED
2212 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002213 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002214 * \retval #PSA_ERROR_NOT_SUPPORTED
2215 * \p alg is not supported or is not an AEAD algorithm.
2216 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2217 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2218 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002219 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002220 * \retval #PSA_ERROR_BAD_STATE
2221 * The library has not been previously initialized by psa_crypto_init().
2222 * It is implementation-dependent whether a failure to initialize
2223 * results in this error code.
2224 */
2225psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2226 psa_key_handle_t handle,
2227 psa_algorithm_t alg);
2228
2229/** Set the key for a multipart authenticated decryption operation.
2230 *
2231 * The sequence of operations to decrypt a message with authentication
2232 * is as follows:
2233 * -# Allocate an operation object which will be passed to all the functions
2234 * listed here.
2235 * -# Initialize the operation object with one of the methods described in the
2236 * documentation for #psa_aead_operation_t, e.g.
2237 * PSA_AEAD_OPERATION_INIT.
2238 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002239 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2240 * inputs to the subsequent calls to psa_aead_update_ad() and
2241 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2242 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002243 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2244 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2245 * of the non-encrypted additional authenticated data each time.
2246 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002247 * of the ciphertext to decrypt each time.
2248 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002249 *
2250 * The application may call psa_aead_abort() at any time after the operation
2251 * has been initialized.
2252 *
2253 * After a successful call to psa_aead_decrypt_setup(), the application must
2254 * eventually terminate the operation. The following events terminate an
2255 * operation:
2256 * - A failed call to any of the \c psa_aead_xxx functions.
2257 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2258 *
2259 * \param[in,out] operation The operation object to set up. It must have
2260 * been initialized as per the documentation for
2261 * #psa_aead_operation_t and not yet in use.
2262 * \param handle Handle to the key to use for the operation.
2263 * It must remain valid until the operation
2264 * terminates.
2265 * \param alg The AEAD algorithm to compute
2266 * (\c PSA_ALG_XXX value such that
2267 * #PSA_ALG_IS_AEAD(\p alg) is true).
2268 *
2269 * \retval #PSA_SUCCESS
2270 * Success.
2271 * \retval #PSA_ERROR_INVALID_HANDLE
2272 * \retval #PSA_ERROR_EMPTY_SLOT
2273 * \retval #PSA_ERROR_NOT_PERMITTED
2274 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002275 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002276 * \retval #PSA_ERROR_NOT_SUPPORTED
2277 * \p alg is not supported or is not an AEAD algorithm.
2278 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2279 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2280 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002281 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002282 * \retval #PSA_ERROR_BAD_STATE
2283 * The library has not been previously initialized by psa_crypto_init().
2284 * It is implementation-dependent whether a failure to initialize
2285 * results in this error code.
2286 */
2287psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2288 psa_key_handle_t handle,
2289 psa_algorithm_t alg);
2290
2291/** Generate a random nonce for an authenticated encryption operation.
2292 *
2293 * This function generates a random nonce for the authenticated encryption
2294 * operation with an appropriate size for the chosen algorithm, key type
2295 * and key size.
2296 *
2297 * The application must call psa_aead_encrypt_setup() before
2298 * calling this function.
2299 *
2300 * If this function returns an error status, the operation becomes inactive.
2301 *
2302 * \param[in,out] operation Active AEAD operation.
2303 * \param[out] nonce Buffer where the generated nonce is to be
2304 * written.
2305 * \param nonce_size Size of the \p nonce buffer in bytes.
2306 * \param[out] nonce_length On success, the number of bytes of the
2307 * generated nonce.
2308 *
2309 * \retval #PSA_SUCCESS
2310 * Success.
2311 * \retval #PSA_ERROR_BAD_STATE
2312 * The operation state is not valid (not set up, or nonce already set).
2313 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2314 * The size of the \p nonce buffer is too small.
2315 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2316 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2317 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002318 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002319 */
2320psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2321 unsigned char *nonce,
2322 size_t nonce_size,
2323 size_t *nonce_length);
2324
2325/** Set the nonce for an authenticated encryption or decryption operation.
2326 *
2327 * This function sets the nonce for the authenticated
2328 * encryption or decryption operation.
2329 *
2330 * The application must call psa_aead_encrypt_setup() before
2331 * calling this function.
2332 *
2333 * If this function returns an error status, the operation becomes inactive.
2334 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002335 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002336 * instead of this function, unless implementing a protocol that requires
2337 * a non-random IV.
2338 *
2339 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002340 * \param[in] nonce Buffer containing the nonce to use.
2341 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002342 *
2343 * \retval #PSA_SUCCESS
2344 * Success.
2345 * \retval #PSA_ERROR_BAD_STATE
2346 * The operation state is not valid (not set up, or nonce already set).
2347 * \retval #PSA_ERROR_INVALID_ARGUMENT
2348 * The size of \p nonce is not acceptable for the chosen algorithm.
2349 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2350 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2351 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002352 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002353 */
2354psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2355 const unsigned char *nonce,
2356 size_t nonce_length);
2357
Gilles Peskinebc59c852019-01-17 15:26:08 +01002358/** Declare the lengths of the message and additional data for AEAD.
2359 *
2360 * The application must call this function before calling
2361 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2362 * the operation requires it. If the algorithm does not require it,
2363 * calling this function is optional, but if this function is called
2364 * then the implementation must enforce the lengths.
2365 *
2366 * You may call this function before or after setting the nonce with
2367 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2368 *
2369 * - For #PSA_ALG_CCM, calling this function is required.
2370 * - For the other AEAD algorithms defined in this specification, calling
2371 * this function is not required.
2372 * - For vendor-defined algorithm, refer to the vendor documentation.
2373 *
2374 * \param[in,out] operation Active AEAD operation.
2375 * \param ad_length Size of the non-encrypted additional
2376 * authenticated data in bytes.
2377 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2378 *
2379 * \retval #PSA_SUCCESS
2380 * Success.
2381 * \retval #PSA_ERROR_BAD_STATE
2382 * The operation state is not valid (not set up, already completed,
2383 * or psa_aead_update_ad() or psa_aead_update() already called).
2384 * \retval #PSA_ERROR_INVALID_ARGUMENT
2385 * At least one of the lengths is not acceptable for the chosen
2386 * algorithm.
2387 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2388 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2389 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002390 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinebc59c852019-01-17 15:26:08 +01002391 */
2392psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2393 size_t ad_length,
2394 size_t plaintext_length);
2395
Gilles Peskine30a9e412019-01-14 18:36:12 +01002396/** Pass additional data to an active AEAD operation.
2397 *
2398 * Additional data is authenticated, but not encrypted.
2399 *
2400 * You may call this function multiple times to pass successive fragments
2401 * of the additional data. You may not call this function after passing
2402 * data to encrypt or decrypt with psa_aead_update().
2403 *
2404 * Before calling this function, you must:
2405 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2406 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2407 *
2408 * If this function returns an error status, the operation becomes inactive.
2409 *
2410 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2411 * there is no guarantee that the input is valid. Therefore, until
2412 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2413 * treat the input as untrusted and prepare to undo any action that
2414 * depends on the input if psa_aead_verify() returns an error status.
2415 *
2416 * \param[in,out] operation Active AEAD operation.
2417 * \param[in] input Buffer containing the fragment of
2418 * additional data.
2419 * \param input_length Size of the \p input buffer in bytes.
2420 *
2421 * \retval #PSA_SUCCESS
2422 * Success.
2423 * \retval #PSA_ERROR_BAD_STATE
2424 * The operation state is not valid (not set up, nonce not set,
2425 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002426 * \retval #PSA_ERROR_INVALID_ARGUMENT
2427 * The total input length overflows the additional data length that
2428 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002429 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2430 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2431 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002432 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002433 */
2434psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2435 const uint8_t *input,
2436 size_t input_length);
2437
2438/** Encrypt or decrypt a message fragment in an active AEAD operation.
2439 *
2440 * Before calling this function, you must:
2441 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2442 * The choice of setup function determines whether this function
2443 * encrypts or decrypts its input.
2444 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2445 * 3. Call psa_aead_update_ad() to pass all the additional data.
2446 *
2447 * If this function returns an error status, the operation becomes inactive.
2448 *
2449 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2450 * there is no guarantee that the input is valid. Therefore, until
2451 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2452 * - Do not use the output in any way other than storing it in a
2453 * confidential location. If you take any action that depends
2454 * on the tentative decrypted data, this action will need to be
2455 * undone if the input turns out not to be valid. Furthermore,
2456 * if an adversary can observe that this action took place
2457 * (for example through timing), they may be able to use this
2458 * fact as an oracle to decrypt any message encrypted with the
2459 * same key.
2460 * - In particular, do not copy the output anywhere but to a
2461 * memory or storage space that you have exclusive access to.
2462 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002463 * This function does not require the input to be aligned to any
2464 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002465 * a whole block at a time, it must consume all the input provided, but
2466 * it may delay the end of the corresponding output until a subsequent
2467 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2468 * provides sufficient input. The amount of data that can be delayed
2469 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002470 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002471 * \param[in,out] operation Active AEAD operation.
2472 * \param[in] input Buffer containing the message fragment to
2473 * encrypt or decrypt.
2474 * \param input_length Size of the \p input buffer in bytes.
2475 * \param[out] output Buffer where the output is to be written.
2476 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002477 * This must be at least
2478 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2479 * \p input_length) where \c alg is the
2480 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002481 * \param[out] output_length On success, the number of bytes
2482 * that make up the returned output.
2483 *
2484 * \retval #PSA_SUCCESS
2485 * Success.
2486 * \retval #PSA_ERROR_BAD_STATE
2487 * The operation state is not valid (not set up, nonce not set
2488 * or already completed).
2489 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2490 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002491 * You can determine a sufficient buffer size by calling
2492 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2493 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002494 * \retval #PSA_ERROR_INVALID_ARGUMENT
2495 * The total length of input to psa_aead_update_ad() so far is
2496 * less than the additional data length that was previously
2497 * specified with psa_aead_set_lengths().
2498 * \retval #PSA_ERROR_INVALID_ARGUMENT
2499 * The total input length overflows the plaintext length that
2500 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002501 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2502 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2503 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002504 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002505 */
2506psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2507 const uint8_t *input,
2508 size_t input_length,
2509 unsigned char *output,
2510 size_t output_size,
2511 size_t *output_length);
2512
2513/** Finish encrypting a message in an AEAD operation.
2514 *
2515 * The operation must have been set up with psa_aead_encrypt_setup().
2516 *
2517 * This function finishes the authentication of the additional data
2518 * formed by concatenating the inputs passed to preceding calls to
2519 * psa_aead_update_ad() with the plaintext formed by concatenating the
2520 * inputs passed to preceding calls to psa_aead_update().
2521 *
2522 * This function has two output buffers:
2523 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002524 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002525 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002526 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002527 * that the operation performs.
2528 *
2529 * When this function returns, the operation becomes inactive.
2530 *
2531 * \param[in,out] operation Active AEAD operation.
2532 * \param[out] ciphertext Buffer where the last part of the ciphertext
2533 * is to be written.
2534 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002535 * This must be at least
2536 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2537 * \c alg is the algorithm that is being
2538 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002539 * \param[out] ciphertext_length On success, the number of bytes of
2540 * returned ciphertext.
2541 * \param[out] tag Buffer where the authentication tag is
2542 * to be written.
2543 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002544 * This must be at least
2545 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2546 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002547 * \param[out] tag_length On success, the number of bytes
2548 * that make up the returned tag.
2549 *
2550 * \retval #PSA_SUCCESS
2551 * Success.
2552 * \retval #PSA_ERROR_BAD_STATE
2553 * The operation state is not valid (not set up, nonce not set,
2554 * decryption, or already completed).
2555 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002556 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002557 * You can determine a sufficient buffer size for \p ciphertext by
2558 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2559 * where \c alg is the algorithm that is being calculated.
2560 * You can determine a sufficient buffer size for \p tag by
2561 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002562 * \retval #PSA_ERROR_INVALID_ARGUMENT
2563 * The total length of input to psa_aead_update_ad() so far is
2564 * less than the additional data length that was previously
2565 * specified with psa_aead_set_lengths().
2566 * \retval #PSA_ERROR_INVALID_ARGUMENT
2567 * The total length of input to psa_aead_update() so far is
2568 * less than the plaintext length that was previously
2569 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002570 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2571 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2572 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002573 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002574 */
2575psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002576 uint8_t *ciphertext,
2577 size_t ciphertext_size,
2578 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002579 uint8_t *tag,
2580 size_t tag_size,
2581 size_t *tag_length);
2582
2583/** Finish authenticating and decrypting a message in an AEAD operation.
2584 *
2585 * The operation must have been set up with psa_aead_decrypt_setup().
2586 *
2587 * This function finishes the authentication of the additional data
2588 * formed by concatenating the inputs passed to preceding calls to
2589 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2590 * inputs passed to preceding calls to psa_aead_update().
2591 *
2592 * When this function returns, the operation becomes inactive.
2593 *
2594 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002595 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002596 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002597 * from previous calls to psa_aead_update()
2598 * that could not be processed until the end
2599 * of the input.
2600 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002601 * This must be at least
2602 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2603 * \c alg is the algorithm that is being
2604 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002605 * \param[out] plaintext_length On success, the number of bytes of
2606 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002607 * \param[in] tag Buffer containing the authentication tag.
2608 * \param tag_length Size of the \p tag buffer in bytes.
2609 *
2610 * \retval #PSA_SUCCESS
2611 * Success.
2612 * \retval #PSA_ERROR_BAD_STATE
2613 * The operation state is not valid (not set up, nonce not set,
2614 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002615 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2616 * The size of the \p plaintext buffer is too small.
2617 * You can determine a sufficient buffer size for \p plaintext by
2618 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2619 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002620 * \retval #PSA_ERROR_INVALID_ARGUMENT
2621 * The total length of input to psa_aead_update_ad() so far is
2622 * less than the additional data length that was previously
2623 * specified with psa_aead_set_lengths().
2624 * \retval #PSA_ERROR_INVALID_ARGUMENT
2625 * The total length of input to psa_aead_update() so far is
2626 * less than the plaintext length that was previously
2627 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002628 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2629 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2630 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002631 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002632 */
2633psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002634 uint8_t *plaintext,
2635 size_t plaintext_size,
2636 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002637 const uint8_t *tag,
2638 size_t tag_length);
2639
2640/** Abort an AEAD operation.
2641 *
2642 * Aborting an operation frees all associated resources except for the
2643 * \p operation structure itself. Once aborted, the operation object
2644 * can be reused for another operation by calling
2645 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2646 *
2647 * You may call this function any time after the operation object has
2648 * been initialized by any of the following methods:
2649 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2650 * whether it succeeds or not.
2651 * - Initializing the \c struct to all-bits-zero.
2652 * - Initializing the \c struct to logical zeros, e.g.
2653 * `psa_aead_operation_t operation = {0}`.
2654 *
2655 * In particular, calling psa_aead_abort() after the operation has been
2656 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2657 * is safe and has no effect.
2658 *
2659 * \param[in,out] operation Initialized AEAD operation.
2660 *
2661 * \retval #PSA_SUCCESS
2662 * \retval #PSA_ERROR_BAD_STATE
2663 * \p operation is not an active AEAD operation.
2664 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2665 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002666 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002667 */
2668psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2669
Gilles Peskine3b555712018-03-03 21:27:57 +01002670/**@}*/
2671
Gilles Peskine20035e32018-02-03 22:44:14 +01002672/** \defgroup asymmetric Asymmetric cryptography
2673 * @{
2674 */
2675
2676/**
2677 * \brief Sign a hash or short message with a private key.
2678 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002679 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002680 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002681 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2682 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2683 * to determine the hash algorithm to use.
2684 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002685 * \param handle Handle to the key to use for the operation.
2686 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002687 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002688 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002689 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002690 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002691 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002692 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002693 * \param[out] signature_length On success, the number of bytes
2694 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002695 *
Gilles Peskine28538492018-07-11 17:34:00 +02002696 * \retval #PSA_SUCCESS
2697 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002698 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002699 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002700 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002701 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002702 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002703 * \retval #PSA_ERROR_NOT_SUPPORTED
2704 * \retval #PSA_ERROR_INVALID_ARGUMENT
2705 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2706 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2707 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002708 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002709 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002710 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002711 * The library has not been previously initialized by psa_crypto_init().
2712 * It is implementation-dependent whether a failure to initialize
2713 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002714 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002715psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002716 psa_algorithm_t alg,
2717 const uint8_t *hash,
2718 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002719 uint8_t *signature,
2720 size_t signature_size,
2721 size_t *signature_length);
2722
2723/**
2724 * \brief Verify the signature a hash or short message using a public key.
2725 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002726 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002727 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002728 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2729 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2730 * to determine the hash algorithm to use.
2731 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002732 * \param handle Handle to the key to use for the operation.
2733 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002734 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002735 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002736 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002737 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002738 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002739 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002740 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002741 *
Gilles Peskine28538492018-07-11 17:34:00 +02002742 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002743 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002744 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002745 * The calculation was perfomed successfully, but the passed
2746 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002747 * \retval #PSA_ERROR_NOT_SUPPORTED
2748 * \retval #PSA_ERROR_INVALID_ARGUMENT
2749 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2750 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2751 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002752 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002753 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002754 * The library has not been previously initialized by psa_crypto_init().
2755 * It is implementation-dependent whether a failure to initialize
2756 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002757 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002758psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002759 psa_algorithm_t alg,
2760 const uint8_t *hash,
2761 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002762 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002763 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002764
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002765/**
2766 * \brief Encrypt a short message with a public key.
2767 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002768 * \param handle Handle to the key to use for the operation.
2769 * It must be a public key or an asymmetric
2770 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002771 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002772 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002773 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002774 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002775 * \param[in] salt A salt or label, if supported by the
2776 * encryption algorithm.
2777 * If the algorithm does not support a
2778 * salt, pass \c NULL.
2779 * If the algorithm supports an optional
2780 * salt and you do not want to pass a salt,
2781 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002782 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002783 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2784 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002785 * \param salt_length Size of the \p salt buffer in bytes.
2786 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002787 * \param[out] output Buffer where the encrypted message is to
2788 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002789 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002790 * \param[out] output_length On success, the number of bytes
2791 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002792 *
Gilles Peskine28538492018-07-11 17:34:00 +02002793 * \retval #PSA_SUCCESS
2794 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002795 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002796 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002797 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002798 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002799 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002800 * \retval #PSA_ERROR_NOT_SUPPORTED
2801 * \retval #PSA_ERROR_INVALID_ARGUMENT
2802 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2803 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2804 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002805 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002806 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002807 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002808 * The library has not been previously initialized by psa_crypto_init().
2809 * It is implementation-dependent whether a failure to initialize
2810 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002811 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002812psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002813 psa_algorithm_t alg,
2814 const uint8_t *input,
2815 size_t input_length,
2816 const uint8_t *salt,
2817 size_t salt_length,
2818 uint8_t *output,
2819 size_t output_size,
2820 size_t *output_length);
2821
2822/**
2823 * \brief Decrypt a short message with a private key.
2824 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002825 * \param handle Handle to the key to use for the operation.
2826 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002827 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002828 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002829 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002830 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002831 * \param[in] salt A salt or label, if supported by the
2832 * encryption algorithm.
2833 * If the algorithm does not support a
2834 * salt, pass \c NULL.
2835 * If the algorithm supports an optional
2836 * salt and you do not want to pass a salt,
2837 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002838 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002839 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2840 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002841 * \param salt_length Size of the \p salt buffer in bytes.
2842 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002843 * \param[out] output Buffer where the decrypted message is to
2844 * be written.
2845 * \param output_size Size of the \c output buffer in bytes.
2846 * \param[out] output_length On success, the number of bytes
2847 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002848 *
Gilles Peskine28538492018-07-11 17:34:00 +02002849 * \retval #PSA_SUCCESS
2850 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002851 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002852 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002853 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002854 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002855 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002856 * \retval #PSA_ERROR_NOT_SUPPORTED
2857 * \retval #PSA_ERROR_INVALID_ARGUMENT
2858 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2859 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2860 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002861 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002862 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2863 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002864 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002865 * The library has not been previously initialized by psa_crypto_init().
2866 * It is implementation-dependent whether a failure to initialize
2867 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002868 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002869psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002870 psa_algorithm_t alg,
2871 const uint8_t *input,
2872 size_t input_length,
2873 const uint8_t *salt,
2874 size_t salt_length,
2875 uint8_t *output,
2876 size_t output_size,
2877 size_t *output_length);
2878
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002879/**@}*/
2880
Gilles Peskine35675b62019-05-16 17:26:11 +02002881/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002882 * @{
2883 */
2884
Gilles Peskine35675b62019-05-16 17:26:11 +02002885/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002886 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002887 * Before calling any function on a key derivation operation object, the
2888 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002889 * - Set the structure to all-bits-zero, for example:
2890 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002891 * psa_key_derivation_operation_t operation;
2892 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002893 * \endcode
2894 * - Initialize the structure to logical zero values, for example:
2895 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002896 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002897 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002898 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002899 * for example:
2900 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002901 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002902 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002903 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002904 * to the structure, for example:
2905 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002906 * psa_key_derivation_operation_t operation;
2907 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002908 * \endcode
2909 *
2910 * This is an implementation-defined \c struct. Applications should not
2911 * make any assumptions about the content of this structure except
2912 * as directed by the documentation of a specific implementation.
2913 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002914typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002915
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002916/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002917 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002918 * This macro returns a suitable initializer for a key derivation operation
2919 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002920 */
2921#ifdef __DOXYGEN_ONLY__
2922/* This is an example definition for documentation purposes.
2923 * Implementations should define a suitable value in `crypto_struct.h`.
2924 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002925#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02002926#endif
2927
Gilles Peskine35675b62019-05-16 17:26:11 +02002928/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002929 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002930static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002931
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002932/** Set up a key derivation operation.
2933 *
2934 * A key derivation algorithm takes some inputs and uses them to generate
2935 * a byte stream in a deterministic way.
2936 * This byte stream can be used to produce keys and other
2937 * cryptographic material.
2938 *
2939 * To derive a key:
2940 * - Start with an initialized object of type #psa_key_derivation_operation_t.
2941 * - Call psa_key_derivation_setup() to select the algorithm.
2942 * - Provide the inputs for the key derivation by calling
2943 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2944 * as appropriate. Which inputs are needed, in what order, and whether
2945 * they may be keys and if so of what type depends on the algorithm.
2946 * - Optionally set the operation's maximum capacity with
2947 * psa_key_derivation_set_capacity(). You may do this before, in the middle
2948 * of or after providing inputs. For some algorithms, this step is mandatory
2949 * because the output depends on the maximum capacity.
2950 * - To derive a key, call psa_key_derivation_output_key().
2951 * To derive a byte string for a different purpose, call
2952 * - psa_key_derivation_output_bytes().
2953 * Successive calls to these functions use successive output bytes
2954 * calculated by the key derivation algorithm.
2955 * - Clean up the key derivation operation object with
2956 * psa_key_derivation_abort().
2957 *
2958 * \param[in,out] operation The key derivation operation object
2959 * to set up. It must
2960 * have been initialized but not set up yet.
2961 * \param alg The key derivation algorithm to compute
2962 * (\c PSA_ALG_XXX value such that
2963 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2964 *
2965 * \retval #PSA_SUCCESS
2966 * Success.
2967 * \retval #PSA_ERROR_INVALID_ARGUMENT
2968 * \c alg is not a key derivation algorithm.
2969 * \retval #PSA_ERROR_NOT_SUPPORTED
2970 * \c alg is not supported or is not a key derivation algorithm.
2971 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2972 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2973 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002974 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002975 * \retval #PSA_ERROR_BAD_STATE
2976 */
2977psa_status_t psa_key_derivation_setup(
2978 psa_key_derivation_operation_t *operation,
2979 psa_algorithm_t alg);
2980
Gilles Peskine35675b62019-05-16 17:26:11 +02002981/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002982 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002983 * The capacity of a key derivation is the maximum number of bytes that it can
2984 * return. When you get *N* bytes of output from a key derivation operation,
2985 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002986 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002987 * \param[in] operation The operation to query.
2988 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002989 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002990 * \retval #PSA_SUCCESS
2991 * \retval #PSA_ERROR_BAD_STATE
2992 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002993 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002994psa_status_t psa_key_derivation_get_capacity(
2995 const psa_key_derivation_operation_t *operation,
2996 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002997
Gilles Peskine35675b62019-05-16 17:26:11 +02002998/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002999 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003000 * The capacity of a key derivation operation is the maximum number of bytes
3001 * that the key derivation operation can return from this point onwards.
3002 *
3003 * \param[in,out] operation The key derivation operation object to modify.
3004 * \param capacity The new capacity of the operation.
3005 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003006 * current capacity.
3007 *
3008 * \retval #PSA_SUCCESS
3009 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003010 * \p capacity is larger than the operation's current capacity.
3011 * In this case, the operation object remains valid and its capacity
3012 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003013 * \retval #PSA_ERROR_BAD_STATE
3014 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3015 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003016psa_status_t psa_key_derivation_set_capacity(
3017 psa_key_derivation_operation_t *operation,
3018 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003019
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003020/** Use the maximum possible capacity for a key derivation operation.
3021 *
3022 * Use this value as the capacity argument when setting up a key derivation
3023 * to indicate that the operation should have the maximum possible capacity.
3024 * The value of the maximum possible capacity depends on the key derivation
3025 * algorithm.
3026 */
3027#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3028
3029/** Provide an input for key derivation or key agreement.
3030 *
3031 * Which inputs are required and in what order depends on the algorithm.
3032 * Refer to the documentation of each key derivation or key agreement
3033 * algorithm for information.
3034 *
3035 * This function passes direct inputs. Some inputs must be passed as keys
3036 * using psa_key_derivation_input_key() instead of this function. Refer to
3037 * the documentation of individual step types for information.
3038 *
3039 * \param[in,out] operation The key derivation operation object to use.
3040 * It must have been set up with
3041 * psa_key_derivation_setup() and must not
3042 * have produced any output yet.
3043 * \param step Which step the input data is for.
3044 * \param[in] data Input data to use.
3045 * \param data_length Size of the \p data buffer in bytes.
3046 *
3047 * \retval #PSA_SUCCESS
3048 * Success.
3049 * \retval #PSA_ERROR_INVALID_ARGUMENT
3050 * \c step is not compatible with the operation's algorithm.
3051 * \retval #PSA_ERROR_INVALID_ARGUMENT
3052 * \c step does not allow direct inputs.
3053 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3054 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3055 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003056 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003057 * \retval #PSA_ERROR_BAD_STATE
3058 * The value of \p step is not valid given the state of \p operation.
3059 * \retval #PSA_ERROR_BAD_STATE
3060 * The library has not been previously initialized by psa_crypto_init().
3061 * It is implementation-dependent whether a failure to initialize
3062 * results in this error code.
3063 */
3064psa_status_t psa_key_derivation_input_bytes(
3065 psa_key_derivation_operation_t *operation,
3066 psa_key_derivation_step_t step,
3067 const uint8_t *data,
3068 size_t data_length);
3069
3070/** Provide an input for key derivation in the form of a key.
3071 *
3072 * Which inputs are required and in what order depends on the algorithm.
3073 * Refer to the documentation of each key derivation or key agreement
3074 * algorithm for information.
3075 *
3076 * This function passes key inputs. Some inputs must be passed as keys
3077 * of the appropriate type using this function, while others must be
3078 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3079 * the documentation of individual step types for information.
3080 *
3081 * \param[in,out] operation The key derivation operation object to use.
3082 * It must have been set up with
3083 * psa_key_derivation_setup() and must not
3084 * have produced any output yet.
3085 * \param step Which step the input data is for.
3086 * \param handle Handle to the key. It must have an
3087 * appropriate type for \p step and must
3088 * allow the usage #PSA_KEY_USAGE_DERIVE.
3089 *
3090 * \retval #PSA_SUCCESS
3091 * Success.
3092 * \retval #PSA_ERROR_INVALID_HANDLE
3093 * \retval #PSA_ERROR_DOES_NOT_EXIST
3094 * \retval #PSA_ERROR_NOT_PERMITTED
3095 * \retval #PSA_ERROR_INVALID_ARGUMENT
3096 * \c step is not compatible with the operation's algorithm.
3097 * \retval #PSA_ERROR_INVALID_ARGUMENT
3098 * \c step does not allow key inputs.
3099 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3100 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3101 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003102 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003103 * \retval #PSA_ERROR_BAD_STATE
3104 * The value of \p step is not valid given the state of \p operation.
3105 * \retval #PSA_ERROR_BAD_STATE
3106 * The library has not been previously initialized by psa_crypto_init().
3107 * It is implementation-dependent whether a failure to initialize
3108 * results in this error code.
3109 */
3110psa_status_t psa_key_derivation_input_key(
3111 psa_key_derivation_operation_t *operation,
3112 psa_key_derivation_step_t step,
3113 psa_key_handle_t handle);
3114
3115/** Perform a key agreement and use the shared secret as input to a key
3116 * derivation.
3117 *
3118 * A key agreement algorithm takes two inputs: a private key \p private_key
3119 * a public key \p peer_key.
3120 * The result of this function is passed as input to a key derivation.
3121 * The output of this key derivation can be extracted by reading from the
3122 * resulting operation to produce keys and other cryptographic material.
3123 *
3124 * \param[in,out] operation The key derivation operation object to use.
3125 * It must have been set up with
3126 * psa_key_derivation_setup() with a
3127 * key agreement and derivation algorithm
3128 * \c alg (\c PSA_ALG_XXX value such that
3129 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3130 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3131 * is false).
3132 * The operation must be ready for an
3133 * input of the type given by \p step.
3134 * \param step Which step the input data is for.
3135 * \param private_key Handle to the private key to use.
3136 * \param[in] peer_key Public key of the peer. The peer key must be in the
3137 * same format that psa_import_key() accepts for the
3138 * public key type corresponding to the type of
3139 * private_key. That is, this function performs the
3140 * equivalent of
3141 * #psa_import_key(...,
3142 * `peer_key`, `peer_key_length`) where
3143 * with key attributes indicating the public key
3144 * type corresponding to the type of `private_key`.
3145 * For example, for EC keys, this means that peer_key
3146 * is interpreted as a point on the curve that the
3147 * private key is on. The standard formats for public
3148 * keys are documented in the documentation of
3149 * psa_export_public_key().
3150 * \param peer_key_length Size of \p peer_key in bytes.
3151 *
3152 * \retval #PSA_SUCCESS
3153 * Success.
3154 * \retval #PSA_ERROR_INVALID_HANDLE
3155 * \retval #PSA_ERROR_DOES_NOT_EXIST
3156 * \retval #PSA_ERROR_NOT_PERMITTED
3157 * \retval #PSA_ERROR_INVALID_ARGUMENT
3158 * \c private_key is not compatible with \c alg,
3159 * or \p peer_key is not valid for \c alg or not compatible with
3160 * \c private_key.
3161 * \retval #PSA_ERROR_NOT_SUPPORTED
3162 * \c alg is not supported or is not a key derivation algorithm.
3163 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3164 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3165 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003166 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003167 */
3168psa_status_t psa_key_derivation_key_agreement(
3169 psa_key_derivation_operation_t *operation,
3170 psa_key_derivation_step_t step,
3171 psa_key_handle_t private_key,
3172 const uint8_t *peer_key,
3173 size_t peer_key_length);
3174
Gilles Peskine35675b62019-05-16 17:26:11 +02003175/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003176 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003177 * This function calculates output bytes from a key derivation algorithm and
3178 * return those bytes.
3179 * If you view the key derivation's output as a stream of bytes, this
3180 * function destructively reads the requested number of bytes from the
3181 * stream.
3182 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003183 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003184 * \param[in,out] operation The key derivation operation object to read from.
3185 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003186 * \param output_length Number of bytes to output.
3187 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003188 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003189 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003190 * The operation's capacity was less than
3191 * \p output_length bytes. Note that in this case,
3192 * no output is written to the output buffer.
3193 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003194 * subsequent calls to this function will not
3195 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003196 * \retval #PSA_ERROR_BAD_STATE
3197 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3198 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3199 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003200 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003201 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003202psa_status_t psa_key_derivation_output_bytes(
3203 psa_key_derivation_operation_t *operation,
3204 uint8_t *output,
3205 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003206
Gilles Peskine35675b62019-05-16 17:26:11 +02003207/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003208 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003209 * This function calculates output bytes from a key derivation algorithm
3210 * and uses those bytes to generate a key deterministically.
3211 * If you view the key derivation's output as a stream of bytes, this
3212 * function destructively reads as many bytes as required from the
3213 * stream.
3214 * The operation's capacity decreases by the number of bytes read.
3215 *
3216 * How much output is produced and consumed from the operation, and how
3217 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003218 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003219 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003220 * of a given size, this function is functionally equivalent to
3221 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003222 * and passing the resulting output to #psa_import_key.
3223 * However, this function has a security benefit:
3224 * if the implementation provides an isolation boundary then
3225 * the key material is not exposed outside the isolation boundary.
3226 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003227 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003228 * The following key types defined in this specification follow this scheme:
3229 *
3230 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003231 * - #PSA_KEY_TYPE_ARC4;
3232 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003233 * - #PSA_KEY_TYPE_DERIVE;
3234 * - #PSA_KEY_TYPE_HMAC.
3235 *
3236 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003237 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003238 * Montgomery curve), this function always draws a byte string whose
3239 * length is determined by the curve, and sets the mandatory bits
3240 * accordingly. That is:
3241 *
3242 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3243 * and process it as specified in RFC 7748 &sect;5.
3244 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3245 * and process it as specified in RFC 7748 &sect;5.
3246 *
3247 * - For key types for which the key is represented by a single sequence of
3248 * \p bits bits with constraints as to which bit sequences are acceptable,
3249 * this function draws a byte string of length (\p bits / 8) bytes rounded
3250 * up to the nearest whole number of bytes. If the resulting byte string
3251 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3252 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003253 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003254 * for the output produced by psa_export_key().
3255 * The following key types defined in this specification follow this scheme:
3256 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003257 * - #PSA_KEY_TYPE_DES.
3258 * Force-set the parity bits, but discard forbidden weak keys.
3259 * For 2-key and 3-key triple-DES, the three keys are generated
3260 * successively (for example, for 3-key triple-DES,
3261 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3262 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003263 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003264 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003265 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003266 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003267 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003268 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003269 * Weierstrass curve).
3270 * For these key types, interpret the byte string as integer
3271 * in big-endian order. Discard it if it is not in the range
3272 * [0, *N* - 2] where *N* is the boundary of the private key domain
3273 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003274 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003275 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003276 * This method allows compliance to NIST standards, specifically
3277 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003278 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3279 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3280 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3281 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003282 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003283 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003284 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003285 * implementation-defined.
3286 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003287 * In all cases, the data that is read is discarded from the operation.
3288 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003289 *
Gilles Peskine20628592019-04-19 19:29:50 +02003290 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003291 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003292 * \param[out] handle On success, a handle to the newly created key.
3293 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003294 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003295 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003296 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003297 * If the key is persistent, the key material and the key's metadata
3298 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003299 * \retval #PSA_ERROR_ALREADY_EXISTS
3300 * This is an attempt to create a persistent key, and there is
3301 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003302 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003303 * There was not enough data to create the desired key.
3304 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003305 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003306 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003307 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003308 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003309 * implementation in general or in this particular location.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003310 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003311 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3312 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3313 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3314 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003315 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003316 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003317 * The library has not been previously initialized by psa_crypto_init().
3318 * It is implementation-dependent whether a failure to initialize
3319 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003320 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003321psa_status_t psa_key_derivation_output_key(
3322 const psa_key_attributes_t *attributes,
3323 psa_key_derivation_operation_t *operation,
3324 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003325
Gilles Peskine35675b62019-05-16 17:26:11 +02003326/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003327 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003328 * Once a key derivation operation has been aborted, its capacity is zero.
3329 * Aborting an operation frees all associated resources except for the
3330 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003331 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003332 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003333 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003334 * psa_key_derivation_operation_init() or a zero value. In particular,
3335 * it is valid to call psa_key_derivation_abort() twice, or to call
3336 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003337 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003338 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003339 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003340 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003341 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003342 * \retval #PSA_SUCCESS
3343 * \retval #PSA_ERROR_BAD_STATE
3344 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3345 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003346 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003347 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003348psa_status_t psa_key_derivation_abort(
3349 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003350
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003351/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003352 *
3353 * \warning The raw result of a key agreement algorithm such as finite-field
3354 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3355 * not be used directly as key material. It should instead be passed as
3356 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003357 * a key derivation, use psa_key_derivation_key_agreement() and other
3358 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003359 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003360 * \param alg The key agreement algorithm to compute
3361 * (\c PSA_ALG_XXX value such that
3362 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3363 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003364 * \param private_key Handle to the private key to use.
3365 * \param[in] peer_key Public key of the peer. It must be
3366 * in the same format that psa_import_key()
3367 * accepts. The standard formats for public
3368 * keys are documented in the documentation
3369 * of psa_export_public_key().
3370 * \param peer_key_length Size of \p peer_key in bytes.
3371 * \param[out] output Buffer where the decrypted message is to
3372 * be written.
3373 * \param output_size Size of the \c output buffer in bytes.
3374 * \param[out] output_length On success, the number of bytes
3375 * that make up the returned output.
3376 *
3377 * \retval #PSA_SUCCESS
3378 * Success.
3379 * \retval #PSA_ERROR_INVALID_HANDLE
3380 * \retval #PSA_ERROR_EMPTY_SLOT
3381 * \retval #PSA_ERROR_NOT_PERMITTED
3382 * \retval #PSA_ERROR_INVALID_ARGUMENT
3383 * \p alg is not a key agreement algorithm
3384 * \retval #PSA_ERROR_INVALID_ARGUMENT
3385 * \p private_key is not compatible with \p alg,
3386 * or \p peer_key is not valid for \p alg or not compatible with
3387 * \p private_key.
3388 * \retval #PSA_ERROR_NOT_SUPPORTED
3389 * \p alg is not a supported key agreement algorithm.
3390 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3391 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3392 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003393 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine769c7a62019-01-18 16:42:29 +01003394 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003395psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3396 psa_key_handle_t private_key,
3397 const uint8_t *peer_key,
3398 size_t peer_key_length,
3399 uint8_t *output,
3400 size_t output_size,
3401 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003402
Gilles Peskineea0fb492018-07-12 17:17:20 +02003403/**@}*/
3404
Gilles Peskineedd76872018-07-20 17:42:05 +02003405/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003406 * @{
3407 */
3408
3409/**
3410 * \brief Generate random bytes.
3411 *
3412 * \warning This function **can** fail! Callers MUST check the return status
3413 * and MUST NOT use the content of the output buffer if the return
3414 * status is not #PSA_SUCCESS.
3415 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003416 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003417 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003418 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003419 * \param output_size Number of bytes to generate and output.
3420 *
Gilles Peskine28538492018-07-11 17:34:00 +02003421 * \retval #PSA_SUCCESS
3422 * \retval #PSA_ERROR_NOT_SUPPORTED
3423 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3424 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3425 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003426 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003427 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003428 * The library has not been previously initialized by psa_crypto_init().
3429 * It is implementation-dependent whether a failure to initialize
3430 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003431 */
3432psa_status_t psa_generate_random(uint8_t *output,
3433 size_t output_size);
3434
3435/**
3436 * \brief Generate a key or key pair.
3437 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003438 * The key is generated randomly.
3439 * Its location, policy, type and size are taken from \p attributes.
3440 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003441 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003442 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003443 * the public exponent is 65537.
3444 * The modulus is a product of two probabilistic primes
3445 * between 2^{n-1} and 2^n where n is the bit size specified in the
3446 * attributes.
3447 *
Gilles Peskine20628592019-04-19 19:29:50 +02003448 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003449 * \param[out] handle On success, a handle to the newly created key.
3450 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003451 *
Gilles Peskine28538492018-07-11 17:34:00 +02003452 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003453 * Success.
3454 * If the key is persistent, the key material and the key's metadata
3455 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003456 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003457 * This is an attempt to create a persistent key, and there is
3458 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003459 * \retval #PSA_ERROR_NOT_SUPPORTED
3460 * \retval #PSA_ERROR_INVALID_ARGUMENT
3461 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3462 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3463 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3464 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003465 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003466 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003467 * The library has not been previously initialized by psa_crypto_init().
3468 * It is implementation-dependent whether a failure to initialize
3469 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003470 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003471psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003472 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003473
3474/**@}*/
3475
Gilles Peskinee59236f2018-01-27 23:32:46 +01003476#ifdef __cplusplus
3477}
3478#endif
3479
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003480/* The file "crypto_sizes.h" contains definitions for size calculation
3481 * macros whose definitions are implementation-specific. */
3482#include "crypto_sizes.h"
3483
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003484/* The file "crypto_struct.h" contains definitions for
3485 * implementation-specific structs that are declared above. */
3486#include "crypto_struct.h"
3487
3488/* The file "crypto_extra.h" contains vendor-specific definitions. This
3489 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003490#include "crypto_extra.h"
3491
3492#endif /* PSA_CRYPTO_H */