blob: e8f9a18b3f5c1f0ccfde2bb8959dad2fffb32713 [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():
135 * - psa_make_key_persistent()
136 * - psa_set_key_type()
137 * - psa_set_key_bits()
138 * - psa_set_key_usage_flags()
139 * - psa_set_key_algorithm()
140 *
Gilles Peskine20628592019-04-19 19:29:50 +0200141 * Before calling any function on a key attribute structure, the application
142 * must initialize it by any of the following means:
143 * - Set the structure to all-bits-zero, for example:
144 * \code
145 * psa_key_attributes_t attributes;
146 * memset(&attributes, 0, sizeof(attributes));
147 * \endcode
148 * - Initialize the structure to logical zero values, for example:
149 * \code
150 * psa_key_attributes_t attributes = {0};
151 * \endcode
152 * - Initialize the structure to the initializer #PSA_KEY_ATTRIBUTES_INIT,
153 * for example:
154 * \code
155 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
156 * \endcode
157 * - Assign the result of the function psa_key_attributes_init()
158 * to the structure, for example:
159 * \code
160 * psa_key_attributes_t attributes;
161 * attributes = psa_key_attributes_init();
162 * \endcode
163 *
164 * A freshly initialized attribute structure contains the following
165 * values:
166 *
167 * - lifetime: #PSA_KEY_LIFETIME_VOLATILE.
168 * - key identifier: unspecified.
169 * - type: \c 0, with no domain parameters.
170 * - key size: \c 0.
171 * - usage flags: \c 0.
172 * - algorithm: \c 0.
173 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200174 * A typical sequence to create a key is as follows:
175 * -# Create and initialize an attribute structure.
176 * -# If the key is persistent, call psa_make_key_persistent().
177 * -# Set the key policy with psa_set_key_usage_flags() and
178 * psa_set_key_algorithm().
179 * -# Set the key type with psa_set_key_type(). If the key type requires
180 * domain parameters, call psa_set_key_domain_parameters() instead.
181 * Skip this step if copying an existing key with psa_copy_key().
182 * -# When generating a random key with psa_generate_key() or deriving a key
183 * with psa_generator_import_key(), set the desired key size with
184 * psa_set_key_bits().
185 * -# Call a key creation function: psa_import_key(), psa_generate_key(),
186 * psa_generator_import_key() or psa_copy_key().
187 * -# The attribute structure is no longer necessary. If you called
188 * psa_set_key_domain_parameters() earlier, you must call
189 * psa_reset_key_attributes() to free any resources used by the
190 * domain parameters. Otherwise calling psa_reset_key_attributes()
191 * is optional.
Gilles Peskine20628592019-04-19 19:29:50 +0200192 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200193 * A typical sequence to query a key's attributes is as follows:
194 * -# Call psa_get_key_attributes().
195 * -# Call `psa_get_key_xxx` functions to retrieve the attribute(s) that
196 * you are interested in.
197 * -# Call psa_reset_key_attributes() to free any resources that may be
198 * used by the attribute structure.
199 *
200 * Once a key has been created, it is impossible to change its attributes.
Gilles Peskine87a5e562019-04-17 12:28:25 +0200201 */
202typedef struct psa_key_attributes_s psa_key_attributes_t;
203
Gilles Peskine20628592019-04-19 19:29:50 +0200204/** Declare a key as persistent.
205 *
206 * This function does not access storage, it merely fills the attribute
207 * structure with given values. The persistent key will be written to
208 * storage when the attribute structure is passed to a key creation
209 * function such as psa_import_key(), psa_generate_key(),
210 * psa_generator_import_key() or psa_copy_key().
211 *
212 * This function overwrites any identifier and lifetime values
213 * previously set in \p attributes.
214 *
215 * This function may be declared as `static` (i.e. without external
216 * linkage). This function may be provided as a function-like macro,
217 * but in this case it must evaluate each of its arguments exactly once.
218 *
219 * \param[out] attributes The attribute structure to write to.
220 * \param id The persistent identifier for the key.
221 * \param lifetime The lifetime for the key.
222 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
223 * key will be volatile, and \p id is ignored.
224 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200225static void psa_make_key_persistent(psa_key_attributes_t *attributes,
226 psa_key_id_t id,
227 psa_key_lifetime_t lifetime);
228
Gilles Peskine20628592019-04-19 19:29:50 +0200229/** Retrieve the key identifier from key attributes.
230 *
231 * This function may be declared as `static` (i.e. without external
232 * linkage). This function may be provided as a function-like macro,
233 * but in this case it must evaluate its argument exactly once.
234 *
235 * \param[in] attributes The key attribute structure to query.
236 *
237 * \return The persistent identifier stored in the attribute structure.
238 * This value is unspecified if the attribute structure declares
239 * the key as volatile.
240 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200241static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
242
Gilles Peskine20628592019-04-19 19:29:50 +0200243/** Retrieve the lifetime from key attributes.
244 *
245 * This function may be declared as `static` (i.e. without external
246 * linkage). This function may be provided as a function-like macro,
247 * but in this case it must evaluate its argument exactly once.
248 *
249 * \param[in] attributes The key attribute structure to query.
250 *
251 * \return The lifetime value stored in the attribute structure.
252 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200253static psa_key_lifetime_t psa_get_key_lifetime(
254 const psa_key_attributes_t *attributes);
255
Gilles Peskine20628592019-04-19 19:29:50 +0200256/** Declare usage flags for a key.
257 *
258 * Usage flags are part of a key's usage policy. They encode what
259 * kind of operations are permitted on the key. For more details,
260 * refer to the documentation of the type #psa_key_usage_t.
261 *
262 * This function overwrites any usage flags
263 * previously set in \p attributes.
264 *
265 * This function may be declared as `static` (i.e. without external
266 * linkage). This function may be provided as a function-like macro,
267 * but in this case it must evaluate each of its arguments exactly once.
268 *
269 * \param[out] attributes The attribute structure to write to.
270 * \param usage_flags The usage flags to write.
271 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200272static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
273 psa_key_usage_t usage_flags);
274
Gilles Peskine20628592019-04-19 19:29:50 +0200275/** Retrieve the usage flags from key attributes.
276 *
277 * This function may be declared as `static` (i.e. without external
278 * linkage). This function may be provided as a function-like macro,
279 * but in this case it must evaluate its argument exactly once.
280 *
281 * \param[in] attributes The key attribute structure to query.
282 *
283 * \return The usage flags stored in the attribute structure.
284 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200285static psa_key_usage_t psa_get_key_usage_flags(
286 const psa_key_attributes_t *attributes);
287
Gilles Peskine20628592019-04-19 19:29:50 +0200288/** Declare the permitted algorithm policy for a key.
289 *
290 * The permitted algorithm policy of a key encodes which algorithm or
291 * algorithms are permitted to be used with this key.
292 *
293 * This function overwrites any algorithm policy
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 alg The permitted algorithm policy to write.
302 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200303static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
304 psa_algorithm_t alg);
305
Gilles Peskine20628592019-04-19 19:29:50 +0200306/** Retrieve the algorithm policy 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 algorithm stored in the attribute structure.
315 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200316static psa_algorithm_t psa_get_key_algorithm(
317 const psa_key_attributes_t *attributes);
318
Gilles Peskine20628592019-04-19 19:29:50 +0200319/** Declare the type of a key.
320 *
321 * If a type requires domain parameters, you must call
322 * psa_set_key_domain_parameters() instead of this function.
323 *
324 * This function overwrites any key type and domain parameters
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 type The key type to write.
333 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200334static void psa_set_key_type(psa_key_attributes_t *attributes,
335 psa_key_type_t type);
336
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200337/** Declare the size of a key.
338 *
339 * This function overwrites any key size previously set in \p attributes.
340 *
341 * This function may be declared as `static` (i.e. without external
342 * linkage). This function may be provided as a function-like macro,
343 * but in this case it must evaluate each of its arguments exactly once.
344 *
345 * \param[out] attributes The attribute structure to write to.
346 * \param bits The key size in bits.
347 */
348static void psa_set_key_bits(psa_key_attributes_t *attributes,
349 size_t bits);
350
Gilles Peskine20628592019-04-19 19:29:50 +0200351/** Retrieve the key type from key attributes.
352 *
353 * This function may be declared as `static` (i.e. without external
354 * linkage). This function may be provided as a function-like macro,
355 * but in this case it must evaluate its argument exactly once.
356 *
357 * \param[in] attributes The key attribute structure to query.
358 *
359 * \return The key type stored in the attribute structure.
360 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200361static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
362
Gilles Peskine20628592019-04-19 19:29:50 +0200363/** Retrieve the key size from key attributes.
364 *
365 * This function may be declared as `static` (i.e. without external
366 * linkage). This function may be provided as a function-like macro,
367 * but in this case it must evaluate its argument exactly once.
368 *
369 * \param[in] attributes The key attribute structure to query.
370 *
371 * \return The key size stored in the attribute structure, in bits.
372 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200373static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
374
Gilles Peskineb699f072019-04-26 16:06:02 +0200375/**
376 * \brief Set domain parameters for a key.
377 *
378 * Some key types require additional domain parameters in addition to
379 * the key type identifier and the key size.
380 * The format for the required domain parameters varies by the key type.
381 *
Gilles Peskinee56e8782019-04-26 17:34:02 +0200382 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEYPAIR),
383 * the domain parameter data consists of the public exponent,
Gilles Peskineb699f072019-04-26 16:06:02 +0200384 * represented as a big-endian integer with no leading zeros.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200385 * This information is used when generating an RSA key pair.
Gilles Peskineb699f072019-04-26 16:06:02 +0200386 * When importing a key, the public exponent is read from the imported
387 * key data and the exponent recorded in the attribute structure is ignored.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200388 * As an exception, the public exponent 65537 is represented by an empty
389 * byte string.
390 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEYPAIR),
Gilles Peskineb699f072019-04-26 16:06:02 +0200391 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
392 * ```
393 * Dss-Parms ::= SEQUENCE {
394 * p INTEGER,
395 * q INTEGER,
396 * g INTEGER
397 * }
398 * ```
Gilles Peskinee56e8782019-04-26 17:34:02 +0200399 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY or
400 * #PSA_KEY_TYPE_DH_KEYPAIR), the
Gilles Peskineb699f072019-04-26 16:06:02 +0200401 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
402 * ```
403 * DomainParameters ::= SEQUENCE {
404 * p INTEGER, -- odd prime, p=jq +1
405 * g INTEGER, -- generator, g
406 * q INTEGER, -- factor of p-1
407 * j INTEGER OPTIONAL, -- subgroup factor
408 * validationParms ValidationParms OPTIONAL
409 * }
410 * ValidationParms ::= SEQUENCE {
411 * seed BIT STRING,
412 * pgenCounter INTEGER
413 * }
414 * ```
415 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200416 * \note This function may allocate memory or other resources.
417 * Once you have called this function on an attribute structure,
418 * you must call psa_reset_key_attributes() to free these resources.
419 *
Gilles Peskineb699f072019-04-26 16:06:02 +0200420 * \param[in,out] attributes Attribute structure where the specified domain
421 * parameters will be stored.
422 * If this function fails, the content of
423 * \p attributes is not modified.
424 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
425 * \param[in] data Buffer containing the key domain parameters.
426 * The content of this buffer is interpreted
427 * according to \p type as described above.
428 * \param data_length Size of the \p data buffer in bytes.
429 *
430 * \retval #PSA_SUCCESS
431 * \retval #PSA_ERROR_INVALID_ARGUMENT
432 * \retval #PSA_ERROR_NOT_SUPPORTED
433 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
434 */
435psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
436 psa_key_type_t type,
437 const uint8_t *data,
438 size_t data_length);
439
440/**
441 * \brief Get domain parameters for a key.
442 *
443 * Get the domain parameters for a key with this function, if any. The format
444 * of the domain parameters written to \p data is specified in the
445 * documentation for psa_set_key_domain_parameters().
446 *
447 * \param[in] attributes The key attribute structure to query.
448 * \param[out] data On success, the key domain parameters.
449 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineaa02c172019-04-28 11:44:17 +0200450 * The buffer is guaranteed to be large
451 * enough if its size in bytes is at least
452 * the value given by
453 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
Gilles Peskineb699f072019-04-26 16:06:02 +0200454 * \param[out] data_length On success, the number of bytes
455 * that make up the key domain parameters data.
456 *
457 * \retval #PSA_SUCCESS
458 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
459 */
460psa_status_t psa_get_key_domain_parameters(
461 const psa_key_attributes_t *attributes,
462 uint8_t *data,
463 size_t data_size,
464 size_t *data_length);
465
Gilles Peskine20628592019-04-19 19:29:50 +0200466/** Retrieve the attributes of a key.
467 *
468 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200469 * psa_reset_key_attributes(). It then copies the attributes of
470 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200471 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200472 * \note This function may allocate memory or other resources.
473 * Once you have called this function on an attribute structure,
474 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200475 *
Gilles Peskine20628592019-04-19 19:29:50 +0200476 * \param[in] handle Handle to the key to query.
477 * \param[in,out] attributes On success, the attributes of the key.
478 * On failure, equivalent to a
479 * freshly-initialized structure.
480 *
481 * \retval #PSA_SUCCESS
482 * \retval #PSA_ERROR_INVALID_HANDLE
483 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
484 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
485 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200486psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
487 psa_key_attributes_t *attributes);
488
Gilles Peskine20628592019-04-19 19:29:50 +0200489/** Reset a key attribute structure to a freshly initialized state.
490 *
491 * You must initialize the attribute structure as described in the
492 * documentation of the type #psa_key_attributes_t before calling this
493 * function. Once the structure has been initialized, you may call this
494 * function at any time.
495 *
496 * This function frees any auxiliary resources that the structure
497 * may contain.
498 *
499 * \param[in,out] attributes The attribute structure to reset.
500 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200501void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200502
Gilles Peskine87a5e562019-04-17 12:28:25 +0200503/**@}*/
504
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100505/** \defgroup key_management Key management
506 * @{
507 */
508
Gilles Peskinef535eb22018-11-30 14:08:36 +0100509/** Open a handle to an existing persistent key.
510 *
511 * Open a handle to a key which was previously created with psa_create_key().
512 *
513 * \param lifetime The lifetime of the key. This designates a storage
514 * area where the key material is stored. This must not
515 * be #PSA_KEY_LIFETIME_VOLATILE.
516 * \param id The persistent identifier of the key.
517 * \param[out] handle On success, a handle to a key slot which contains
518 * the data and metadata loaded from the specified
519 * persistent location.
520 *
521 * \retval #PSA_SUCCESS
522 * Success. The application can now use the value of `*handle`
523 * to access the newly allocated key slot.
524 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200525 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100526 * \retval #PSA_ERROR_INVALID_ARGUMENT
527 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
528 * \retval #PSA_ERROR_INVALID_ARGUMENT
529 * \p id is invalid for the specified lifetime.
530 * \retval #PSA_ERROR_NOT_SUPPORTED
531 * \p lifetime is not supported.
532 * \retval #PSA_ERROR_NOT_PERMITTED
533 * The specified key exists, but the application does not have the
534 * permission to access it. Note that this specification does not
535 * define any way to create such a key, but it may be possible
536 * through implementation-specific means.
537 */
538psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
539 psa_key_id_t id,
540 psa_key_handle_t *handle);
541
Gilles Peskinef535eb22018-11-30 14:08:36 +0100542/** Close a key handle.
543 *
544 * If the handle designates a volatile key, destroy the key material and
545 * free all associated resources, just like psa_destroy_key().
546 *
547 * If the handle designates a persistent key, free all resources associated
548 * with the key in volatile memory. The key slot in persistent storage is
549 * not affected and can be opened again later with psa_open_key().
550 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100551 * If the key is currently in use in a multipart operation,
552 * the multipart operation is aborted.
553 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100554 * \param handle The key handle to close.
555 *
556 * \retval #PSA_SUCCESS
557 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100558 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100559 */
560psa_status_t psa_close_key(psa_key_handle_t handle);
561
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100562/**@}*/
563
564/** \defgroup import_export Key import and export
565 * @{
566 */
567
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568/**
569 * \brief Import a key in binary format.
570 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100571 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100572 * documentation of psa_export_public_key() for the format of public keys
573 * and to the documentation of psa_export_key() for the format for
574 * other key types.
575 *
576 * This specification supports a single format for each key type.
577 * Implementations may support other formats as long as the standard
578 * format is supported. Implementations that support other formats
579 * should ensure that the formats are clearly unambiguous so as to
580 * minimize the risk that an invalid input is accidentally interpreted
581 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100582 *
Gilles Peskine20628592019-04-19 19:29:50 +0200583 * \param[in] attributes The attributes for the new key.
584 * The key size field in \p attributes is
585 * ignored; the actual key size is determined
586 * from the \p data buffer.
587 * \param[out] handle On success, a handle to the newly created key.
588 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100589 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine20628592019-04-19 19:29:50 +0200590 * buffer is interpreted according to the type and,
591 * if applicable, domain parameters declared in
592 * \p attributes.
593 * All implementations must support at least the format
594 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100595 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200596 * the chosen type. Implementations may allow other
597 * formats, but should be conservative: implementations
598 * should err on the side of rejecting content if it
599 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200600 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100601 *
Gilles Peskine28538492018-07-11 17:34:00 +0200602 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100603 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100604 * If the key is persistent, the key material and the key's metadata
605 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200606 * \retval #PSA_ERROR_ALREADY_EXISTS
607 * This is an attempt to create a persistent key, and there is
608 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200609 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200610 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200611 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200612 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200613 * The key attributes, as a whole, are invalid,
Gilles Peskine308b91d2018-02-08 09:47:44 +0100614 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200615 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
616 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
617 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100618 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200619 * \retval #PSA_ERROR_HARDWARE_FAILURE
620 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300621 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300622 * The library has not been previously initialized by psa_crypto_init().
623 * It is implementation-dependent whether a failure to initialize
624 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100625 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200626psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
627 psa_key_handle_t *handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100628 const uint8_t *data,
629 size_t data_length);
630
631/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100632 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200633 *
634 * This function destroys the content of the key slot from both volatile
635 * memory and, if applicable, non-volatile storage. Implementations shall
636 * make a best effort to ensure that any previous content of the slot is
637 * unrecoverable.
638 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100639 * This function also erases any metadata such as policies and frees all
640 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200641 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100642 * If the key is currently in use in a multipart operation,
643 * the multipart operation is aborted.
644 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100645 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646 *
Gilles Peskine28538492018-07-11 17:34:00 +0200647 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200648 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200649 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200650 * The slot holds content and cannot be erased because it is
651 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100652 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200653 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200654 * There was an failure in communication with the cryptoprocessor.
655 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200656 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200657 * The storage is corrupted. Implementations shall make a best effort
658 * to erase key material even in this stage, however applications
659 * should be aware that it may be impossible to guarantee that the
660 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200661 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200662 * An unexpected condition which is not a storage corruption or
663 * a communication failure occurred. The cryptoprocessor may have
664 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300665 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300666 * The library has not been previously initialized by psa_crypto_init().
667 * It is implementation-dependent whether a failure to initialize
668 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100669 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100670psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100671
672/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673 * \brief Export a key in binary format.
674 *
675 * The output of this function can be passed to psa_import_key() to
676 * create an equivalent object.
677 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100678 * If the implementation of psa_import_key() supports other formats
679 * beyond the format specified here, the output from psa_export_key()
680 * must use the representation specified here, not the original
681 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100682 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100683 * For standard key types, the output format is as follows:
684 *
685 * - For symmetric keys (including MAC keys), the format is the
686 * raw bytes of the key.
687 * - For DES, the key data consists of 8 bytes. The parity bits must be
688 * correct.
689 * - For Triple-DES, the format is the concatenation of the
690 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100691 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200692 * is the non-encrypted DER encoding of the representation defined by
693 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
694 * ```
695 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200696 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200697 * modulus INTEGER, -- n
698 * publicExponent INTEGER, -- e
699 * privateExponent INTEGER, -- d
700 * prime1 INTEGER, -- p
701 * prime2 INTEGER, -- q
702 * exponent1 INTEGER, -- d mod (p-1)
703 * exponent2 INTEGER, -- d mod (q-1)
704 * coefficient INTEGER, -- (inverse of q) mod p
705 * }
706 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000707 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
708 * representation of the private key `x` as a big-endian byte string. The
709 * length of the byte string is the private key size in bytes (leading zeroes
710 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200711 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100712 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100713 * a representation of the private value as a `ceiling(m/8)`-byte string
714 * where `m` is the bit size associated with the curve, i.e. the bit size
715 * of the order of the curve's coordinate field. This byte string is
716 * in little-endian order for Montgomery curves (curve types
717 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
718 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
719 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100720 * This is the content of the `privateKey` field of the `ECPrivateKey`
721 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000722 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
723 * format is the representation of the private key `x` as a big-endian byte
724 * string. The length of the byte string is the private key size in bytes
725 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200726 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
727 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100728 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100729 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200730 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200731 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200732 * \param[out] data_length On success, the number of bytes
733 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100734 *
Gilles Peskine28538492018-07-11 17:34:00 +0200735 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100736 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200737 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200738 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100739 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200740 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
741 * The size of the \p data buffer is too small. You can determine a
742 * sufficient buffer size by calling
743 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
744 * where \c type is the key type
745 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200746 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
747 * \retval #PSA_ERROR_HARDWARE_FAILURE
748 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300749 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300750 * The library has not been previously initialized by psa_crypto_init().
751 * It is implementation-dependent whether a failure to initialize
752 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100753 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100754psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100755 uint8_t *data,
756 size_t data_size,
757 size_t *data_length);
758
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100759/**
760 * \brief Export a public key or the public part of a key pair in binary format.
761 *
762 * The output of this function can be passed to psa_import_key() to
763 * create an object that is equivalent to the public key.
764 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000765 * This specification supports a single format for each key type.
766 * Implementations may support other formats as long as the standard
767 * format is supported. Implementations that support other formats
768 * should ensure that the formats are clearly unambiguous so as to
769 * minimize the risk that an invalid input is accidentally interpreted
770 * according to a different format.
771 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000772 * For standard key types, the output format is as follows:
773 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
774 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
775 * ```
776 * RSAPublicKey ::= SEQUENCE {
777 * modulus INTEGER, -- n
778 * publicExponent INTEGER } -- e
779 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000780 * - For elliptic curve public keys (key types for which
781 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
782 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
783 * Let `m` be the bit size associated with the curve, i.e. the bit size of
784 * `q` for a curve over `F_q`. The representation consists of:
785 * - The byte 0x04;
786 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
787 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000788 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
789 * representation of the public key `y = g^x mod p` as a big-endian byte
790 * string. The length of the byte string is the length of the base prime `p`
791 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000792 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
793 * the format is the representation of the public key `y = g^x mod p` as a
794 * big-endian byte string. The length of the byte string is the length of the
795 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100796 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100797 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200798 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200799 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200800 * \param[out] data_length On success, the number of bytes
801 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100802 *
Gilles Peskine28538492018-07-11 17:34:00 +0200803 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100804 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200805 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200806 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200807 * The key is neither a public key nor a key pair.
808 * \retval #PSA_ERROR_NOT_SUPPORTED
809 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
810 * The size of the \p data buffer is too small. You can determine a
811 * sufficient buffer size by calling
812 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
813 * where \c type is the key type
814 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200815 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
816 * \retval #PSA_ERROR_HARDWARE_FAILURE
817 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300818 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300819 * The library has not been previously initialized by psa_crypto_init().
820 * It is implementation-dependent whether a failure to initialize
821 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100822 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100823psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100824 uint8_t *data,
825 size_t data_size,
826 size_t *data_length);
827
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100828/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100829 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100830 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000831 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100832 * This function is primarily useful to copy a key from one location
833 * to another, since it populates a key using the material from
834 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200835 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100836 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100837 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100838 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100839 *
Gilles Peskine20628592019-04-19 19:29:50 +0200840 * The resulting key may only be used in a way that conforms to
841 * both the policy of the original key and the policy specified in
842 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100843 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200844 * usage flags on the source policy and the usage flags in \p attributes.
845 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100846 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200847 * - If either of the policies allows an algorithm and the other policy
848 * allows a wildcard-based algorithm policy that includes this algorithm,
849 * the resulting key allows the same algorithm.
850 * - If the policies do not allow any algorithm in common, this function
851 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200852 *
Gilles Peskine20628592019-04-19 19:29:50 +0200853 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100854 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200855 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100856 * \param source_handle The key to copy. It must be a handle to an
857 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200858 * \param[in] attributes The attributes for the new key.
859 * They are used as follows:
860 * - The key type, key size and domain parameters
861 * are ignored. This information is copied
862 * from the source key.
863 * - The key location (the lifetime and, for
864 * persistent keys, the key identifier) is
865 * used directly.
866 * - The policy constraints (usage flags and
867 * algorithm policy) are combined from
868 * the source key and \p attributes so that
869 * both sets of restrictions apply, as
870 * described in the documentation of this function.
871 * \param[out] target_handle On success, a handle to the newly created key.
872 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200873 *
874 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100875 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200876 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200877 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200878 * This is an attempt to create a persistent key, and there is
879 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200880 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200881 * The lifetime or identifier in \p attributes are invalid.
882 * \retval #PSA_ERROR_INVALID_ARGUMENT
883 * The policy constraints on the source and specified in
884 * \p attributes are incompatible.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100885 * \retval #PSA_ERROR_NOT_PERMITTED
886 * The source key is not exportable and its lifetime does not
887 * allow copying it to the target's lifetime.
888 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
889 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200890 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
891 * \retval #PSA_ERROR_HARDWARE_FAILURE
892 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100893 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100894psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200895 const psa_key_attributes_t *attributes,
896 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100897
898/**@}*/
899
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100900/** \defgroup hash Message digests
901 * @{
902 */
903
Gilles Peskine69647a42019-01-14 20:18:12 +0100904/** Calculate the hash (digest) of a message.
905 *
906 * \note To verify the hash of a message against an
907 * expected value, use psa_hash_compare() instead.
908 *
909 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
910 * such that #PSA_ALG_IS_HASH(\p alg) is true).
911 * \param[in] input Buffer containing the message to hash.
912 * \param input_length Size of the \p input buffer in bytes.
913 * \param[out] hash Buffer where the hash is to be written.
914 * \param hash_size Size of the \p hash buffer in bytes.
915 * \param[out] hash_length On success, the number of bytes
916 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100917 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100918 *
919 * \retval #PSA_SUCCESS
920 * Success.
921 * \retval #PSA_ERROR_NOT_SUPPORTED
922 * \p alg is not supported or is not a hash algorithm.
923 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
924 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
925 * \retval #PSA_ERROR_HARDWARE_FAILURE
926 * \retval #PSA_ERROR_TAMPERING_DETECTED
927 */
928psa_status_t psa_hash_compute(psa_algorithm_t alg,
929 const uint8_t *input,
930 size_t input_length,
931 uint8_t *hash,
932 size_t hash_size,
933 size_t *hash_length);
934
935/** Calculate the hash (digest) of a message and compare it with a
936 * reference value.
937 *
938 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
939 * such that #PSA_ALG_IS_HASH(\p alg) is true).
940 * \param[in] input Buffer containing the message to hash.
941 * \param input_length Size of the \p input buffer in bytes.
942 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100943 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100944 *
945 * \retval #PSA_SUCCESS
946 * The expected hash is identical to the actual hash of the input.
947 * \retval #PSA_ERROR_INVALID_SIGNATURE
948 * The hash of the message was calculated successfully, but it
949 * differs from the expected hash.
950 * \retval #PSA_ERROR_NOT_SUPPORTED
951 * \p alg is not supported or is not a hash algorithm.
952 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
953 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
954 * \retval #PSA_ERROR_HARDWARE_FAILURE
955 * \retval #PSA_ERROR_TAMPERING_DETECTED
956 */
957psa_status_t psa_hash_compare(psa_algorithm_t alg,
958 const uint8_t *input,
959 size_t input_length,
960 const uint8_t *hash,
961 const size_t hash_length);
962
Gilles Peskine308b91d2018-02-08 09:47:44 +0100963/** The type of the state data structure for multipart hash operations.
964 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000965 * Before calling any function on a hash operation object, the application must
966 * initialize it by any of the following means:
967 * - Set the structure to all-bits-zero, for example:
968 * \code
969 * psa_hash_operation_t operation;
970 * memset(&operation, 0, sizeof(operation));
971 * \endcode
972 * - Initialize the structure to logical zero values, for example:
973 * \code
974 * psa_hash_operation_t operation = {0};
975 * \endcode
976 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
977 * for example:
978 * \code
979 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
980 * \endcode
981 * - Assign the result of the function psa_hash_operation_init()
982 * to the structure, for example:
983 * \code
984 * psa_hash_operation_t operation;
985 * operation = psa_hash_operation_init();
986 * \endcode
987 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100988 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100989 * make any assumptions about the content of this structure except
990 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100991typedef struct psa_hash_operation_s psa_hash_operation_t;
992
Jaeden Amero6a25b412019-01-04 11:47:44 +0000993/** \def PSA_HASH_OPERATION_INIT
994 *
995 * This macro returns a suitable initializer for a hash operation object
996 * of type #psa_hash_operation_t.
997 */
998#ifdef __DOXYGEN_ONLY__
999/* This is an example definition for documentation purposes.
1000 * Implementations should define a suitable value in `crypto_struct.h`.
1001 */
1002#define PSA_HASH_OPERATION_INIT {0}
1003#endif
1004
1005/** Return an initial value for a hash operation object.
1006 */
1007static psa_hash_operation_t psa_hash_operation_init(void);
1008
Gilles Peskinef45adda2019-01-14 18:29:18 +01001009/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001010 *
1011 * The sequence of operations to calculate a hash (message digest)
1012 * is as follows:
1013 * -# Allocate an operation object which will be passed to all the functions
1014 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +00001015 * -# Initialize the operation object with one of the methods described in the
1016 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001017 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001018 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001019 * of the message each time. The hash that is calculated is the hash
1020 * of the concatenation of these messages in order.
1021 * -# To calculate the hash, call psa_hash_finish().
1022 * To compare the hash with an expected value, call psa_hash_verify().
1023 *
1024 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +00001025 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001026 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001027 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001028 * eventually terminate the operation. The following events terminate an
1029 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001030 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001031 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001032 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001033 * \param[in,out] operation The operation object to set up. It must have
1034 * been initialized as per the documentation for
1035 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001036 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1037 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001038 *
Gilles Peskine28538492018-07-11 17:34:00 +02001039 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001040 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001041 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001042 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001043 * \retval #PSA_ERROR_BAD_STATE
1044 * The operation state is not valid (already set up and not
1045 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001046 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1047 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1048 * \retval #PSA_ERROR_HARDWARE_FAILURE
1049 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001050 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001051psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001052 psa_algorithm_t alg);
1053
Gilles Peskine308b91d2018-02-08 09:47:44 +01001054/** Add a message fragment to a multipart hash operation.
1055 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001056 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001057 *
1058 * If this function returns an error status, the operation becomes inactive.
1059 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001060 * \param[in,out] operation Active hash operation.
1061 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001062 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001063 *
Gilles Peskine28538492018-07-11 17:34:00 +02001064 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001065 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001066 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001067 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001068 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1069 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1070 * \retval #PSA_ERROR_HARDWARE_FAILURE
1071 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001072 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001073psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1074 const uint8_t *input,
1075 size_t input_length);
1076
Gilles Peskine308b91d2018-02-08 09:47:44 +01001077/** Finish the calculation of the hash of a message.
1078 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001079 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001080 * This function calculates the hash of the message formed by concatenating
1081 * the inputs passed to preceding calls to psa_hash_update().
1082 *
1083 * When this function returns, the operation becomes inactive.
1084 *
1085 * \warning Applications should not call this function if they expect
1086 * a specific value for the hash. Call psa_hash_verify() instead.
1087 * Beware that comparing integrity or authenticity data such as
1088 * hash values with a function such as \c memcmp is risky
1089 * because the time taken by the comparison may leak information
1090 * about the hashed data which could allow an attacker to guess
1091 * a valid hash and thereby bypass security controls.
1092 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001093 * \param[in,out] operation Active hash operation.
1094 * \param[out] hash Buffer where the hash is to be written.
1095 * \param hash_size Size of the \p hash buffer in bytes.
1096 * \param[out] hash_length On success, the number of bytes
1097 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001098 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001099 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001100 *
Gilles Peskine28538492018-07-11 17:34:00 +02001101 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001102 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001103 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001104 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001105 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001106 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001107 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001108 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001109 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1110 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1111 * \retval #PSA_ERROR_HARDWARE_FAILURE
1112 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001113 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001114psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1115 uint8_t *hash,
1116 size_t hash_size,
1117 size_t *hash_length);
1118
Gilles Peskine308b91d2018-02-08 09:47:44 +01001119/** Finish the calculation of the hash of a message and compare it with
1120 * an expected value.
1121 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001122 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001123 * This function calculates the hash of the message formed by concatenating
1124 * the inputs passed to preceding calls to psa_hash_update(). It then
1125 * compares the calculated hash with the expected hash passed as a
1126 * parameter to this function.
1127 *
1128 * When this function returns, the operation becomes inactive.
1129 *
Gilles Peskine19067982018-03-20 17:54:53 +01001130 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001131 * comparison between the actual hash and the expected hash is performed
1132 * in constant time.
1133 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001134 * \param[in,out] operation Active hash operation.
1135 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001136 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001137 *
Gilles Peskine28538492018-07-11 17:34:00 +02001138 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001139 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001140 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001141 * The hash of the message was calculated successfully, but it
1142 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001143 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001144 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001145 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1146 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1147 * \retval #PSA_ERROR_HARDWARE_FAILURE
1148 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001149 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001150psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1151 const uint8_t *hash,
1152 size_t hash_length);
1153
Gilles Peskine308b91d2018-02-08 09:47:44 +01001154/** Abort a hash operation.
1155 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001156 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001157 * \p operation structure itself. Once aborted, the operation object
1158 * can be reused for another operation by calling
1159 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001160 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001161 * You may call this function any time after the operation object has
1162 * been initialized by any of the following methods:
1163 * - A call to psa_hash_setup(), whether it succeeds or not.
1164 * - Initializing the \c struct to all-bits-zero.
1165 * - Initializing the \c struct to logical zeros, e.g.
1166 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001167 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001168 * In particular, calling psa_hash_abort() after the operation has been
1169 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1170 * psa_hash_verify() is safe and has no effect.
1171 *
1172 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001173 *
Gilles Peskine28538492018-07-11 17:34:00 +02001174 * \retval #PSA_SUCCESS
1175 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001176 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001177 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1178 * \retval #PSA_ERROR_HARDWARE_FAILURE
1179 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001180 */
1181psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001182
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001183/** Clone a hash operation.
1184 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001185 * This function copies the state of an ongoing hash operation to
1186 * a new operation object. In other words, this function is equivalent
1187 * to calling psa_hash_setup() on \p target_operation with the same
1188 * algorithm that \p source_operation was set up for, then
1189 * psa_hash_update() on \p target_operation with the same input that
1190 * that was passed to \p source_operation. After this function returns, the
1191 * two objects are independent, i.e. subsequent calls involving one of
1192 * the objects do not affect the other object.
1193 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001194 * \param[in] source_operation The active hash operation to clone.
1195 * \param[in,out] target_operation The operation object to set up.
1196 * It must be initialized but not active.
1197 *
1198 * \retval #PSA_SUCCESS
1199 * \retval #PSA_ERROR_BAD_STATE
1200 * \p source_operation is not an active hash operation.
1201 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001202 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001203 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1204 * \retval #PSA_ERROR_HARDWARE_FAILURE
1205 * \retval #PSA_ERROR_TAMPERING_DETECTED
1206 */
1207psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1208 psa_hash_operation_t *target_operation);
1209
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001210/**@}*/
1211
Gilles Peskine8c9def32018-02-08 10:02:12 +01001212/** \defgroup MAC Message authentication codes
1213 * @{
1214 */
1215
Gilles Peskine69647a42019-01-14 20:18:12 +01001216/** Calculate the MAC (message authentication code) of a message.
1217 *
1218 * \note To verify the MAC of a message against an
1219 * expected value, use psa_mac_verify() instead.
1220 * Beware that comparing integrity or authenticity data such as
1221 * MAC values with a function such as \c memcmp is risky
1222 * because the time taken by the comparison may leak information
1223 * about the MAC value which could allow an attacker to guess
1224 * a valid MAC and thereby bypass security controls.
1225 *
1226 * \param handle Handle to the key to use for the operation.
1227 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001228 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001229 * \param[in] input Buffer containing the input message.
1230 * \param input_length Size of the \p input buffer in bytes.
1231 * \param[out] mac Buffer where the MAC value is to be written.
1232 * \param mac_size Size of the \p mac buffer in bytes.
1233 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001234 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001235 *
1236 * \retval #PSA_SUCCESS
1237 * Success.
1238 * \retval #PSA_ERROR_INVALID_HANDLE
1239 * \retval #PSA_ERROR_EMPTY_SLOT
1240 * \retval #PSA_ERROR_NOT_PERMITTED
1241 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001242 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001243 * \retval #PSA_ERROR_NOT_SUPPORTED
1244 * \p alg is not supported or is not a MAC algorithm.
1245 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1246 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1247 * \retval #PSA_ERROR_HARDWARE_FAILURE
1248 * \retval #PSA_ERROR_TAMPERING_DETECTED
1249 * \retval #PSA_ERROR_BAD_STATE
1250 * The library has not been previously initialized by psa_crypto_init().
1251 * It is implementation-dependent whether a failure to initialize
1252 * results in this error code.
1253 */
1254psa_status_t psa_mac_compute(psa_key_handle_t handle,
1255 psa_algorithm_t alg,
1256 const uint8_t *input,
1257 size_t input_length,
1258 uint8_t *mac,
1259 size_t mac_size,
1260 size_t *mac_length);
1261
1262/** Calculate the MAC of a message and compare it with a reference value.
1263 *
1264 * \param handle Handle to the key to use for the operation.
1265 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001266 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001267 * \param[in] input Buffer containing the input message.
1268 * \param input_length Size of the \p input buffer in bytes.
1269 * \param[out] mac Buffer containing the expected MAC value.
1270 * \param mac_length Size of the \p mac buffer in bytes.
1271 *
1272 * \retval #PSA_SUCCESS
1273 * The expected MAC is identical to the actual MAC of the input.
1274 * \retval #PSA_ERROR_INVALID_SIGNATURE
1275 * The MAC of the message was calculated successfully, but it
1276 * differs from the expected value.
1277 * \retval #PSA_ERROR_INVALID_HANDLE
1278 * \retval #PSA_ERROR_EMPTY_SLOT
1279 * \retval #PSA_ERROR_NOT_PERMITTED
1280 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001281 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001282 * \retval #PSA_ERROR_NOT_SUPPORTED
1283 * \p alg is not supported or is not a MAC algorithm.
1284 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1285 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1286 * \retval #PSA_ERROR_HARDWARE_FAILURE
1287 * \retval #PSA_ERROR_TAMPERING_DETECTED
1288 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001289psa_status_t psa_mac_verify(psa_key_handle_t handle,
1290 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001291 const uint8_t *input,
1292 size_t input_length,
1293 const uint8_t *mac,
1294 const size_t mac_length);
1295
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001296/** The type of the state data structure for multipart MAC operations.
1297 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001298 * Before calling any function on a MAC operation object, the application must
1299 * initialize it by any of the following means:
1300 * - Set the structure to all-bits-zero, for example:
1301 * \code
1302 * psa_mac_operation_t operation;
1303 * memset(&operation, 0, sizeof(operation));
1304 * \endcode
1305 * - Initialize the structure to logical zero values, for example:
1306 * \code
1307 * psa_mac_operation_t operation = {0};
1308 * \endcode
1309 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1310 * for example:
1311 * \code
1312 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1313 * \endcode
1314 * - Assign the result of the function psa_mac_operation_init()
1315 * to the structure, for example:
1316 * \code
1317 * psa_mac_operation_t operation;
1318 * operation = psa_mac_operation_init();
1319 * \endcode
1320 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001321 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001322 * make any assumptions about the content of this structure except
1323 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001324typedef struct psa_mac_operation_s psa_mac_operation_t;
1325
Jaeden Amero769ce272019-01-04 11:48:03 +00001326/** \def PSA_MAC_OPERATION_INIT
1327 *
1328 * This macro returns a suitable initializer for a MAC operation object of type
1329 * #psa_mac_operation_t.
1330 */
1331#ifdef __DOXYGEN_ONLY__
1332/* This is an example definition for documentation purposes.
1333 * Implementations should define a suitable value in `crypto_struct.h`.
1334 */
1335#define PSA_MAC_OPERATION_INIT {0}
1336#endif
1337
1338/** Return an initial value for a MAC operation object.
1339 */
1340static psa_mac_operation_t psa_mac_operation_init(void);
1341
Gilles Peskinef45adda2019-01-14 18:29:18 +01001342/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001343 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001344 * This function sets up the calculation of the MAC
1345 * (message authentication code) of a byte string.
1346 * To verify the MAC of a message against an
1347 * expected value, use psa_mac_verify_setup() instead.
1348 *
1349 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001350 * -# Allocate an operation object which will be passed to all the functions
1351 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001352 * -# Initialize the operation object with one of the methods described in the
1353 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001354 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001355 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1356 * of the message each time. The MAC that is calculated is the MAC
1357 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001358 * -# At the end of the message, call psa_mac_sign_finish() to finish
1359 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001360 *
1361 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001362 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001363 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001364 * After a successful call to psa_mac_sign_setup(), the application must
1365 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001366 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001367 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001368 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001369 * \param[in,out] operation The operation object to set up. It must have
1370 * been initialized as per the documentation for
1371 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001372 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001373 * It must remain valid until the operation
1374 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001375 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001376 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001377 *
Gilles Peskine28538492018-07-11 17:34:00 +02001378 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001379 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001380 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001381 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001382 * \retval #PSA_ERROR_NOT_PERMITTED
1383 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001384 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001385 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001386 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001387 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1388 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1389 * \retval #PSA_ERROR_HARDWARE_FAILURE
1390 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001391 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001392 * The operation state is not valid (already set up and not
1393 * subsequently completed).
1394 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001395 * The library has not been previously initialized by psa_crypto_init().
1396 * It is implementation-dependent whether a failure to initialize
1397 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001398 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001399psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001400 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001401 psa_algorithm_t alg);
1402
Gilles Peskinef45adda2019-01-14 18:29:18 +01001403/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001404 *
1405 * This function sets up the verification of the MAC
1406 * (message authentication code) of a byte string against an expected value.
1407 *
1408 * The sequence of operations to verify a MAC is as follows:
1409 * -# Allocate an operation object which will be passed to all the functions
1410 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001411 * -# Initialize the operation object with one of the methods described in the
1412 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001413 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001414 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1415 * of the message each time. The MAC that is calculated is the MAC
1416 * of the concatenation of these messages in order.
1417 * -# At the end of the message, call psa_mac_verify_finish() to finish
1418 * calculating the actual MAC of the message and verify it against
1419 * the expected value.
1420 *
1421 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001422 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001423 *
1424 * After a successful call to psa_mac_verify_setup(), the application must
1425 * eventually terminate the operation through one of the following methods:
1426 * - A failed call to psa_mac_update().
1427 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1428 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001429 * \param[in,out] operation The operation object to set up. It must have
1430 * been initialized as per the documentation for
1431 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001432 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001433 * It must remain valid until the operation
1434 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001435 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1436 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001437 *
Gilles Peskine28538492018-07-11 17:34:00 +02001438 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001439 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001440 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001441 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001442 * \retval #PSA_ERROR_NOT_PERMITTED
1443 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001444 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001445 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001446 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001447 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1448 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1449 * \retval #PSA_ERROR_HARDWARE_FAILURE
1450 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001451 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001452 * The operation state is not valid (already set up and not
1453 * subsequently completed).
1454 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001455 * The library has not been previously initialized by psa_crypto_init().
1456 * It is implementation-dependent whether a failure to initialize
1457 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001458 */
1459psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001460 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001461 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001462
Gilles Peskinedcd14942018-07-12 00:30:52 +02001463/** Add a message fragment to a multipart MAC operation.
1464 *
1465 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1466 * before calling this function.
1467 *
1468 * If this function returns an error status, the operation becomes inactive.
1469 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001470 * \param[in,out] operation Active MAC operation.
1471 * \param[in] input Buffer containing the message fragment to add to
1472 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001473 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001474 *
1475 * \retval #PSA_SUCCESS
1476 * Success.
1477 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001478 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001479 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1480 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1481 * \retval #PSA_ERROR_HARDWARE_FAILURE
1482 * \retval #PSA_ERROR_TAMPERING_DETECTED
1483 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001484psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1485 const uint8_t *input,
1486 size_t input_length);
1487
Gilles Peskinedcd14942018-07-12 00:30:52 +02001488/** Finish the calculation of the MAC of a message.
1489 *
1490 * The application must call psa_mac_sign_setup() before calling this function.
1491 * This function calculates the MAC of the message formed by concatenating
1492 * the inputs passed to preceding calls to psa_mac_update().
1493 *
1494 * When this function returns, the operation becomes inactive.
1495 *
1496 * \warning Applications should not call this function if they expect
1497 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1498 * Beware that comparing integrity or authenticity data such as
1499 * MAC values with a function such as \c memcmp is risky
1500 * because the time taken by the comparison may leak information
1501 * about the MAC value which could allow an attacker to guess
1502 * a valid MAC and thereby bypass security controls.
1503 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001504 * \param[in,out] operation Active MAC operation.
1505 * \param[out] mac Buffer where the MAC value is to be written.
1506 * \param mac_size Size of the \p mac buffer in bytes.
1507 * \param[out] mac_length On success, the number of bytes
1508 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001509 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001510 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001511 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001512 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001513 *
1514 * \retval #PSA_SUCCESS
1515 * Success.
1516 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001517 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001518 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001519 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001520 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1521 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1522 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1523 * \retval #PSA_ERROR_HARDWARE_FAILURE
1524 * \retval #PSA_ERROR_TAMPERING_DETECTED
1525 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001526psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1527 uint8_t *mac,
1528 size_t mac_size,
1529 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001530
Gilles Peskinedcd14942018-07-12 00:30:52 +02001531/** Finish the calculation of the MAC of a message and compare it with
1532 * an expected value.
1533 *
1534 * The application must call psa_mac_verify_setup() before calling this function.
1535 * This function calculates the MAC of the message formed by concatenating
1536 * the inputs passed to preceding calls to psa_mac_update(). It then
1537 * compares the calculated MAC with the expected MAC passed as a
1538 * parameter to this function.
1539 *
1540 * When this function returns, the operation becomes inactive.
1541 *
1542 * \note Implementations shall make the best effort to ensure that the
1543 * comparison between the actual MAC and the expected MAC is performed
1544 * in constant time.
1545 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001546 * \param[in,out] operation Active MAC operation.
1547 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001548 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001549 *
1550 * \retval #PSA_SUCCESS
1551 * The expected MAC is identical to the actual MAC of the message.
1552 * \retval #PSA_ERROR_INVALID_SIGNATURE
1553 * The MAC of the message was calculated successfully, but it
1554 * differs from the expected MAC.
1555 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001556 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001557 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1558 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1559 * \retval #PSA_ERROR_HARDWARE_FAILURE
1560 * \retval #PSA_ERROR_TAMPERING_DETECTED
1561 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001562psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1563 const uint8_t *mac,
1564 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001565
Gilles Peskinedcd14942018-07-12 00:30:52 +02001566/** Abort a MAC operation.
1567 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001568 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001569 * \p operation structure itself. Once aborted, the operation object
1570 * can be reused for another operation by calling
1571 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001572 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001573 * You may call this function any time after the operation object has
1574 * been initialized by any of the following methods:
1575 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1576 * it succeeds or not.
1577 * - Initializing the \c struct to all-bits-zero.
1578 * - Initializing the \c struct to logical zeros, e.g.
1579 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001580 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001581 * In particular, calling psa_mac_abort() after the operation has been
1582 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1583 * psa_mac_verify_finish() is safe and has no effect.
1584 *
1585 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001586 *
1587 * \retval #PSA_SUCCESS
1588 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001589 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001590 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1591 * \retval #PSA_ERROR_HARDWARE_FAILURE
1592 * \retval #PSA_ERROR_TAMPERING_DETECTED
1593 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001594psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1595
1596/**@}*/
1597
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001598/** \defgroup cipher Symmetric ciphers
1599 * @{
1600 */
1601
Gilles Peskine69647a42019-01-14 20:18:12 +01001602/** Encrypt a message using a symmetric cipher.
1603 *
1604 * This function encrypts a message with a random IV (initialization
1605 * vector).
1606 *
1607 * \param handle Handle to the key to use for the operation.
1608 * It must remain valid until the operation
1609 * terminates.
1610 * \param alg The cipher algorithm to compute
1611 * (\c PSA_ALG_XXX value such that
1612 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1613 * \param[in] input Buffer containing the message to encrypt.
1614 * \param input_length Size of the \p input buffer in bytes.
1615 * \param[out] output Buffer where the output is to be written.
1616 * The output contains the IV followed by
1617 * the ciphertext proper.
1618 * \param output_size Size of the \p output buffer in bytes.
1619 * \param[out] output_length On success, the number of bytes
1620 * that make up the output.
1621 *
1622 * \retval #PSA_SUCCESS
1623 * Success.
1624 * \retval #PSA_ERROR_INVALID_HANDLE
1625 * \retval #PSA_ERROR_EMPTY_SLOT
1626 * \retval #PSA_ERROR_NOT_PERMITTED
1627 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001628 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001629 * \retval #PSA_ERROR_NOT_SUPPORTED
1630 * \p alg is not supported or is not a cipher algorithm.
1631 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1632 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1633 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1634 * \retval #PSA_ERROR_HARDWARE_FAILURE
1635 * \retval #PSA_ERROR_TAMPERING_DETECTED
1636 */
1637psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1638 psa_algorithm_t alg,
1639 const uint8_t *input,
1640 size_t input_length,
1641 uint8_t *output,
1642 size_t output_size,
1643 size_t *output_length);
1644
1645/** Decrypt a message using a symmetric cipher.
1646 *
1647 * This function decrypts a message encrypted with a symmetric cipher.
1648 *
1649 * \param handle Handle to the key to use for the operation.
1650 * It must remain valid until the operation
1651 * terminates.
1652 * \param alg The cipher algorithm to compute
1653 * (\c PSA_ALG_XXX value such that
1654 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1655 * \param[in] input Buffer containing the message to decrypt.
1656 * This consists of the IV followed by the
1657 * ciphertext proper.
1658 * \param input_length Size of the \p input buffer in bytes.
1659 * \param[out] output Buffer where the plaintext is to be written.
1660 * \param output_size Size of the \p output buffer in bytes.
1661 * \param[out] output_length On success, the number of bytes
1662 * that make up the output.
1663 *
1664 * \retval #PSA_SUCCESS
1665 * Success.
1666 * \retval #PSA_ERROR_INVALID_HANDLE
1667 * \retval #PSA_ERROR_EMPTY_SLOT
1668 * \retval #PSA_ERROR_NOT_PERMITTED
1669 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001670 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001671 * \retval #PSA_ERROR_NOT_SUPPORTED
1672 * \p alg is not supported or is not a cipher algorithm.
1673 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1674 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1675 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1676 * \retval #PSA_ERROR_HARDWARE_FAILURE
1677 * \retval #PSA_ERROR_TAMPERING_DETECTED
1678 */
1679psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1680 psa_algorithm_t alg,
1681 const uint8_t *input,
1682 size_t input_length,
1683 uint8_t *output,
1684 size_t output_size,
1685 size_t *output_length);
1686
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001687/** The type of the state data structure for multipart cipher operations.
1688 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001689 * Before calling any function on a cipher operation object, the application
1690 * must initialize it by any of the following means:
1691 * - Set the structure to all-bits-zero, for example:
1692 * \code
1693 * psa_cipher_operation_t operation;
1694 * memset(&operation, 0, sizeof(operation));
1695 * \endcode
1696 * - Initialize the structure to logical zero values, for example:
1697 * \code
1698 * psa_cipher_operation_t operation = {0};
1699 * \endcode
1700 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1701 * for example:
1702 * \code
1703 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1704 * \endcode
1705 * - Assign the result of the function psa_cipher_operation_init()
1706 * to the structure, for example:
1707 * \code
1708 * psa_cipher_operation_t operation;
1709 * operation = psa_cipher_operation_init();
1710 * \endcode
1711 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001712 * This is an implementation-defined \c struct. Applications should not
1713 * make any assumptions about the content of this structure except
1714 * as directed by the documentation of a specific implementation. */
1715typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1716
Jaeden Amero5bae2272019-01-04 11:48:27 +00001717/** \def PSA_CIPHER_OPERATION_INIT
1718 *
1719 * This macro returns a suitable initializer for a cipher operation object of
1720 * type #psa_cipher_operation_t.
1721 */
1722#ifdef __DOXYGEN_ONLY__
1723/* This is an example definition for documentation purposes.
1724 * Implementations should define a suitable value in `crypto_struct.h`.
1725 */
1726#define PSA_CIPHER_OPERATION_INIT {0}
1727#endif
1728
1729/** Return an initial value for a cipher operation object.
1730 */
1731static psa_cipher_operation_t psa_cipher_operation_init(void);
1732
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001733/** Set the key for a multipart symmetric encryption operation.
1734 *
1735 * The sequence of operations to encrypt a message with a symmetric cipher
1736 * is as follows:
1737 * -# Allocate an operation object which will be passed to all the functions
1738 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001739 * -# Initialize the operation object with one of the methods described in the
1740 * documentation for #psa_cipher_operation_t, e.g.
1741 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001742 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001743 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001744 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001745 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001746 * requires a specific IV value.
1747 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1748 * of the message each time.
1749 * -# Call psa_cipher_finish().
1750 *
1751 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001752 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001753 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001754 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001755 * eventually terminate the operation. The following events terminate an
1756 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001757 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001758 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001759 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001760 * \param[in,out] operation The operation object to set up. It must have
1761 * been initialized as per the documentation for
1762 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001763 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001764 * It must remain valid until the operation
1765 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001766 * \param alg The cipher algorithm to compute
1767 * (\c PSA_ALG_XXX value such that
1768 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001769 *
Gilles Peskine28538492018-07-11 17:34:00 +02001770 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001771 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001772 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001773 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001774 * \retval #PSA_ERROR_NOT_PERMITTED
1775 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001776 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001777 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001778 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001779 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1780 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1781 * \retval #PSA_ERROR_HARDWARE_FAILURE
1782 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001783 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001784 * The operation state is not valid (already set up and not
1785 * subsequently completed).
1786 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001787 * The library has not been previously initialized by psa_crypto_init().
1788 * It is implementation-dependent whether a failure to initialize
1789 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001790 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001791psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001792 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001793 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001794
1795/** Set the key for a multipart symmetric decryption operation.
1796 *
1797 * The sequence of operations to decrypt a message with a symmetric cipher
1798 * is as follows:
1799 * -# Allocate an operation object which will be passed to all the functions
1800 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001801 * -# Initialize the operation object with one of the methods described in the
1802 * documentation for #psa_cipher_operation_t, e.g.
1803 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001804 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001805 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001806 * decryption. If the IV is prepended to the ciphertext, you can call
1807 * psa_cipher_update() on a buffer containing the IV followed by the
1808 * beginning of the message.
1809 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1810 * of the message each time.
1811 * -# Call psa_cipher_finish().
1812 *
1813 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001814 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001815 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001816 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001817 * eventually terminate the operation. The following events terminate an
1818 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001819 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001820 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001821 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001822 * \param[in,out] operation The operation object to set up. It must have
1823 * been initialized as per the documentation for
1824 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001825 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001826 * It must remain valid until the operation
1827 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001828 * \param alg The cipher algorithm to compute
1829 * (\c PSA_ALG_XXX value such that
1830 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001831 *
Gilles Peskine28538492018-07-11 17:34:00 +02001832 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001833 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001834 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001835 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001836 * \retval #PSA_ERROR_NOT_PERMITTED
1837 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001838 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001839 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001840 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001841 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1842 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1843 * \retval #PSA_ERROR_HARDWARE_FAILURE
1844 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001845 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001846 * The operation state is not valid (already set up and not
1847 * subsequently completed).
1848 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001849 * The library has not been previously initialized by psa_crypto_init().
1850 * It is implementation-dependent whether a failure to initialize
1851 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001852 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001853psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001854 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001855 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001856
Gilles Peskinedcd14942018-07-12 00:30:52 +02001857/** Generate an IV for a symmetric encryption operation.
1858 *
1859 * This function generates a random IV (initialization vector), nonce
1860 * or initial counter value for the encryption operation as appropriate
1861 * for the chosen algorithm, key type and key size.
1862 *
1863 * The application must call psa_cipher_encrypt_setup() before
1864 * calling this function.
1865 *
1866 * If this function returns an error status, the operation becomes inactive.
1867 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001868 * \param[in,out] operation Active cipher operation.
1869 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001870 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001871 * \param[out] iv_length On success, the number of bytes of the
1872 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001873 *
1874 * \retval #PSA_SUCCESS
1875 * Success.
1876 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001877 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001878 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001879 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001880 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1881 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1882 * \retval #PSA_ERROR_HARDWARE_FAILURE
1883 * \retval #PSA_ERROR_TAMPERING_DETECTED
1884 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001885psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1886 unsigned char *iv,
1887 size_t iv_size,
1888 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001889
Gilles Peskinedcd14942018-07-12 00:30:52 +02001890/** Set the IV for a symmetric encryption or decryption operation.
1891 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001892 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001893 * or initial counter value for the encryption or decryption operation.
1894 *
1895 * The application must call psa_cipher_encrypt_setup() before
1896 * calling this function.
1897 *
1898 * If this function returns an error status, the operation becomes inactive.
1899 *
1900 * \note When encrypting, applications should use psa_cipher_generate_iv()
1901 * instead of this function, unless implementing a protocol that requires
1902 * a non-random IV.
1903 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001904 * \param[in,out] operation Active cipher operation.
1905 * \param[in] iv Buffer containing the IV to use.
1906 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001907 *
1908 * \retval #PSA_SUCCESS
1909 * Success.
1910 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001911 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001912 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001913 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001914 * or the chosen algorithm does not use an IV.
1915 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1916 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1917 * \retval #PSA_ERROR_HARDWARE_FAILURE
1918 * \retval #PSA_ERROR_TAMPERING_DETECTED
1919 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001920psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1921 const unsigned char *iv,
1922 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001923
Gilles Peskinedcd14942018-07-12 00:30:52 +02001924/** Encrypt or decrypt a message fragment in an active cipher operation.
1925 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001926 * Before calling this function, you must:
1927 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1928 * The choice of setup function determines whether this function
1929 * encrypts or decrypts its input.
1930 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1931 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001932 *
1933 * If this function returns an error status, the operation becomes inactive.
1934 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001935 * \param[in,out] operation Active cipher operation.
1936 * \param[in] input Buffer containing the message fragment to
1937 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001938 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001939 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001940 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001941 * \param[out] output_length On success, the number of bytes
1942 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001943 *
1944 * \retval #PSA_SUCCESS
1945 * Success.
1946 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001947 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001948 * not set, or already completed).
1949 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1950 * The size of the \p output buffer is too small.
1951 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1952 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1953 * \retval #PSA_ERROR_HARDWARE_FAILURE
1954 * \retval #PSA_ERROR_TAMPERING_DETECTED
1955 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001956psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1957 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001958 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001959 unsigned char *output,
1960 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001961 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001962
Gilles Peskinedcd14942018-07-12 00:30:52 +02001963/** Finish encrypting or decrypting a message in a cipher operation.
1964 *
1965 * The application must call psa_cipher_encrypt_setup() or
1966 * psa_cipher_decrypt_setup() before calling this function. The choice
1967 * of setup function determines whether this function encrypts or
1968 * decrypts its input.
1969 *
1970 * This function finishes the encryption or decryption of the message
1971 * formed by concatenating the inputs passed to preceding calls to
1972 * psa_cipher_update().
1973 *
1974 * When this function returns, the operation becomes inactive.
1975 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001976 * \param[in,out] operation Active cipher operation.
1977 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001978 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001979 * \param[out] output_length On success, the number of bytes
1980 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001981 *
1982 * \retval #PSA_SUCCESS
1983 * Success.
1984 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001985 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001986 * not set, or already completed).
1987 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1988 * The size of the \p output buffer is too small.
1989 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1990 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1991 * \retval #PSA_ERROR_HARDWARE_FAILURE
1992 * \retval #PSA_ERROR_TAMPERING_DETECTED
1993 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001994psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001995 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001996 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001997 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001998
Gilles Peskinedcd14942018-07-12 00:30:52 +02001999/** Abort a cipher operation.
2000 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002001 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002002 * \p operation structure itself. Once aborted, the operation object
2003 * can be reused for another operation by calling
2004 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002005 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002006 * You may call this function any time after the operation object has
2007 * been initialized by any of the following methods:
2008 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2009 * whether it succeeds or not.
2010 * - Initializing the \c struct to all-bits-zero.
2011 * - Initializing the \c struct to logical zeros, e.g.
2012 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002013 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002014 * In particular, calling psa_cipher_abort() after the operation has been
2015 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2016 * is safe and has no effect.
2017 *
2018 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002019 *
2020 * \retval #PSA_SUCCESS
2021 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002022 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002023 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2024 * \retval #PSA_ERROR_HARDWARE_FAILURE
2025 * \retval #PSA_ERROR_TAMPERING_DETECTED
2026 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002027psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2028
2029/**@}*/
2030
Gilles Peskine3b555712018-03-03 21:27:57 +01002031/** \defgroup aead Authenticated encryption with associated data (AEAD)
2032 * @{
2033 */
2034
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002035/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002036 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002037 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002038 * \param alg The AEAD algorithm to compute
2039 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002040 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002041 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002042 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002043 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002044 * but not encrypted.
2045 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002046 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002047 * encrypted.
2048 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002049 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002050 * encrypted data. The additional data is not
2051 * part of this output. For algorithms where the
2052 * encrypted data and the authentication tag
2053 * are defined as separate outputs, the
2054 * authentication tag is appended to the
2055 * encrypted data.
2056 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2057 * This must be at least
2058 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2059 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002060 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002061 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002062 *
Gilles Peskine28538492018-07-11 17:34:00 +02002063 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002064 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002065 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002066 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002067 * \retval #PSA_ERROR_NOT_PERMITTED
2068 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002069 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002070 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002071 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002072 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2073 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2074 * \retval #PSA_ERROR_HARDWARE_FAILURE
2075 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002076 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002077 * The library has not been previously initialized by psa_crypto_init().
2078 * It is implementation-dependent whether a failure to initialize
2079 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002080 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002081psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002082 psa_algorithm_t alg,
2083 const uint8_t *nonce,
2084 size_t nonce_length,
2085 const uint8_t *additional_data,
2086 size_t additional_data_length,
2087 const uint8_t *plaintext,
2088 size_t plaintext_length,
2089 uint8_t *ciphertext,
2090 size_t ciphertext_size,
2091 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002092
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002093/** Process an authenticated decryption 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 has been 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] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002105 * encrypted. For algorithms where the
2106 * encrypted data and the authentication tag
2107 * are defined as separate inputs, the buffer
2108 * must contain the encrypted data followed
2109 * by the authentication tag.
2110 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002111 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002112 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2113 * This must be at least
2114 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2115 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002116 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002117 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002118 *
Gilles Peskine28538492018-07-11 17:34:00 +02002119 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002120 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002121 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002122 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002123 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002124 * The ciphertext is not authentic.
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_decrypt(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 *ciphertext,
2146 size_t ciphertext_length,
2147 uint8_t *plaintext,
2148 size_t plaintext_size,
2149 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002150
Gilles Peskine30a9e412019-01-14 18:36:12 +01002151/** The type of the state data structure for multipart AEAD operations.
2152 *
2153 * Before calling any function on an AEAD operation object, the application
2154 * must initialize it by any of the following means:
2155 * - Set the structure to all-bits-zero, for example:
2156 * \code
2157 * psa_aead_operation_t operation;
2158 * memset(&operation, 0, sizeof(operation));
2159 * \endcode
2160 * - Initialize the structure to logical zero values, for example:
2161 * \code
2162 * psa_aead_operation_t operation = {0};
2163 * \endcode
2164 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2165 * for example:
2166 * \code
2167 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2168 * \endcode
2169 * - Assign the result of the function psa_aead_operation_init()
2170 * to the structure, for example:
2171 * \code
2172 * psa_aead_operation_t operation;
2173 * operation = psa_aead_operation_init();
2174 * \endcode
2175 *
2176 * This is an implementation-defined \c struct. Applications should not
2177 * make any assumptions about the content of this structure except
2178 * as directed by the documentation of a specific implementation. */
2179typedef struct psa_aead_operation_s psa_aead_operation_t;
2180
2181/** \def PSA_AEAD_OPERATION_INIT
2182 *
2183 * This macro returns a suitable initializer for an AEAD operation object of
2184 * type #psa_aead_operation_t.
2185 */
2186#ifdef __DOXYGEN_ONLY__
2187/* This is an example definition for documentation purposes.
2188 * Implementations should define a suitable value in `crypto_struct.h`.
2189 */
2190#define PSA_AEAD_OPERATION_INIT {0}
2191#endif
2192
2193/** Return an initial value for an AEAD operation object.
2194 */
2195static psa_aead_operation_t psa_aead_operation_init(void);
2196
2197/** Set the key for a multipart authenticated encryption operation.
2198 *
2199 * The sequence of operations to encrypt a message with authentication
2200 * is as follows:
2201 * -# Allocate an operation object which will be passed to all the functions
2202 * listed here.
2203 * -# Initialize the operation object with one of the methods described in the
2204 * documentation for #psa_aead_operation_t, e.g.
2205 * PSA_AEAD_OPERATION_INIT.
2206 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002207 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2208 * inputs to the subsequent calls to psa_aead_update_ad() and
2209 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2210 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002211 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2212 * generate or set the nonce. You should use
2213 * psa_aead_generate_nonce() unless the protocol you are implementing
2214 * requires a specific nonce value.
2215 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2216 * of the non-encrypted additional authenticated data each time.
2217 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002218 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002219 * -# Call psa_aead_finish().
2220 *
2221 * The application may call psa_aead_abort() at any time after the operation
2222 * has been initialized.
2223 *
2224 * After a successful call to psa_aead_encrypt_setup(), the application must
2225 * eventually terminate the operation. The following events terminate an
2226 * operation:
2227 * - A failed call to any of the \c psa_aead_xxx functions.
2228 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2229 *
2230 * \param[in,out] operation The operation object to set up. It must have
2231 * been initialized as per the documentation for
2232 * #psa_aead_operation_t and not yet in use.
2233 * \param handle Handle to the key to use for the operation.
2234 * It must remain valid until the operation
2235 * terminates.
2236 * \param alg The AEAD algorithm to compute
2237 * (\c PSA_ALG_XXX value such that
2238 * #PSA_ALG_IS_AEAD(\p alg) is true).
2239 *
2240 * \retval #PSA_SUCCESS
2241 * Success.
2242 * \retval #PSA_ERROR_INVALID_HANDLE
2243 * \retval #PSA_ERROR_EMPTY_SLOT
2244 * \retval #PSA_ERROR_NOT_PERMITTED
2245 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002246 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002247 * \retval #PSA_ERROR_NOT_SUPPORTED
2248 * \p alg is not supported or is not an AEAD algorithm.
2249 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2250 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2251 * \retval #PSA_ERROR_HARDWARE_FAILURE
2252 * \retval #PSA_ERROR_TAMPERING_DETECTED
2253 * \retval #PSA_ERROR_BAD_STATE
2254 * The library has not been previously initialized by psa_crypto_init().
2255 * It is implementation-dependent whether a failure to initialize
2256 * results in this error code.
2257 */
2258psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2259 psa_key_handle_t handle,
2260 psa_algorithm_t alg);
2261
2262/** Set the key for a multipart authenticated decryption operation.
2263 *
2264 * The sequence of operations to decrypt a message with authentication
2265 * is as follows:
2266 * -# Allocate an operation object which will be passed to all the functions
2267 * listed here.
2268 * -# Initialize the operation object with one of the methods described in the
2269 * documentation for #psa_aead_operation_t, e.g.
2270 * PSA_AEAD_OPERATION_INIT.
2271 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002272 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2273 * inputs to the subsequent calls to psa_aead_update_ad() and
2274 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2275 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002276 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2277 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2278 * of the non-encrypted additional authenticated data each time.
2279 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002280 * of the ciphertext to decrypt each time.
2281 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002282 *
2283 * The application may call psa_aead_abort() at any time after the operation
2284 * has been initialized.
2285 *
2286 * After a successful call to psa_aead_decrypt_setup(), the application must
2287 * eventually terminate the operation. The following events terminate an
2288 * operation:
2289 * - A failed call to any of the \c psa_aead_xxx functions.
2290 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2291 *
2292 * \param[in,out] operation The operation object to set up. It must have
2293 * been initialized as per the documentation for
2294 * #psa_aead_operation_t and not yet in use.
2295 * \param handle Handle to the key to use for the operation.
2296 * It must remain valid until the operation
2297 * terminates.
2298 * \param alg The AEAD algorithm to compute
2299 * (\c PSA_ALG_XXX value such that
2300 * #PSA_ALG_IS_AEAD(\p alg) is true).
2301 *
2302 * \retval #PSA_SUCCESS
2303 * Success.
2304 * \retval #PSA_ERROR_INVALID_HANDLE
2305 * \retval #PSA_ERROR_EMPTY_SLOT
2306 * \retval #PSA_ERROR_NOT_PERMITTED
2307 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002308 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002309 * \retval #PSA_ERROR_NOT_SUPPORTED
2310 * \p alg is not supported or is not an AEAD algorithm.
2311 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2312 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2313 * \retval #PSA_ERROR_HARDWARE_FAILURE
2314 * \retval #PSA_ERROR_TAMPERING_DETECTED
2315 * \retval #PSA_ERROR_BAD_STATE
2316 * The library has not been previously initialized by psa_crypto_init().
2317 * It is implementation-dependent whether a failure to initialize
2318 * results in this error code.
2319 */
2320psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2321 psa_key_handle_t handle,
2322 psa_algorithm_t alg);
2323
2324/** Generate a random nonce for an authenticated encryption operation.
2325 *
2326 * This function generates a random nonce for the authenticated encryption
2327 * operation with an appropriate size for the chosen algorithm, key type
2328 * and key size.
2329 *
2330 * The application must call psa_aead_encrypt_setup() before
2331 * calling this function.
2332 *
2333 * If this function returns an error status, the operation becomes inactive.
2334 *
2335 * \param[in,out] operation Active AEAD operation.
2336 * \param[out] nonce Buffer where the generated nonce is to be
2337 * written.
2338 * \param nonce_size Size of the \p nonce buffer in bytes.
2339 * \param[out] nonce_length On success, the number of bytes of the
2340 * generated nonce.
2341 *
2342 * \retval #PSA_SUCCESS
2343 * Success.
2344 * \retval #PSA_ERROR_BAD_STATE
2345 * The operation state is not valid (not set up, or nonce already set).
2346 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2347 * The size of the \p nonce buffer is too small.
2348 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2349 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2350 * \retval #PSA_ERROR_HARDWARE_FAILURE
2351 * \retval #PSA_ERROR_TAMPERING_DETECTED
2352 */
2353psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2354 unsigned char *nonce,
2355 size_t nonce_size,
2356 size_t *nonce_length);
2357
2358/** Set the nonce for an authenticated encryption or decryption operation.
2359 *
2360 * This function sets the nonce for the authenticated
2361 * encryption or decryption operation.
2362 *
2363 * The application must call psa_aead_encrypt_setup() before
2364 * calling this function.
2365 *
2366 * If this function returns an error status, the operation becomes inactive.
2367 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002368 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002369 * instead of this function, unless implementing a protocol that requires
2370 * a non-random IV.
2371 *
2372 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002373 * \param[in] nonce Buffer containing the nonce to use.
2374 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002375 *
2376 * \retval #PSA_SUCCESS
2377 * Success.
2378 * \retval #PSA_ERROR_BAD_STATE
2379 * The operation state is not valid (not set up, or nonce already set).
2380 * \retval #PSA_ERROR_INVALID_ARGUMENT
2381 * The size of \p nonce is not acceptable for the chosen algorithm.
2382 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2383 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2384 * \retval #PSA_ERROR_HARDWARE_FAILURE
2385 * \retval #PSA_ERROR_TAMPERING_DETECTED
2386 */
2387psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2388 const unsigned char *nonce,
2389 size_t nonce_length);
2390
Gilles Peskinebc59c852019-01-17 15:26:08 +01002391/** Declare the lengths of the message and additional data for AEAD.
2392 *
2393 * The application must call this function before calling
2394 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2395 * the operation requires it. If the algorithm does not require it,
2396 * calling this function is optional, but if this function is called
2397 * then the implementation must enforce the lengths.
2398 *
2399 * You may call this function before or after setting the nonce with
2400 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2401 *
2402 * - For #PSA_ALG_CCM, calling this function is required.
2403 * - For the other AEAD algorithms defined in this specification, calling
2404 * this function is not required.
2405 * - For vendor-defined algorithm, refer to the vendor documentation.
2406 *
2407 * \param[in,out] operation Active AEAD operation.
2408 * \param ad_length Size of the non-encrypted additional
2409 * authenticated data in bytes.
2410 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2411 *
2412 * \retval #PSA_SUCCESS
2413 * Success.
2414 * \retval #PSA_ERROR_BAD_STATE
2415 * The operation state is not valid (not set up, already completed,
2416 * or psa_aead_update_ad() or psa_aead_update() already called).
2417 * \retval #PSA_ERROR_INVALID_ARGUMENT
2418 * At least one of the lengths is not acceptable for the chosen
2419 * algorithm.
2420 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2421 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2422 * \retval #PSA_ERROR_HARDWARE_FAILURE
2423 * \retval #PSA_ERROR_TAMPERING_DETECTED
2424 */
2425psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2426 size_t ad_length,
2427 size_t plaintext_length);
2428
Gilles Peskine30a9e412019-01-14 18:36:12 +01002429/** Pass additional data to an active AEAD operation.
2430 *
2431 * Additional data is authenticated, but not encrypted.
2432 *
2433 * You may call this function multiple times to pass successive fragments
2434 * of the additional data. You may not call this function after passing
2435 * data to encrypt or decrypt with psa_aead_update().
2436 *
2437 * Before calling this function, you must:
2438 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2439 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2440 *
2441 * If this function returns an error status, the operation becomes inactive.
2442 *
2443 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2444 * there is no guarantee that the input is valid. Therefore, until
2445 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2446 * treat the input as untrusted and prepare to undo any action that
2447 * depends on the input if psa_aead_verify() returns an error status.
2448 *
2449 * \param[in,out] operation Active AEAD operation.
2450 * \param[in] input Buffer containing the fragment of
2451 * additional data.
2452 * \param input_length Size of the \p input buffer in bytes.
2453 *
2454 * \retval #PSA_SUCCESS
2455 * Success.
2456 * \retval #PSA_ERROR_BAD_STATE
2457 * The operation state is not valid (not set up, nonce not set,
2458 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002459 * \retval #PSA_ERROR_INVALID_ARGUMENT
2460 * The total input length overflows the additional data length that
2461 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002462 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2463 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2464 * \retval #PSA_ERROR_HARDWARE_FAILURE
2465 * \retval #PSA_ERROR_TAMPERING_DETECTED
2466 */
2467psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2468 const uint8_t *input,
2469 size_t input_length);
2470
2471/** Encrypt or decrypt a message fragment in an active AEAD operation.
2472 *
2473 * Before calling this function, you must:
2474 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2475 * The choice of setup function determines whether this function
2476 * encrypts or decrypts its input.
2477 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2478 * 3. Call psa_aead_update_ad() to pass all the additional data.
2479 *
2480 * If this function returns an error status, the operation becomes inactive.
2481 *
2482 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2483 * there is no guarantee that the input is valid. Therefore, until
2484 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2485 * - Do not use the output in any way other than storing it in a
2486 * confidential location. If you take any action that depends
2487 * on the tentative decrypted data, this action will need to be
2488 * undone if the input turns out not to be valid. Furthermore,
2489 * if an adversary can observe that this action took place
2490 * (for example through timing), they may be able to use this
2491 * fact as an oracle to decrypt any message encrypted with the
2492 * same key.
2493 * - In particular, do not copy the output anywhere but to a
2494 * memory or storage space that you have exclusive access to.
2495 *
2496 * \param[in,out] operation Active AEAD operation.
2497 * \param[in] input Buffer containing the message fragment to
2498 * encrypt or decrypt.
2499 * \param input_length Size of the \p input buffer in bytes.
2500 * \param[out] output Buffer where the output is to be written.
2501 * \param output_size Size of the \p output buffer in bytes.
2502 * \param[out] output_length On success, the number of bytes
2503 * that make up the returned output.
2504 *
2505 * \retval #PSA_SUCCESS
2506 * Success.
2507 * \retval #PSA_ERROR_BAD_STATE
2508 * The operation state is not valid (not set up, nonce not set
2509 * or already completed).
2510 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2511 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002512 * \retval #PSA_ERROR_INVALID_ARGUMENT
2513 * The total length of input to psa_aead_update_ad() so far is
2514 * less than the additional data length that was previously
2515 * specified with psa_aead_set_lengths().
2516 * \retval #PSA_ERROR_INVALID_ARGUMENT
2517 * The total input length overflows the plaintext length that
2518 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002519 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2520 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2521 * \retval #PSA_ERROR_HARDWARE_FAILURE
2522 * \retval #PSA_ERROR_TAMPERING_DETECTED
2523 */
2524psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2525 const uint8_t *input,
2526 size_t input_length,
2527 unsigned char *output,
2528 size_t output_size,
2529 size_t *output_length);
2530
2531/** Finish encrypting a message in an AEAD operation.
2532 *
2533 * The operation must have been set up with psa_aead_encrypt_setup().
2534 *
2535 * This function finishes the authentication of the additional data
2536 * formed by concatenating the inputs passed to preceding calls to
2537 * psa_aead_update_ad() with the plaintext formed by concatenating the
2538 * inputs passed to preceding calls to psa_aead_update().
2539 *
2540 * This function has two output buffers:
2541 * - \p ciphertext contains trailing ciphertext that was buffered from
2542 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2543 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2544 * will not contain any output and can be a 0-sized buffer.
2545 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002546 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002547 * that the operation performs.
2548 *
2549 * When this function returns, the operation becomes inactive.
2550 *
2551 * \param[in,out] operation Active AEAD operation.
2552 * \param[out] ciphertext Buffer where the last part of the ciphertext
2553 * is to be written.
2554 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2555 * \param[out] ciphertext_length On success, the number of bytes of
2556 * returned ciphertext.
2557 * \param[out] tag Buffer where the authentication tag is
2558 * to be written.
2559 * \param tag_size Size of the \p tag buffer in bytes.
2560 * \param[out] tag_length On success, the number of bytes
2561 * that make up the returned tag.
2562 *
2563 * \retval #PSA_SUCCESS
2564 * Success.
2565 * \retval #PSA_ERROR_BAD_STATE
2566 * The operation state is not valid (not set up, nonce not set,
2567 * decryption, or already completed).
2568 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002569 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002570 * \retval #PSA_ERROR_INVALID_ARGUMENT
2571 * The total length of input to psa_aead_update_ad() so far is
2572 * less than the additional data length that was previously
2573 * specified with psa_aead_set_lengths().
2574 * \retval #PSA_ERROR_INVALID_ARGUMENT
2575 * The total length of input to psa_aead_update() so far is
2576 * less than the plaintext length that was previously
2577 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002578 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2579 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2580 * \retval #PSA_ERROR_HARDWARE_FAILURE
2581 * \retval #PSA_ERROR_TAMPERING_DETECTED
2582 */
2583psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002584 uint8_t *ciphertext,
2585 size_t ciphertext_size,
2586 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002587 uint8_t *tag,
2588 size_t tag_size,
2589 size_t *tag_length);
2590
2591/** Finish authenticating and decrypting a message in an AEAD operation.
2592 *
2593 * The operation must have been set up with psa_aead_decrypt_setup().
2594 *
2595 * This function finishes the authentication of the additional data
2596 * formed by concatenating the inputs passed to preceding calls to
2597 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2598 * inputs passed to preceding calls to psa_aead_update().
2599 *
2600 * When this function returns, the operation becomes inactive.
2601 *
2602 * \param[in,out] operation Active AEAD operation.
2603 * \param[in] tag Buffer containing the authentication tag.
2604 * \param tag_length Size of the \p tag buffer in bytes.
2605 *
2606 * \retval #PSA_SUCCESS
2607 * Success.
2608 * \retval #PSA_ERROR_BAD_STATE
2609 * The operation state is not valid (not set up, nonce not set,
2610 * encryption, or already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002611 * \retval #PSA_ERROR_INVALID_ARGUMENT
2612 * The total length of input to psa_aead_update_ad() so far is
2613 * less than the additional data length that was previously
2614 * specified with psa_aead_set_lengths().
2615 * \retval #PSA_ERROR_INVALID_ARGUMENT
2616 * The total length of input to psa_aead_update() so far is
2617 * less than the plaintext length that was previously
2618 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002619 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2620 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2621 * \retval #PSA_ERROR_HARDWARE_FAILURE
2622 * \retval #PSA_ERROR_TAMPERING_DETECTED
2623 */
2624psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2625 const uint8_t *tag,
2626 size_t tag_length);
2627
2628/** Abort an AEAD operation.
2629 *
2630 * Aborting an operation frees all associated resources except for the
2631 * \p operation structure itself. Once aborted, the operation object
2632 * can be reused for another operation by calling
2633 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2634 *
2635 * You may call this function any time after the operation object has
2636 * been initialized by any of the following methods:
2637 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2638 * whether it succeeds or not.
2639 * - Initializing the \c struct to all-bits-zero.
2640 * - Initializing the \c struct to logical zeros, e.g.
2641 * `psa_aead_operation_t operation = {0}`.
2642 *
2643 * In particular, calling psa_aead_abort() after the operation has been
2644 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2645 * is safe and has no effect.
2646 *
2647 * \param[in,out] operation Initialized AEAD operation.
2648 *
2649 * \retval #PSA_SUCCESS
2650 * \retval #PSA_ERROR_BAD_STATE
2651 * \p operation is not an active AEAD operation.
2652 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2653 * \retval #PSA_ERROR_HARDWARE_FAILURE
2654 * \retval #PSA_ERROR_TAMPERING_DETECTED
2655 */
2656psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2657
Gilles Peskine3b555712018-03-03 21:27:57 +01002658/**@}*/
2659
Gilles Peskine20035e32018-02-03 22:44:14 +01002660/** \defgroup asymmetric Asymmetric cryptography
2661 * @{
2662 */
2663
2664/**
2665 * \brief Sign a hash or short message with a private key.
2666 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002667 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002668 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002669 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2670 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2671 * to determine the hash algorithm to use.
2672 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002673 * \param handle Handle to the key to use for the operation.
2674 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002675 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002676 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002677 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002678 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002679 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002680 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002681 * \param[out] signature_length On success, the number of bytes
2682 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002683 *
Gilles Peskine28538492018-07-11 17:34:00 +02002684 * \retval #PSA_SUCCESS
2685 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002686 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002687 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002688 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002689 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002690 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002691 * \retval #PSA_ERROR_NOT_SUPPORTED
2692 * \retval #PSA_ERROR_INVALID_ARGUMENT
2693 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2694 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2695 * \retval #PSA_ERROR_HARDWARE_FAILURE
2696 * \retval #PSA_ERROR_TAMPERING_DETECTED
2697 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002698 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002699 * The library has not been previously initialized by psa_crypto_init().
2700 * It is implementation-dependent whether a failure to initialize
2701 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002702 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002703psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002704 psa_algorithm_t alg,
2705 const uint8_t *hash,
2706 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002707 uint8_t *signature,
2708 size_t signature_size,
2709 size_t *signature_length);
2710
2711/**
2712 * \brief Verify the signature a hash or short message using a public key.
2713 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002714 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002715 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002716 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2717 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2718 * to determine the hash algorithm to use.
2719 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002720 * \param handle Handle to the key to use for the operation.
2721 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002722 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002723 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002724 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002725 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002726 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002727 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002728 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002729 *
Gilles Peskine28538492018-07-11 17:34:00 +02002730 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002731 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002732 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002733 * The calculation was perfomed successfully, but the passed
2734 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002735 * \retval #PSA_ERROR_NOT_SUPPORTED
2736 * \retval #PSA_ERROR_INVALID_ARGUMENT
2737 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2738 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2739 * \retval #PSA_ERROR_HARDWARE_FAILURE
2740 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002741 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002742 * The library has not been previously initialized by psa_crypto_init().
2743 * It is implementation-dependent whether a failure to initialize
2744 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002745 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002746psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002747 psa_algorithm_t alg,
2748 const uint8_t *hash,
2749 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002750 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002751 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002752
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002753/**
2754 * \brief Encrypt a short message with a public key.
2755 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002756 * \param handle Handle to the key to use for the operation.
2757 * It must be a public key or an asymmetric
2758 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002759 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002760 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002761 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002762 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002763 * \param[in] salt A salt or label, if supported by the
2764 * encryption algorithm.
2765 * If the algorithm does not support a
2766 * salt, pass \c NULL.
2767 * If the algorithm supports an optional
2768 * salt and you do not want to pass a salt,
2769 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002770 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002771 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2772 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002773 * \param salt_length Size of the \p salt buffer in bytes.
2774 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002775 * \param[out] output Buffer where the encrypted message is to
2776 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002777 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002778 * \param[out] output_length On success, the number of bytes
2779 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002780 *
Gilles Peskine28538492018-07-11 17:34:00 +02002781 * \retval #PSA_SUCCESS
2782 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002783 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002784 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002785 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002786 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002787 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002788 * \retval #PSA_ERROR_NOT_SUPPORTED
2789 * \retval #PSA_ERROR_INVALID_ARGUMENT
2790 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2791 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2792 * \retval #PSA_ERROR_HARDWARE_FAILURE
2793 * \retval #PSA_ERROR_TAMPERING_DETECTED
2794 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002795 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002796 * The library has not been previously initialized by psa_crypto_init().
2797 * It is implementation-dependent whether a failure to initialize
2798 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002799 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002800psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002801 psa_algorithm_t alg,
2802 const uint8_t *input,
2803 size_t input_length,
2804 const uint8_t *salt,
2805 size_t salt_length,
2806 uint8_t *output,
2807 size_t output_size,
2808 size_t *output_length);
2809
2810/**
2811 * \brief Decrypt a short message with a private key.
2812 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002813 * \param handle Handle to the key to use for the operation.
2814 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002815 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002816 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002817 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002818 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002819 * \param[in] salt A salt or label, if supported by the
2820 * encryption algorithm.
2821 * If the algorithm does not support a
2822 * salt, pass \c NULL.
2823 * If the algorithm supports an optional
2824 * salt and you do not want to pass a salt,
2825 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002826 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002827 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2828 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002829 * \param salt_length Size of the \p salt buffer in bytes.
2830 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002831 * \param[out] output Buffer where the decrypted message is to
2832 * be written.
2833 * \param output_size Size of the \c output buffer in bytes.
2834 * \param[out] output_length On success, the number of bytes
2835 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002836 *
Gilles Peskine28538492018-07-11 17:34:00 +02002837 * \retval #PSA_SUCCESS
2838 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002839 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002840 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002841 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002842 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002843 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002844 * \retval #PSA_ERROR_NOT_SUPPORTED
2845 * \retval #PSA_ERROR_INVALID_ARGUMENT
2846 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2847 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2848 * \retval #PSA_ERROR_HARDWARE_FAILURE
2849 * \retval #PSA_ERROR_TAMPERING_DETECTED
2850 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2851 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002852 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002853 * The library has not been previously initialized by psa_crypto_init().
2854 * It is implementation-dependent whether a failure to initialize
2855 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002856 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002857psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002858 psa_algorithm_t alg,
2859 const uint8_t *input,
2860 size_t input_length,
2861 const uint8_t *salt,
2862 size_t salt_length,
2863 uint8_t *output,
2864 size_t output_size,
2865 size_t *output_length);
2866
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002867/**@}*/
2868
Gilles Peskineedd76872018-07-20 17:42:05 +02002869/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002870 * @{
2871 */
2872
2873/** The type of the state data structure for generators.
2874 *
2875 * Before calling any function on a generator, the application must
2876 * initialize it by any of the following means:
2877 * - Set the structure to all-bits-zero, for example:
2878 * \code
2879 * psa_crypto_generator_t generator;
2880 * memset(&generator, 0, sizeof(generator));
2881 * \endcode
2882 * - Initialize the structure to logical zero values, for example:
2883 * \code
2884 * psa_crypto_generator_t generator = {0};
2885 * \endcode
2886 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2887 * for example:
2888 * \code
2889 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2890 * \endcode
2891 * - Assign the result of the function psa_crypto_generator_init()
2892 * to the structure, for example:
2893 * \code
2894 * psa_crypto_generator_t generator;
2895 * generator = psa_crypto_generator_init();
2896 * \endcode
2897 *
2898 * This is an implementation-defined \c struct. Applications should not
2899 * make any assumptions about the content of this structure except
2900 * as directed by the documentation of a specific implementation.
2901 */
2902typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2903
2904/** \def PSA_CRYPTO_GENERATOR_INIT
2905 *
2906 * This macro returns a suitable initializer for a generator object
2907 * of type #psa_crypto_generator_t.
2908 */
2909#ifdef __DOXYGEN_ONLY__
2910/* This is an example definition for documentation purposes.
2911 * Implementations should define a suitable value in `crypto_struct.h`.
2912 */
2913#define PSA_CRYPTO_GENERATOR_INIT {0}
2914#endif
2915
2916/** Return an initial value for a generator object.
2917 */
2918static psa_crypto_generator_t psa_crypto_generator_init(void);
2919
2920/** Retrieve the current capacity of a generator.
2921 *
2922 * The capacity of a generator is the maximum number of bytes that it can
2923 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2924 *
2925 * \param[in] generator The generator to query.
2926 * \param[out] capacity On success, the capacity of the generator.
2927 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002928 * \retval #PSA_SUCCESS
2929 * \retval #PSA_ERROR_BAD_STATE
2930 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002931 */
2932psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2933 size_t *capacity);
2934
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002935/** Set the maximum capacity of a generator.
2936 *
2937 * \param[in,out] generator The generator object to modify.
2938 * \param capacity The new capacity of the generator.
2939 * It must be less or equal to the generator's
2940 * current capacity.
2941 *
2942 * \retval #PSA_SUCCESS
2943 * \retval #PSA_ERROR_INVALID_ARGUMENT
2944 * \p capacity is larger than the generator's current capacity.
2945 * \retval #PSA_ERROR_BAD_STATE
2946 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2947 */
2948psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2949 size_t capacity);
2950
Gilles Peskineeab56e42018-07-12 17:12:33 +02002951/** Read some data from a generator.
2952 *
2953 * This function reads and returns a sequence of bytes from a generator.
2954 * The data that is read is discarded from the generator. The generator's
2955 * capacity is decreased by the number of bytes read.
2956 *
2957 * \param[in,out] generator The generator object to read from.
2958 * \param[out] output Buffer where the generator output will be
2959 * written.
2960 * \param output_length Number of bytes to output.
2961 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002962 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02002963 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02002964 * There were fewer than \p output_length bytes
2965 * in the generator. Note that in this case, no
2966 * output is written to the output buffer.
2967 * The generator's capacity is set to 0, thus
2968 * subsequent calls to this function will not
2969 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002970 * \retval #PSA_ERROR_BAD_STATE
2971 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2972 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2973 * \retval #PSA_ERROR_HARDWARE_FAILURE
2974 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002975 */
2976psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2977 uint8_t *output,
2978 size_t output_length);
2979
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002980/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002981 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002982 * This function uses the output of a generator to derive a key.
2983 * How much output it consumes and how the key is derived depends on the
2984 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002985 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002986 * - For key types for which the key is an arbitrary sequence of bytes
2987 * of a given size,
2988 * this function is functionally equivalent to calling #psa_generator_read
2989 * and passing the resulting output to #psa_import_key.
2990 * However, this function has a security benefit:
2991 * if the implementation provides an isolation boundary then
2992 * the key material is not exposed outside the isolation boundary.
2993 * As a consequence, for these key types, this function always consumes
2994 * exactly (\p bits / 8) bytes from the generator.
2995 * The following key types defined in this specification follow this scheme:
2996 *
2997 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002998 * - #PSA_KEY_TYPE_ARC4;
2999 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003000 * - #PSA_KEY_TYPE_DERIVE;
3001 * - #PSA_KEY_TYPE_HMAC.
3002 *
3003 * - For ECC keys on a Montgomery elliptic curve
3004 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3005 * Montgomery curve), this function always draws a byte string whose
3006 * length is determined by the curve, and sets the mandatory bits
3007 * accordingly. That is:
3008 *
3009 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3010 * and process it as specified in RFC 7748 &sect;5.
3011 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3012 * and process it as specified in RFC 7748 &sect;5.
3013 *
3014 * - For key types for which the key is represented by a single sequence of
3015 * \p bits bits with constraints as to which bit sequences are acceptable,
3016 * this function draws a byte string of length (\p bits / 8) bytes rounded
3017 * up to the nearest whole number of bytes. If the resulting byte string
3018 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3019 * This process is repeated until an acceptable byte string is drawn.
3020 * The byte string drawn from the generator is interpreted as specified
3021 * for the output produced by psa_export_key().
3022 * The following key types defined in this specification follow this scheme:
3023 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003024 * - #PSA_KEY_TYPE_DES.
3025 * Force-set the parity bits, but discard forbidden weak keys.
3026 * For 2-key and 3-key triple-DES, the three keys are generated
3027 * successively (for example, for 3-key triple-DES,
3028 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3029 * discard the first 8 bytes, use the next 8 bytes as the first key,
3030 * and continue reading output from the generator to derive the other
3031 * two keys).
3032 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
3033 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
3034 * ECC keys on a Weierstrass elliptic curve
3035 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3036 * Weierstrass curve).
3037 * For these key types, interpret the byte string as integer
3038 * in big-endian order. Discard it if it is not in the range
3039 * [0, *N* - 2] where *N* is the boundary of the private key domain
3040 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003041 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003042 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003043 * This method allows compliance to NIST standards, specifically
3044 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003045 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3046 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3047 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3048 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003049 *
3050 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
3051 * the way in which the generator output is consumed is
3052 * implementation-defined.
3053 *
3054 * In all cases, the data that is read is discarded from the generator.
3055 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003056 *
Gilles Peskine20628592019-04-19 19:29:50 +02003057 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003058 * \param[out] handle On success, a handle to the newly created key.
3059 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003060 * \param[in,out] generator The generator object to read from.
3061 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003062 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003063 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003064 * If the key is persistent, the key material and the key's metadata
3065 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003066 * \retval #PSA_ERROR_ALREADY_EXISTS
3067 * This is an attempt to create a persistent key, and there is
3068 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003069 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003070 * There was not enough data to create the desired key.
3071 * Note that in this case, no output is written to the output buffer.
3072 * The generator's capacity is set to 0, thus subsequent calls to
3073 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003074 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003075 * The key type or key size is not supported, either by the
3076 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003077 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003078 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3079 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3080 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3081 * \retval #PSA_ERROR_HARDWARE_FAILURE
3082 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003083 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003084 * The library has not been previously initialized by psa_crypto_init().
3085 * It is implementation-dependent whether a failure to initialize
3086 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003087 */
Gilles Peskine87a5e562019-04-17 12:28:25 +02003088psa_status_t psa_generator_import_key(const psa_key_attributes_t *attributes,
3089 psa_key_handle_t *handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003090 psa_crypto_generator_t *generator);
3091
3092/** Abort a generator.
3093 *
3094 * Once a generator has been aborted, its capacity is zero.
3095 * Aborting a generator frees all associated resources except for the
3096 * \c generator structure itself.
3097 *
3098 * This function may be called at any time as long as the generator
3099 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3100 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3101 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3102 * on a generator that has not been set up.
3103 *
3104 * Once aborted, the generator object may be called.
3105 *
3106 * \param[in,out] generator The generator to abort.
3107 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003108 * \retval #PSA_SUCCESS
3109 * \retval #PSA_ERROR_BAD_STATE
3110 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3111 * \retval #PSA_ERROR_HARDWARE_FAILURE
3112 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003113 */
3114psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3115
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003116/** Use the maximum possible capacity for a generator.
3117 *
3118 * Use this value as the capacity argument when setting up a generator
3119 * to indicate that the generator should have the maximum possible capacity.
3120 * The value of the maximum possible capacity depends on the generator
3121 * algorithm.
3122 */
3123#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3124
Gilles Peskineeab56e42018-07-12 17:12:33 +02003125/**@}*/
3126
Gilles Peskineea0fb492018-07-12 17:17:20 +02003127/** \defgroup derivation Key derivation
3128 * @{
3129 */
3130
3131/** Set up a key derivation operation.
3132 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003133 * A key derivation algorithm takes some inputs and uses them to create
3134 * a byte generator which can be used to produce keys and other
3135 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003136 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003137 * To use a generator for key derivation:
3138 * - Start with an initialized object of type #psa_crypto_generator_t.
3139 * - Call psa_key_derivation_setup() to select the algorithm.
3140 * - Provide the inputs for the key derivation by calling
3141 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3142 * as appropriate. Which inputs are needed, in what order, and whether
3143 * they may be keys and if so of what type depends on the algorithm.
3144 * - Optionally set the generator's maximum capacity with
3145 * psa_set_generator_capacity(). You may do this before, in the middle of
3146 * or after providing inputs. For some algorithms, this step is mandatory
3147 * because the output depends on the maximum capacity.
3148 * - Generate output with psa_generator_read() or
3149 * psa_generator_import_key(). Successive calls to these functions
3150 * use successive output bytes from the generator.
3151 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003152 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003153 * \param[in,out] generator The generator object to set up. It must
3154 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003155 * \param alg The key derivation algorithm to compute
3156 * (\c PSA_ALG_XXX value such that
3157 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003158 *
3159 * \retval #PSA_SUCCESS
3160 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003161 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003162 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003163 * \retval #PSA_ERROR_NOT_SUPPORTED
3164 * \c alg is not supported or is not a key derivation algorithm.
3165 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3166 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3167 * \retval #PSA_ERROR_HARDWARE_FAILURE
3168 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003169 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003170 */
3171psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3172 psa_algorithm_t alg);
3173
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003174/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003175 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003176 * Which inputs are required and in what order depends on the algorithm.
3177 * Refer to the documentation of each key derivation or key agreement
3178 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003179 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003180 * This function passes direct inputs. Some inputs must be passed as keys
3181 * using psa_key_derivation_input_key() instead of this function. Refer to
3182 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003183 *
3184 * \param[in,out] generator The generator object to use. It must
3185 * have been set up with
3186 * psa_key_derivation_setup() and must not
3187 * have produced any output yet.
3188 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003189 * \param[in] data Input data to use.
3190 * \param data_length Size of the \p data buffer in bytes.
3191 *
3192 * \retval #PSA_SUCCESS
3193 * Success.
3194 * \retval #PSA_ERROR_INVALID_ARGUMENT
3195 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003196 * \retval #PSA_ERROR_INVALID_ARGUMENT
3197 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003198 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3199 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3200 * \retval #PSA_ERROR_HARDWARE_FAILURE
3201 * \retval #PSA_ERROR_TAMPERING_DETECTED
3202 * \retval #PSA_ERROR_BAD_STATE
3203 * The value of \p step is not valid given the state of \p generator.
3204 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003205 * The library has not been previously initialized by psa_crypto_init().
3206 * It is implementation-dependent whether a failure to initialize
3207 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003208 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003209psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3210 psa_key_derivation_step_t step,
3211 const uint8_t *data,
3212 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003213
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003214/** Provide an input for key derivation in the form of a key.
3215 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003216 * Which inputs are required and in what order depends on the algorithm.
3217 * Refer to the documentation of each key derivation or key agreement
3218 * algorithm for information.
3219 *
3220 * This function passes key inputs. Some inputs must be passed as keys
3221 * of the appropriate type using this function, while others must be
3222 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3223 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003224 *
3225 * \param[in,out] generator The generator object to use. It must
3226 * have been set up with
3227 * psa_key_derivation_setup() and must not
3228 * have produced any output yet.
3229 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003230 * \param handle Handle to the key. It must have an
3231 * appropriate type for \p step and must
3232 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003233 *
3234 * \retval #PSA_SUCCESS
3235 * Success.
3236 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003237 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003238 * \retval #PSA_ERROR_NOT_PERMITTED
3239 * \retval #PSA_ERROR_INVALID_ARGUMENT
3240 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003241 * \retval #PSA_ERROR_INVALID_ARGUMENT
3242 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003243 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3244 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3245 * \retval #PSA_ERROR_HARDWARE_FAILURE
3246 * \retval #PSA_ERROR_TAMPERING_DETECTED
3247 * \retval #PSA_ERROR_BAD_STATE
3248 * The value of \p step is not valid given the state of \p generator.
3249 * \retval #PSA_ERROR_BAD_STATE
3250 * The library has not been previously initialized by psa_crypto_init().
3251 * It is implementation-dependent whether a failure to initialize
3252 * results in this error code.
3253 */
3254psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3255 psa_key_derivation_step_t step,
3256 psa_key_handle_t handle);
3257
Gilles Peskine969c5d62019-01-16 15:53:06 +01003258/** Perform a key agreement and use the shared secret as input to a key
3259 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003260 *
3261 * A key agreement algorithm takes two inputs: a private key \p private_key
3262 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003263 * The result of this function is passed as input to a key derivation.
3264 * The output of this key derivation can be extracted by reading from the
3265 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003266 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003267 * \param[in,out] generator The generator object to use. It must
3268 * have been set up with
3269 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003270 * key agreement and derivation algorithm
3271 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003272 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3273 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003274 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003275 * The generator must be ready for an
3276 * input of the type given by \p step.
3277 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003278 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003279 * \param[in] peer_key Public key of the peer. The peer key must be in the
3280 * same format that psa_import_key() accepts for the
3281 * public key type corresponding to the type of
3282 * private_key. That is, this function performs the
3283 * equivalent of
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003284 * #psa_import_key(`internal_public_key_handle`,
3285 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(`private_key_type`),
3286 * `peer_key`, `peer_key_length`) where
Jaeden Amero8afbff82019-01-14 16:56:20 +00003287 * `private_key_type` is the type of `private_key`.
3288 * For example, for EC keys, this means that peer_key
3289 * is interpreted as a point on the curve that the
3290 * private key is on. The standard formats for public
3291 * keys are documented in the documentation of
3292 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003293 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003294 *
3295 * \retval #PSA_SUCCESS
3296 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003297 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003298 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003299 * \retval #PSA_ERROR_NOT_PERMITTED
3300 * \retval #PSA_ERROR_INVALID_ARGUMENT
3301 * \c private_key is not compatible with \c alg,
3302 * or \p peer_key is not valid for \c alg or not compatible with
3303 * \c private_key.
3304 * \retval #PSA_ERROR_NOT_SUPPORTED
3305 * \c alg is not supported or is not a key derivation algorithm.
3306 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3307 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3308 * \retval #PSA_ERROR_HARDWARE_FAILURE
3309 * \retval #PSA_ERROR_TAMPERING_DETECTED
3310 */
3311psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003312 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003313 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003314 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003315 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003316
Gilles Peskine769c7a62019-01-18 16:42:29 +01003317/** Perform a key agreement and use the shared secret as input to a key
3318 * derivation.
3319 *
3320 * A key agreement algorithm takes two inputs: a private key \p private_key
3321 * a public key \p peer_key.
3322 *
3323 * \warning The raw result of a key agreement algorithm such as finite-field
3324 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3325 * not be used directly as key material. It should instead be passed as
3326 * input to a key derivation algorithm. To chain a key agreement with
3327 * a key derivation, use psa_key_agreement() and other functions from
3328 * the key derivation and generator interface.
3329 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003330 * \param alg The key agreement algorithm to compute
3331 * (\c PSA_ALG_XXX value such that
3332 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3333 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003334 * \param private_key Handle to the private key to use.
3335 * \param[in] peer_key Public key of the peer. It must be
3336 * in the same format that psa_import_key()
3337 * accepts. The standard formats for public
3338 * keys are documented in the documentation
3339 * of psa_export_public_key().
3340 * \param peer_key_length Size of \p peer_key in bytes.
3341 * \param[out] output Buffer where the decrypted message is to
3342 * be written.
3343 * \param output_size Size of the \c output buffer in bytes.
3344 * \param[out] output_length On success, the number of bytes
3345 * that make up the returned output.
3346 *
3347 * \retval #PSA_SUCCESS
3348 * Success.
3349 * \retval #PSA_ERROR_INVALID_HANDLE
3350 * \retval #PSA_ERROR_EMPTY_SLOT
3351 * \retval #PSA_ERROR_NOT_PERMITTED
3352 * \retval #PSA_ERROR_INVALID_ARGUMENT
3353 * \p alg is not a key agreement algorithm
3354 * \retval #PSA_ERROR_INVALID_ARGUMENT
3355 * \p private_key is not compatible with \p alg,
3356 * or \p peer_key is not valid for \p alg or not compatible with
3357 * \p private_key.
3358 * \retval #PSA_ERROR_NOT_SUPPORTED
3359 * \p alg is not a supported key agreement algorithm.
3360 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3361 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3362 * \retval #PSA_ERROR_HARDWARE_FAILURE
3363 * \retval #PSA_ERROR_TAMPERING_DETECTED
3364 */
3365psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3366 psa_key_handle_t private_key,
3367 const uint8_t *peer_key,
3368 size_t peer_key_length,
3369 uint8_t *output,
3370 size_t output_size,
3371 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003372
3373/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003374
3375/** \defgroup random Random generation
3376 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003377 */
3378
3379/**
3380 * \brief Generate random bytes.
3381 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003382 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003383 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003384 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003385 *
3386 * \note To generate a key, use psa_generate_key() instead.
3387 *
3388 * \param[out] output Output buffer for the generated data.
3389 * \param output_size Number of bytes to generate and output.
3390 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003391 * \retval #PSA_SUCCESS
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003392 * \retval #PSA_ERROR_NOT_SUPPORTED
3393 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003394 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003395 * \retval #PSA_ERROR_HARDWARE_FAILURE
3396 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003397 * \retval #PSA_ERROR_BAD_STATE
3398 * The library has not been previously initialized by psa_crypto_init().
3399 * It is implementation-dependent whether a failure to initialize
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003400 * results in this error code.
3401 */
3402psa_status_t psa_generate_random(uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003403 size_t output_size);
3404
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003405/**
3406 * \brief Generate a key or key pair.
3407 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003408 * The key is generated randomly.
3409 * Its location, policy, type and size are taken from \p attributes.
3410 *
3411 * If the type requires additional domain parameters, these are taken
3412 * from \p attributes as well. The following types use domain parameters:
3413 * - When generating an RSA key (#PSA_KEY_TYPE_RSA_KEYPAIR),
3414 * the default public exponent is 65537. This value is used if
3415 * \p attributes was set with psa_set_key_type() or by passing an empty
3416 * byte string as domain parameters to psa_set_key_domain_parameters().
3417 * If psa_set_key_domain_parameters() was used to set a non-empty
3418 * domain parameter string in \p attributes, this string is read as
3419 * a big-endian integer which is used as the public exponent.
3420 * - When generating a DSA key (#PSA_KEY_TYPE_DSA_KEYPAIR) or a
3421 * Diffie-Hellman key (#PSA_KEY_TYPE_DH_KEYPAIR), the domain parameters
3422 * from \p attributes are interpreted as described for
3423 * psa_set_key_domain_parameters().
3424 *
Gilles Peskine20628592019-04-19 19:29:50 +02003425 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003426 * \param[out] handle On success, a handle to the newly created key.
3427 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003428 *
Gilles Peskine28538492018-07-11 17:34:00 +02003429 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003430 * Success.
3431 * If the key is persistent, the key material and the key's metadata
3432 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003433 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003434 * This is an attempt to create a persistent key, and there is
3435 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003436 * \retval #PSA_ERROR_NOT_SUPPORTED
3437 * \retval #PSA_ERROR_INVALID_ARGUMENT
3438 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3439 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3440 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3441 * \retval #PSA_ERROR_HARDWARE_FAILURE
3442 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003443 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003444 * The library has not been previously initialized by psa_crypto_init().
3445 * It is implementation-dependent whether a failure to initialize
3446 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003447 */
Gilles Peskine87a5e562019-04-17 12:28:25 +02003448psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003449 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003450
3451/**@}*/
3452
Gilles Peskinee59236f2018-01-27 23:32:46 +01003453#ifdef __cplusplus
3454}
3455#endif
3456
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003457/* The file "crypto_sizes.h" contains definitions for size calculation
3458 * macros whose definitions are implementation-specific. */
3459#include "crypto_sizes.h"
3460
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003461/* The file "crypto_struct.h" contains definitions for
3462 * implementation-specific structs that are declared above. */
3463#include "crypto_struct.h"
3464
3465/* The file "crypto_extra.h" contains vendor-specific definitions. This
3466 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003467#include "crypto_extra.h"
3468
3469#endif /* PSA_CRYPTO_H */