blob: 17af57dec141ee7a59df07799d3f043fb0ff5e93 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
89 * \retval #PSA_ERROR_TAMPERING_DETECTED
90 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine87a5e562019-04-17 12:28:25 +020096/** \defgroup attributes Key attributes
97 * @{
98 */
99
100/** The type of a structure containing key attributes.
101 *
102 * This is an opaque structure that can represent the metadata of a key
Gilles Peskine9c640f92019-04-28 11:36:21 +0200103 * object. Metadata that can be stored in attributes includes:
104 * - The location of the key in storage, indicated by its key identifier
105 * and its lifetime.
106 * - The key's policy, comprising usage flags and a specification of
107 * the permitted algorithm(s).
108 * - Information about the key itself: the key type, the key size, and
109 * for some key type additional domain parameters.
110 * - Implementations may define additional attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200111 *
112 * The actual key material is not considered an attribute of a key.
113 * Key attributes do not contain information that is generally considered
114 * highly confidential.
Gilles Peskine20628592019-04-19 19:29:50 +0200115 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200116 * An attribute structure can be a simple data structure where each function
117 * `psa_set_key_xxx` sets a field and the corresponding function
118 * `psa_get_key_xxx` retrieves the value of the corresponding field.
119 * However, implementations may report values that are equivalent to the
120 * original one, but have a different encoding. For example, an
121 * implementation may use a more compact representation for types where
122 * many bit-patterns are invalid or not supported, and store all values
123 * that it does not support as a special marker value. In such an
124 * implementation, after setting an invalid value, the corresponding
125 * get function returns an invalid value which may not be the one that
126 * was originally stored.
127 *
128 * An attribute structure may contain references to auxiliary resources,
129 * for example pointers to allocated memory or indirect references to
130 * pre-calculated values. In order to free such resources, the application
131 * must call psa_reset_key_attributes(). As an exception, calling
132 * psa_reset_key_attributes() on an attribute structure is optional if
133 * the structure has only been modified by the following functions
134 * since it was initialized or last reset with psa_reset_key_attributes():
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200135 * - psa_set_key_id()
136 * - psa_set_key_lifetime()
Gilles Peskine9c640f92019-04-28 11:36:21 +0200137 * - psa_set_key_type()
138 * - psa_set_key_bits()
139 * - psa_set_key_usage_flags()
140 * - psa_set_key_algorithm()
141 *
Gilles Peskine20628592019-04-19 19:29:50 +0200142 * Before calling any function on a key attribute structure, the application
143 * must initialize it by any of the following means:
144 * - Set the structure to all-bits-zero, for example:
145 * \code
146 * psa_key_attributes_t attributes;
147 * memset(&attributes, 0, sizeof(attributes));
148 * \endcode
149 * - Initialize the structure to logical zero values, for example:
150 * \code
151 * psa_key_attributes_t attributes = {0};
152 * \endcode
153 * - Initialize the structure to the initializer #PSA_KEY_ATTRIBUTES_INIT,
154 * for example:
155 * \code
156 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
157 * \endcode
158 * - Assign the result of the function psa_key_attributes_init()
159 * to the structure, for example:
160 * \code
161 * psa_key_attributes_t attributes;
162 * attributes = psa_key_attributes_init();
163 * \endcode
164 *
165 * A freshly initialized attribute structure contains the following
166 * values:
167 *
168 * - lifetime: #PSA_KEY_LIFETIME_VOLATILE.
169 * - key identifier: unspecified.
170 * - type: \c 0, with no domain parameters.
171 * - key size: \c 0.
172 * - usage flags: \c 0.
173 * - algorithm: \c 0.
174 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200175 * A typical sequence to create a key is as follows:
176 * -# Create and initialize an attribute structure.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200177 * -# If the key is persistent, call psa_set_key_id().
178 * Also call psa_set_key_lifetime() to place the key in a non-default
179 * location.
Gilles Peskine9c640f92019-04-28 11:36:21 +0200180 * -# Set the key policy with psa_set_key_usage_flags() and
181 * psa_set_key_algorithm().
182 * -# Set the key type with psa_set_key_type(). If the key type requires
183 * domain parameters, call psa_set_key_domain_parameters() instead.
184 * Skip this step if copying an existing key with psa_copy_key().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100185 * -# When generating a random key with psa_generate_random_key() or deriving a key
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200186 * with psa_key_derivation_output_key(), set the desired key size with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200187 * psa_set_key_bits().
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +0100188 * -# Call a key creation function: psa_import_key(), psa_generate_random_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200189 * psa_key_derivation_output_key() or psa_copy_key(). This function reads
Gilles Peskine1ea5e442019-05-02 20:31:10 +0200190 * the attribute structure, creates a key with these attributes, and
191 * outputs a handle to the newly created key.
192 * -# The attribute structure is now no longer necessary. If you called
Gilles Peskine9c640f92019-04-28 11:36:21 +0200193 * psa_set_key_domain_parameters() earlier, you must call
194 * psa_reset_key_attributes() to free any resources used by the
195 * domain parameters. Otherwise calling psa_reset_key_attributes()
196 * is optional.
Gilles Peskine20628592019-04-19 19:29:50 +0200197 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200198 * A typical sequence to query a key's attributes is as follows:
199 * -# Call psa_get_key_attributes().
200 * -# Call `psa_get_key_xxx` functions to retrieve the attribute(s) that
201 * you are interested in.
202 * -# Call psa_reset_key_attributes() to free any resources that may be
203 * used by the attribute structure.
204 *
205 * Once a key has been created, it is impossible to change its attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200206 */
207typedef struct psa_key_attributes_s psa_key_attributes_t;
208
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200209/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200210 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200211 * If the attribute structure currently declares the key as volatile (which
212 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200213 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200214 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200215 * This function does not access storage, it merely stores the given
216 * value in the structure.
217 * The persistent key will be written to storage when the attribute
218 * structure is passed to a key creation function such as
219 * psa_import_key(), psa_generate_random_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200220 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200221 *
Gilles Peskine20628592019-04-19 19:29:50 +0200222 * This function may be declared as `static` (i.e. without external
223 * linkage). This function may be provided as a function-like macro,
224 * but in this case it must evaluate each of its arguments exactly once.
225 *
226 * \param[out] attributes The attribute structure to write to.
227 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200228 */
229static void psa_set_key_id(psa_key_attributes_t *attributes,
230 psa_key_id_t id);
231
232/** Set the location of a persistent key.
233 *
234 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200235 * with psa_set_key_id(). By default, a key that has a persistent identifier
236 * is stored in the default storage area identifier by
237 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
238 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200239 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200240 * This function does not access storage, it merely stores the given
241 * value in the structure.
242 * The persistent key will be written to storage when the attribute
243 * structure is passed to a key creation function such as
244 * psa_import_key(), psa_generate_random_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200245 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate each of its arguments exactly once.
250 *
251 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200252 * \param lifetime The lifetime for the key.
253 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200254 * key will be volatile, and the key identifier
255 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200256 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200257static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
258 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200259
Gilles Peskine20628592019-04-19 19:29:50 +0200260/** Retrieve the key identifier from key attributes.
261 *
262 * This function may be declared as `static` (i.e. without external
263 * linkage). This function may be provided as a function-like macro,
264 * but in this case it must evaluate its argument exactly once.
265 *
266 * \param[in] attributes The key attribute structure to query.
267 *
268 * \return The persistent identifier stored in the attribute structure.
269 * This value is unspecified if the attribute structure declares
270 * the key as volatile.
271 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200272static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
273
Gilles Peskine20628592019-04-19 19:29:50 +0200274/** Retrieve the lifetime 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 lifetime value stored in the attribute structure.
283 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200284static psa_key_lifetime_t psa_get_key_lifetime(
285 const psa_key_attributes_t *attributes);
286
Gilles Peskine20628592019-04-19 19:29:50 +0200287/** Declare usage flags for a key.
288 *
289 * Usage flags are part of a key's usage policy. They encode what
290 * kind of operations are permitted on the key. For more details,
291 * refer to the documentation of the type #psa_key_usage_t.
292 *
293 * This function overwrites any usage flags
294 * previously set in \p attributes.
295 *
296 * This function may be declared as `static` (i.e. without external
297 * linkage). This function may be provided as a function-like macro,
298 * but in this case it must evaluate each of its arguments exactly once.
299 *
300 * \param[out] attributes The attribute structure to write to.
301 * \param usage_flags The usage flags to write.
302 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200303static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
304 psa_key_usage_t usage_flags);
305
Gilles Peskine20628592019-04-19 19:29:50 +0200306/** Retrieve the usage flags from key attributes.
307 *
308 * This function may be declared as `static` (i.e. without external
309 * linkage). This function may be provided as a function-like macro,
310 * but in this case it must evaluate its argument exactly once.
311 *
312 * \param[in] attributes The key attribute structure to query.
313 *
314 * \return The usage flags stored in the attribute structure.
315 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200316static psa_key_usage_t psa_get_key_usage_flags(
317 const psa_key_attributes_t *attributes);
318
Gilles Peskine20628592019-04-19 19:29:50 +0200319/** Declare the permitted algorithm policy for a key.
320 *
321 * The permitted algorithm policy of a key encodes which algorithm or
322 * algorithms are permitted to be used with this key.
323 *
324 * This function overwrites any algorithm policy
325 * previously set in \p attributes.
326 *
327 * This function may be declared as `static` (i.e. without external
328 * linkage). This function may be provided as a function-like macro,
329 * but in this case it must evaluate each of its arguments exactly once.
330 *
331 * \param[out] attributes The attribute structure to write to.
332 * \param alg The permitted algorithm policy to write.
333 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200334static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
335 psa_algorithm_t alg);
336
Gilles Peskine20628592019-04-19 19:29:50 +0200337/** Retrieve the algorithm policy from key attributes.
338 *
339 * This function may be declared as `static` (i.e. without external
340 * linkage). This function may be provided as a function-like macro,
341 * but in this case it must evaluate its argument exactly once.
342 *
343 * \param[in] attributes The key attribute structure to query.
344 *
345 * \return The algorithm stored in the attribute structure.
346 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200347static psa_algorithm_t psa_get_key_algorithm(
348 const psa_key_attributes_t *attributes);
349
Gilles Peskine20628592019-04-19 19:29:50 +0200350/** Declare the type of a key.
351 *
352 * If a type requires domain parameters, you must call
353 * psa_set_key_domain_parameters() instead of this function.
354 *
355 * This function overwrites any key type and domain parameters
356 * previously set in \p attributes.
357 *
358 * This function may be declared as `static` (i.e. without external
359 * linkage). This function may be provided as a function-like macro,
360 * but in this case it must evaluate each of its arguments exactly once.
361 *
362 * \param[out] attributes The attribute structure to write to.
363 * \param type The key type to write.
364 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200365static void psa_set_key_type(psa_key_attributes_t *attributes,
366 psa_key_type_t type);
367
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200368/** Declare the size of a key.
369 *
370 * This function overwrites any key size previously set in \p attributes.
371 *
372 * This function may be declared as `static` (i.e. without external
373 * linkage). This function may be provided as a function-like macro,
374 * but in this case it must evaluate each of its arguments exactly once.
375 *
376 * \param[out] attributes The attribute structure to write to.
377 * \param bits The key size in bits.
378 */
379static void psa_set_key_bits(psa_key_attributes_t *attributes,
380 size_t bits);
381
Gilles Peskine20628592019-04-19 19:29:50 +0200382/** Retrieve the key type from key attributes.
383 *
384 * This function may be declared as `static` (i.e. without external
385 * linkage). This function may be provided as a function-like macro,
386 * but in this case it must evaluate its argument exactly once.
387 *
388 * \param[in] attributes The key attribute structure to query.
389 *
390 * \return The key type stored in the attribute structure.
391 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200392static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
393
Gilles Peskine20628592019-04-19 19:29:50 +0200394/** Retrieve the key size from key attributes.
395 *
396 * This function may be declared as `static` (i.e. without external
397 * linkage). This function may be provided as a function-like macro,
398 * but in this case it must evaluate its argument exactly once.
399 *
400 * \param[in] attributes The key attribute structure to query.
401 *
402 * \return The key size stored in the attribute structure, in bits.
403 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200404static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
405
Gilles Peskineb699f072019-04-26 16:06:02 +0200406/**
407 * \brief Set domain parameters for a key.
408 *
409 * Some key types require additional domain parameters in addition to
410 * the key type identifier and the key size.
411 * The format for the required domain parameters varies by the key type.
412 *
Gilles Peskinee56e8782019-04-26 17:34:02 +0200413 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEYPAIR),
414 * the domain parameter data consists of the public exponent,
Gilles Peskineb699f072019-04-26 16:06:02 +0200415 * represented as a big-endian integer with no leading zeros.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200416 * This information is used when generating an RSA key pair.
Gilles Peskineb699f072019-04-26 16:06:02 +0200417 * When importing a key, the public exponent is read from the imported
418 * key data and the exponent recorded in the attribute structure is ignored.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200419 * As an exception, the public exponent 65537 is represented by an empty
420 * byte string.
421 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEYPAIR),
Gilles Peskineb699f072019-04-26 16:06:02 +0200422 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
423 * ```
424 * Dss-Parms ::= SEQUENCE {
425 * p INTEGER,
426 * q INTEGER,
427 * g INTEGER
428 * }
429 * ```
Gilles Peskinee56e8782019-04-26 17:34:02 +0200430 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY or
431 * #PSA_KEY_TYPE_DH_KEYPAIR), the
Gilles Peskineb699f072019-04-26 16:06:02 +0200432 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
433 * ```
434 * DomainParameters ::= SEQUENCE {
435 * p INTEGER, -- odd prime, p=jq +1
436 * g INTEGER, -- generator, g
437 * q INTEGER, -- factor of p-1
438 * j INTEGER OPTIONAL, -- subgroup factor
439 * validationParms ValidationParms OPTIONAL
440 * }
441 * ValidationParms ::= SEQUENCE {
442 * seed BIT STRING,
443 * pgenCounter INTEGER
444 * }
445 * ```
446 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200447 * \note This function may allocate memory or other resources.
448 * Once you have called this function on an attribute structure,
449 * you must call psa_reset_key_attributes() to free these resources.
450 *
Gilles Peskineb699f072019-04-26 16:06:02 +0200451 * \param[in,out] attributes Attribute structure where the specified domain
452 * parameters will be stored.
453 * If this function fails, the content of
454 * \p attributes is not modified.
455 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
456 * \param[in] data Buffer containing the key domain parameters.
457 * The content of this buffer is interpreted
458 * according to \p type as described above.
459 * \param data_length Size of the \p data buffer in bytes.
460 *
461 * \retval #PSA_SUCCESS
462 * \retval #PSA_ERROR_INVALID_ARGUMENT
463 * \retval #PSA_ERROR_NOT_SUPPORTED
464 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
465 */
466psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
467 psa_key_type_t type,
468 const uint8_t *data,
469 size_t data_length);
470
471/**
472 * \brief Get domain parameters for a key.
473 *
474 * Get the domain parameters for a key with this function, if any. The format
475 * of the domain parameters written to \p data is specified in the
476 * documentation for psa_set_key_domain_parameters().
477 *
478 * \param[in] attributes The key attribute structure to query.
479 * \param[out] data On success, the key domain parameters.
480 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineaa02c172019-04-28 11:44:17 +0200481 * The buffer is guaranteed to be large
482 * enough if its size in bytes is at least
483 * the value given by
484 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
Gilles Peskineb699f072019-04-26 16:06:02 +0200485 * \param[out] data_length On success, the number of bytes
486 * that make up the key domain parameters data.
487 *
488 * \retval #PSA_SUCCESS
489 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
490 */
491psa_status_t psa_get_key_domain_parameters(
492 const psa_key_attributes_t *attributes,
493 uint8_t *data,
494 size_t data_size,
495 size_t *data_length);
496
Gilles Peskine20628592019-04-19 19:29:50 +0200497/** Retrieve the attributes of a key.
498 *
499 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200500 * psa_reset_key_attributes(). It then copies the attributes of
501 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200502 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200503 * \note This function may allocate memory or other resources.
504 * Once you have called this function on an attribute structure,
505 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200506 *
Gilles Peskine20628592019-04-19 19:29:50 +0200507 * \param[in] handle Handle to the key to query.
508 * \param[in,out] attributes On success, the attributes of the key.
509 * On failure, equivalent to a
510 * freshly-initialized structure.
511 *
512 * \retval #PSA_SUCCESS
513 * \retval #PSA_ERROR_INVALID_HANDLE
514 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
515 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
516 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200517psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
518 psa_key_attributes_t *attributes);
519
Gilles Peskine20628592019-04-19 19:29:50 +0200520/** Reset a key attribute structure to a freshly initialized state.
521 *
522 * You must initialize the attribute structure as described in the
523 * documentation of the type #psa_key_attributes_t before calling this
524 * function. Once the structure has been initialized, you may call this
525 * function at any time.
526 *
527 * This function frees any auxiliary resources that the structure
528 * may contain.
529 *
530 * \param[in,out] attributes The attribute structure to reset.
531 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200532void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200533
Gilles Peskine87a5e562019-04-17 12:28:25 +0200534/**@}*/
535
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100536/** \defgroup key_management Key management
537 * @{
538 */
539
Gilles Peskinef535eb22018-11-30 14:08:36 +0100540/** Open a handle to an existing persistent key.
541 *
542 * Open a handle to a key which was previously created with psa_create_key().
543 *
Gilles Peskine4a231b82019-05-06 18:56:14 +0200544 * Implementations may provide additional keys that can be opened with
545 * psa_open_key(). Such keys have a key identifier in the vendor range,
546 * as documented in the description of #psa_key_id_t.
547 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100548 * \param id The persistent identifier of the key.
549 * \param[out] handle On success, a handle to a key slot which contains
550 * the data and metadata loaded from the specified
551 * persistent location.
552 *
553 * \retval #PSA_SUCCESS
554 * Success. The application can now use the value of `*handle`
555 * to access the newly allocated key slot.
556 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200557 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100558 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine225010f2019-05-06 18:44:55 +0200559 * \p id is invalid.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100560 * \retval #PSA_ERROR_NOT_PERMITTED
561 * The specified key exists, but the application does not have the
562 * permission to access it. Note that this specification does not
563 * define any way to create such a key, but it may be possible
564 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200565 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
566 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100567 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200568psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100569 psa_key_handle_t *handle);
570
Gilles Peskinef535eb22018-11-30 14:08:36 +0100571/** Close a key handle.
572 *
573 * If the handle designates a volatile key, destroy the key material and
574 * free all associated resources, just like psa_destroy_key().
575 *
576 * If the handle designates a persistent key, free all resources associated
577 * with the key in volatile memory. The key slot in persistent storage is
578 * not affected and can be opened again later with psa_open_key().
579 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100580 * If the key is currently in use in a multipart operation,
581 * the multipart operation is aborted.
582 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100583 * \param handle The key handle to close.
584 *
585 * \retval #PSA_SUCCESS
586 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100587 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100588 */
589psa_status_t psa_close_key(psa_key_handle_t handle);
590
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100591/**@}*/
592
593/** \defgroup import_export Key import and export
594 * @{
595 */
596
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100597/**
598 * \brief Import a key in binary format.
599 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100600 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100601 * documentation of psa_export_public_key() for the format of public keys
602 * and to the documentation of psa_export_key() for the format for
603 * other key types.
604 *
605 * This specification supports a single format for each key type.
606 * Implementations may support other formats as long as the standard
607 * format is supported. Implementations that support other formats
608 * should ensure that the formats are clearly unambiguous so as to
609 * minimize the risk that an invalid input is accidentally interpreted
610 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100611 *
Gilles Peskine20628592019-04-19 19:29:50 +0200612 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200613 * The key size is always determined from the
614 * \p data buffer.
615 * If the key size in \p attributes is nonzero,
616 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200617 * \param[out] handle On success, a handle to the newly created key.
618 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100619 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine20628592019-04-19 19:29:50 +0200620 * buffer is interpreted according to the type and,
621 * if applicable, domain parameters declared in
622 * \p attributes.
623 * All implementations must support at least the format
624 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100625 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200626 * the chosen type. Implementations may allow other
627 * formats, but should be conservative: implementations
628 * should err on the side of rejecting content if it
629 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200630 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100631 *
Gilles Peskine28538492018-07-11 17:34:00 +0200632 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100633 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100634 * If the key is persistent, the key material and the key's metadata
635 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200636 * \retval #PSA_ERROR_ALREADY_EXISTS
637 * This is an attempt to create a persistent key, and there is
638 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200639 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200640 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200641 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200642 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200643 * The key attributes, as a whole, are invalid.
644 * \retval #PSA_ERROR_INVALID_ARGUMENT
645 * The key data is not correctly formatted.
646 * \retval #PSA_ERROR_INVALID_ARGUMENT
647 * The size in \p attributes is nonzero and does not match the size
648 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200649 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
650 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
651 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100652 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200653 * \retval #PSA_ERROR_HARDWARE_FAILURE
654 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300655 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300656 * The library has not been previously initialized by psa_crypto_init().
657 * It is implementation-dependent whether a failure to initialize
658 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200660psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200662 size_t data_length,
663 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100664
665/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100666 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200667 *
668 * This function destroys the content of the key slot from both volatile
669 * memory and, if applicable, non-volatile storage. Implementations shall
670 * make a best effort to ensure that any previous content of the slot is
671 * unrecoverable.
672 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100673 * This function also erases any metadata such as policies and frees all
674 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200675 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100676 * If the key is currently in use in a multipart operation,
677 * the multipart operation is aborted.
678 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100679 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680 *
Gilles Peskine28538492018-07-11 17:34:00 +0200681 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200682 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200683 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200684 * The slot holds content and cannot be erased because it is
685 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100686 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200687 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200688 * There was an failure in communication with the cryptoprocessor.
689 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200690 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200691 * The storage is corrupted. Implementations shall make a best effort
692 * to erase key material even in this stage, however applications
693 * should be aware that it may be impossible to guarantee that the
694 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200695 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200696 * An unexpected condition which is not a storage corruption or
697 * a communication failure occurred. The cryptoprocessor may have
698 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300699 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300700 * The library has not been previously initialized by psa_crypto_init().
701 * It is implementation-dependent whether a failure to initialize
702 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100703 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100704psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100705
706/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100707 * \brief Export a key in binary format.
708 *
709 * The output of this function can be passed to psa_import_key() to
710 * create an equivalent object.
711 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100712 * If the implementation of psa_import_key() supports other formats
713 * beyond the format specified here, the output from psa_export_key()
714 * must use the representation specified here, not the original
715 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100716 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100717 * For standard key types, the output format is as follows:
718 *
719 * - For symmetric keys (including MAC keys), the format is the
720 * raw bytes of the key.
721 * - For DES, the key data consists of 8 bytes. The parity bits must be
722 * correct.
723 * - For Triple-DES, the format is the concatenation of the
724 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100725 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200726 * is the non-encrypted DER encoding of the representation defined by
727 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
728 * ```
729 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200730 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200731 * modulus INTEGER, -- n
732 * publicExponent INTEGER, -- e
733 * privateExponent INTEGER, -- d
734 * prime1 INTEGER, -- p
735 * prime2 INTEGER, -- q
736 * exponent1 INTEGER, -- d mod (p-1)
737 * exponent2 INTEGER, -- d mod (q-1)
738 * coefficient INTEGER, -- (inverse of q) mod p
739 * }
740 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000741 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
742 * representation of the private key `x` as a big-endian byte string. The
743 * length of the byte string is the private key size in bytes (leading zeroes
744 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200745 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100746 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100747 * a representation of the private value as a `ceiling(m/8)`-byte string
748 * where `m` is the bit size associated with the curve, i.e. the bit size
749 * of the order of the curve's coordinate field. This byte string is
750 * in little-endian order for Montgomery curves (curve types
751 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
752 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
753 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100754 * This is the content of the `privateKey` field of the `ECPrivateKey`
755 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000756 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
757 * format is the representation of the private key `x` as a big-endian byte
758 * string. The length of the byte string is the private key size in bytes
759 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200760 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
761 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100762 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200763 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
764 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100765 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200766 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200767 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200768 * \param[out] data_length On success, the number of bytes
769 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100770 *
Gilles Peskine28538492018-07-11 17:34:00 +0200771 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100772 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200773 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200774 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200775 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100776 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200777 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
778 * The size of the \p data buffer is too small. You can determine a
779 * sufficient buffer size by calling
780 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
781 * where \c type is the key type
782 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200783 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
784 * \retval #PSA_ERROR_HARDWARE_FAILURE
785 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300786 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300787 * The library has not been previously initialized by psa_crypto_init().
788 * It is implementation-dependent whether a failure to initialize
789 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100790 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100791psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100792 uint8_t *data,
793 size_t data_size,
794 size_t *data_length);
795
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100796/**
797 * \brief Export a public key or the public part of a key pair in binary format.
798 *
799 * The output of this function can be passed to psa_import_key() to
800 * create an object that is equivalent to the public key.
801 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000802 * This specification supports a single format for each key type.
803 * Implementations may support other formats as long as the standard
804 * format is supported. Implementations that support other formats
805 * should ensure that the formats are clearly unambiguous so as to
806 * minimize the risk that an invalid input is accidentally interpreted
807 * according to a different format.
808 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000809 * For standard key types, the output format is as follows:
810 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
811 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
812 * ```
813 * RSAPublicKey ::= SEQUENCE {
814 * modulus INTEGER, -- n
815 * publicExponent INTEGER } -- e
816 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000817 * - For elliptic curve public keys (key types for which
818 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
819 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
820 * Let `m` be the bit size associated with the curve, i.e. the bit size of
821 * `q` for a curve over `F_q`. The representation consists of:
822 * - The byte 0x04;
823 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
824 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000825 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
826 * representation of the public key `y = g^x mod p` as a big-endian byte
827 * string. The length of the byte string is the length of the base prime `p`
828 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000829 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
830 * the format is the representation of the public key `y = g^x mod p` as a
831 * big-endian byte string. The length of the byte string is the length of the
832 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100833 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200834 * Exporting a public key object or the public part of a key pair is
835 * always permitted, regardless of the key's usage flags.
836 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100837 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200838 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200839 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200840 * \param[out] data_length On success, the number of bytes
841 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100842 *
Gilles Peskine28538492018-07-11 17:34:00 +0200843 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100844 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200845 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200846 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200847 * The key is neither a public key nor a key pair.
848 * \retval #PSA_ERROR_NOT_SUPPORTED
849 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
850 * The size of the \p data buffer is too small. You can determine a
851 * sufficient buffer size by calling
852 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
853 * where \c type is the key type
854 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200855 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
856 * \retval #PSA_ERROR_HARDWARE_FAILURE
857 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300858 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300859 * The library has not been previously initialized by psa_crypto_init().
860 * It is implementation-dependent whether a failure to initialize
861 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100862 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100863psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100864 uint8_t *data,
865 size_t data_size,
866 size_t *data_length);
867
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100868/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100869 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100870 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000871 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100872 * This function is primarily useful to copy a key from one location
873 * to another, since it populates a key using the material from
874 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200875 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100876 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100877 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100878 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100879 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200880 * The policy on the source key must have the usage flag
881 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200882 * This flag is sufficient to permit the copy if the key has the lifetime
883 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
884 * Some secure elements do not provide a way to copy a key without
885 * making it extractable from the secure element. If a key is located
886 * in such a secure element, then the key must have both usage flags
887 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
888 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200889 *
Gilles Peskine20628592019-04-19 19:29:50 +0200890 * The resulting key may only be used in a way that conforms to
891 * both the policy of the original key and the policy specified in
892 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100893 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200894 * usage flags on the source policy and the usage flags in \p attributes.
895 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100896 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200897 * - If either of the policies allows an algorithm and the other policy
898 * allows a wildcard-based algorithm policy that includes this algorithm,
899 * the resulting key allows the same algorithm.
900 * - If the policies do not allow any algorithm in common, this function
901 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200902 *
Gilles Peskine20628592019-04-19 19:29:50 +0200903 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100904 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200905 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100906 * \param source_handle The key to copy. It must be a handle to an
907 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200908 * \param[in] attributes The attributes for the new key.
909 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200910 * - The key type and size may be 0. If either is
911 * nonzero, it must match the corresponding
912 * attribute of the source key.
913 * - If \p attributes contains domain parameters,
914 * they must match the domain parameters of
915 * the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200916 * - The key location (the lifetime and, for
917 * persistent keys, the key identifier) is
918 * used directly.
919 * - The policy constraints (usage flags and
920 * algorithm policy) are combined from
921 * the source key and \p attributes so that
922 * both sets of restrictions apply, as
923 * described in the documentation of this function.
924 * \param[out] target_handle On success, a handle to the newly created key.
925 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200926 *
927 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100928 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200929 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200930 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200931 * This is an attempt to create a persistent key, and there is
932 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200933 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200934 * The lifetime or identifier in \p attributes are invalid.
935 * \retval #PSA_ERROR_INVALID_ARGUMENT
936 * The policy constraints on the source and specified in
937 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200938 * \retval #PSA_ERROR_INVALID_ARGUMENT
939 * \p attributes specifies a key type, domain parameters or key size
940 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100941 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200942 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
943 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100944 * The source key is not exportable and its lifetime does not
945 * allow copying it to the target's lifetime.
946 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
947 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200948 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
949 * \retval #PSA_ERROR_HARDWARE_FAILURE
950 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100951 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100952psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200953 const psa_key_attributes_t *attributes,
954 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100955
956/**@}*/
957
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100958/** \defgroup hash Message digests
959 * @{
960 */
961
Gilles Peskine69647a42019-01-14 20:18:12 +0100962/** Calculate the hash (digest) of a message.
963 *
964 * \note To verify the hash of a message against an
965 * expected value, use psa_hash_compare() instead.
966 *
967 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
968 * such that #PSA_ALG_IS_HASH(\p alg) is true).
969 * \param[in] input Buffer containing the message to hash.
970 * \param input_length Size of the \p input buffer in bytes.
971 * \param[out] hash Buffer where the hash is to be written.
972 * \param hash_size Size of the \p hash buffer in bytes.
973 * \param[out] hash_length On success, the number of bytes
974 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100975 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100976 *
977 * \retval #PSA_SUCCESS
978 * Success.
979 * \retval #PSA_ERROR_NOT_SUPPORTED
980 * \p alg is not supported or is not a hash algorithm.
981 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
982 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
983 * \retval #PSA_ERROR_HARDWARE_FAILURE
984 * \retval #PSA_ERROR_TAMPERING_DETECTED
985 */
986psa_status_t psa_hash_compute(psa_algorithm_t alg,
987 const uint8_t *input,
988 size_t input_length,
989 uint8_t *hash,
990 size_t hash_size,
991 size_t *hash_length);
992
993/** Calculate the hash (digest) of a message and compare it with a
994 * reference value.
995 *
996 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
997 * such that #PSA_ALG_IS_HASH(\p alg) is true).
998 * \param[in] input Buffer containing the message to hash.
999 * \param input_length Size of the \p input buffer in bytes.
1000 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +01001001 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +01001002 *
1003 * \retval #PSA_SUCCESS
1004 * The expected hash is identical to the actual hash of the input.
1005 * \retval #PSA_ERROR_INVALID_SIGNATURE
1006 * The hash of the message was calculated successfully, but it
1007 * differs from the expected hash.
1008 * \retval #PSA_ERROR_NOT_SUPPORTED
1009 * \p alg is not supported or is not a hash algorithm.
1010 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1011 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1012 * \retval #PSA_ERROR_HARDWARE_FAILURE
1013 * \retval #PSA_ERROR_TAMPERING_DETECTED
1014 */
1015psa_status_t psa_hash_compare(psa_algorithm_t alg,
1016 const uint8_t *input,
1017 size_t input_length,
1018 const uint8_t *hash,
1019 const size_t hash_length);
1020
Gilles Peskine308b91d2018-02-08 09:47:44 +01001021/** The type of the state data structure for multipart hash operations.
1022 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001023 * Before calling any function on a hash operation object, the application must
1024 * initialize it by any of the following means:
1025 * - Set the structure to all-bits-zero, for example:
1026 * \code
1027 * psa_hash_operation_t operation;
1028 * memset(&operation, 0, sizeof(operation));
1029 * \endcode
1030 * - Initialize the structure to logical zero values, for example:
1031 * \code
1032 * psa_hash_operation_t operation = {0};
1033 * \endcode
1034 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
1035 * for example:
1036 * \code
1037 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
1038 * \endcode
1039 * - Assign the result of the function psa_hash_operation_init()
1040 * to the structure, for example:
1041 * \code
1042 * psa_hash_operation_t operation;
1043 * operation = psa_hash_operation_init();
1044 * \endcode
1045 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001046 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 * make any assumptions about the content of this structure except
1048 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001049typedef struct psa_hash_operation_s psa_hash_operation_t;
1050
Jaeden Amero6a25b412019-01-04 11:47:44 +00001051/** \def PSA_HASH_OPERATION_INIT
1052 *
1053 * This macro returns a suitable initializer for a hash operation object
1054 * of type #psa_hash_operation_t.
1055 */
1056#ifdef __DOXYGEN_ONLY__
1057/* This is an example definition for documentation purposes.
1058 * Implementations should define a suitable value in `crypto_struct.h`.
1059 */
1060#define PSA_HASH_OPERATION_INIT {0}
1061#endif
1062
1063/** Return an initial value for a hash operation object.
1064 */
1065static psa_hash_operation_t psa_hash_operation_init(void);
1066
Gilles Peskinef45adda2019-01-14 18:29:18 +01001067/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001068 *
1069 * The sequence of operations to calculate a hash (message digest)
1070 * is as follows:
1071 * -# Allocate an operation object which will be passed to all the functions
1072 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +00001073 * -# Initialize the operation object with one of the methods described in the
1074 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001075 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001076 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001077 * of the message each time. The hash that is calculated is the hash
1078 * of the concatenation of these messages in order.
1079 * -# To calculate the hash, call psa_hash_finish().
1080 * To compare the hash with an expected value, call psa_hash_verify().
1081 *
1082 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +00001083 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001084 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001085 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001086 * eventually terminate the operation. The following events terminate an
1087 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001088 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001089 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001091 * \param[in,out] operation The operation object to set up. It must have
1092 * been initialized as per the documentation for
1093 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001094 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1095 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001096 *
Gilles Peskine28538492018-07-11 17:34:00 +02001097 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001098 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001099 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001100 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001101 * \retval #PSA_ERROR_BAD_STATE
1102 * The operation state is not valid (already set up and not
1103 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001104 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1105 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1106 * \retval #PSA_ERROR_HARDWARE_FAILURE
1107 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001108 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001109psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001110 psa_algorithm_t alg);
1111
Gilles Peskine308b91d2018-02-08 09:47:44 +01001112/** Add a message fragment to a multipart hash operation.
1113 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001114 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001115 *
1116 * If this function returns an error status, the operation becomes inactive.
1117 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001118 * \param[in,out] operation Active hash operation.
1119 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001120 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001121 *
Gilles Peskine28538492018-07-11 17:34:00 +02001122 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001123 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001124 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001125 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001126 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1127 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1128 * \retval #PSA_ERROR_HARDWARE_FAILURE
1129 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001130 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001131psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1132 const uint8_t *input,
1133 size_t input_length);
1134
Gilles Peskine308b91d2018-02-08 09:47:44 +01001135/** Finish the calculation of the hash of a message.
1136 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001137 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001138 * This function calculates the hash of the message formed by concatenating
1139 * the inputs passed to preceding calls to psa_hash_update().
1140 *
1141 * When this function returns, the operation becomes inactive.
1142 *
1143 * \warning Applications should not call this function if they expect
1144 * a specific value for the hash. Call psa_hash_verify() instead.
1145 * Beware that comparing integrity or authenticity data such as
1146 * hash values with a function such as \c memcmp is risky
1147 * because the time taken by the comparison may leak information
1148 * about the hashed data which could allow an attacker to guess
1149 * a valid hash and thereby bypass security controls.
1150 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001151 * \param[in,out] operation Active hash operation.
1152 * \param[out] hash Buffer where the hash is to be written.
1153 * \param hash_size Size of the \p hash buffer in bytes.
1154 * \param[out] hash_length On success, the number of bytes
1155 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001156 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001157 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001158 *
Gilles Peskine28538492018-07-11 17:34:00 +02001159 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001160 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001161 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001162 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001163 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001164 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001165 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001166 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001167 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1168 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1169 * \retval #PSA_ERROR_HARDWARE_FAILURE
1170 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001171 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001172psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1173 uint8_t *hash,
1174 size_t hash_size,
1175 size_t *hash_length);
1176
Gilles Peskine308b91d2018-02-08 09:47:44 +01001177/** Finish the calculation of the hash of a message and compare it with
1178 * an expected value.
1179 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001180 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001181 * This function calculates the hash of the message formed by concatenating
1182 * the inputs passed to preceding calls to psa_hash_update(). It then
1183 * compares the calculated hash with the expected hash passed as a
1184 * parameter to this function.
1185 *
1186 * When this function returns, the operation becomes inactive.
1187 *
Gilles Peskine19067982018-03-20 17:54:53 +01001188 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001189 * comparison between the actual hash and the expected hash is performed
1190 * in constant time.
1191 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001192 * \param[in,out] operation Active hash operation.
1193 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001194 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001195 *
Gilles Peskine28538492018-07-11 17:34:00 +02001196 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001197 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001198 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001199 * The hash of the message was calculated successfully, but it
1200 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001201 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001202 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001203 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1204 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1205 * \retval #PSA_ERROR_HARDWARE_FAILURE
1206 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001207 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001208psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1209 const uint8_t *hash,
1210 size_t hash_length);
1211
Gilles Peskine308b91d2018-02-08 09:47:44 +01001212/** Abort a hash operation.
1213 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001214 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001215 * \p operation structure itself. Once aborted, the operation object
1216 * can be reused for another operation by calling
1217 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001218 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001219 * You may call this function any time after the operation object has
1220 * been initialized by any of the following methods:
1221 * - A call to psa_hash_setup(), whether it succeeds or not.
1222 * - Initializing the \c struct to all-bits-zero.
1223 * - Initializing the \c struct to logical zeros, e.g.
1224 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001225 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001226 * In particular, calling psa_hash_abort() after the operation has been
1227 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1228 * psa_hash_verify() is safe and has no effect.
1229 *
1230 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001231 *
Gilles Peskine28538492018-07-11 17:34:00 +02001232 * \retval #PSA_SUCCESS
1233 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001234 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001235 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1236 * \retval #PSA_ERROR_HARDWARE_FAILURE
1237 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001238 */
1239psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001240
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001241/** Clone a hash operation.
1242 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001243 * This function copies the state of an ongoing hash operation to
1244 * a new operation object. In other words, this function is equivalent
1245 * to calling psa_hash_setup() on \p target_operation with the same
1246 * algorithm that \p source_operation was set up for, then
1247 * psa_hash_update() on \p target_operation with the same input that
1248 * that was passed to \p source_operation. After this function returns, the
1249 * two objects are independent, i.e. subsequent calls involving one of
1250 * the objects do not affect the other object.
1251 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001252 * \param[in] source_operation The active hash operation to clone.
1253 * \param[in,out] target_operation The operation object to set up.
1254 * It must be initialized but not active.
1255 *
1256 * \retval #PSA_SUCCESS
1257 * \retval #PSA_ERROR_BAD_STATE
1258 * \p source_operation is not an active hash operation.
1259 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001260 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001261 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1262 * \retval #PSA_ERROR_HARDWARE_FAILURE
1263 * \retval #PSA_ERROR_TAMPERING_DETECTED
1264 */
1265psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1266 psa_hash_operation_t *target_operation);
1267
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001268/**@}*/
1269
Gilles Peskine8c9def32018-02-08 10:02:12 +01001270/** \defgroup MAC Message authentication codes
1271 * @{
1272 */
1273
Gilles Peskine69647a42019-01-14 20:18:12 +01001274/** Calculate the MAC (message authentication code) of a message.
1275 *
1276 * \note To verify the MAC of a message against an
1277 * expected value, use psa_mac_verify() instead.
1278 * Beware that comparing integrity or authenticity data such as
1279 * MAC values with a function such as \c memcmp is risky
1280 * because the time taken by the comparison may leak information
1281 * about the MAC value which could allow an attacker to guess
1282 * a valid MAC and thereby bypass security controls.
1283 *
1284 * \param handle Handle to the key to use for the operation.
1285 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001286 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001287 * \param[in] input Buffer containing the input message.
1288 * \param input_length Size of the \p input buffer in bytes.
1289 * \param[out] mac Buffer where the MAC value is to be written.
1290 * \param mac_size Size of the \p mac buffer in bytes.
1291 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001292 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001293 *
1294 * \retval #PSA_SUCCESS
1295 * Success.
1296 * \retval #PSA_ERROR_INVALID_HANDLE
1297 * \retval #PSA_ERROR_EMPTY_SLOT
1298 * \retval #PSA_ERROR_NOT_PERMITTED
1299 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001300 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001301 * \retval #PSA_ERROR_NOT_SUPPORTED
1302 * \p alg is not supported or is not a MAC algorithm.
1303 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1304 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1305 * \retval #PSA_ERROR_HARDWARE_FAILURE
1306 * \retval #PSA_ERROR_TAMPERING_DETECTED
1307 * \retval #PSA_ERROR_BAD_STATE
1308 * The library has not been previously initialized by psa_crypto_init().
1309 * It is implementation-dependent whether a failure to initialize
1310 * results in this error code.
1311 */
1312psa_status_t psa_mac_compute(psa_key_handle_t handle,
1313 psa_algorithm_t alg,
1314 const uint8_t *input,
1315 size_t input_length,
1316 uint8_t *mac,
1317 size_t mac_size,
1318 size_t *mac_length);
1319
1320/** Calculate the MAC of a message and compare it with a reference value.
1321 *
1322 * \param handle Handle to the key to use for the operation.
1323 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001324 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001325 * \param[in] input Buffer containing the input message.
1326 * \param input_length Size of the \p input buffer in bytes.
1327 * \param[out] mac Buffer containing the expected MAC value.
1328 * \param mac_length Size of the \p mac buffer in bytes.
1329 *
1330 * \retval #PSA_SUCCESS
1331 * The expected MAC is identical to the actual MAC of the input.
1332 * \retval #PSA_ERROR_INVALID_SIGNATURE
1333 * The MAC of the message was calculated successfully, but it
1334 * differs from the expected value.
1335 * \retval #PSA_ERROR_INVALID_HANDLE
1336 * \retval #PSA_ERROR_EMPTY_SLOT
1337 * \retval #PSA_ERROR_NOT_PERMITTED
1338 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001339 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001340 * \retval #PSA_ERROR_NOT_SUPPORTED
1341 * \p alg is not supported or is not a MAC algorithm.
1342 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1343 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1344 * \retval #PSA_ERROR_HARDWARE_FAILURE
1345 * \retval #PSA_ERROR_TAMPERING_DETECTED
1346 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001347psa_status_t psa_mac_verify(psa_key_handle_t handle,
1348 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001349 const uint8_t *input,
1350 size_t input_length,
1351 const uint8_t *mac,
1352 const size_t mac_length);
1353
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001354/** The type of the state data structure for multipart MAC operations.
1355 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001356 * Before calling any function on a MAC operation object, the application must
1357 * initialize it by any of the following means:
1358 * - Set the structure to all-bits-zero, for example:
1359 * \code
1360 * psa_mac_operation_t operation;
1361 * memset(&operation, 0, sizeof(operation));
1362 * \endcode
1363 * - Initialize the structure to logical zero values, for example:
1364 * \code
1365 * psa_mac_operation_t operation = {0};
1366 * \endcode
1367 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1368 * for example:
1369 * \code
1370 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1371 * \endcode
1372 * - Assign the result of the function psa_mac_operation_init()
1373 * to the structure, for example:
1374 * \code
1375 * psa_mac_operation_t operation;
1376 * operation = psa_mac_operation_init();
1377 * \endcode
1378 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001379 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001380 * make any assumptions about the content of this structure except
1381 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001382typedef struct psa_mac_operation_s psa_mac_operation_t;
1383
Jaeden Amero769ce272019-01-04 11:48:03 +00001384/** \def PSA_MAC_OPERATION_INIT
1385 *
1386 * This macro returns a suitable initializer for a MAC operation object of type
1387 * #psa_mac_operation_t.
1388 */
1389#ifdef __DOXYGEN_ONLY__
1390/* This is an example definition for documentation purposes.
1391 * Implementations should define a suitable value in `crypto_struct.h`.
1392 */
1393#define PSA_MAC_OPERATION_INIT {0}
1394#endif
1395
1396/** Return an initial value for a MAC operation object.
1397 */
1398static psa_mac_operation_t psa_mac_operation_init(void);
1399
Gilles Peskinef45adda2019-01-14 18:29:18 +01001400/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001401 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001402 * This function sets up the calculation of the MAC
1403 * (message authentication code) of a byte string.
1404 * To verify the MAC of a message against an
1405 * expected value, use psa_mac_verify_setup() instead.
1406 *
1407 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001408 * -# Allocate an operation object which will be passed to all the functions
1409 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001410 * -# Initialize the operation object with one of the methods described in the
1411 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001412 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001413 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1414 * of the message each time. The MAC that is calculated is the MAC
1415 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001416 * -# At the end of the message, call psa_mac_sign_finish() to finish
1417 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001418 *
1419 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001420 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001421 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001422 * After a successful call to psa_mac_sign_setup(), the application must
1423 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001424 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001425 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001426 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001427 * \param[in,out] operation The operation object to set up. It must have
1428 * been initialized as per the documentation for
1429 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001430 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001431 * It must remain valid until the operation
1432 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001433 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001434 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001435 *
Gilles Peskine28538492018-07-11 17:34:00 +02001436 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001437 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001438 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001439 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001440 * \retval #PSA_ERROR_NOT_PERMITTED
1441 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001442 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001443 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001444 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001445 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1446 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1447 * \retval #PSA_ERROR_HARDWARE_FAILURE
1448 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001449 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001450 * The operation state is not valid (already set up and not
1451 * subsequently completed).
1452 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001453 * The library has not been previously initialized by psa_crypto_init().
1454 * It is implementation-dependent whether a failure to initialize
1455 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001456 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001457psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001458 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001459 psa_algorithm_t alg);
1460
Gilles Peskinef45adda2019-01-14 18:29:18 +01001461/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001462 *
1463 * This function sets up the verification of the MAC
1464 * (message authentication code) of a byte string against an expected value.
1465 *
1466 * The sequence of operations to verify a MAC is as follows:
1467 * -# Allocate an operation object which will be passed to all the functions
1468 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001469 * -# Initialize the operation object with one of the methods described in the
1470 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001471 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001472 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1473 * of the message each time. The MAC that is calculated is the MAC
1474 * of the concatenation of these messages in order.
1475 * -# At the end of the message, call psa_mac_verify_finish() to finish
1476 * calculating the actual MAC of the message and verify it against
1477 * the expected value.
1478 *
1479 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001480 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001481 *
1482 * After a successful call to psa_mac_verify_setup(), the application must
1483 * eventually terminate the operation through one of the following methods:
1484 * - A failed call to psa_mac_update().
1485 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1486 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001487 * \param[in,out] operation The operation object to set up. It must have
1488 * been initialized as per the documentation for
1489 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001490 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001491 * It must remain valid until the operation
1492 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001493 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1494 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001495 *
Gilles Peskine28538492018-07-11 17:34:00 +02001496 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001497 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001498 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001499 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001500 * \retval #PSA_ERROR_NOT_PERMITTED
1501 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001502 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001503 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001504 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001505 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1506 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1507 * \retval #PSA_ERROR_HARDWARE_FAILURE
1508 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001509 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001510 * The operation state is not valid (already set up and not
1511 * subsequently completed).
1512 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001513 * The library has not been previously initialized by psa_crypto_init().
1514 * It is implementation-dependent whether a failure to initialize
1515 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001516 */
1517psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001518 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001519 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001520
Gilles Peskinedcd14942018-07-12 00:30:52 +02001521/** Add a message fragment to a multipart MAC operation.
1522 *
1523 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1524 * before calling this function.
1525 *
1526 * If this function returns an error status, the operation becomes inactive.
1527 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001528 * \param[in,out] operation Active MAC operation.
1529 * \param[in] input Buffer containing the message fragment to add to
1530 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001531 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001532 *
1533 * \retval #PSA_SUCCESS
1534 * Success.
1535 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001536 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001537 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1538 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1539 * \retval #PSA_ERROR_HARDWARE_FAILURE
1540 * \retval #PSA_ERROR_TAMPERING_DETECTED
1541 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001542psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1543 const uint8_t *input,
1544 size_t input_length);
1545
Gilles Peskinedcd14942018-07-12 00:30:52 +02001546/** Finish the calculation of the MAC of a message.
1547 *
1548 * The application must call psa_mac_sign_setup() before calling this function.
1549 * This function calculates the MAC of the message formed by concatenating
1550 * the inputs passed to preceding calls to psa_mac_update().
1551 *
1552 * When this function returns, the operation becomes inactive.
1553 *
1554 * \warning Applications should not call this function if they expect
1555 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1556 * Beware that comparing integrity or authenticity data such as
1557 * MAC values with a function such as \c memcmp is risky
1558 * because the time taken by the comparison may leak information
1559 * about the MAC value which could allow an attacker to guess
1560 * a valid MAC and thereby bypass security controls.
1561 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001562 * \param[in,out] operation Active MAC operation.
1563 * \param[out] mac Buffer where the MAC value is to be written.
1564 * \param mac_size Size of the \p mac buffer in bytes.
1565 * \param[out] mac_length On success, the number of bytes
1566 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001567 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001568 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001569 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001570 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001571 *
1572 * \retval #PSA_SUCCESS
1573 * Success.
1574 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001575 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001576 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001577 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001578 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1579 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1580 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1581 * \retval #PSA_ERROR_HARDWARE_FAILURE
1582 * \retval #PSA_ERROR_TAMPERING_DETECTED
1583 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001584psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1585 uint8_t *mac,
1586 size_t mac_size,
1587 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001588
Gilles Peskinedcd14942018-07-12 00:30:52 +02001589/** Finish the calculation of the MAC of a message and compare it with
1590 * an expected value.
1591 *
1592 * The application must call psa_mac_verify_setup() before calling this function.
1593 * This function calculates the MAC of the message formed by concatenating
1594 * the inputs passed to preceding calls to psa_mac_update(). It then
1595 * compares the calculated MAC with the expected MAC passed as a
1596 * parameter to this function.
1597 *
1598 * When this function returns, the operation becomes inactive.
1599 *
1600 * \note Implementations shall make the best effort to ensure that the
1601 * comparison between the actual MAC and the expected MAC is performed
1602 * in constant time.
1603 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001604 * \param[in,out] operation Active MAC operation.
1605 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001606 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001607 *
1608 * \retval #PSA_SUCCESS
1609 * The expected MAC is identical to the actual MAC of the message.
1610 * \retval #PSA_ERROR_INVALID_SIGNATURE
1611 * The MAC of the message was calculated successfully, but it
1612 * differs from the expected MAC.
1613 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001614 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001615 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1616 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1617 * \retval #PSA_ERROR_HARDWARE_FAILURE
1618 * \retval #PSA_ERROR_TAMPERING_DETECTED
1619 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001620psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1621 const uint8_t *mac,
1622 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001623
Gilles Peskinedcd14942018-07-12 00:30:52 +02001624/** Abort a MAC operation.
1625 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001626 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001627 * \p operation structure itself. Once aborted, the operation object
1628 * can be reused for another operation by calling
1629 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001630 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001631 * You may call this function any time after the operation object has
1632 * been initialized by any of the following methods:
1633 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1634 * it succeeds or not.
1635 * - Initializing the \c struct to all-bits-zero.
1636 * - Initializing the \c struct to logical zeros, e.g.
1637 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001638 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001639 * In particular, calling psa_mac_abort() after the operation has been
1640 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1641 * psa_mac_verify_finish() is safe and has no effect.
1642 *
1643 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001644 *
1645 * \retval #PSA_SUCCESS
1646 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001647 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001648 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1649 * \retval #PSA_ERROR_HARDWARE_FAILURE
1650 * \retval #PSA_ERROR_TAMPERING_DETECTED
1651 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001652psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1653
1654/**@}*/
1655
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001656/** \defgroup cipher Symmetric ciphers
1657 * @{
1658 */
1659
Gilles Peskine69647a42019-01-14 20:18:12 +01001660/** Encrypt a message using a symmetric cipher.
1661 *
1662 * This function encrypts a message with a random IV (initialization
1663 * vector).
1664 *
1665 * \param handle Handle to the key to use for the operation.
1666 * It must remain valid until the operation
1667 * terminates.
1668 * \param alg The cipher algorithm to compute
1669 * (\c PSA_ALG_XXX value such that
1670 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1671 * \param[in] input Buffer containing the message to encrypt.
1672 * \param input_length Size of the \p input buffer in bytes.
1673 * \param[out] output Buffer where the output is to be written.
1674 * The output contains the IV followed by
1675 * the ciphertext proper.
1676 * \param output_size Size of the \p output buffer in bytes.
1677 * \param[out] output_length On success, the number of bytes
1678 * that make up the output.
1679 *
1680 * \retval #PSA_SUCCESS
1681 * Success.
1682 * \retval #PSA_ERROR_INVALID_HANDLE
1683 * \retval #PSA_ERROR_EMPTY_SLOT
1684 * \retval #PSA_ERROR_NOT_PERMITTED
1685 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001686 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001687 * \retval #PSA_ERROR_NOT_SUPPORTED
1688 * \p alg is not supported or is not a cipher algorithm.
1689 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1690 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1691 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1692 * \retval #PSA_ERROR_HARDWARE_FAILURE
1693 * \retval #PSA_ERROR_TAMPERING_DETECTED
1694 */
1695psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1696 psa_algorithm_t alg,
1697 const uint8_t *input,
1698 size_t input_length,
1699 uint8_t *output,
1700 size_t output_size,
1701 size_t *output_length);
1702
1703/** Decrypt a message using a symmetric cipher.
1704 *
1705 * This function decrypts a message encrypted with a symmetric cipher.
1706 *
1707 * \param handle Handle to the key to use for the operation.
1708 * It must remain valid until the operation
1709 * terminates.
1710 * \param alg The cipher algorithm to compute
1711 * (\c PSA_ALG_XXX value such that
1712 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1713 * \param[in] input Buffer containing the message to decrypt.
1714 * This consists of the IV followed by the
1715 * ciphertext proper.
1716 * \param input_length Size of the \p input buffer in bytes.
1717 * \param[out] output Buffer where the plaintext is to be written.
1718 * \param output_size Size of the \p output buffer in bytes.
1719 * \param[out] output_length On success, the number of bytes
1720 * that make up the output.
1721 *
1722 * \retval #PSA_SUCCESS
1723 * Success.
1724 * \retval #PSA_ERROR_INVALID_HANDLE
1725 * \retval #PSA_ERROR_EMPTY_SLOT
1726 * \retval #PSA_ERROR_NOT_PERMITTED
1727 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001728 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001729 * \retval #PSA_ERROR_NOT_SUPPORTED
1730 * \p alg is not supported or is not a cipher algorithm.
1731 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1732 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1733 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1734 * \retval #PSA_ERROR_HARDWARE_FAILURE
1735 * \retval #PSA_ERROR_TAMPERING_DETECTED
1736 */
1737psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1738 psa_algorithm_t alg,
1739 const uint8_t *input,
1740 size_t input_length,
1741 uint8_t *output,
1742 size_t output_size,
1743 size_t *output_length);
1744
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001745/** The type of the state data structure for multipart cipher operations.
1746 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001747 * Before calling any function on a cipher operation object, the application
1748 * must initialize it by any of the following means:
1749 * - Set the structure to all-bits-zero, for example:
1750 * \code
1751 * psa_cipher_operation_t operation;
1752 * memset(&operation, 0, sizeof(operation));
1753 * \endcode
1754 * - Initialize the structure to logical zero values, for example:
1755 * \code
1756 * psa_cipher_operation_t operation = {0};
1757 * \endcode
1758 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1759 * for example:
1760 * \code
1761 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1762 * \endcode
1763 * - Assign the result of the function psa_cipher_operation_init()
1764 * to the structure, for example:
1765 * \code
1766 * psa_cipher_operation_t operation;
1767 * operation = psa_cipher_operation_init();
1768 * \endcode
1769 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001770 * This is an implementation-defined \c struct. Applications should not
1771 * make any assumptions about the content of this structure except
1772 * as directed by the documentation of a specific implementation. */
1773typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1774
Jaeden Amero5bae2272019-01-04 11:48:27 +00001775/** \def PSA_CIPHER_OPERATION_INIT
1776 *
1777 * This macro returns a suitable initializer for a cipher operation object of
1778 * type #psa_cipher_operation_t.
1779 */
1780#ifdef __DOXYGEN_ONLY__
1781/* This is an example definition for documentation purposes.
1782 * Implementations should define a suitable value in `crypto_struct.h`.
1783 */
1784#define PSA_CIPHER_OPERATION_INIT {0}
1785#endif
1786
1787/** Return an initial value for a cipher operation object.
1788 */
1789static psa_cipher_operation_t psa_cipher_operation_init(void);
1790
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001791/** Set the key for a multipart symmetric encryption operation.
1792 *
1793 * The sequence of operations to encrypt a message with a symmetric cipher
1794 * is as follows:
1795 * -# Allocate an operation object which will be passed to all the functions
1796 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001797 * -# Initialize the operation object with one of the methods described in the
1798 * documentation for #psa_cipher_operation_t, e.g.
1799 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001800 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001801 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001802 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001803 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001804 * requires a specific IV value.
1805 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1806 * of the message each time.
1807 * -# Call psa_cipher_finish().
1808 *
1809 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001810 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001811 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001812 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001813 * eventually terminate the operation. The following events terminate an
1814 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001815 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001816 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001817 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001818 * \param[in,out] operation The operation object to set up. It must have
1819 * been initialized as per the documentation for
1820 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001821 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001822 * It must remain valid until the operation
1823 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001824 * \param alg The cipher algorithm to compute
1825 * (\c PSA_ALG_XXX value such that
1826 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001827 *
Gilles Peskine28538492018-07-11 17:34:00 +02001828 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001829 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001830 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001831 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001832 * \retval #PSA_ERROR_NOT_PERMITTED
1833 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001834 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001835 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001836 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001837 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1838 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1839 * \retval #PSA_ERROR_HARDWARE_FAILURE
1840 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001841 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001842 * The operation state is not valid (already set up and not
1843 * subsequently completed).
1844 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001845 * The library has not been previously initialized by psa_crypto_init().
1846 * It is implementation-dependent whether a failure to initialize
1847 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001848 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001849psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001850 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001851 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001852
1853/** Set the key for a multipart symmetric decryption operation.
1854 *
1855 * The sequence of operations to decrypt a message with a symmetric cipher
1856 * is as follows:
1857 * -# Allocate an operation object which will be passed to all the functions
1858 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001859 * -# Initialize the operation object with one of the methods described in the
1860 * documentation for #psa_cipher_operation_t, e.g.
1861 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001862 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001863 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001864 * decryption. If the IV is prepended to the ciphertext, you can call
1865 * psa_cipher_update() on a buffer containing the IV followed by the
1866 * beginning of the message.
1867 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1868 * of the message each time.
1869 * -# Call psa_cipher_finish().
1870 *
1871 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001872 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001873 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001874 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001875 * eventually terminate the operation. The following events terminate an
1876 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001877 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001878 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001879 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001880 * \param[in,out] operation The operation object to set up. It must have
1881 * been initialized as per the documentation for
1882 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001883 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001884 * It must remain valid until the operation
1885 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001886 * \param alg The cipher algorithm to compute
1887 * (\c PSA_ALG_XXX value such that
1888 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001889 *
Gilles Peskine28538492018-07-11 17:34:00 +02001890 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001891 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001892 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001893 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001894 * \retval #PSA_ERROR_NOT_PERMITTED
1895 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001896 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001897 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001898 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001899 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1900 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1901 * \retval #PSA_ERROR_HARDWARE_FAILURE
1902 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001903 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001904 * The operation state is not valid (already set up and not
1905 * subsequently completed).
1906 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001907 * The library has not been previously initialized by psa_crypto_init().
1908 * It is implementation-dependent whether a failure to initialize
1909 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001910 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001911psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001912 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001913 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001914
Gilles Peskinedcd14942018-07-12 00:30:52 +02001915/** Generate an IV for a symmetric encryption operation.
1916 *
1917 * This function generates a random IV (initialization vector), nonce
1918 * or initial counter value for the encryption operation as appropriate
1919 * for the chosen algorithm, key type and key size.
1920 *
1921 * The application must call psa_cipher_encrypt_setup() before
1922 * calling this function.
1923 *
1924 * If this function returns an error status, the operation becomes inactive.
1925 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001926 * \param[in,out] operation Active cipher operation.
1927 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001928 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001929 * \param[out] iv_length On success, the number of bytes of the
1930 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001931 *
1932 * \retval #PSA_SUCCESS
1933 * Success.
1934 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001935 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001936 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001937 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001938 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1939 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1940 * \retval #PSA_ERROR_HARDWARE_FAILURE
1941 * \retval #PSA_ERROR_TAMPERING_DETECTED
1942 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001943psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1944 unsigned char *iv,
1945 size_t iv_size,
1946 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001947
Gilles Peskinedcd14942018-07-12 00:30:52 +02001948/** Set the IV for a symmetric encryption or decryption operation.
1949 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001950 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001951 * or initial counter value for the encryption or decryption operation.
1952 *
1953 * The application must call psa_cipher_encrypt_setup() before
1954 * calling this function.
1955 *
1956 * If this function returns an error status, the operation becomes inactive.
1957 *
1958 * \note When encrypting, applications should use psa_cipher_generate_iv()
1959 * instead of this function, unless implementing a protocol that requires
1960 * a non-random IV.
1961 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001962 * \param[in,out] operation Active cipher operation.
1963 * \param[in] iv Buffer containing the IV to use.
1964 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001965 *
1966 * \retval #PSA_SUCCESS
1967 * Success.
1968 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001969 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001970 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001971 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001972 * or the chosen algorithm does not use an IV.
1973 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1974 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1975 * \retval #PSA_ERROR_HARDWARE_FAILURE
1976 * \retval #PSA_ERROR_TAMPERING_DETECTED
1977 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001978psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1979 const unsigned char *iv,
1980 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001981
Gilles Peskinedcd14942018-07-12 00:30:52 +02001982/** Encrypt or decrypt a message fragment in an active cipher operation.
1983 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001984 * Before calling this function, you must:
1985 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1986 * The choice of setup function determines whether this function
1987 * encrypts or decrypts its input.
1988 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1989 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001990 *
1991 * If this function returns an error status, the operation becomes inactive.
1992 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001993 * \param[in,out] operation Active cipher operation.
1994 * \param[in] input Buffer containing the message fragment to
1995 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001996 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001997 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001998 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001999 * \param[out] output_length On success, the number of bytes
2000 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002001 *
2002 * \retval #PSA_SUCCESS
2003 * Success.
2004 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002005 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002006 * not set, or already completed).
2007 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2008 * The size of the \p output buffer is too small.
2009 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2010 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2011 * \retval #PSA_ERROR_HARDWARE_FAILURE
2012 * \retval #PSA_ERROR_TAMPERING_DETECTED
2013 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002014psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2015 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002016 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002017 unsigned char *output,
2018 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002019 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002020
Gilles Peskinedcd14942018-07-12 00:30:52 +02002021/** Finish encrypting or decrypting a message in a cipher operation.
2022 *
2023 * The application must call psa_cipher_encrypt_setup() or
2024 * psa_cipher_decrypt_setup() before calling this function. The choice
2025 * of setup function determines whether this function encrypts or
2026 * decrypts its input.
2027 *
2028 * This function finishes the encryption or decryption of the message
2029 * formed by concatenating the inputs passed to preceding calls to
2030 * psa_cipher_update().
2031 *
2032 * When this function returns, the operation becomes inactive.
2033 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002034 * \param[in,out] operation Active cipher operation.
2035 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002036 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002037 * \param[out] output_length On success, the number of bytes
2038 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002039 *
2040 * \retval #PSA_SUCCESS
2041 * Success.
2042 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002043 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002044 * not set, or already completed).
2045 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2046 * The size of the \p output buffer is too small.
2047 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2048 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2049 * \retval #PSA_ERROR_HARDWARE_FAILURE
2050 * \retval #PSA_ERROR_TAMPERING_DETECTED
2051 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002052psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002053 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002054 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002055 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002056
Gilles Peskinedcd14942018-07-12 00:30:52 +02002057/** Abort a cipher operation.
2058 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002059 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002060 * \p operation structure itself. Once aborted, the operation object
2061 * can be reused for another operation by calling
2062 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002063 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002064 * You may call this function any time after the operation object has
2065 * been initialized by any of the following methods:
2066 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2067 * whether it succeeds or not.
2068 * - Initializing the \c struct to all-bits-zero.
2069 * - Initializing the \c struct to logical zeros, e.g.
2070 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002071 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002072 * In particular, calling psa_cipher_abort() after the operation has been
2073 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2074 * is safe and has no effect.
2075 *
2076 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002077 *
2078 * \retval #PSA_SUCCESS
2079 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002080 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002081 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2082 * \retval #PSA_ERROR_HARDWARE_FAILURE
2083 * \retval #PSA_ERROR_TAMPERING_DETECTED
2084 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002085psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2086
2087/**@}*/
2088
Gilles Peskine3b555712018-03-03 21:27:57 +01002089/** \defgroup aead Authenticated encryption with associated data (AEAD)
2090 * @{
2091 */
2092
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002093/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002094 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002095 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002096 * \param alg The AEAD algorithm to compute
2097 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002098 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002099 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002100 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002101 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002102 * but not encrypted.
2103 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002104 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002105 * encrypted.
2106 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002107 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002108 * encrypted data. The additional data is not
2109 * part of this output. For algorithms where the
2110 * encrypted data and the authentication tag
2111 * are defined as separate outputs, the
2112 * authentication tag is appended to the
2113 * encrypted data.
2114 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2115 * This must be at least
2116 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2117 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002118 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002119 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002120 *
Gilles Peskine28538492018-07-11 17:34:00 +02002121 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002122 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002123 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002124 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002125 * \retval #PSA_ERROR_NOT_PERMITTED
2126 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002127 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002128 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002129 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002130 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2131 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2132 * \retval #PSA_ERROR_HARDWARE_FAILURE
2133 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002134 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002135 * The library has not been previously initialized by psa_crypto_init().
2136 * It is implementation-dependent whether a failure to initialize
2137 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002138 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002139psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002140 psa_algorithm_t alg,
2141 const uint8_t *nonce,
2142 size_t nonce_length,
2143 const uint8_t *additional_data,
2144 size_t additional_data_length,
2145 const uint8_t *plaintext,
2146 size_t plaintext_length,
2147 uint8_t *ciphertext,
2148 size_t ciphertext_size,
2149 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002150
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002151/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002152 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002153 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002154 * \param alg The AEAD algorithm to compute
2155 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002156 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002157 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002158 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002159 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002160 * but not encrypted.
2161 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002162 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002163 * encrypted. For algorithms where the
2164 * encrypted data and the authentication tag
2165 * are defined as separate inputs, the buffer
2166 * must contain the encrypted data followed
2167 * by the authentication tag.
2168 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002169 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002170 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2171 * This must be at least
2172 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2173 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002174 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002175 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002176 *
Gilles Peskine28538492018-07-11 17:34:00 +02002177 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002178 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002179 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002180 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002181 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002182 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002183 * \retval #PSA_ERROR_NOT_PERMITTED
2184 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002185 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002186 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002187 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002188 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2189 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2190 * \retval #PSA_ERROR_HARDWARE_FAILURE
2191 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002192 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002193 * The library has not been previously initialized by psa_crypto_init().
2194 * It is implementation-dependent whether a failure to initialize
2195 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002196 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002197psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002198 psa_algorithm_t alg,
2199 const uint8_t *nonce,
2200 size_t nonce_length,
2201 const uint8_t *additional_data,
2202 size_t additional_data_length,
2203 const uint8_t *ciphertext,
2204 size_t ciphertext_length,
2205 uint8_t *plaintext,
2206 size_t plaintext_size,
2207 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002208
Gilles Peskine30a9e412019-01-14 18:36:12 +01002209/** The type of the state data structure for multipart AEAD operations.
2210 *
2211 * Before calling any function on an AEAD operation object, the application
2212 * must initialize it by any of the following means:
2213 * - Set the structure to all-bits-zero, for example:
2214 * \code
2215 * psa_aead_operation_t operation;
2216 * memset(&operation, 0, sizeof(operation));
2217 * \endcode
2218 * - Initialize the structure to logical zero values, for example:
2219 * \code
2220 * psa_aead_operation_t operation = {0};
2221 * \endcode
2222 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2223 * for example:
2224 * \code
2225 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2226 * \endcode
2227 * - Assign the result of the function psa_aead_operation_init()
2228 * to the structure, for example:
2229 * \code
2230 * psa_aead_operation_t operation;
2231 * operation = psa_aead_operation_init();
2232 * \endcode
2233 *
2234 * This is an implementation-defined \c struct. Applications should not
2235 * make any assumptions about the content of this structure except
2236 * as directed by the documentation of a specific implementation. */
2237typedef struct psa_aead_operation_s psa_aead_operation_t;
2238
2239/** \def PSA_AEAD_OPERATION_INIT
2240 *
2241 * This macro returns a suitable initializer for an AEAD operation object of
2242 * type #psa_aead_operation_t.
2243 */
2244#ifdef __DOXYGEN_ONLY__
2245/* This is an example definition for documentation purposes.
2246 * Implementations should define a suitable value in `crypto_struct.h`.
2247 */
2248#define PSA_AEAD_OPERATION_INIT {0}
2249#endif
2250
2251/** Return an initial value for an AEAD operation object.
2252 */
2253static psa_aead_operation_t psa_aead_operation_init(void);
2254
2255/** Set the key for a multipart authenticated encryption operation.
2256 *
2257 * The sequence of operations to encrypt a message with authentication
2258 * is as follows:
2259 * -# Allocate an operation object which will be passed to all the functions
2260 * listed here.
2261 * -# Initialize the operation object with one of the methods described in the
2262 * documentation for #psa_aead_operation_t, e.g.
2263 * PSA_AEAD_OPERATION_INIT.
2264 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002265 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2266 * inputs to the subsequent calls to psa_aead_update_ad() and
2267 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2268 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002269 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2270 * generate or set the nonce. You should use
2271 * psa_aead_generate_nonce() unless the protocol you are implementing
2272 * requires a specific nonce value.
2273 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2274 * of the non-encrypted additional authenticated data each time.
2275 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002276 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002277 * -# Call psa_aead_finish().
2278 *
2279 * The application may call psa_aead_abort() at any time after the operation
2280 * has been initialized.
2281 *
2282 * After a successful call to psa_aead_encrypt_setup(), the application must
2283 * eventually terminate the operation. The following events terminate an
2284 * operation:
2285 * - A failed call to any of the \c psa_aead_xxx functions.
2286 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2287 *
2288 * \param[in,out] operation The operation object to set up. It must have
2289 * been initialized as per the documentation for
2290 * #psa_aead_operation_t and not yet in use.
2291 * \param handle Handle to the key to use for the operation.
2292 * It must remain valid until the operation
2293 * terminates.
2294 * \param alg The AEAD algorithm to compute
2295 * (\c PSA_ALG_XXX value such that
2296 * #PSA_ALG_IS_AEAD(\p alg) is true).
2297 *
2298 * \retval #PSA_SUCCESS
2299 * Success.
2300 * \retval #PSA_ERROR_INVALID_HANDLE
2301 * \retval #PSA_ERROR_EMPTY_SLOT
2302 * \retval #PSA_ERROR_NOT_PERMITTED
2303 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002304 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002305 * \retval #PSA_ERROR_NOT_SUPPORTED
2306 * \p alg is not supported or is not an AEAD algorithm.
2307 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2308 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2309 * \retval #PSA_ERROR_HARDWARE_FAILURE
2310 * \retval #PSA_ERROR_TAMPERING_DETECTED
2311 * \retval #PSA_ERROR_BAD_STATE
2312 * The library has not been previously initialized by psa_crypto_init().
2313 * It is implementation-dependent whether a failure to initialize
2314 * results in this error code.
2315 */
2316psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2317 psa_key_handle_t handle,
2318 psa_algorithm_t alg);
2319
2320/** Set the key for a multipart authenticated decryption operation.
2321 *
2322 * The sequence of operations to decrypt a message with authentication
2323 * is as follows:
2324 * -# Allocate an operation object which will be passed to all the functions
2325 * listed here.
2326 * -# Initialize the operation object with one of the methods described in the
2327 * documentation for #psa_aead_operation_t, e.g.
2328 * PSA_AEAD_OPERATION_INIT.
2329 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002330 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2331 * inputs to the subsequent calls to psa_aead_update_ad() and
2332 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2333 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002334 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2335 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2336 * of the non-encrypted additional authenticated data each time.
2337 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002338 * of the ciphertext to decrypt each time.
2339 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002340 *
2341 * The application may call psa_aead_abort() at any time after the operation
2342 * has been initialized.
2343 *
2344 * After a successful call to psa_aead_decrypt_setup(), the application must
2345 * eventually terminate the operation. The following events terminate an
2346 * operation:
2347 * - A failed call to any of the \c psa_aead_xxx functions.
2348 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2349 *
2350 * \param[in,out] operation The operation object to set up. It must have
2351 * been initialized as per the documentation for
2352 * #psa_aead_operation_t and not yet in use.
2353 * \param handle Handle to the key to use for the operation.
2354 * It must remain valid until the operation
2355 * terminates.
2356 * \param alg The AEAD algorithm to compute
2357 * (\c PSA_ALG_XXX value such that
2358 * #PSA_ALG_IS_AEAD(\p alg) is true).
2359 *
2360 * \retval #PSA_SUCCESS
2361 * Success.
2362 * \retval #PSA_ERROR_INVALID_HANDLE
2363 * \retval #PSA_ERROR_EMPTY_SLOT
2364 * \retval #PSA_ERROR_NOT_PERMITTED
2365 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002366 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002367 * \retval #PSA_ERROR_NOT_SUPPORTED
2368 * \p alg is not supported or is not an AEAD algorithm.
2369 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2370 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2371 * \retval #PSA_ERROR_HARDWARE_FAILURE
2372 * \retval #PSA_ERROR_TAMPERING_DETECTED
2373 * \retval #PSA_ERROR_BAD_STATE
2374 * The library has not been previously initialized by psa_crypto_init().
2375 * It is implementation-dependent whether a failure to initialize
2376 * results in this error code.
2377 */
2378psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2379 psa_key_handle_t handle,
2380 psa_algorithm_t alg);
2381
2382/** Generate a random nonce for an authenticated encryption operation.
2383 *
2384 * This function generates a random nonce for the authenticated encryption
2385 * operation with an appropriate size for the chosen algorithm, key type
2386 * and key size.
2387 *
2388 * The application must call psa_aead_encrypt_setup() before
2389 * calling this function.
2390 *
2391 * If this function returns an error status, the operation becomes inactive.
2392 *
2393 * \param[in,out] operation Active AEAD operation.
2394 * \param[out] nonce Buffer where the generated nonce is to be
2395 * written.
2396 * \param nonce_size Size of the \p nonce buffer in bytes.
2397 * \param[out] nonce_length On success, the number of bytes of the
2398 * generated nonce.
2399 *
2400 * \retval #PSA_SUCCESS
2401 * Success.
2402 * \retval #PSA_ERROR_BAD_STATE
2403 * The operation state is not valid (not set up, or nonce already set).
2404 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2405 * The size of the \p nonce buffer is too small.
2406 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2407 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2408 * \retval #PSA_ERROR_HARDWARE_FAILURE
2409 * \retval #PSA_ERROR_TAMPERING_DETECTED
2410 */
2411psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2412 unsigned char *nonce,
2413 size_t nonce_size,
2414 size_t *nonce_length);
2415
2416/** Set the nonce for an authenticated encryption or decryption operation.
2417 *
2418 * This function sets the nonce for the authenticated
2419 * encryption or decryption operation.
2420 *
2421 * The application must call psa_aead_encrypt_setup() before
2422 * calling this function.
2423 *
2424 * If this function returns an error status, the operation becomes inactive.
2425 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002426 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002427 * instead of this function, unless implementing a protocol that requires
2428 * a non-random IV.
2429 *
2430 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002431 * \param[in] nonce Buffer containing the nonce to use.
2432 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002433 *
2434 * \retval #PSA_SUCCESS
2435 * Success.
2436 * \retval #PSA_ERROR_BAD_STATE
2437 * The operation state is not valid (not set up, or nonce already set).
2438 * \retval #PSA_ERROR_INVALID_ARGUMENT
2439 * The size of \p nonce is not acceptable for the chosen algorithm.
2440 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2441 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2442 * \retval #PSA_ERROR_HARDWARE_FAILURE
2443 * \retval #PSA_ERROR_TAMPERING_DETECTED
2444 */
2445psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2446 const unsigned char *nonce,
2447 size_t nonce_length);
2448
Gilles Peskinebc59c852019-01-17 15:26:08 +01002449/** Declare the lengths of the message and additional data for AEAD.
2450 *
2451 * The application must call this function before calling
2452 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2453 * the operation requires it. If the algorithm does not require it,
2454 * calling this function is optional, but if this function is called
2455 * then the implementation must enforce the lengths.
2456 *
2457 * You may call this function before or after setting the nonce with
2458 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2459 *
2460 * - For #PSA_ALG_CCM, calling this function is required.
2461 * - For the other AEAD algorithms defined in this specification, calling
2462 * this function is not required.
2463 * - For vendor-defined algorithm, refer to the vendor documentation.
2464 *
2465 * \param[in,out] operation Active AEAD operation.
2466 * \param ad_length Size of the non-encrypted additional
2467 * authenticated data in bytes.
2468 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2469 *
2470 * \retval #PSA_SUCCESS
2471 * Success.
2472 * \retval #PSA_ERROR_BAD_STATE
2473 * The operation state is not valid (not set up, already completed,
2474 * or psa_aead_update_ad() or psa_aead_update() already called).
2475 * \retval #PSA_ERROR_INVALID_ARGUMENT
2476 * At least one of the lengths is not acceptable for the chosen
2477 * algorithm.
2478 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2479 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2480 * \retval #PSA_ERROR_HARDWARE_FAILURE
2481 * \retval #PSA_ERROR_TAMPERING_DETECTED
2482 */
2483psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2484 size_t ad_length,
2485 size_t plaintext_length);
2486
Gilles Peskine30a9e412019-01-14 18:36:12 +01002487/** Pass additional data to an active AEAD operation.
2488 *
2489 * Additional data is authenticated, but not encrypted.
2490 *
2491 * You may call this function multiple times to pass successive fragments
2492 * of the additional data. You may not call this function after passing
2493 * data to encrypt or decrypt with psa_aead_update().
2494 *
2495 * Before calling this function, you must:
2496 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2497 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2498 *
2499 * If this function returns an error status, the operation becomes inactive.
2500 *
2501 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2502 * there is no guarantee that the input is valid. Therefore, until
2503 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2504 * treat the input as untrusted and prepare to undo any action that
2505 * depends on the input if psa_aead_verify() returns an error status.
2506 *
2507 * \param[in,out] operation Active AEAD operation.
2508 * \param[in] input Buffer containing the fragment of
2509 * additional data.
2510 * \param input_length Size of the \p input buffer in bytes.
2511 *
2512 * \retval #PSA_SUCCESS
2513 * Success.
2514 * \retval #PSA_ERROR_BAD_STATE
2515 * The operation state is not valid (not set up, nonce not set,
2516 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002517 * \retval #PSA_ERROR_INVALID_ARGUMENT
2518 * The total input length overflows the additional data length that
2519 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002520 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2521 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2522 * \retval #PSA_ERROR_HARDWARE_FAILURE
2523 * \retval #PSA_ERROR_TAMPERING_DETECTED
2524 */
2525psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2526 const uint8_t *input,
2527 size_t input_length);
2528
2529/** Encrypt or decrypt a message fragment in an active AEAD operation.
2530 *
2531 * Before calling this function, you must:
2532 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2533 * The choice of setup function determines whether this function
2534 * encrypts or decrypts its input.
2535 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2536 * 3. Call psa_aead_update_ad() to pass all the additional data.
2537 *
2538 * If this function returns an error status, the operation becomes inactive.
2539 *
2540 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2541 * there is no guarantee that the input is valid. Therefore, until
2542 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2543 * - Do not use the output in any way other than storing it in a
2544 * confidential location. If you take any action that depends
2545 * on the tentative decrypted data, this action will need to be
2546 * undone if the input turns out not to be valid. Furthermore,
2547 * if an adversary can observe that this action took place
2548 * (for example through timing), they may be able to use this
2549 * fact as an oracle to decrypt any message encrypted with the
2550 * same key.
2551 * - In particular, do not copy the output anywhere but to a
2552 * memory or storage space that you have exclusive access to.
2553 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002554 * This function does not require the input to be aligned to any
2555 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002556 * a whole block at a time, it must consume all the input provided, but
2557 * it may delay the end of the corresponding output until a subsequent
2558 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2559 * provides sufficient input. The amount of data that can be delayed
2560 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002561 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002562 * \param[in,out] operation Active AEAD operation.
2563 * \param[in] input Buffer containing the message fragment to
2564 * encrypt or decrypt.
2565 * \param input_length Size of the \p input buffer in bytes.
2566 * \param[out] output Buffer where the output is to be written.
2567 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002568 * This must be at least
2569 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2570 * \p input_length) where \c alg is the
2571 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002572 * \param[out] output_length On success, the number of bytes
2573 * that make up the returned output.
2574 *
2575 * \retval #PSA_SUCCESS
2576 * Success.
2577 * \retval #PSA_ERROR_BAD_STATE
2578 * The operation state is not valid (not set up, nonce not set
2579 * or already completed).
2580 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2581 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002582 * You can determine a sufficient buffer size by calling
2583 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2584 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002585 * \retval #PSA_ERROR_INVALID_ARGUMENT
2586 * The total length of input to psa_aead_update_ad() so far is
2587 * less than the additional data length that was previously
2588 * specified with psa_aead_set_lengths().
2589 * \retval #PSA_ERROR_INVALID_ARGUMENT
2590 * The total input length overflows the plaintext length that
2591 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002592 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2593 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2594 * \retval #PSA_ERROR_HARDWARE_FAILURE
2595 * \retval #PSA_ERROR_TAMPERING_DETECTED
2596 */
2597psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2598 const uint8_t *input,
2599 size_t input_length,
2600 unsigned char *output,
2601 size_t output_size,
2602 size_t *output_length);
2603
2604/** Finish encrypting a message in an AEAD operation.
2605 *
2606 * The operation must have been set up with psa_aead_encrypt_setup().
2607 *
2608 * This function finishes the authentication of the additional data
2609 * formed by concatenating the inputs passed to preceding calls to
2610 * psa_aead_update_ad() with the plaintext formed by concatenating the
2611 * inputs passed to preceding calls to psa_aead_update().
2612 *
2613 * This function has two output buffers:
2614 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002615 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002616 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002617 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002618 * that the operation performs.
2619 *
2620 * When this function returns, the operation becomes inactive.
2621 *
2622 * \param[in,out] operation Active AEAD operation.
2623 * \param[out] ciphertext Buffer where the last part of the ciphertext
2624 * is to be written.
2625 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002626 * This must be at least
2627 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2628 * \c alg is the algorithm that is being
2629 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002630 * \param[out] ciphertext_length On success, the number of bytes of
2631 * returned ciphertext.
2632 * \param[out] tag Buffer where the authentication tag is
2633 * to be written.
2634 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002635 * This must be at least
2636 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2637 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002638 * \param[out] tag_length On success, the number of bytes
2639 * that make up the returned tag.
2640 *
2641 * \retval #PSA_SUCCESS
2642 * Success.
2643 * \retval #PSA_ERROR_BAD_STATE
2644 * The operation state is not valid (not set up, nonce not set,
2645 * decryption, or already completed).
2646 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002647 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002648 * You can determine a sufficient buffer size for \p ciphertext by
2649 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2650 * where \c alg is the algorithm that is being calculated.
2651 * You can determine a sufficient buffer size for \p tag by
2652 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002653 * \retval #PSA_ERROR_INVALID_ARGUMENT
2654 * The total length of input to psa_aead_update_ad() so far is
2655 * less than the additional data length that was previously
2656 * specified with psa_aead_set_lengths().
2657 * \retval #PSA_ERROR_INVALID_ARGUMENT
2658 * The total length of input to psa_aead_update() so far is
2659 * less than the plaintext length that was previously
2660 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002661 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2662 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2663 * \retval #PSA_ERROR_HARDWARE_FAILURE
2664 * \retval #PSA_ERROR_TAMPERING_DETECTED
2665 */
2666psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002667 uint8_t *ciphertext,
2668 size_t ciphertext_size,
2669 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002670 uint8_t *tag,
2671 size_t tag_size,
2672 size_t *tag_length);
2673
2674/** Finish authenticating and decrypting a message in an AEAD operation.
2675 *
2676 * The operation must have been set up with psa_aead_decrypt_setup().
2677 *
2678 * This function finishes the authentication of the additional data
2679 * formed by concatenating the inputs passed to preceding calls to
2680 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2681 * inputs passed to preceding calls to psa_aead_update().
2682 *
2683 * When this function returns, the operation becomes inactive.
2684 *
2685 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002686 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002687 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002688 * from previous calls to psa_aead_update()
2689 * that could not be processed until the end
2690 * of the input.
2691 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002692 * This must be at least
2693 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2694 * \c alg is the algorithm that is being
2695 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002696 * \param[out] plaintext_length On success, the number of bytes of
2697 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002698 * \param[in] tag Buffer containing the authentication tag.
2699 * \param tag_length Size of the \p tag buffer in bytes.
2700 *
2701 * \retval #PSA_SUCCESS
2702 * Success.
2703 * \retval #PSA_ERROR_BAD_STATE
2704 * The operation state is not valid (not set up, nonce not set,
2705 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002706 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2707 * The size of the \p plaintext buffer is too small.
2708 * You can determine a sufficient buffer size for \p plaintext by
2709 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2710 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002711 * \retval #PSA_ERROR_INVALID_ARGUMENT
2712 * The total length of input to psa_aead_update_ad() so far is
2713 * less than the additional data length that was previously
2714 * specified with psa_aead_set_lengths().
2715 * \retval #PSA_ERROR_INVALID_ARGUMENT
2716 * The total length of input to psa_aead_update() so far is
2717 * less than the plaintext length that was previously
2718 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002719 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2720 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2721 * \retval #PSA_ERROR_HARDWARE_FAILURE
2722 * \retval #PSA_ERROR_TAMPERING_DETECTED
2723 */
2724psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002725 uint8_t *plaintext,
2726 size_t plaintext_size,
2727 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002728 const uint8_t *tag,
2729 size_t tag_length);
2730
2731/** Abort an AEAD operation.
2732 *
2733 * Aborting an operation frees all associated resources except for the
2734 * \p operation structure itself. Once aborted, the operation object
2735 * can be reused for another operation by calling
2736 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2737 *
2738 * You may call this function any time after the operation object has
2739 * been initialized by any of the following methods:
2740 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2741 * whether it succeeds or not.
2742 * - Initializing the \c struct to all-bits-zero.
2743 * - Initializing the \c struct to logical zeros, e.g.
2744 * `psa_aead_operation_t operation = {0}`.
2745 *
2746 * In particular, calling psa_aead_abort() after the operation has been
2747 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2748 * is safe and has no effect.
2749 *
2750 * \param[in,out] operation Initialized AEAD operation.
2751 *
2752 * \retval #PSA_SUCCESS
2753 * \retval #PSA_ERROR_BAD_STATE
2754 * \p operation is not an active AEAD operation.
2755 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2756 * \retval #PSA_ERROR_HARDWARE_FAILURE
2757 * \retval #PSA_ERROR_TAMPERING_DETECTED
2758 */
2759psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2760
Gilles Peskine3b555712018-03-03 21:27:57 +01002761/**@}*/
2762
Gilles Peskine20035e32018-02-03 22:44:14 +01002763/** \defgroup asymmetric Asymmetric cryptography
2764 * @{
2765 */
2766
2767/**
2768 * \brief Sign a hash or short message with a private key.
2769 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002770 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002771 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002772 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2773 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2774 * to determine the hash algorithm to use.
2775 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002776 * \param handle Handle to the key to use for the operation.
2777 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002778 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002779 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002780 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002781 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002782 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002783 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002784 * \param[out] signature_length On success, the number of bytes
2785 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002786 *
Gilles Peskine28538492018-07-11 17:34:00 +02002787 * \retval #PSA_SUCCESS
2788 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002789 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002790 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002791 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002792 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002793 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002794 * \retval #PSA_ERROR_NOT_SUPPORTED
2795 * \retval #PSA_ERROR_INVALID_ARGUMENT
2796 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2797 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2798 * \retval #PSA_ERROR_HARDWARE_FAILURE
2799 * \retval #PSA_ERROR_TAMPERING_DETECTED
2800 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002801 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002802 * The library has not been previously initialized by psa_crypto_init().
2803 * It is implementation-dependent whether a failure to initialize
2804 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002805 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002806psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002807 psa_algorithm_t alg,
2808 const uint8_t *hash,
2809 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002810 uint8_t *signature,
2811 size_t signature_size,
2812 size_t *signature_length);
2813
2814/**
2815 * \brief Verify the signature a hash or short message using a public key.
2816 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002817 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002818 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002819 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2820 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2821 * to determine the hash algorithm to use.
2822 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002823 * \param handle Handle to the key to use for the operation.
2824 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002825 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002826 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002827 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002828 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002829 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002830 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002831 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002832 *
Gilles Peskine28538492018-07-11 17:34:00 +02002833 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002834 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002835 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002836 * The calculation was perfomed successfully, but the passed
2837 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002838 * \retval #PSA_ERROR_NOT_SUPPORTED
2839 * \retval #PSA_ERROR_INVALID_ARGUMENT
2840 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2841 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2842 * \retval #PSA_ERROR_HARDWARE_FAILURE
2843 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002844 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002845 * The library has not been previously initialized by psa_crypto_init().
2846 * It is implementation-dependent whether a failure to initialize
2847 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002848 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002849psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002850 psa_algorithm_t alg,
2851 const uint8_t *hash,
2852 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002853 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002854 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002855
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002856/**
2857 * \brief Encrypt a short message with a public key.
2858 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002859 * \param handle Handle to the key to use for the operation.
2860 * It must be a public key or an asymmetric
2861 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002862 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002863 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002864 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002865 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002866 * \param[in] salt A salt or label, if supported by the
2867 * encryption algorithm.
2868 * If the algorithm does not support a
2869 * salt, pass \c NULL.
2870 * If the algorithm supports an optional
2871 * salt and you do not want to pass a salt,
2872 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002873 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002874 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2875 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002876 * \param salt_length Size of the \p salt buffer in bytes.
2877 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002878 * \param[out] output Buffer where the encrypted message is to
2879 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002880 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002881 * \param[out] output_length On success, the number of bytes
2882 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002883 *
Gilles Peskine28538492018-07-11 17:34:00 +02002884 * \retval #PSA_SUCCESS
2885 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002886 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002887 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002888 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002889 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002890 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002891 * \retval #PSA_ERROR_NOT_SUPPORTED
2892 * \retval #PSA_ERROR_INVALID_ARGUMENT
2893 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2894 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2895 * \retval #PSA_ERROR_HARDWARE_FAILURE
2896 * \retval #PSA_ERROR_TAMPERING_DETECTED
2897 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002898 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002899 * The library has not been previously initialized by psa_crypto_init().
2900 * It is implementation-dependent whether a failure to initialize
2901 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002902 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002903psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002904 psa_algorithm_t alg,
2905 const uint8_t *input,
2906 size_t input_length,
2907 const uint8_t *salt,
2908 size_t salt_length,
2909 uint8_t *output,
2910 size_t output_size,
2911 size_t *output_length);
2912
2913/**
2914 * \brief Decrypt a short message with a private key.
2915 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002916 * \param handle Handle to the key to use for the operation.
2917 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002918 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002919 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002920 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002921 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002922 * \param[in] salt A salt or label, if supported by the
2923 * encryption algorithm.
2924 * If the algorithm does not support a
2925 * salt, pass \c NULL.
2926 * If the algorithm supports an optional
2927 * salt and you do not want to pass a salt,
2928 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002929 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002930 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2931 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002932 * \param salt_length Size of the \p salt buffer in bytes.
2933 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002934 * \param[out] output Buffer where the decrypted message is to
2935 * be written.
2936 * \param output_size Size of the \c output buffer in bytes.
2937 * \param[out] output_length On success, the number of bytes
2938 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002939 *
Gilles Peskine28538492018-07-11 17:34:00 +02002940 * \retval #PSA_SUCCESS
2941 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002942 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002943 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002944 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002945 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002946 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002947 * \retval #PSA_ERROR_NOT_SUPPORTED
2948 * \retval #PSA_ERROR_INVALID_ARGUMENT
2949 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2950 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2951 * \retval #PSA_ERROR_HARDWARE_FAILURE
2952 * \retval #PSA_ERROR_TAMPERING_DETECTED
2953 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2954 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002955 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002956 * The library has not been previously initialized by psa_crypto_init().
2957 * It is implementation-dependent whether a failure to initialize
2958 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002959 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002960psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002961 psa_algorithm_t alg,
2962 const uint8_t *input,
2963 size_t input_length,
2964 const uint8_t *salt,
2965 size_t salt_length,
2966 uint8_t *output,
2967 size_t output_size,
2968 size_t *output_length);
2969
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002970/**@}*/
2971
Gilles Peskineedd76872018-07-20 17:42:05 +02002972/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002973 * @{
2974 */
2975
2976/** The type of the state data structure for generators.
2977 *
2978 * Before calling any function on a generator, the application must
2979 * initialize it by any of the following means:
2980 * - Set the structure to all-bits-zero, for example:
2981 * \code
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002982 * psa_key_derivation_operation_t generator;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002983 * memset(&generator, 0, sizeof(generator));
2984 * \endcode
2985 * - Initialize the structure to logical zero values, for example:
2986 * \code
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002987 * psa_key_derivation_operation_t generator = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002988 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002989 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002990 * for example:
2991 * \code
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002992 * psa_key_derivation_operation_t generator = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002993 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002994 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002995 * to the structure, for example:
2996 * \code
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002997 * psa_key_derivation_operation_t generator;
2998 * generator = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002999 * \endcode
3000 *
3001 * This is an implementation-defined \c struct. Applications should not
3002 * make any assumptions about the content of this structure except
3003 * as directed by the documentation of a specific implementation.
3004 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003005typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003006
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003007/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003008 *
3009 * This macro returns a suitable initializer for a generator object
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003010 * of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003011 */
3012#ifdef __DOXYGEN_ONLY__
3013/* This is an example definition for documentation purposes.
3014 * Implementations should define a suitable value in `crypto_struct.h`.
3015 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003016#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003017#endif
3018
3019/** Return an initial value for a generator object.
3020 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003021static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003022
3023/** Retrieve the current capacity of a generator.
3024 *
3025 * The capacity of a generator is the maximum number of bytes that it can
3026 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
3027 *
3028 * \param[in] generator The generator to query.
3029 * \param[out] capacity On success, the capacity of the generator.
3030 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003031 * \retval #PSA_SUCCESS
3032 * \retval #PSA_ERROR_BAD_STATE
3033 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02003034 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003035psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *generator,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003036 size_t *capacity);
3037
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003038/** Set the maximum capacity of a generator.
3039 *
3040 * \param[in,out] generator The generator object to modify.
3041 * \param capacity The new capacity of the generator.
3042 * It must be less or equal to the generator's
3043 * current capacity.
3044 *
3045 * \retval #PSA_SUCCESS
3046 * \retval #PSA_ERROR_INVALID_ARGUMENT
3047 * \p capacity is larger than the generator's current capacity.
3048 * \retval #PSA_ERROR_BAD_STATE
3049 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3050 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003051psa_status_t psa_key_derivation_set_capacity(psa_key_derivation_operation_t *generator,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003052 size_t capacity);
3053
Gilles Peskineeab56e42018-07-12 17:12:33 +02003054/** Read some data from a generator.
3055 *
3056 * This function reads and returns a sequence of bytes from a generator.
3057 * The data that is read is discarded from the generator. The generator's
3058 * capacity is decreased by the number of bytes read.
3059 *
3060 * \param[in,out] generator The generator object to read from.
3061 * \param[out] output Buffer where the generator output will be
3062 * written.
3063 * \param output_length Number of bytes to output.
3064 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003065 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003066 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02003067 * There were fewer than \p output_length bytes
3068 * in the generator. Note that in this case, no
3069 * output is written to the output buffer.
3070 * The generator's capacity is set to 0, thus
3071 * subsequent calls to this function will not
3072 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003073 * \retval #PSA_ERROR_BAD_STATE
3074 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3075 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3076 * \retval #PSA_ERROR_HARDWARE_FAILURE
3077 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003078 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003079psa_status_t psa_key_derivation_output_bytes(psa_key_derivation_operation_t *generator,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003080 uint8_t *output,
3081 size_t output_length);
3082
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003083/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003084 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003085 * This function uses the output of a generator to derive a key.
3086 * How much output it consumes and how the key is derived depends on the
3087 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003088 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003089 * - For key types for which the key is an arbitrary sequence of bytes
3090 * of a given size,
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003091 * this function is functionally equivalent to calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003092 * and passing the resulting output to #psa_import_key.
3093 * However, this function has a security benefit:
3094 * if the implementation provides an isolation boundary then
3095 * the key material is not exposed outside the isolation boundary.
3096 * As a consequence, for these key types, this function always consumes
3097 * exactly (\p bits / 8) bytes from the generator.
3098 * The following key types defined in this specification follow this scheme:
3099 *
3100 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003101 * - #PSA_KEY_TYPE_ARC4;
3102 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003103 * - #PSA_KEY_TYPE_DERIVE;
3104 * - #PSA_KEY_TYPE_HMAC.
3105 *
3106 * - For ECC keys on a Montgomery elliptic curve
3107 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3108 * Montgomery curve), this function always draws a byte string whose
3109 * length is determined by the curve, and sets the mandatory bits
3110 * accordingly. That is:
3111 *
3112 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3113 * and process it as specified in RFC 7748 &sect;5.
3114 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3115 * and process it as specified in RFC 7748 &sect;5.
3116 *
3117 * - For key types for which the key is represented by a single sequence of
3118 * \p bits bits with constraints as to which bit sequences are acceptable,
3119 * this function draws a byte string of length (\p bits / 8) bytes rounded
3120 * up to the nearest whole number of bytes. If the resulting byte string
3121 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3122 * This process is repeated until an acceptable byte string is drawn.
3123 * The byte string drawn from the generator is interpreted as specified
3124 * for the output produced by psa_export_key().
3125 * The following key types defined in this specification follow this scheme:
3126 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003127 * - #PSA_KEY_TYPE_DES.
3128 * Force-set the parity bits, but discard forbidden weak keys.
3129 * For 2-key and 3-key triple-DES, the three keys are generated
3130 * successively (for example, for 3-key triple-DES,
3131 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3132 * discard the first 8 bytes, use the next 8 bytes as the first key,
3133 * and continue reading output from the generator to derive the other
3134 * two keys).
3135 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
3136 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
3137 * ECC keys on a Weierstrass elliptic curve
3138 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3139 * Weierstrass curve).
3140 * For these key types, interpret the byte string as integer
3141 * in big-endian order. Discard it if it is not in the range
3142 * [0, *N* - 2] where *N* is the boundary of the private key domain
3143 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003144 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003145 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003146 * This method allows compliance to NIST standards, specifically
3147 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003148 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3149 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3150 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3151 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003152 *
3153 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
3154 * the way in which the generator output is consumed is
3155 * implementation-defined.
3156 *
3157 * In all cases, the data that is read is discarded from the generator.
3158 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003159 *
Gilles Peskine20628592019-04-19 19:29:50 +02003160 * \param[in] attributes The attributes for the new key.
Gilles Peskine98dd7792019-05-15 19:43:49 +02003161 * \param[in,out] generator The generator object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003162 * \param[out] handle On success, a handle to the newly created key.
3163 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003164 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003165 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003166 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003167 * If the key is persistent, the key material and the key's metadata
3168 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003169 * \retval #PSA_ERROR_ALREADY_EXISTS
3170 * This is an attempt to create a persistent key, and there is
3171 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003172 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003173 * There was not enough data to create the desired key.
3174 * Note that in this case, no output is written to the output buffer.
3175 * The generator's capacity is set to 0, thus subsequent calls to
3176 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003177 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003178 * The key type or key size is not supported, either by the
3179 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003180 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003181 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3182 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3183 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3184 * \retval #PSA_ERROR_HARDWARE_FAILURE
3185 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003186 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003187 * The library has not been previously initialized by psa_crypto_init().
3188 * It is implementation-dependent whether a failure to initialize
3189 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003190 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003191psa_status_t psa_key_derivation_output_key(const psa_key_attributes_t *attributes,
3192 psa_key_derivation_operation_t *generator,
Gilles Peskine98dd7792019-05-15 19:43:49 +02003193 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003194
3195/** Abort a generator.
3196 *
3197 * Once a generator has been aborted, its capacity is zero.
3198 * Aborting a generator frees all associated resources except for the
3199 * \c generator structure itself.
3200 *
3201 * This function may be called at any time as long as the generator
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003202 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
3203 * psa_key_derivation_operation_init() or a zero value. In particular, it is valid
3204 * to call psa_key_derivation_abort() twice, or to call psa_key_derivation_abort()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003205 * on a generator that has not been set up.
3206 *
3207 * Once aborted, the generator object may be called.
3208 *
3209 * \param[in,out] generator The generator to abort.
3210 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003211 * \retval #PSA_SUCCESS
3212 * \retval #PSA_ERROR_BAD_STATE
3213 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3214 * \retval #PSA_ERROR_HARDWARE_FAILURE
3215 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003216 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003217psa_status_t psa_key_derivation_abort(psa_key_derivation_operation_t *generator);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003218
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003219/** Use the maximum possible capacity for a generator.
3220 *
3221 * Use this value as the capacity argument when setting up a generator
3222 * to indicate that the generator should have the maximum possible capacity.
3223 * The value of the maximum possible capacity depends on the generator
3224 * algorithm.
3225 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003226#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003227
Gilles Peskineeab56e42018-07-12 17:12:33 +02003228/**@}*/
3229
Gilles Peskineea0fb492018-07-12 17:17:20 +02003230/** \defgroup derivation Key derivation
3231 * @{
3232 */
3233
3234/** Set up a key derivation operation.
3235 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003236 * A key derivation algorithm takes some inputs and uses them to create
3237 * a byte generator which can be used to produce keys and other
3238 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003239 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003240 * To use a generator for key derivation:
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003241 * - Start with an initialized object of type #psa_key_derivation_operation_t.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003242 * - Call psa_key_derivation_setup() to select the algorithm.
3243 * - Provide the inputs for the key derivation by calling
3244 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3245 * as appropriate. Which inputs are needed, in what order, and whether
3246 * they may be keys and if so of what type depends on the algorithm.
3247 * - Optionally set the generator's maximum capacity with
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003248 * psa_key_derivation_set_capacity(). You may do this before, in the middle of
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003249 * or after providing inputs. For some algorithms, this step is mandatory
3250 * because the output depends on the maximum capacity.
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003251 * - Generate output with psa_key_derivation_output_bytes() or
3252 * psa_key_derivation_output_key(). Successive calls to these functions
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003253 * use successive output bytes from the generator.
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003254 * - Clean up the generator object with psa_key_derivation_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003255 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003256 * \param[in,out] generator The generator object to set up. It must
3257 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003258 * \param alg The key derivation algorithm to compute
3259 * (\c PSA_ALG_XXX value such that
3260 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003261 *
3262 * \retval #PSA_SUCCESS
3263 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003264 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003265 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003266 * \retval #PSA_ERROR_NOT_SUPPORTED
3267 * \c alg is not supported or is not a key derivation algorithm.
3268 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3269 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3270 * \retval #PSA_ERROR_HARDWARE_FAILURE
3271 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003272 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003273 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003274psa_status_t psa_key_derivation_setup(psa_key_derivation_operation_t *generator,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003275 psa_algorithm_t alg);
3276
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003277/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003278 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003279 * Which inputs are required and in what order depends on the algorithm.
3280 * Refer to the documentation of each key derivation or key agreement
3281 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003282 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003283 * This function passes direct inputs. Some inputs must be passed as keys
3284 * using psa_key_derivation_input_key() instead of this function. Refer to
3285 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003286 *
3287 * \param[in,out] generator The generator object to use. It must
3288 * have been set up with
3289 * psa_key_derivation_setup() and must not
3290 * have produced any output yet.
3291 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003292 * \param[in] data Input data to use.
3293 * \param data_length Size of the \p data buffer in bytes.
3294 *
3295 * \retval #PSA_SUCCESS
3296 * Success.
3297 * \retval #PSA_ERROR_INVALID_ARGUMENT
3298 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003299 * \retval #PSA_ERROR_INVALID_ARGUMENT
3300 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003301 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3302 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3303 * \retval #PSA_ERROR_HARDWARE_FAILURE
3304 * \retval #PSA_ERROR_TAMPERING_DETECTED
3305 * \retval #PSA_ERROR_BAD_STATE
3306 * The value of \p step is not valid given the state of \p generator.
3307 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003308 * The library has not been previously initialized by psa_crypto_init().
3309 * It is implementation-dependent whether a failure to initialize
3310 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003311 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003312psa_status_t psa_key_derivation_input_bytes(psa_key_derivation_operation_t *generator,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003313 psa_key_derivation_step_t step,
3314 const uint8_t *data,
3315 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003316
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003317/** Provide an input for key derivation in the form of a key.
3318 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003319 * Which inputs are required and in what order depends on the algorithm.
3320 * Refer to the documentation of each key derivation or key agreement
3321 * algorithm for information.
3322 *
3323 * This function passes key inputs. Some inputs must be passed as keys
3324 * of the appropriate type using this function, while others must be
3325 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3326 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003327 *
3328 * \param[in,out] generator The generator object to use. It must
3329 * have been set up with
3330 * psa_key_derivation_setup() and must not
3331 * have produced any output yet.
3332 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003333 * \param handle Handle to the key. It must have an
3334 * appropriate type for \p step and must
3335 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003336 *
3337 * \retval #PSA_SUCCESS
3338 * Success.
3339 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003340 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003341 * \retval #PSA_ERROR_NOT_PERMITTED
3342 * \retval #PSA_ERROR_INVALID_ARGUMENT
3343 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003344 * \retval #PSA_ERROR_INVALID_ARGUMENT
3345 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003346 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3347 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3348 * \retval #PSA_ERROR_HARDWARE_FAILURE
3349 * \retval #PSA_ERROR_TAMPERING_DETECTED
3350 * \retval #PSA_ERROR_BAD_STATE
3351 * The value of \p step is not valid given the state of \p generator.
3352 * \retval #PSA_ERROR_BAD_STATE
3353 * The library has not been previously initialized by psa_crypto_init().
3354 * It is implementation-dependent whether a failure to initialize
3355 * results in this error code.
3356 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003357psa_status_t psa_key_derivation_input_key(psa_key_derivation_operation_t *generator,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003358 psa_key_derivation_step_t step,
3359 psa_key_handle_t handle);
3360
Gilles Peskine969c5d62019-01-16 15:53:06 +01003361/** Perform a key agreement and use the shared secret as input to a key
3362 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003363 *
3364 * A key agreement algorithm takes two inputs: a private key \p private_key
3365 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003366 * The result of this function is passed as input to a key derivation.
3367 * The output of this key derivation can be extracted by reading from the
3368 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003369 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003370 * \param[in,out] generator The generator object to use. It must
3371 * have been set up with
3372 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003373 * key agreement and derivation algorithm
3374 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003375 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3376 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003377 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003378 * The generator must be ready for an
3379 * input of the type given by \p step.
3380 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003381 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003382 * \param[in] peer_key Public key of the peer. The peer key must be in the
3383 * same format that psa_import_key() accepts for the
3384 * public key type corresponding to the type of
3385 * private_key. That is, this function performs the
3386 * equivalent of
Gilles Peskine806051f2019-05-15 19:50:17 +02003387 * #psa_import_key(...,
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003388 * `peer_key`, `peer_key_length`) where
Gilles Peskine806051f2019-05-15 19:50:17 +02003389 * with key attributes indicating the public key
3390 * type corresponding to the type of `private_key`.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003391 * For example, for EC keys, this means that peer_key
3392 * is interpreted as a point on the curve that the
3393 * private key is on. The standard formats for public
3394 * keys are documented in the documentation of
3395 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003396 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003397 *
3398 * \retval #PSA_SUCCESS
3399 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003400 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003401 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003402 * \retval #PSA_ERROR_NOT_PERMITTED
3403 * \retval #PSA_ERROR_INVALID_ARGUMENT
3404 * \c private_key is not compatible with \c alg,
3405 * or \p peer_key is not valid for \c alg or not compatible with
3406 * \c private_key.
3407 * \retval #PSA_ERROR_NOT_SUPPORTED
3408 * \c alg is not supported or is not a key derivation algorithm.
3409 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3410 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3411 * \retval #PSA_ERROR_HARDWARE_FAILURE
3412 * \retval #PSA_ERROR_TAMPERING_DETECTED
3413 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003414psa_status_t psa_key_derivation_key_agreement(psa_key_derivation_operation_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003415 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003416 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003417 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003418 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003419
Gilles Peskine769c7a62019-01-18 16:42:29 +01003420/** Perform a key agreement and use the shared secret as input to a key
3421 * derivation.
3422 *
3423 * A key agreement algorithm takes two inputs: a private key \p private_key
3424 * a public key \p peer_key.
3425 *
3426 * \warning The raw result of a key agreement algorithm such as finite-field
3427 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3428 * not be used directly as key material. It should instead be passed as
3429 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003430 * a key derivation, use psa_key_derivation_key_agreement() and other functions from
Gilles Peskine769c7a62019-01-18 16:42:29 +01003431 * the key derivation and generator interface.
3432 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003433 * \param alg The key agreement algorithm to compute
3434 * (\c PSA_ALG_XXX value such that
3435 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3436 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003437 * \param private_key Handle to the private key to use.
3438 * \param[in] peer_key Public key of the peer. It must be
3439 * in the same format that psa_import_key()
3440 * accepts. The standard formats for public
3441 * keys are documented in the documentation
3442 * of psa_export_public_key().
3443 * \param peer_key_length Size of \p peer_key in bytes.
3444 * \param[out] output Buffer where the decrypted message is to
3445 * be written.
3446 * \param output_size Size of the \c output buffer in bytes.
3447 * \param[out] output_length On success, the number of bytes
3448 * that make up the returned output.
3449 *
3450 * \retval #PSA_SUCCESS
3451 * Success.
3452 * \retval #PSA_ERROR_INVALID_HANDLE
3453 * \retval #PSA_ERROR_EMPTY_SLOT
3454 * \retval #PSA_ERROR_NOT_PERMITTED
3455 * \retval #PSA_ERROR_INVALID_ARGUMENT
3456 * \p alg is not a key agreement algorithm
3457 * \retval #PSA_ERROR_INVALID_ARGUMENT
3458 * \p private_key is not compatible with \p alg,
3459 * or \p peer_key is not valid for \p alg or not compatible with
3460 * \p private_key.
3461 * \retval #PSA_ERROR_NOT_SUPPORTED
3462 * \p alg is not a supported key agreement algorithm.
3463 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3464 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3465 * \retval #PSA_ERROR_HARDWARE_FAILURE
3466 * \retval #PSA_ERROR_TAMPERING_DETECTED
3467 */
3468psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3469 psa_key_handle_t private_key,
3470 const uint8_t *peer_key,
3471 size_t peer_key_length,
3472 uint8_t *output,
3473 size_t output_size,
3474 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003475
3476/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003477
3478/** \defgroup random Random generation
3479 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003480 */
3481
3482/**
3483 * \brief Generate random bytes.
3484 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003485 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003486 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003487 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003488 *
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003489 * \note To generate a key, use psa_generate_random_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003490 *
3491 * \param[out] output Output buffer for the generated data.
3492 * \param output_size Number of bytes to generate and output.
3493 *
3494 * \retval #PSA_SUCCESS
3495 * \retval #PSA_ERROR_NOT_SUPPORTED
3496 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3497 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3498 * \retval #PSA_ERROR_HARDWARE_FAILURE
3499 * \retval #PSA_ERROR_TAMPERING_DETECTED
3500 * \retval #PSA_ERROR_BAD_STATE
3501 * The library has not been previously initialized by psa_crypto_init().
3502 * It is implementation-dependent whether a failure to initialize
3503 * results in this error code.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003504 */
3505psa_status_t psa_generate_random(uint8_t *output,
3506 size_t output_size);
3507
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003508/**
3509 * \brief Generate a key or key pair.
3510 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003511 * The key is generated randomly.
3512 * Its location, policy, type and size are taken from \p attributes.
3513 *
3514 * If the type requires additional domain parameters, these are taken
3515 * from \p attributes as well. The following types use domain parameters:
3516 * - When generating an RSA key (#PSA_KEY_TYPE_RSA_KEYPAIR),
3517 * the default public exponent is 65537. This value is used if
3518 * \p attributes was set with psa_set_key_type() or by passing an empty
3519 * byte string as domain parameters to psa_set_key_domain_parameters().
3520 * If psa_set_key_domain_parameters() was used to set a non-empty
3521 * domain parameter string in \p attributes, this string is read as
3522 * a big-endian integer which is used as the public exponent.
3523 * - When generating a DSA key (#PSA_KEY_TYPE_DSA_KEYPAIR) or a
3524 * Diffie-Hellman key (#PSA_KEY_TYPE_DH_KEYPAIR), the domain parameters
3525 * from \p attributes are interpreted as described for
3526 * psa_set_key_domain_parameters().
3527 *
Gilles Peskine20628592019-04-19 19:29:50 +02003528 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003529 * \param[out] handle On success, a handle to the newly created key.
3530 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003531 *
Gilles Peskine28538492018-07-11 17:34:00 +02003532 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003533 * Success.
3534 * If the key is persistent, the key material and the key's metadata
3535 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003536 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003537 * This is an attempt to create a persistent key, and there is
3538 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003539 * \retval #PSA_ERROR_NOT_SUPPORTED
3540 * \retval #PSA_ERROR_INVALID_ARGUMENT
3541 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3542 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3543 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3544 * \retval #PSA_ERROR_HARDWARE_FAILURE
3545 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003546 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003547 * The library has not been previously initialized by psa_crypto_init().
3548 * It is implementation-dependent whether a failure to initialize
3549 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003550 */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01003551psa_status_t psa_generate_random_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003552 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003553
3554/**@}*/
3555
Gilles Peskinee59236f2018-01-27 23:32:46 +01003556#ifdef __cplusplus
3557}
3558#endif
3559
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003560/* The file "crypto_sizes.h" contains definitions for size calculation
3561 * macros whose definitions are implementation-specific. */
3562#include "crypto_sizes.h"
3563
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003564/* The file "crypto_struct.h" contains definitions for
3565 * implementation-specific structs that are declared above. */
3566#include "crypto_struct.h"
3567
3568/* The file "crypto_extra.h" contains vendor-specific definitions. This
3569 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003570#include "crypto_extra.h"
3571
3572#endif /* PSA_CRYPTO_H */