blob: 9ec3b90749251da5ac9f88024f23d7d6af09bf3b [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
103 * object, including the key type and size, domain parameters, usage policies,
104 * location in storage, and any other similar information.
105 *
106 * The actual key material is not considered an attribute of a key.
107 * Key attributes do not contain information that is generally considered
108 * highly confidential.
Gilles Peskine20628592019-04-19 19:29:50 +0200109 *
110 * Before calling any function on a key attribute structure, the application
111 * must initialize it by any of the following means:
112 * - Set the structure to all-bits-zero, for example:
113 * \code
114 * psa_key_attributes_t attributes;
115 * memset(&attributes, 0, sizeof(attributes));
116 * \endcode
117 * - Initialize the structure to logical zero values, for example:
118 * \code
119 * psa_key_attributes_t attributes = {0};
120 * \endcode
121 * - Initialize the structure to the initializer #PSA_KEY_ATTRIBUTES_INIT,
122 * for example:
123 * \code
124 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
125 * \endcode
126 * - Assign the result of the function psa_key_attributes_init()
127 * to the structure, for example:
128 * \code
129 * psa_key_attributes_t attributes;
130 * attributes = psa_key_attributes_init();
131 * \endcode
132 *
133 * A freshly initialized attribute structure contains the following
134 * values:
135 *
136 * - lifetime: #PSA_KEY_LIFETIME_VOLATILE.
137 * - key identifier: unspecified.
138 * - type: \c 0, with no domain parameters.
139 * - key size: \c 0.
140 * - usage flags: \c 0.
141 * - algorithm: \c 0.
142 *
143 * A freshly initialized attribute structure does not own any auxiliary
144 * resources such as pointers to allocated memory, and therefore can be
145 * freed simply by freeing the memory allocated for the structure itself.
146 * This property still holds if the structure has only been modified
147 * by the following functions:
148 * - psa_make_key_persistent()
149 * - psa_set_key_type()
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200150 * - psa_set_key_bits()
Gilles Peskine20628592019-04-19 19:29:50 +0200151 * - psa_set_key_usage_flags()
152 * - psa_set_key_algorithm()
153 * - psa_reset_key_attributes()
154 * - psa_get_key_attributes() on a key which has been created with
155 * attribute structure that itself did not contain auxiliary resources
156 *
157 * If the attribute structure has been modified with other functions,
158 * you must free auxiliary resources by calling psa_reset_key_attributes().
159 * The following functions may create auxiliary resouces:
160 * - psa_set_key_domain_parameters()
Gilles Peskine87a5e562019-04-17 12:28:25 +0200161 */
162typedef struct psa_key_attributes_s psa_key_attributes_t;
163
Gilles Peskine20628592019-04-19 19:29:50 +0200164/** Declare a key as persistent.
165 *
166 * This function does not access storage, it merely fills the attribute
167 * structure with given values. The persistent key will be written to
168 * storage when the attribute structure is passed to a key creation
169 * function such as psa_import_key(), psa_generate_key(),
170 * psa_generator_import_key() or psa_copy_key().
171 *
172 * This function overwrites any identifier and lifetime values
173 * previously set in \p attributes.
174 *
175 * This function may be declared as `static` (i.e. without external
176 * linkage). This function may be provided as a function-like macro,
177 * but in this case it must evaluate each of its arguments exactly once.
178 *
179 * \param[out] attributes The attribute structure to write to.
180 * \param id The persistent identifier for the key.
181 * \param lifetime The lifetime for the key.
182 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
183 * key will be volatile, and \p id is ignored.
184 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200185static void psa_make_key_persistent(psa_key_attributes_t *attributes,
186 psa_key_id_t id,
187 psa_key_lifetime_t lifetime);
188
Gilles Peskine20628592019-04-19 19:29:50 +0200189/** Retrieve the key identifier from key attributes.
190 *
191 * This function may be declared as `static` (i.e. without external
192 * linkage). This function may be provided as a function-like macro,
193 * but in this case it must evaluate its argument exactly once.
194 *
195 * \param[in] attributes The key attribute structure to query.
196 *
197 * \return The persistent identifier stored in the attribute structure.
198 * This value is unspecified if the attribute structure declares
199 * the key as volatile.
200 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200201static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
202
Gilles Peskine20628592019-04-19 19:29:50 +0200203/** Retrieve the lifetime from key attributes.
204 *
205 * This function may be declared as `static` (i.e. without external
206 * linkage). This function may be provided as a function-like macro,
207 * but in this case it must evaluate its argument exactly once.
208 *
209 * \param[in] attributes The key attribute structure to query.
210 *
211 * \return The lifetime value stored in the attribute structure.
212 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200213static psa_key_lifetime_t psa_get_key_lifetime(
214 const psa_key_attributes_t *attributes);
215
Gilles Peskine20628592019-04-19 19:29:50 +0200216/** Declare usage flags for a key.
217 *
218 * Usage flags are part of a key's usage policy. They encode what
219 * kind of operations are permitted on the key. For more details,
220 * refer to the documentation of the type #psa_key_usage_t.
221 *
222 * This function overwrites any usage flags
223 * previously set in \p attributes.
224 *
225 * This function may be declared as `static` (i.e. without external
226 * linkage). This function may be provided as a function-like macro,
227 * but in this case it must evaluate each of its arguments exactly once.
228 *
229 * \param[out] attributes The attribute structure to write to.
230 * \param usage_flags The usage flags to write.
231 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200232static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
233 psa_key_usage_t usage_flags);
234
Gilles Peskine20628592019-04-19 19:29:50 +0200235/** Retrieve the usage flags from key attributes.
236 *
237 * This function may be declared as `static` (i.e. without external
238 * linkage). This function may be provided as a function-like macro,
239 * but in this case it must evaluate its argument exactly once.
240 *
241 * \param[in] attributes The key attribute structure to query.
242 *
243 * \return The usage flags stored in the attribute structure.
244 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200245static psa_key_usage_t psa_get_key_usage_flags(
246 const psa_key_attributes_t *attributes);
247
Gilles Peskine20628592019-04-19 19:29:50 +0200248/** Declare the permitted algorithm policy for a key.
249 *
250 * The permitted algorithm policy of a key encodes which algorithm or
251 * algorithms are permitted to be used with this key.
252 *
253 * This function overwrites any algorithm policy
254 * previously set in \p attributes.
255 *
256 * This function may be declared as `static` (i.e. without external
257 * linkage). This function may be provided as a function-like macro,
258 * but in this case it must evaluate each of its arguments exactly once.
259 *
260 * \param[out] attributes The attribute structure to write to.
261 * \param alg The permitted algorithm policy to write.
262 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200263static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
264 psa_algorithm_t alg);
265
Gilles Peskine20628592019-04-19 19:29:50 +0200266/** Retrieve the algorithm policy from key attributes.
267 *
268 * This function may be declared as `static` (i.e. without external
269 * linkage). This function may be provided as a function-like macro,
270 * but in this case it must evaluate its argument exactly once.
271 *
272 * \param[in] attributes The key attribute structure to query.
273 *
274 * \return The algorithm stored in the attribute structure.
275 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200276static psa_algorithm_t psa_get_key_algorithm(
277 const psa_key_attributes_t *attributes);
278
Gilles Peskine20628592019-04-19 19:29:50 +0200279/** Declare the type of a key.
280 *
281 * If a type requires domain parameters, you must call
282 * psa_set_key_domain_parameters() instead of this function.
283 *
284 * This function overwrites any key type and domain parameters
285 * previously set in \p attributes.
286 *
287 * This function may be declared as `static` (i.e. without external
288 * linkage). This function may be provided as a function-like macro,
289 * but in this case it must evaluate each of its arguments exactly once.
290 *
291 * \param[out] attributes The attribute structure to write to.
292 * \param type The key type to write.
293 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200294static void psa_set_key_type(psa_key_attributes_t *attributes,
295 psa_key_type_t type);
296
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200297/** Declare the size of a key.
298 *
299 * This function overwrites any key size previously set in \p attributes.
300 *
301 * This function may be declared as `static` (i.e. without external
302 * linkage). This function may be provided as a function-like macro,
303 * but in this case it must evaluate each of its arguments exactly once.
304 *
305 * \param[out] attributes The attribute structure to write to.
306 * \param bits The key size in bits.
307 */
308static void psa_set_key_bits(psa_key_attributes_t *attributes,
309 size_t bits);
310
Gilles Peskine20628592019-04-19 19:29:50 +0200311/** Retrieve the key type from key attributes.
312 *
313 * This function may be declared as `static` (i.e. without external
314 * linkage). This function may be provided as a function-like macro,
315 * but in this case it must evaluate its argument exactly once.
316 *
317 * \param[in] attributes The key attribute structure to query.
318 *
319 * \return The key type stored in the attribute structure.
320 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200321static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
322
Gilles Peskine20628592019-04-19 19:29:50 +0200323/** Retrieve the key size from key attributes.
324 *
325 * This function may be declared as `static` (i.e. without external
326 * linkage). This function may be provided as a function-like macro,
327 * but in this case it must evaluate its argument exactly once.
328 *
329 * \param[in] attributes The key attribute structure to query.
330 *
331 * \return The key size stored in the attribute structure, in bits.
332 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200333static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
334
Gilles Peskineb699f072019-04-26 16:06:02 +0200335/**
336 * \brief Set domain parameters for a key.
337 *
338 * Some key types require additional domain parameters in addition to
339 * the key type identifier and the key size.
340 * The format for the required domain parameters varies by the key type.
341 *
Gilles Peskinee56e8782019-04-26 17:34:02 +0200342 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEYPAIR),
343 * the domain parameter data consists of the public exponent,
Gilles Peskineb699f072019-04-26 16:06:02 +0200344 * represented as a big-endian integer with no leading zeros.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200345 * This information is used when generating an RSA key pair.
Gilles Peskineb699f072019-04-26 16:06:02 +0200346 * When importing a key, the public exponent is read from the imported
347 * key data and the exponent recorded in the attribute structure is ignored.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200348 * As an exception, the public exponent 65537 is represented by an empty
349 * byte string.
350 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEYPAIR),
Gilles Peskineb699f072019-04-26 16:06:02 +0200351 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
352 * ```
353 * Dss-Parms ::= SEQUENCE {
354 * p INTEGER,
355 * q INTEGER,
356 * g INTEGER
357 * }
358 * ```
Gilles Peskinee56e8782019-04-26 17:34:02 +0200359 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY or
360 * #PSA_KEY_TYPE_DH_KEYPAIR), the
Gilles Peskineb699f072019-04-26 16:06:02 +0200361 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
362 * ```
363 * DomainParameters ::= SEQUENCE {
364 * p INTEGER, -- odd prime, p=jq +1
365 * g INTEGER, -- generator, g
366 * q INTEGER, -- factor of p-1
367 * j INTEGER OPTIONAL, -- subgroup factor
368 * validationParms ValidationParms OPTIONAL
369 * }
370 * ValidationParms ::= SEQUENCE {
371 * seed BIT STRING,
372 * pgenCounter INTEGER
373 * }
374 * ```
375 *
376 * \param[in,out] attributes Attribute structure where the specified domain
377 * parameters will be stored.
378 * If this function fails, the content of
379 * \p attributes is not modified.
380 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
381 * \param[in] data Buffer containing the key domain parameters.
382 * The content of this buffer is interpreted
383 * according to \p type as described above.
384 * \param data_length Size of the \p data buffer in bytes.
385 *
386 * \retval #PSA_SUCCESS
387 * \retval #PSA_ERROR_INVALID_ARGUMENT
388 * \retval #PSA_ERROR_NOT_SUPPORTED
389 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
390 */
391psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
392 psa_key_type_t type,
393 const uint8_t *data,
394 size_t data_length);
395
396/**
397 * \brief Get domain parameters for a key.
398 *
399 * Get the domain parameters for a key with this function, if any. The format
400 * of the domain parameters written to \p data is specified in the
401 * documentation for psa_set_key_domain_parameters().
402 *
403 * \param[in] attributes The key attribute structure to query.
404 * \param[out] data On success, the key domain parameters.
405 * \param data_size Size of the \p data buffer in bytes.
406 * \param[out] data_length On success, the number of bytes
407 * that make up the key domain parameters data.
408 *
409 * \retval #PSA_SUCCESS
410 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
411 */
412psa_status_t psa_get_key_domain_parameters(
413 const psa_key_attributes_t *attributes,
414 uint8_t *data,
415 size_t data_size,
416 size_t *data_length);
417
Gilles Peskine20628592019-04-19 19:29:50 +0200418/** Retrieve the attributes of a key.
419 *
420 * This function first resets the attribute structure as with
421 * psa_reset_key_attributes(). It then populates the attribute
422 * structure with the attributes of the given key.
423 *
424 * The attributes that were set when creating the key are reported in a
425 * semantically equivalent manner, not necessarily with the same
426 * numerical value or the same bit pattern. In this specification,
427 * all key types, usage flags, algorithms and lifetime values are
428 * equivalent only if they have the same numerical encoding, but this
429 * property may not hold in future versions of this specification or
430 * for implementation-specific values.
431 *
Gilles Peskine20628592019-04-19 19:29:50 +0200432 * \param[in] handle Handle to the key to query.
433 * \param[in,out] attributes On success, the attributes of the key.
434 * On failure, equivalent to a
435 * freshly-initialized structure.
436 *
437 * \retval #PSA_SUCCESS
438 * \retval #PSA_ERROR_INVALID_HANDLE
439 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
440 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
441 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200442psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
443 psa_key_attributes_t *attributes);
444
Gilles Peskine20628592019-04-19 19:29:50 +0200445/** Reset a key attribute structure to a freshly initialized state.
446 *
447 * You must initialize the attribute structure as described in the
448 * documentation of the type #psa_key_attributes_t before calling this
449 * function. Once the structure has been initialized, you may call this
450 * function at any time.
451 *
452 * This function frees any auxiliary resources that the structure
453 * may contain.
454 *
455 * \param[in,out] attributes The attribute structure to reset.
456 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200457void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200458
Gilles Peskine87a5e562019-04-17 12:28:25 +0200459/**@}*/
460
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100461/** \defgroup key_management Key management
462 * @{
463 */
464
Gilles Peskinef535eb22018-11-30 14:08:36 +0100465/** Open a handle to an existing persistent key.
466 *
467 * Open a handle to a key which was previously created with psa_create_key().
468 *
469 * \param lifetime The lifetime of the key. This designates a storage
470 * area where the key material is stored. This must not
471 * be #PSA_KEY_LIFETIME_VOLATILE.
472 * \param id The persistent identifier of the key.
473 * \param[out] handle On success, a handle to a key slot which contains
474 * the data and metadata loaded from the specified
475 * persistent location.
476 *
477 * \retval #PSA_SUCCESS
478 * Success. The application can now use the value of `*handle`
479 * to access the newly allocated key slot.
480 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200481 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100482 * \retval #PSA_ERROR_INVALID_ARGUMENT
483 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
484 * \retval #PSA_ERROR_INVALID_ARGUMENT
485 * \p id is invalid for the specified lifetime.
486 * \retval #PSA_ERROR_NOT_SUPPORTED
487 * \p lifetime is not supported.
488 * \retval #PSA_ERROR_NOT_PERMITTED
489 * The specified key exists, but the application does not have the
490 * permission to access it. Note that this specification does not
491 * define any way to create such a key, but it may be possible
492 * through implementation-specific means.
493 */
494psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
495 psa_key_id_t id,
496 psa_key_handle_t *handle);
497
Gilles Peskinef535eb22018-11-30 14:08:36 +0100498/** Close a key handle.
499 *
500 * If the handle designates a volatile key, destroy the key material and
501 * free all associated resources, just like psa_destroy_key().
502 *
503 * If the handle designates a persistent key, free all resources associated
504 * with the key in volatile memory. The key slot in persistent storage is
505 * not affected and can be opened again later with psa_open_key().
506 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100507 * If the key is currently in use in a multipart operation,
508 * the multipart operation is aborted.
509 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100510 * \param handle The key handle to close.
511 *
512 * \retval #PSA_SUCCESS
513 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100514 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100515 */
516psa_status_t psa_close_key(psa_key_handle_t handle);
517
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100518/**@}*/
519
520/** \defgroup import_export Key import and export
521 * @{
522 */
523
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100524/**
525 * \brief Import a key in binary format.
526 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100527 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100528 * documentation of psa_export_public_key() for the format of public keys
529 * and to the documentation of psa_export_key() for the format for
530 * other key types.
531 *
532 * This specification supports a single format for each key type.
533 * Implementations may support other formats as long as the standard
534 * format is supported. Implementations that support other formats
535 * should ensure that the formats are clearly unambiguous so as to
536 * minimize the risk that an invalid input is accidentally interpreted
537 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100538 *
Gilles Peskine20628592019-04-19 19:29:50 +0200539 * \param[in] attributes The attributes for the new key.
540 * The key size field in \p attributes is
541 * ignored; the actual key size is determined
542 * from the \p data buffer.
543 * \param[out] handle On success, a handle to the newly created key.
544 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100545 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine20628592019-04-19 19:29:50 +0200546 * buffer is interpreted according to the type and,
547 * if applicable, domain parameters declared in
548 * \p attributes.
549 * All implementations must support at least the format
550 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100551 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200552 * the chosen type. Implementations may allow other
553 * formats, but should be conservative: implementations
554 * should err on the side of rejecting content if it
555 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200556 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100557 *
Gilles Peskine28538492018-07-11 17:34:00 +0200558 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100559 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100560 * If the key is persistent, the key material and the key's metadata
561 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200562 * \retval #PSA_ERROR_ALREADY_EXISTS
563 * This is an attempt to create a persistent key, and there is
564 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200565 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200566 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200567 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200568 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200569 * The key attributes, as a whole, are invalid,
Gilles Peskine308b91d2018-02-08 09:47:44 +0100570 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200571 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
572 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
573 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100574 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200575 * \retval #PSA_ERROR_HARDWARE_FAILURE
576 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300577 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300578 * The library has not been previously initialized by psa_crypto_init().
579 * It is implementation-dependent whether a failure to initialize
580 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100581 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200582psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
583 psa_key_handle_t *handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584 const uint8_t *data,
585 size_t data_length);
586
587/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100588 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200589 *
590 * This function destroys the content of the key slot from both volatile
591 * memory and, if applicable, non-volatile storage. Implementations shall
592 * make a best effort to ensure that any previous content of the slot is
593 * unrecoverable.
594 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100595 * This function also erases any metadata such as policies and frees all
596 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200597 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100598 * If the key is currently in use in a multipart operation,
599 * the multipart operation is aborted.
600 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100601 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100602 *
Gilles Peskine28538492018-07-11 17:34:00 +0200603 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200604 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200605 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200606 * The slot holds content and cannot be erased because it is
607 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100608 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200609 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200610 * There was an failure in communication with the cryptoprocessor.
611 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200612 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200613 * The storage is corrupted. Implementations shall make a best effort
614 * to erase key material even in this stage, however applications
615 * should be aware that it may be impossible to guarantee that the
616 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200617 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200618 * An unexpected condition which is not a storage corruption or
619 * a communication failure occurred. The cryptoprocessor may have
620 * been compromised.
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 Peskineae32aac2018-11-30 14:39:32 +0100626psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100627
628/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100629 * \brief Export a key in binary format.
630 *
631 * The output of this function can be passed to psa_import_key() to
632 * create an equivalent object.
633 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100634 * If the implementation of psa_import_key() supports other formats
635 * beyond the format specified here, the output from psa_export_key()
636 * must use the representation specified here, not the original
637 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100638 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100639 * For standard key types, the output format is as follows:
640 *
641 * - For symmetric keys (including MAC keys), the format is the
642 * raw bytes of the key.
643 * - For DES, the key data consists of 8 bytes. The parity bits must be
644 * correct.
645 * - For Triple-DES, the format is the concatenation of the
646 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100647 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200648 * is the non-encrypted DER encoding of the representation defined by
649 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
650 * ```
651 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200652 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200653 * modulus INTEGER, -- n
654 * publicExponent INTEGER, -- e
655 * privateExponent INTEGER, -- d
656 * prime1 INTEGER, -- p
657 * prime2 INTEGER, -- q
658 * exponent1 INTEGER, -- d mod (p-1)
659 * exponent2 INTEGER, -- d mod (q-1)
660 * coefficient INTEGER, -- (inverse of q) mod p
661 * }
662 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000663 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
664 * representation of the private key `x` as a big-endian byte string. The
665 * length of the byte string is the private key size in bytes (leading zeroes
666 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200667 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100668 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100669 * a representation of the private value as a `ceiling(m/8)`-byte string
670 * where `m` is the bit size associated with the curve, i.e. the bit size
671 * of the order of the curve's coordinate field. This byte string is
672 * in little-endian order for Montgomery curves (curve types
673 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
674 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
675 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100676 * This is the content of the `privateKey` field of the `ECPrivateKey`
677 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000678 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
679 * format is the representation of the private key `x` as a big-endian byte
680 * string. The length of the byte string is the private key size in bytes
681 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200682 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
683 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100684 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100685 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200686 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200687 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200688 * \param[out] data_length On success, the number of bytes
689 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100690 *
Gilles Peskine28538492018-07-11 17:34:00 +0200691 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100692 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200693 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200694 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100695 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200696 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
697 * The size of the \p data buffer is too small. You can determine a
698 * sufficient buffer size by calling
699 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
700 * where \c type is the key type
701 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200702 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
703 * \retval #PSA_ERROR_HARDWARE_FAILURE
704 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300705 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300706 * The library has not been previously initialized by psa_crypto_init().
707 * It is implementation-dependent whether a failure to initialize
708 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100709 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100710psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100711 uint8_t *data,
712 size_t data_size,
713 size_t *data_length);
714
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100715/**
716 * \brief Export a public key or the public part of a key pair in binary format.
717 *
718 * The output of this function can be passed to psa_import_key() to
719 * create an object that is equivalent to the public key.
720 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000721 * This specification supports a single format for each key type.
722 * Implementations may support other formats as long as the standard
723 * format is supported. Implementations that support other formats
724 * should ensure that the formats are clearly unambiguous so as to
725 * minimize the risk that an invalid input is accidentally interpreted
726 * according to a different format.
727 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000728 * For standard key types, the output format is as follows:
729 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
730 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
731 * ```
732 * RSAPublicKey ::= SEQUENCE {
733 * modulus INTEGER, -- n
734 * publicExponent INTEGER } -- e
735 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000736 * - For elliptic curve public keys (key types for which
737 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
738 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
739 * Let `m` be the bit size associated with the curve, i.e. the bit size of
740 * `q` for a curve over `F_q`. The representation consists of:
741 * - The byte 0x04;
742 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
743 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000744 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
745 * representation of the public key `y = g^x mod p` as a big-endian byte
746 * string. The length of the byte string is the length of the base prime `p`
747 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000748 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
749 * the format is the representation of the public key `y = g^x mod p` as a
750 * big-endian byte string. The length of the byte string is the length of the
751 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100752 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100753 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200754 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200755 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200756 * \param[out] data_length On success, the number of bytes
757 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100758 *
Gilles Peskine28538492018-07-11 17:34:00 +0200759 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100760 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200761 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200762 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200763 * The key is neither a public key nor a key pair.
764 * \retval #PSA_ERROR_NOT_SUPPORTED
765 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
766 * The size of the \p data buffer is too small. You can determine a
767 * sufficient buffer size by calling
768 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
769 * where \c type is the key type
770 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200771 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
772 * \retval #PSA_ERROR_HARDWARE_FAILURE
773 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300774 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300775 * The library has not been previously initialized by psa_crypto_init().
776 * It is implementation-dependent whether a failure to initialize
777 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100778 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100779psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100780 uint8_t *data,
781 size_t data_size,
782 size_t *data_length);
783
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100784/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100785 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100786 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000787 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100788 * This function is primarily useful to copy a key from one location
789 * to another, since it populates a key using the material from
790 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200791 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100792 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100793 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100794 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100795 *
Gilles Peskine20628592019-04-19 19:29:50 +0200796 * The resulting key may only be used in a way that conforms to
797 * both the policy of the original key and the policy specified in
798 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100799 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200800 * usage flags on the source policy and the usage flags in \p attributes.
801 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100802 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200803 * - If either of the policies allows an algorithm and the other policy
804 * allows a wildcard-based algorithm policy that includes this algorithm,
805 * the resulting key allows the same algorithm.
806 * - If the policies do not allow any algorithm in common, this function
807 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200808 *
Gilles Peskine20628592019-04-19 19:29:50 +0200809 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100810 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200811 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100812 * \param source_handle The key to copy. It must be a handle to an
813 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200814 * \param[in] attributes The attributes for the new key.
815 * They are used as follows:
816 * - The key type, key size and domain parameters
817 * are ignored. This information is copied
818 * from the source key.
819 * - The key location (the lifetime and, for
820 * persistent keys, the key identifier) is
821 * used directly.
822 * - The policy constraints (usage flags and
823 * algorithm policy) are combined from
824 * the source key and \p attributes so that
825 * both sets of restrictions apply, as
826 * described in the documentation of this function.
827 * \param[out] target_handle On success, a handle to the newly created key.
828 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200829 *
830 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100831 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200832 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200833 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200834 * This is an attempt to create a persistent key, and there is
835 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200836 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200837 * The lifetime or identifier in \p attributes are invalid.
838 * \retval #PSA_ERROR_INVALID_ARGUMENT
839 * The policy constraints on the source and specified in
840 * \p attributes are incompatible.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100841 * \retval #PSA_ERROR_NOT_PERMITTED
842 * The source key is not exportable and its lifetime does not
843 * allow copying it to the target's lifetime.
844 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
845 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200846 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
847 * \retval #PSA_ERROR_HARDWARE_FAILURE
848 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100849 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100850psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200851 const psa_key_attributes_t *attributes,
852 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100853
854/**@}*/
855
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100856/** \defgroup hash Message digests
857 * @{
858 */
859
Gilles Peskine69647a42019-01-14 20:18:12 +0100860/** Calculate the hash (digest) of a message.
861 *
862 * \note To verify the hash of a message against an
863 * expected value, use psa_hash_compare() instead.
864 *
865 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
866 * such that #PSA_ALG_IS_HASH(\p alg) is true).
867 * \param[in] input Buffer containing the message to hash.
868 * \param input_length Size of the \p input buffer in bytes.
869 * \param[out] hash Buffer where the hash is to be written.
870 * \param hash_size Size of the \p hash buffer in bytes.
871 * \param[out] hash_length On success, the number of bytes
872 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100873 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100874 *
875 * \retval #PSA_SUCCESS
876 * Success.
877 * \retval #PSA_ERROR_NOT_SUPPORTED
878 * \p alg is not supported or is not a hash algorithm.
879 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
880 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
881 * \retval #PSA_ERROR_HARDWARE_FAILURE
882 * \retval #PSA_ERROR_TAMPERING_DETECTED
883 */
884psa_status_t psa_hash_compute(psa_algorithm_t alg,
885 const uint8_t *input,
886 size_t input_length,
887 uint8_t *hash,
888 size_t hash_size,
889 size_t *hash_length);
890
891/** Calculate the hash (digest) of a message and compare it with a
892 * reference value.
893 *
894 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
895 * such that #PSA_ALG_IS_HASH(\p alg) is true).
896 * \param[in] input Buffer containing the message to hash.
897 * \param input_length Size of the \p input buffer in bytes.
898 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100899 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100900 *
901 * \retval #PSA_SUCCESS
902 * The expected hash is identical to the actual hash of the input.
903 * \retval #PSA_ERROR_INVALID_SIGNATURE
904 * The hash of the message was calculated successfully, but it
905 * differs from the expected hash.
906 * \retval #PSA_ERROR_NOT_SUPPORTED
907 * \p alg is not supported or is not a hash algorithm.
908 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
909 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
910 * \retval #PSA_ERROR_HARDWARE_FAILURE
911 * \retval #PSA_ERROR_TAMPERING_DETECTED
912 */
913psa_status_t psa_hash_compare(psa_algorithm_t alg,
914 const uint8_t *input,
915 size_t input_length,
916 const uint8_t *hash,
917 const size_t hash_length);
918
Gilles Peskine308b91d2018-02-08 09:47:44 +0100919/** The type of the state data structure for multipart hash operations.
920 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000921 * Before calling any function on a hash operation object, the application must
922 * initialize it by any of the following means:
923 * - Set the structure to all-bits-zero, for example:
924 * \code
925 * psa_hash_operation_t operation;
926 * memset(&operation, 0, sizeof(operation));
927 * \endcode
928 * - Initialize the structure to logical zero values, for example:
929 * \code
930 * psa_hash_operation_t operation = {0};
931 * \endcode
932 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
933 * for example:
934 * \code
935 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
936 * \endcode
937 * - Assign the result of the function psa_hash_operation_init()
938 * to the structure, for example:
939 * \code
940 * psa_hash_operation_t operation;
941 * operation = psa_hash_operation_init();
942 * \endcode
943 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100944 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100945 * make any assumptions about the content of this structure except
946 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100947typedef struct psa_hash_operation_s psa_hash_operation_t;
948
Jaeden Amero6a25b412019-01-04 11:47:44 +0000949/** \def PSA_HASH_OPERATION_INIT
950 *
951 * This macro returns a suitable initializer for a hash operation object
952 * of type #psa_hash_operation_t.
953 */
954#ifdef __DOXYGEN_ONLY__
955/* This is an example definition for documentation purposes.
956 * Implementations should define a suitable value in `crypto_struct.h`.
957 */
958#define PSA_HASH_OPERATION_INIT {0}
959#endif
960
961/** Return an initial value for a hash operation object.
962 */
963static psa_hash_operation_t psa_hash_operation_init(void);
964
Gilles Peskinef45adda2019-01-14 18:29:18 +0100965/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100966 *
967 * The sequence of operations to calculate a hash (message digest)
968 * is as follows:
969 * -# Allocate an operation object which will be passed to all the functions
970 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000971 * -# Initialize the operation object with one of the methods described in the
972 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200973 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100974 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100975 * of the message each time. The hash that is calculated is the hash
976 * of the concatenation of these messages in order.
977 * -# To calculate the hash, call psa_hash_finish().
978 * To compare the hash with an expected value, call psa_hash_verify().
979 *
980 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000981 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100982 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200983 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100984 * eventually terminate the operation. The following events terminate an
985 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100986 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100987 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100988 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000989 * \param[in,out] operation The operation object to set up. It must have
990 * been initialized as per the documentation for
991 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200992 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
993 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100994 *
Gilles Peskine28538492018-07-11 17:34:00 +0200995 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100996 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200997 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200998 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100999 * \retval #PSA_ERROR_BAD_STATE
1000 * The operation state is not valid (already set up and not
1001 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001002 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1003 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1004 * \retval #PSA_ERROR_HARDWARE_FAILURE
1005 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001006 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001007psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001008 psa_algorithm_t alg);
1009
Gilles Peskine308b91d2018-02-08 09:47:44 +01001010/** Add a message fragment to a multipart hash operation.
1011 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001012 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001013 *
1014 * If this function returns an error status, the operation becomes inactive.
1015 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001016 * \param[in,out] operation Active hash operation.
1017 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001018 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001019 *
Gilles Peskine28538492018-07-11 17:34:00 +02001020 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001021 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001022 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001023 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001024 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1025 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1026 * \retval #PSA_ERROR_HARDWARE_FAILURE
1027 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001028 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001029psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1030 const uint8_t *input,
1031 size_t input_length);
1032
Gilles Peskine308b91d2018-02-08 09:47:44 +01001033/** Finish the calculation of the hash of a message.
1034 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001035 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001036 * This function calculates the hash of the message formed by concatenating
1037 * the inputs passed to preceding calls to psa_hash_update().
1038 *
1039 * When this function returns, the operation becomes inactive.
1040 *
1041 * \warning Applications should not call this function if they expect
1042 * a specific value for the hash. Call psa_hash_verify() instead.
1043 * Beware that comparing integrity or authenticity data such as
1044 * hash values with a function such as \c memcmp is risky
1045 * because the time taken by the comparison may leak information
1046 * about the hashed data which could allow an attacker to guess
1047 * a valid hash and thereby bypass security controls.
1048 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001049 * \param[in,out] operation Active hash operation.
1050 * \param[out] hash Buffer where the hash is to be written.
1051 * \param hash_size Size of the \p hash buffer in bytes.
1052 * \param[out] hash_length On success, the number of bytes
1053 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001054 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001055 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001056 *
Gilles Peskine28538492018-07-11 17:34:00 +02001057 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001058 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001059 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001060 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001061 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001062 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001063 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001064 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001065 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1066 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1067 * \retval #PSA_ERROR_HARDWARE_FAILURE
1068 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001069 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001070psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1071 uint8_t *hash,
1072 size_t hash_size,
1073 size_t *hash_length);
1074
Gilles Peskine308b91d2018-02-08 09:47:44 +01001075/** Finish the calculation of the hash of a message and compare it with
1076 * an expected value.
1077 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001078 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001079 * This function calculates the hash of the message formed by concatenating
1080 * the inputs passed to preceding calls to psa_hash_update(). It then
1081 * compares the calculated hash with the expected hash passed as a
1082 * parameter to this function.
1083 *
1084 * When this function returns, the operation becomes inactive.
1085 *
Gilles Peskine19067982018-03-20 17:54:53 +01001086 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001087 * comparison between the actual hash and the expected hash is performed
1088 * in constant time.
1089 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001090 * \param[in,out] operation Active hash operation.
1091 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001092 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001093 *
Gilles Peskine28538492018-07-11 17:34:00 +02001094 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001095 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001096 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001097 * The hash of the message was calculated successfully, but it
1098 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001099 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001100 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001101 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1102 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1103 * \retval #PSA_ERROR_HARDWARE_FAILURE
1104 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001105 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001106psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1107 const uint8_t *hash,
1108 size_t hash_length);
1109
Gilles Peskine308b91d2018-02-08 09:47:44 +01001110/** Abort a hash operation.
1111 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001112 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001113 * \p operation structure itself. Once aborted, the operation object
1114 * can be reused for another operation by calling
1115 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001116 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001117 * You may call this function any time after the operation object has
1118 * been initialized by any of the following methods:
1119 * - A call to psa_hash_setup(), whether it succeeds or not.
1120 * - Initializing the \c struct to all-bits-zero.
1121 * - Initializing the \c struct to logical zeros, e.g.
1122 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001123 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001124 * In particular, calling psa_hash_abort() after the operation has been
1125 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1126 * psa_hash_verify() is safe and has no effect.
1127 *
1128 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001129 *
Gilles Peskine28538492018-07-11 17:34:00 +02001130 * \retval #PSA_SUCCESS
1131 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001132 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001133 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1134 * \retval #PSA_ERROR_HARDWARE_FAILURE
1135 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001136 */
1137psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001138
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001139/** Clone a hash operation.
1140 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001141 * This function copies the state of an ongoing hash operation to
1142 * a new operation object. In other words, this function is equivalent
1143 * to calling psa_hash_setup() on \p target_operation with the same
1144 * algorithm that \p source_operation was set up for, then
1145 * psa_hash_update() on \p target_operation with the same input that
1146 * that was passed to \p source_operation. After this function returns, the
1147 * two objects are independent, i.e. subsequent calls involving one of
1148 * the objects do not affect the other object.
1149 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001150 * \param[in] source_operation The active hash operation to clone.
1151 * \param[in,out] target_operation The operation object to set up.
1152 * It must be initialized but not active.
1153 *
1154 * \retval #PSA_SUCCESS
1155 * \retval #PSA_ERROR_BAD_STATE
1156 * \p source_operation is not an active hash operation.
1157 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001158 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001159 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1160 * \retval #PSA_ERROR_HARDWARE_FAILURE
1161 * \retval #PSA_ERROR_TAMPERING_DETECTED
1162 */
1163psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1164 psa_hash_operation_t *target_operation);
1165
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001166/**@}*/
1167
Gilles Peskine8c9def32018-02-08 10:02:12 +01001168/** \defgroup MAC Message authentication codes
1169 * @{
1170 */
1171
Gilles Peskine69647a42019-01-14 20:18:12 +01001172/** Calculate the MAC (message authentication code) of a message.
1173 *
1174 * \note To verify the MAC of a message against an
1175 * expected value, use psa_mac_verify() instead.
1176 * Beware that comparing integrity or authenticity data such as
1177 * MAC values with a function such as \c memcmp is risky
1178 * because the time taken by the comparison may leak information
1179 * about the MAC value which could allow an attacker to guess
1180 * a valid MAC and thereby bypass security controls.
1181 *
1182 * \param handle Handle to the key to use for the operation.
1183 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001184 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001185 * \param[in] input Buffer containing the input message.
1186 * \param input_length Size of the \p input buffer in bytes.
1187 * \param[out] mac Buffer where the MAC value is to be written.
1188 * \param mac_size Size of the \p mac buffer in bytes.
1189 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001190 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001191 *
1192 * \retval #PSA_SUCCESS
1193 * Success.
1194 * \retval #PSA_ERROR_INVALID_HANDLE
1195 * \retval #PSA_ERROR_EMPTY_SLOT
1196 * \retval #PSA_ERROR_NOT_PERMITTED
1197 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001198 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001199 * \retval #PSA_ERROR_NOT_SUPPORTED
1200 * \p alg is not supported or is not a MAC algorithm.
1201 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1202 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1203 * \retval #PSA_ERROR_HARDWARE_FAILURE
1204 * \retval #PSA_ERROR_TAMPERING_DETECTED
1205 * \retval #PSA_ERROR_BAD_STATE
1206 * The library has not been previously initialized by psa_crypto_init().
1207 * It is implementation-dependent whether a failure to initialize
1208 * results in this error code.
1209 */
1210psa_status_t psa_mac_compute(psa_key_handle_t handle,
1211 psa_algorithm_t alg,
1212 const uint8_t *input,
1213 size_t input_length,
1214 uint8_t *mac,
1215 size_t mac_size,
1216 size_t *mac_length);
1217
1218/** Calculate the MAC of a message and compare it with a reference value.
1219 *
1220 * \param handle Handle to the key to use for the operation.
1221 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001222 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001223 * \param[in] input Buffer containing the input message.
1224 * \param input_length Size of the \p input buffer in bytes.
1225 * \param[out] mac Buffer containing the expected MAC value.
1226 * \param mac_length Size of the \p mac buffer in bytes.
1227 *
1228 * \retval #PSA_SUCCESS
1229 * The expected MAC is identical to the actual MAC of the input.
1230 * \retval #PSA_ERROR_INVALID_SIGNATURE
1231 * The MAC of the message was calculated successfully, but it
1232 * differs from the expected value.
1233 * \retval #PSA_ERROR_INVALID_HANDLE
1234 * \retval #PSA_ERROR_EMPTY_SLOT
1235 * \retval #PSA_ERROR_NOT_PERMITTED
1236 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001237 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001238 * \retval #PSA_ERROR_NOT_SUPPORTED
1239 * \p alg is not supported or is not a MAC algorithm.
1240 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1241 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1242 * \retval #PSA_ERROR_HARDWARE_FAILURE
1243 * \retval #PSA_ERROR_TAMPERING_DETECTED
1244 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001245psa_status_t psa_mac_verify(psa_key_handle_t handle,
1246 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001247 const uint8_t *input,
1248 size_t input_length,
1249 const uint8_t *mac,
1250 const size_t mac_length);
1251
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001252/** The type of the state data structure for multipart MAC operations.
1253 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001254 * Before calling any function on a MAC operation object, the application must
1255 * initialize it by any of the following means:
1256 * - Set the structure to all-bits-zero, for example:
1257 * \code
1258 * psa_mac_operation_t operation;
1259 * memset(&operation, 0, sizeof(operation));
1260 * \endcode
1261 * - Initialize the structure to logical zero values, for example:
1262 * \code
1263 * psa_mac_operation_t operation = {0};
1264 * \endcode
1265 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1266 * for example:
1267 * \code
1268 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1269 * \endcode
1270 * - Assign the result of the function psa_mac_operation_init()
1271 * to the structure, for example:
1272 * \code
1273 * psa_mac_operation_t operation;
1274 * operation = psa_mac_operation_init();
1275 * \endcode
1276 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001277 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001278 * make any assumptions about the content of this structure except
1279 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001280typedef struct psa_mac_operation_s psa_mac_operation_t;
1281
Jaeden Amero769ce272019-01-04 11:48:03 +00001282/** \def PSA_MAC_OPERATION_INIT
1283 *
1284 * This macro returns a suitable initializer for a MAC operation object of type
1285 * #psa_mac_operation_t.
1286 */
1287#ifdef __DOXYGEN_ONLY__
1288/* This is an example definition for documentation purposes.
1289 * Implementations should define a suitable value in `crypto_struct.h`.
1290 */
1291#define PSA_MAC_OPERATION_INIT {0}
1292#endif
1293
1294/** Return an initial value for a MAC operation object.
1295 */
1296static psa_mac_operation_t psa_mac_operation_init(void);
1297
Gilles Peskinef45adda2019-01-14 18:29:18 +01001298/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001299 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001300 * This function sets up the calculation of the MAC
1301 * (message authentication code) of a byte string.
1302 * To verify the MAC of a message against an
1303 * expected value, use psa_mac_verify_setup() instead.
1304 *
1305 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001306 * -# Allocate an operation object which will be passed to all the functions
1307 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001308 * -# Initialize the operation object with one of the methods described in the
1309 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001310 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001311 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1312 * of the message each time. The MAC that is calculated is the MAC
1313 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001314 * -# At the end of the message, call psa_mac_sign_finish() to finish
1315 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001316 *
1317 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001318 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001319 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001320 * After a successful call to psa_mac_sign_setup(), the application must
1321 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001322 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001323 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001324 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001325 * \param[in,out] operation The operation object to set up. It must have
1326 * been initialized as per the documentation for
1327 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001328 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001329 * It must remain valid until the operation
1330 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001331 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001332 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001333 *
Gilles Peskine28538492018-07-11 17:34:00 +02001334 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001335 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001336 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001337 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001338 * \retval #PSA_ERROR_NOT_PERMITTED
1339 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001340 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001341 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001342 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001343 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1344 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1345 * \retval #PSA_ERROR_HARDWARE_FAILURE
1346 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001347 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001348 * The operation state is not valid (already set up and not
1349 * subsequently completed).
1350 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001351 * The library has not been previously initialized by psa_crypto_init().
1352 * It is implementation-dependent whether a failure to initialize
1353 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001354 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001355psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001356 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001357 psa_algorithm_t alg);
1358
Gilles Peskinef45adda2019-01-14 18:29:18 +01001359/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001360 *
1361 * This function sets up the verification of the MAC
1362 * (message authentication code) of a byte string against an expected value.
1363 *
1364 * The sequence of operations to verify a MAC is as follows:
1365 * -# Allocate an operation object which will be passed to all the functions
1366 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001367 * -# Initialize the operation object with one of the methods described in the
1368 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001369 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001370 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1371 * of the message each time. The MAC that is calculated is the MAC
1372 * of the concatenation of these messages in order.
1373 * -# At the end of the message, call psa_mac_verify_finish() to finish
1374 * calculating the actual MAC of the message and verify it against
1375 * the expected value.
1376 *
1377 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001378 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001379 *
1380 * After a successful call to psa_mac_verify_setup(), the application must
1381 * eventually terminate the operation through one of the following methods:
1382 * - A failed call to psa_mac_update().
1383 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1384 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001385 * \param[in,out] operation The operation object to set up. It must have
1386 * been initialized as per the documentation for
1387 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001388 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001389 * It must remain valid until the operation
1390 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001391 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1392 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001393 *
Gilles Peskine28538492018-07-11 17:34:00 +02001394 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001395 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001396 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001397 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001398 * \retval #PSA_ERROR_NOT_PERMITTED
1399 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001400 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001401 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001402 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001403 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1404 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1405 * \retval #PSA_ERROR_HARDWARE_FAILURE
1406 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001407 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001408 * The operation state is not valid (already set up and not
1409 * subsequently completed).
1410 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001411 * The library has not been previously initialized by psa_crypto_init().
1412 * It is implementation-dependent whether a failure to initialize
1413 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001414 */
1415psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001416 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001417 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001418
Gilles Peskinedcd14942018-07-12 00:30:52 +02001419/** Add a message fragment to a multipart MAC operation.
1420 *
1421 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1422 * before calling this function.
1423 *
1424 * If this function returns an error status, the operation becomes inactive.
1425 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001426 * \param[in,out] operation Active MAC operation.
1427 * \param[in] input Buffer containing the message fragment to add to
1428 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001429 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001430 *
1431 * \retval #PSA_SUCCESS
1432 * Success.
1433 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001434 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001435 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1436 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1437 * \retval #PSA_ERROR_HARDWARE_FAILURE
1438 * \retval #PSA_ERROR_TAMPERING_DETECTED
1439 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001440psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1441 const uint8_t *input,
1442 size_t input_length);
1443
Gilles Peskinedcd14942018-07-12 00:30:52 +02001444/** Finish the calculation of the MAC of a message.
1445 *
1446 * The application must call psa_mac_sign_setup() before calling this function.
1447 * This function calculates the MAC of the message formed by concatenating
1448 * the inputs passed to preceding calls to psa_mac_update().
1449 *
1450 * When this function returns, the operation becomes inactive.
1451 *
1452 * \warning Applications should not call this function if they expect
1453 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1454 * Beware that comparing integrity or authenticity data such as
1455 * MAC values with a function such as \c memcmp is risky
1456 * because the time taken by the comparison may leak information
1457 * about the MAC value which could allow an attacker to guess
1458 * a valid MAC and thereby bypass security controls.
1459 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001460 * \param[in,out] operation Active MAC operation.
1461 * \param[out] mac Buffer where the MAC value is to be written.
1462 * \param mac_size Size of the \p mac buffer in bytes.
1463 * \param[out] mac_length On success, the number of bytes
1464 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001465 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001466 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001467 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001468 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001469 *
1470 * \retval #PSA_SUCCESS
1471 * Success.
1472 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001473 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001474 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001475 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001476 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1477 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1478 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1479 * \retval #PSA_ERROR_HARDWARE_FAILURE
1480 * \retval #PSA_ERROR_TAMPERING_DETECTED
1481 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001482psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1483 uint8_t *mac,
1484 size_t mac_size,
1485 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001486
Gilles Peskinedcd14942018-07-12 00:30:52 +02001487/** Finish the calculation of the MAC of a message and compare it with
1488 * an expected value.
1489 *
1490 * The application must call psa_mac_verify_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(). It then
1493 * compares the calculated MAC with the expected MAC passed as a
1494 * parameter to this function.
1495 *
1496 * When this function returns, the operation becomes inactive.
1497 *
1498 * \note Implementations shall make the best effort to ensure that the
1499 * comparison between the actual MAC and the expected MAC is performed
1500 * in constant time.
1501 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001502 * \param[in,out] operation Active MAC operation.
1503 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001504 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001505 *
1506 * \retval #PSA_SUCCESS
1507 * The expected MAC is identical to the actual MAC of the message.
1508 * \retval #PSA_ERROR_INVALID_SIGNATURE
1509 * The MAC of the message was calculated successfully, but it
1510 * differs from the expected MAC.
1511 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001512 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001513 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1514 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1515 * \retval #PSA_ERROR_HARDWARE_FAILURE
1516 * \retval #PSA_ERROR_TAMPERING_DETECTED
1517 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001518psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1519 const uint8_t *mac,
1520 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001521
Gilles Peskinedcd14942018-07-12 00:30:52 +02001522/** Abort a MAC operation.
1523 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001524 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001525 * \p operation structure itself. Once aborted, the operation object
1526 * can be reused for another operation by calling
1527 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001528 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001529 * You may call this function any time after the operation object has
1530 * been initialized by any of the following methods:
1531 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1532 * it succeeds or not.
1533 * - Initializing the \c struct to all-bits-zero.
1534 * - Initializing the \c struct to logical zeros, e.g.
1535 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001536 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001537 * In particular, calling psa_mac_abort() after the operation has been
1538 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1539 * psa_mac_verify_finish() is safe and has no effect.
1540 *
1541 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001542 *
1543 * \retval #PSA_SUCCESS
1544 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001545 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001546 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1547 * \retval #PSA_ERROR_HARDWARE_FAILURE
1548 * \retval #PSA_ERROR_TAMPERING_DETECTED
1549 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001550psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1551
1552/**@}*/
1553
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001554/** \defgroup cipher Symmetric ciphers
1555 * @{
1556 */
1557
Gilles Peskine69647a42019-01-14 20:18:12 +01001558/** Encrypt a message using a symmetric cipher.
1559 *
1560 * This function encrypts a message with a random IV (initialization
1561 * vector).
1562 *
1563 * \param handle Handle to the key to use for the operation.
1564 * It must remain valid until the operation
1565 * terminates.
1566 * \param alg The cipher algorithm to compute
1567 * (\c PSA_ALG_XXX value such that
1568 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1569 * \param[in] input Buffer containing the message to encrypt.
1570 * \param input_length Size of the \p input buffer in bytes.
1571 * \param[out] output Buffer where the output is to be written.
1572 * The output contains the IV followed by
1573 * the ciphertext proper.
1574 * \param output_size Size of the \p output buffer in bytes.
1575 * \param[out] output_length On success, the number of bytes
1576 * that make up the output.
1577 *
1578 * \retval #PSA_SUCCESS
1579 * Success.
1580 * \retval #PSA_ERROR_INVALID_HANDLE
1581 * \retval #PSA_ERROR_EMPTY_SLOT
1582 * \retval #PSA_ERROR_NOT_PERMITTED
1583 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001584 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001585 * \retval #PSA_ERROR_NOT_SUPPORTED
1586 * \p alg is not supported or is not a cipher algorithm.
1587 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1588 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1589 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1590 * \retval #PSA_ERROR_HARDWARE_FAILURE
1591 * \retval #PSA_ERROR_TAMPERING_DETECTED
1592 */
1593psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1594 psa_algorithm_t alg,
1595 const uint8_t *input,
1596 size_t input_length,
1597 uint8_t *output,
1598 size_t output_size,
1599 size_t *output_length);
1600
1601/** Decrypt a message using a symmetric cipher.
1602 *
1603 * This function decrypts a message encrypted with a symmetric cipher.
1604 *
1605 * \param handle Handle to the key to use for the operation.
1606 * It must remain valid until the operation
1607 * terminates.
1608 * \param alg The cipher algorithm to compute
1609 * (\c PSA_ALG_XXX value such that
1610 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1611 * \param[in] input Buffer containing the message to decrypt.
1612 * This consists of the IV followed by the
1613 * ciphertext proper.
1614 * \param input_length Size of the \p input buffer in bytes.
1615 * \param[out] output Buffer where the plaintext is to be written.
1616 * \param output_size Size of the \p output buffer in bytes.
1617 * \param[out] output_length On success, the number of bytes
1618 * that make up the output.
1619 *
1620 * \retval #PSA_SUCCESS
1621 * Success.
1622 * \retval #PSA_ERROR_INVALID_HANDLE
1623 * \retval #PSA_ERROR_EMPTY_SLOT
1624 * \retval #PSA_ERROR_NOT_PERMITTED
1625 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001626 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001627 * \retval #PSA_ERROR_NOT_SUPPORTED
1628 * \p alg is not supported or is not a cipher algorithm.
1629 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1630 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1631 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1632 * \retval #PSA_ERROR_HARDWARE_FAILURE
1633 * \retval #PSA_ERROR_TAMPERING_DETECTED
1634 */
1635psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1636 psa_algorithm_t alg,
1637 const uint8_t *input,
1638 size_t input_length,
1639 uint8_t *output,
1640 size_t output_size,
1641 size_t *output_length);
1642
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001643/** The type of the state data structure for multipart cipher operations.
1644 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001645 * Before calling any function on a cipher operation object, the application
1646 * must initialize it by any of the following means:
1647 * - Set the structure to all-bits-zero, for example:
1648 * \code
1649 * psa_cipher_operation_t operation;
1650 * memset(&operation, 0, sizeof(operation));
1651 * \endcode
1652 * - Initialize the structure to logical zero values, for example:
1653 * \code
1654 * psa_cipher_operation_t operation = {0};
1655 * \endcode
1656 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1657 * for example:
1658 * \code
1659 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1660 * \endcode
1661 * - Assign the result of the function psa_cipher_operation_init()
1662 * to the structure, for example:
1663 * \code
1664 * psa_cipher_operation_t operation;
1665 * operation = psa_cipher_operation_init();
1666 * \endcode
1667 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001668 * This is an implementation-defined \c struct. Applications should not
1669 * make any assumptions about the content of this structure except
1670 * as directed by the documentation of a specific implementation. */
1671typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1672
Jaeden Amero5bae2272019-01-04 11:48:27 +00001673/** \def PSA_CIPHER_OPERATION_INIT
1674 *
1675 * This macro returns a suitable initializer for a cipher operation object of
1676 * type #psa_cipher_operation_t.
1677 */
1678#ifdef __DOXYGEN_ONLY__
1679/* This is an example definition for documentation purposes.
1680 * Implementations should define a suitable value in `crypto_struct.h`.
1681 */
1682#define PSA_CIPHER_OPERATION_INIT {0}
1683#endif
1684
1685/** Return an initial value for a cipher operation object.
1686 */
1687static psa_cipher_operation_t psa_cipher_operation_init(void);
1688
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001689/** Set the key for a multipart symmetric encryption operation.
1690 *
1691 * The sequence of operations to encrypt a message with a symmetric cipher
1692 * is as follows:
1693 * -# Allocate an operation object which will be passed to all the functions
1694 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001695 * -# Initialize the operation object with one of the methods described in the
1696 * documentation for #psa_cipher_operation_t, e.g.
1697 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001698 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001699 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001700 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001701 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001702 * requires a specific IV value.
1703 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1704 * of the message each time.
1705 * -# Call psa_cipher_finish().
1706 *
1707 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001708 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001709 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001710 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001711 * eventually terminate the operation. The following events terminate an
1712 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001713 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001714 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001715 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001716 * \param[in,out] operation The operation object to set up. It must have
1717 * been initialized as per the documentation for
1718 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001719 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001720 * It must remain valid until the operation
1721 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001722 * \param alg The cipher algorithm to compute
1723 * (\c PSA_ALG_XXX value such that
1724 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001725 *
Gilles Peskine28538492018-07-11 17:34:00 +02001726 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001727 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001728 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001729 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001730 * \retval #PSA_ERROR_NOT_PERMITTED
1731 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001732 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001733 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001734 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001735 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1736 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1737 * \retval #PSA_ERROR_HARDWARE_FAILURE
1738 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001739 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001740 * The operation state is not valid (already set up and not
1741 * subsequently completed).
1742 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001743 * The library has not been previously initialized by psa_crypto_init().
1744 * It is implementation-dependent whether a failure to initialize
1745 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001746 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001747psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001748 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001749 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001750
1751/** Set the key for a multipart symmetric decryption operation.
1752 *
1753 * The sequence of operations to decrypt a message with a symmetric cipher
1754 * is as follows:
1755 * -# Allocate an operation object which will be passed to all the functions
1756 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001757 * -# Initialize the operation object with one of the methods described in the
1758 * documentation for #psa_cipher_operation_t, e.g.
1759 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001760 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001761 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001762 * decryption. If the IV is prepended to the ciphertext, you can call
1763 * psa_cipher_update() on a buffer containing the IV followed by the
1764 * beginning of the message.
1765 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1766 * of the message each time.
1767 * -# Call psa_cipher_finish().
1768 *
1769 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001770 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001771 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001772 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001773 * eventually terminate the operation. The following events terminate an
1774 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001775 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001776 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001777 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001778 * \param[in,out] operation The operation object to set up. It must have
1779 * been initialized as per the documentation for
1780 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001781 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001782 * It must remain valid until the operation
1783 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001784 * \param alg The cipher algorithm to compute
1785 * (\c PSA_ALG_XXX value such that
1786 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001787 *
Gilles Peskine28538492018-07-11 17:34:00 +02001788 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001789 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001790 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001791 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001792 * \retval #PSA_ERROR_NOT_PERMITTED
1793 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001794 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001795 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001796 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001797 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1798 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1799 * \retval #PSA_ERROR_HARDWARE_FAILURE
1800 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001801 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001802 * The operation state is not valid (already set up and not
1803 * subsequently completed).
1804 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001805 * The library has not been previously initialized by psa_crypto_init().
1806 * It is implementation-dependent whether a failure to initialize
1807 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001808 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001809psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001810 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001811 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001812
Gilles Peskinedcd14942018-07-12 00:30:52 +02001813/** Generate an IV for a symmetric encryption operation.
1814 *
1815 * This function generates a random IV (initialization vector), nonce
1816 * or initial counter value for the encryption operation as appropriate
1817 * for the chosen algorithm, key type and key size.
1818 *
1819 * The application must call psa_cipher_encrypt_setup() before
1820 * calling this function.
1821 *
1822 * If this function returns an error status, the operation becomes inactive.
1823 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001824 * \param[in,out] operation Active cipher operation.
1825 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001826 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001827 * \param[out] iv_length On success, the number of bytes of the
1828 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001829 *
1830 * \retval #PSA_SUCCESS
1831 * Success.
1832 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001833 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001834 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001835 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001836 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1837 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1838 * \retval #PSA_ERROR_HARDWARE_FAILURE
1839 * \retval #PSA_ERROR_TAMPERING_DETECTED
1840 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001841psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1842 unsigned char *iv,
1843 size_t iv_size,
1844 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001845
Gilles Peskinedcd14942018-07-12 00:30:52 +02001846/** Set the IV for a symmetric encryption or decryption operation.
1847 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001848 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001849 * or initial counter value for the encryption or decryption operation.
1850 *
1851 * The application must call psa_cipher_encrypt_setup() before
1852 * calling this function.
1853 *
1854 * If this function returns an error status, the operation becomes inactive.
1855 *
1856 * \note When encrypting, applications should use psa_cipher_generate_iv()
1857 * instead of this function, unless implementing a protocol that requires
1858 * a non-random IV.
1859 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001860 * \param[in,out] operation Active cipher operation.
1861 * \param[in] iv Buffer containing the IV to use.
1862 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001863 *
1864 * \retval #PSA_SUCCESS
1865 * Success.
1866 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001867 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001868 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001869 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001870 * or the chosen algorithm does not use an IV.
1871 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1872 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1873 * \retval #PSA_ERROR_HARDWARE_FAILURE
1874 * \retval #PSA_ERROR_TAMPERING_DETECTED
1875 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001876psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1877 const unsigned char *iv,
1878 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001879
Gilles Peskinedcd14942018-07-12 00:30:52 +02001880/** Encrypt or decrypt a message fragment in an active cipher operation.
1881 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001882 * Before calling this function, you must:
1883 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1884 * The choice of setup function determines whether this function
1885 * encrypts or decrypts its input.
1886 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1887 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001888 *
1889 * If this function returns an error status, the operation becomes inactive.
1890 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001891 * \param[in,out] operation Active cipher operation.
1892 * \param[in] input Buffer containing the message fragment to
1893 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001894 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001895 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001896 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001897 * \param[out] output_length On success, the number of bytes
1898 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001899 *
1900 * \retval #PSA_SUCCESS
1901 * Success.
1902 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001903 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001904 * not set, or already completed).
1905 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1906 * The size of the \p output buffer is too small.
1907 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1908 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1909 * \retval #PSA_ERROR_HARDWARE_FAILURE
1910 * \retval #PSA_ERROR_TAMPERING_DETECTED
1911 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001912psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1913 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001914 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001915 unsigned char *output,
1916 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001917 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001918
Gilles Peskinedcd14942018-07-12 00:30:52 +02001919/** Finish encrypting or decrypting a message in a cipher operation.
1920 *
1921 * The application must call psa_cipher_encrypt_setup() or
1922 * psa_cipher_decrypt_setup() before calling this function. The choice
1923 * of setup function determines whether this function encrypts or
1924 * decrypts its input.
1925 *
1926 * This function finishes the encryption or decryption of the message
1927 * formed by concatenating the inputs passed to preceding calls to
1928 * psa_cipher_update().
1929 *
1930 * When this function returns, the operation becomes inactive.
1931 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001932 * \param[in,out] operation Active cipher operation.
1933 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001934 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001935 * \param[out] output_length On success, the number of bytes
1936 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001937 *
1938 * \retval #PSA_SUCCESS
1939 * Success.
1940 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001941 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001942 * not set, or already completed).
1943 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1944 * The size of the \p output buffer is too small.
1945 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1946 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1947 * \retval #PSA_ERROR_HARDWARE_FAILURE
1948 * \retval #PSA_ERROR_TAMPERING_DETECTED
1949 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001950psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001951 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001952 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001953 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001954
Gilles Peskinedcd14942018-07-12 00:30:52 +02001955/** Abort a cipher operation.
1956 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001957 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001958 * \p operation structure itself. Once aborted, the operation object
1959 * can be reused for another operation by calling
1960 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001961 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001962 * You may call this function any time after the operation object has
1963 * been initialized by any of the following methods:
1964 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1965 * whether it succeeds or not.
1966 * - Initializing the \c struct to all-bits-zero.
1967 * - Initializing the \c struct to logical zeros, e.g.
1968 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001969 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001970 * In particular, calling psa_cipher_abort() after the operation has been
1971 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1972 * is safe and has no effect.
1973 *
1974 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001975 *
1976 * \retval #PSA_SUCCESS
1977 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001978 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001979 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1980 * \retval #PSA_ERROR_HARDWARE_FAILURE
1981 * \retval #PSA_ERROR_TAMPERING_DETECTED
1982 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001983psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1984
1985/**@}*/
1986
Gilles Peskine3b555712018-03-03 21:27:57 +01001987/** \defgroup aead Authenticated encryption with associated data (AEAD)
1988 * @{
1989 */
1990
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001991/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001992 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001993 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001994 * \param alg The AEAD algorithm to compute
1995 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001996 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001997 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001998 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001999 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002000 * but not encrypted.
2001 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002002 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002003 * encrypted.
2004 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002005 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002006 * encrypted data. The additional data is not
2007 * part of this output. For algorithms where the
2008 * encrypted data and the authentication tag
2009 * are defined as separate outputs, the
2010 * authentication tag is appended to the
2011 * encrypted data.
2012 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2013 * This must be at least
2014 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2015 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002016 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002017 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002018 *
Gilles Peskine28538492018-07-11 17:34:00 +02002019 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002020 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002021 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002022 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002023 * \retval #PSA_ERROR_NOT_PERMITTED
2024 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002025 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002026 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002027 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002028 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2029 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2030 * \retval #PSA_ERROR_HARDWARE_FAILURE
2031 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002032 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002033 * The library has not been previously initialized by psa_crypto_init().
2034 * It is implementation-dependent whether a failure to initialize
2035 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002036 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002037psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002038 psa_algorithm_t alg,
2039 const uint8_t *nonce,
2040 size_t nonce_length,
2041 const uint8_t *additional_data,
2042 size_t additional_data_length,
2043 const uint8_t *plaintext,
2044 size_t plaintext_length,
2045 uint8_t *ciphertext,
2046 size_t ciphertext_size,
2047 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002048
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002049/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002050 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002051 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002052 * \param alg The AEAD algorithm to compute
2053 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002054 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002055 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002056 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002057 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002058 * but not encrypted.
2059 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002060 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002061 * encrypted. For algorithms where the
2062 * encrypted data and the authentication tag
2063 * are defined as separate inputs, the buffer
2064 * must contain the encrypted data followed
2065 * by the authentication tag.
2066 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002067 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002068 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2069 * This must be at least
2070 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2071 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002072 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002073 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002074 *
Gilles Peskine28538492018-07-11 17:34:00 +02002075 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002076 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002077 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002078 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002079 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002080 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002081 * \retval #PSA_ERROR_NOT_PERMITTED
2082 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002083 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002084 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002085 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002086 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2087 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2088 * \retval #PSA_ERROR_HARDWARE_FAILURE
2089 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002090 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002091 * The library has not been previously initialized by psa_crypto_init().
2092 * It is implementation-dependent whether a failure to initialize
2093 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002094 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002095psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002096 psa_algorithm_t alg,
2097 const uint8_t *nonce,
2098 size_t nonce_length,
2099 const uint8_t *additional_data,
2100 size_t additional_data_length,
2101 const uint8_t *ciphertext,
2102 size_t ciphertext_length,
2103 uint8_t *plaintext,
2104 size_t plaintext_size,
2105 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002106
Gilles Peskine30a9e412019-01-14 18:36:12 +01002107/** The type of the state data structure for multipart AEAD operations.
2108 *
2109 * Before calling any function on an AEAD operation object, the application
2110 * must initialize it by any of the following means:
2111 * - Set the structure to all-bits-zero, for example:
2112 * \code
2113 * psa_aead_operation_t operation;
2114 * memset(&operation, 0, sizeof(operation));
2115 * \endcode
2116 * - Initialize the structure to logical zero values, for example:
2117 * \code
2118 * psa_aead_operation_t operation = {0};
2119 * \endcode
2120 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2121 * for example:
2122 * \code
2123 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2124 * \endcode
2125 * - Assign the result of the function psa_aead_operation_init()
2126 * to the structure, for example:
2127 * \code
2128 * psa_aead_operation_t operation;
2129 * operation = psa_aead_operation_init();
2130 * \endcode
2131 *
2132 * This is an implementation-defined \c struct. Applications should not
2133 * make any assumptions about the content of this structure except
2134 * as directed by the documentation of a specific implementation. */
2135typedef struct psa_aead_operation_s psa_aead_operation_t;
2136
2137/** \def PSA_AEAD_OPERATION_INIT
2138 *
2139 * This macro returns a suitable initializer for an AEAD operation object of
2140 * type #psa_aead_operation_t.
2141 */
2142#ifdef __DOXYGEN_ONLY__
2143/* This is an example definition for documentation purposes.
2144 * Implementations should define a suitable value in `crypto_struct.h`.
2145 */
2146#define PSA_AEAD_OPERATION_INIT {0}
2147#endif
2148
2149/** Return an initial value for an AEAD operation object.
2150 */
2151static psa_aead_operation_t psa_aead_operation_init(void);
2152
2153/** Set the key for a multipart authenticated encryption operation.
2154 *
2155 * The sequence of operations to encrypt a message with authentication
2156 * is as follows:
2157 * -# Allocate an operation object which will be passed to all the functions
2158 * listed here.
2159 * -# Initialize the operation object with one of the methods described in the
2160 * documentation for #psa_aead_operation_t, e.g.
2161 * PSA_AEAD_OPERATION_INIT.
2162 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002163 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2164 * inputs to the subsequent calls to psa_aead_update_ad() and
2165 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2166 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002167 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2168 * generate or set the nonce. You should use
2169 * psa_aead_generate_nonce() unless the protocol you are implementing
2170 * requires a specific nonce value.
2171 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2172 * of the non-encrypted additional authenticated data each time.
2173 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002174 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002175 * -# Call psa_aead_finish().
2176 *
2177 * The application may call psa_aead_abort() at any time after the operation
2178 * has been initialized.
2179 *
2180 * After a successful call to psa_aead_encrypt_setup(), the application must
2181 * eventually terminate the operation. The following events terminate an
2182 * operation:
2183 * - A failed call to any of the \c psa_aead_xxx functions.
2184 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2185 *
2186 * \param[in,out] operation The operation object to set up. It must have
2187 * been initialized as per the documentation for
2188 * #psa_aead_operation_t and not yet in use.
2189 * \param handle Handle to the key to use for the operation.
2190 * It must remain valid until the operation
2191 * terminates.
2192 * \param alg The AEAD algorithm to compute
2193 * (\c PSA_ALG_XXX value such that
2194 * #PSA_ALG_IS_AEAD(\p alg) is true).
2195 *
2196 * \retval #PSA_SUCCESS
2197 * Success.
2198 * \retval #PSA_ERROR_INVALID_HANDLE
2199 * \retval #PSA_ERROR_EMPTY_SLOT
2200 * \retval #PSA_ERROR_NOT_PERMITTED
2201 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002202 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002203 * \retval #PSA_ERROR_NOT_SUPPORTED
2204 * \p alg is not supported or is not an AEAD algorithm.
2205 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2206 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2207 * \retval #PSA_ERROR_HARDWARE_FAILURE
2208 * \retval #PSA_ERROR_TAMPERING_DETECTED
2209 * \retval #PSA_ERROR_BAD_STATE
2210 * The library has not been previously initialized by psa_crypto_init().
2211 * It is implementation-dependent whether a failure to initialize
2212 * results in this error code.
2213 */
2214psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2215 psa_key_handle_t handle,
2216 psa_algorithm_t alg);
2217
2218/** Set the key for a multipart authenticated decryption operation.
2219 *
2220 * The sequence of operations to decrypt a message with authentication
2221 * is as follows:
2222 * -# Allocate an operation object which will be passed to all the functions
2223 * listed here.
2224 * -# Initialize the operation object with one of the methods described in the
2225 * documentation for #psa_aead_operation_t, e.g.
2226 * PSA_AEAD_OPERATION_INIT.
2227 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002228 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2229 * inputs to the subsequent calls to psa_aead_update_ad() and
2230 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2231 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002232 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2233 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2234 * of the non-encrypted additional authenticated data each time.
2235 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002236 * of the ciphertext to decrypt each time.
2237 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002238 *
2239 * The application may call psa_aead_abort() at any time after the operation
2240 * has been initialized.
2241 *
2242 * After a successful call to psa_aead_decrypt_setup(), the application must
2243 * eventually terminate the operation. The following events terminate an
2244 * operation:
2245 * - A failed call to any of the \c psa_aead_xxx functions.
2246 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2247 *
2248 * \param[in,out] operation The operation object to set up. It must have
2249 * been initialized as per the documentation for
2250 * #psa_aead_operation_t and not yet in use.
2251 * \param handle Handle to the key to use for the operation.
2252 * It must remain valid until the operation
2253 * terminates.
2254 * \param alg The AEAD algorithm to compute
2255 * (\c PSA_ALG_XXX value such that
2256 * #PSA_ALG_IS_AEAD(\p alg) is true).
2257 *
2258 * \retval #PSA_SUCCESS
2259 * Success.
2260 * \retval #PSA_ERROR_INVALID_HANDLE
2261 * \retval #PSA_ERROR_EMPTY_SLOT
2262 * \retval #PSA_ERROR_NOT_PERMITTED
2263 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002264 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002265 * \retval #PSA_ERROR_NOT_SUPPORTED
2266 * \p alg is not supported or is not an AEAD algorithm.
2267 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2268 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2269 * \retval #PSA_ERROR_HARDWARE_FAILURE
2270 * \retval #PSA_ERROR_TAMPERING_DETECTED
2271 * \retval #PSA_ERROR_BAD_STATE
2272 * The library has not been previously initialized by psa_crypto_init().
2273 * It is implementation-dependent whether a failure to initialize
2274 * results in this error code.
2275 */
2276psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2277 psa_key_handle_t handle,
2278 psa_algorithm_t alg);
2279
2280/** Generate a random nonce for an authenticated encryption operation.
2281 *
2282 * This function generates a random nonce for the authenticated encryption
2283 * operation with an appropriate size for the chosen algorithm, key type
2284 * and key size.
2285 *
2286 * The application must call psa_aead_encrypt_setup() before
2287 * calling this function.
2288 *
2289 * If this function returns an error status, the operation becomes inactive.
2290 *
2291 * \param[in,out] operation Active AEAD operation.
2292 * \param[out] nonce Buffer where the generated nonce is to be
2293 * written.
2294 * \param nonce_size Size of the \p nonce buffer in bytes.
2295 * \param[out] nonce_length On success, the number of bytes of the
2296 * generated nonce.
2297 *
2298 * \retval #PSA_SUCCESS
2299 * Success.
2300 * \retval #PSA_ERROR_BAD_STATE
2301 * The operation state is not valid (not set up, or nonce already set).
2302 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2303 * The size of the \p nonce buffer is too small.
2304 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2305 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2306 * \retval #PSA_ERROR_HARDWARE_FAILURE
2307 * \retval #PSA_ERROR_TAMPERING_DETECTED
2308 */
2309psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2310 unsigned char *nonce,
2311 size_t nonce_size,
2312 size_t *nonce_length);
2313
2314/** Set the nonce for an authenticated encryption or decryption operation.
2315 *
2316 * This function sets the nonce for the authenticated
2317 * encryption or decryption operation.
2318 *
2319 * The application must call psa_aead_encrypt_setup() before
2320 * calling this function.
2321 *
2322 * If this function returns an error status, the operation becomes inactive.
2323 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002324 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002325 * instead of this function, unless implementing a protocol that requires
2326 * a non-random IV.
2327 *
2328 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002329 * \param[in] nonce Buffer containing the nonce to use.
2330 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002331 *
2332 * \retval #PSA_SUCCESS
2333 * Success.
2334 * \retval #PSA_ERROR_BAD_STATE
2335 * The operation state is not valid (not set up, or nonce already set).
2336 * \retval #PSA_ERROR_INVALID_ARGUMENT
2337 * The size of \p nonce is not acceptable for the chosen algorithm.
2338 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2339 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2340 * \retval #PSA_ERROR_HARDWARE_FAILURE
2341 * \retval #PSA_ERROR_TAMPERING_DETECTED
2342 */
2343psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2344 const unsigned char *nonce,
2345 size_t nonce_length);
2346
Gilles Peskinebc59c852019-01-17 15:26:08 +01002347/** Declare the lengths of the message and additional data for AEAD.
2348 *
2349 * The application must call this function before calling
2350 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2351 * the operation requires it. If the algorithm does not require it,
2352 * calling this function is optional, but if this function is called
2353 * then the implementation must enforce the lengths.
2354 *
2355 * You may call this function before or after setting the nonce with
2356 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2357 *
2358 * - For #PSA_ALG_CCM, calling this function is required.
2359 * - For the other AEAD algorithms defined in this specification, calling
2360 * this function is not required.
2361 * - For vendor-defined algorithm, refer to the vendor documentation.
2362 *
2363 * \param[in,out] operation Active AEAD operation.
2364 * \param ad_length Size of the non-encrypted additional
2365 * authenticated data in bytes.
2366 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2367 *
2368 * \retval #PSA_SUCCESS
2369 * Success.
2370 * \retval #PSA_ERROR_BAD_STATE
2371 * The operation state is not valid (not set up, already completed,
2372 * or psa_aead_update_ad() or psa_aead_update() already called).
2373 * \retval #PSA_ERROR_INVALID_ARGUMENT
2374 * At least one of the lengths is not acceptable for the chosen
2375 * algorithm.
2376 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2377 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2378 * \retval #PSA_ERROR_HARDWARE_FAILURE
2379 * \retval #PSA_ERROR_TAMPERING_DETECTED
2380 */
2381psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2382 size_t ad_length,
2383 size_t plaintext_length);
2384
Gilles Peskine30a9e412019-01-14 18:36:12 +01002385/** Pass additional data to an active AEAD operation.
2386 *
2387 * Additional data is authenticated, but not encrypted.
2388 *
2389 * You may call this function multiple times to pass successive fragments
2390 * of the additional data. You may not call this function after passing
2391 * data to encrypt or decrypt with psa_aead_update().
2392 *
2393 * Before calling this function, you must:
2394 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2395 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2396 *
2397 * If this function returns an error status, the operation becomes inactive.
2398 *
2399 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2400 * there is no guarantee that the input is valid. Therefore, until
2401 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2402 * treat the input as untrusted and prepare to undo any action that
2403 * depends on the input if psa_aead_verify() returns an error status.
2404 *
2405 * \param[in,out] operation Active AEAD operation.
2406 * \param[in] input Buffer containing the fragment of
2407 * additional data.
2408 * \param input_length Size of the \p input buffer in bytes.
2409 *
2410 * \retval #PSA_SUCCESS
2411 * Success.
2412 * \retval #PSA_ERROR_BAD_STATE
2413 * The operation state is not valid (not set up, nonce not set,
2414 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002415 * \retval #PSA_ERROR_INVALID_ARGUMENT
2416 * The total input length overflows the additional data length that
2417 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002418 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2419 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2420 * \retval #PSA_ERROR_HARDWARE_FAILURE
2421 * \retval #PSA_ERROR_TAMPERING_DETECTED
2422 */
2423psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2424 const uint8_t *input,
2425 size_t input_length);
2426
2427/** Encrypt or decrypt a message fragment in an active AEAD operation.
2428 *
2429 * Before calling this function, you must:
2430 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2431 * The choice of setup function determines whether this function
2432 * encrypts or decrypts its input.
2433 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2434 * 3. Call psa_aead_update_ad() to pass all the additional data.
2435 *
2436 * If this function returns an error status, the operation becomes inactive.
2437 *
2438 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2439 * there is no guarantee that the input is valid. Therefore, until
2440 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2441 * - Do not use the output in any way other than storing it in a
2442 * confidential location. If you take any action that depends
2443 * on the tentative decrypted data, this action will need to be
2444 * undone if the input turns out not to be valid. Furthermore,
2445 * if an adversary can observe that this action took place
2446 * (for example through timing), they may be able to use this
2447 * fact as an oracle to decrypt any message encrypted with the
2448 * same key.
2449 * - In particular, do not copy the output anywhere but to a
2450 * memory or storage space that you have exclusive access to.
2451 *
2452 * \param[in,out] operation Active AEAD operation.
2453 * \param[in] input Buffer containing the message fragment to
2454 * encrypt or decrypt.
2455 * \param input_length Size of the \p input buffer in bytes.
2456 * \param[out] output Buffer where the output is to be written.
2457 * \param output_size Size of the \p output buffer in bytes.
2458 * \param[out] output_length On success, the number of bytes
2459 * that make up the returned output.
2460 *
2461 * \retval #PSA_SUCCESS
2462 * Success.
2463 * \retval #PSA_ERROR_BAD_STATE
2464 * The operation state is not valid (not set up, nonce not set
2465 * or already completed).
2466 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2467 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002468 * \retval #PSA_ERROR_INVALID_ARGUMENT
2469 * The total length of input to psa_aead_update_ad() so far is
2470 * less than the additional data length that was previously
2471 * specified with psa_aead_set_lengths().
2472 * \retval #PSA_ERROR_INVALID_ARGUMENT
2473 * The total input length overflows the plaintext length that
2474 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002475 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2476 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2477 * \retval #PSA_ERROR_HARDWARE_FAILURE
2478 * \retval #PSA_ERROR_TAMPERING_DETECTED
2479 */
2480psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2481 const uint8_t *input,
2482 size_t input_length,
2483 unsigned char *output,
2484 size_t output_size,
2485 size_t *output_length);
2486
2487/** Finish encrypting a message in an AEAD operation.
2488 *
2489 * The operation must have been set up with psa_aead_encrypt_setup().
2490 *
2491 * This function finishes the authentication of the additional data
2492 * formed by concatenating the inputs passed to preceding calls to
2493 * psa_aead_update_ad() with the plaintext formed by concatenating the
2494 * inputs passed to preceding calls to psa_aead_update().
2495 *
2496 * This function has two output buffers:
2497 * - \p ciphertext contains trailing ciphertext that was buffered from
2498 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2499 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2500 * will not contain any output and can be a 0-sized buffer.
2501 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002502 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002503 * that the operation performs.
2504 *
2505 * When this function returns, the operation becomes inactive.
2506 *
2507 * \param[in,out] operation Active AEAD operation.
2508 * \param[out] ciphertext Buffer where the last part of the ciphertext
2509 * is to be written.
2510 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2511 * \param[out] ciphertext_length On success, the number of bytes of
2512 * returned ciphertext.
2513 * \param[out] tag Buffer where the authentication tag is
2514 * to be written.
2515 * \param tag_size Size of the \p tag buffer in bytes.
2516 * \param[out] tag_length On success, the number of bytes
2517 * that make up the returned tag.
2518 *
2519 * \retval #PSA_SUCCESS
2520 * Success.
2521 * \retval #PSA_ERROR_BAD_STATE
2522 * The operation state is not valid (not set up, nonce not set,
2523 * decryption, or already completed).
2524 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002525 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002526 * \retval #PSA_ERROR_INVALID_ARGUMENT
2527 * The total length of input to psa_aead_update_ad() so far is
2528 * less than the additional data length that was previously
2529 * specified with psa_aead_set_lengths().
2530 * \retval #PSA_ERROR_INVALID_ARGUMENT
2531 * The total length of input to psa_aead_update() so far is
2532 * less than the plaintext length that was previously
2533 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002534 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2535 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2536 * \retval #PSA_ERROR_HARDWARE_FAILURE
2537 * \retval #PSA_ERROR_TAMPERING_DETECTED
2538 */
2539psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002540 uint8_t *ciphertext,
2541 size_t ciphertext_size,
2542 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002543 uint8_t *tag,
2544 size_t tag_size,
2545 size_t *tag_length);
2546
2547/** Finish authenticating and decrypting a message in an AEAD operation.
2548 *
2549 * The operation must have been set up with psa_aead_decrypt_setup().
2550 *
2551 * This function finishes the authentication of the additional data
2552 * formed by concatenating the inputs passed to preceding calls to
2553 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2554 * inputs passed to preceding calls to psa_aead_update().
2555 *
2556 * When this function returns, the operation becomes inactive.
2557 *
2558 * \param[in,out] operation Active AEAD operation.
2559 * \param[in] tag Buffer containing the authentication tag.
2560 * \param tag_length Size of the \p tag buffer in bytes.
2561 *
2562 * \retval #PSA_SUCCESS
2563 * Success.
2564 * \retval #PSA_ERROR_BAD_STATE
2565 * The operation state is not valid (not set up, nonce not set,
2566 * encryption, or already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002567 * \retval #PSA_ERROR_INVALID_ARGUMENT
2568 * The total length of input to psa_aead_update_ad() so far is
2569 * less than the additional data length that was previously
2570 * specified with psa_aead_set_lengths().
2571 * \retval #PSA_ERROR_INVALID_ARGUMENT
2572 * The total length of input to psa_aead_update() so far is
2573 * less than the plaintext length that was previously
2574 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002575 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2576 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2577 * \retval #PSA_ERROR_HARDWARE_FAILURE
2578 * \retval #PSA_ERROR_TAMPERING_DETECTED
2579 */
2580psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2581 const uint8_t *tag,
2582 size_t tag_length);
2583
2584/** Abort an AEAD operation.
2585 *
2586 * Aborting an operation frees all associated resources except for the
2587 * \p operation structure itself. Once aborted, the operation object
2588 * can be reused for another operation by calling
2589 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2590 *
2591 * You may call this function any time after the operation object has
2592 * been initialized by any of the following methods:
2593 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2594 * whether it succeeds or not.
2595 * - Initializing the \c struct to all-bits-zero.
2596 * - Initializing the \c struct to logical zeros, e.g.
2597 * `psa_aead_operation_t operation = {0}`.
2598 *
2599 * In particular, calling psa_aead_abort() after the operation has been
2600 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2601 * is safe and has no effect.
2602 *
2603 * \param[in,out] operation Initialized AEAD operation.
2604 *
2605 * \retval #PSA_SUCCESS
2606 * \retval #PSA_ERROR_BAD_STATE
2607 * \p operation is not an active AEAD operation.
2608 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2609 * \retval #PSA_ERROR_HARDWARE_FAILURE
2610 * \retval #PSA_ERROR_TAMPERING_DETECTED
2611 */
2612psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2613
Gilles Peskine3b555712018-03-03 21:27:57 +01002614/**@}*/
2615
Gilles Peskine20035e32018-02-03 22:44:14 +01002616/** \defgroup asymmetric Asymmetric cryptography
2617 * @{
2618 */
2619
2620/**
2621 * \brief Sign a hash or short message with a private key.
2622 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002623 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002624 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002625 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2626 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2627 * to determine the hash algorithm to use.
2628 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002629 * \param handle Handle to the key to use for the operation.
2630 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002631 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002632 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002633 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002634 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002635 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002636 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002637 * \param[out] signature_length On success, the number of bytes
2638 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002639 *
Gilles Peskine28538492018-07-11 17:34:00 +02002640 * \retval #PSA_SUCCESS
2641 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002642 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002643 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002644 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002645 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002646 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002647 * \retval #PSA_ERROR_NOT_SUPPORTED
2648 * \retval #PSA_ERROR_INVALID_ARGUMENT
2649 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2650 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2651 * \retval #PSA_ERROR_HARDWARE_FAILURE
2652 * \retval #PSA_ERROR_TAMPERING_DETECTED
2653 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002654 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002655 * The library has not been previously initialized by psa_crypto_init().
2656 * It is implementation-dependent whether a failure to initialize
2657 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002658 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002659psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002660 psa_algorithm_t alg,
2661 const uint8_t *hash,
2662 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002663 uint8_t *signature,
2664 size_t signature_size,
2665 size_t *signature_length);
2666
2667/**
2668 * \brief Verify the signature a hash or short message using a public key.
2669 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002670 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002671 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002672 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2673 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2674 * to determine the hash algorithm to use.
2675 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002676 * \param handle Handle to the key to use for the operation.
2677 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002678 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002679 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002680 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002681 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002682 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002683 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002684 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002685 *
Gilles Peskine28538492018-07-11 17:34:00 +02002686 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002687 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002688 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002689 * The calculation was perfomed successfully, but the passed
2690 * signature is not a valid signature.
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
itayzafrir90d8c7a2018-09-12 11:44:52 +03002697 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002698 * The library has not been previously initialized by psa_crypto_init().
2699 * It is implementation-dependent whether a failure to initialize
2700 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002701 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002702psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002703 psa_algorithm_t alg,
2704 const uint8_t *hash,
2705 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002706 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002707 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002708
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002709/**
2710 * \brief Encrypt a short message with a public key.
2711 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002712 * \param handle Handle to the key to use for the operation.
2713 * It must be a public key or an asymmetric
2714 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002715 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002716 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002717 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002718 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002719 * \param[in] salt A salt or label, if supported by the
2720 * encryption algorithm.
2721 * If the algorithm does not support a
2722 * salt, pass \c NULL.
2723 * If the algorithm supports an optional
2724 * salt and you do not want to pass a salt,
2725 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002726 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002727 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2728 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002729 * \param salt_length Size of the \p salt buffer in bytes.
2730 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002731 * \param[out] output Buffer where the encrypted message is to
2732 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002733 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002734 * \param[out] output_length On success, the number of bytes
2735 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002736 *
Gilles Peskine28538492018-07-11 17:34:00 +02002737 * \retval #PSA_SUCCESS
2738 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002739 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002740 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002741 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002742 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002743 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002744 * \retval #PSA_ERROR_NOT_SUPPORTED
2745 * \retval #PSA_ERROR_INVALID_ARGUMENT
2746 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2747 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2748 * \retval #PSA_ERROR_HARDWARE_FAILURE
2749 * \retval #PSA_ERROR_TAMPERING_DETECTED
2750 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002751 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002752 * The library has not been previously initialized by psa_crypto_init().
2753 * It is implementation-dependent whether a failure to initialize
2754 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002755 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002756psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002757 psa_algorithm_t alg,
2758 const uint8_t *input,
2759 size_t input_length,
2760 const uint8_t *salt,
2761 size_t salt_length,
2762 uint8_t *output,
2763 size_t output_size,
2764 size_t *output_length);
2765
2766/**
2767 * \brief Decrypt a short message with a private key.
2768 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002769 * \param handle Handle to the key to use for the operation.
2770 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002771 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002772 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002773 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002774 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002775 * \param[in] salt A salt or label, if supported by the
2776 * encryption algorithm.
2777 * If the algorithm does not support a
2778 * salt, pass \c NULL.
2779 * If the algorithm supports an optional
2780 * salt and you do not want to pass a salt,
2781 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002782 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002783 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2784 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002785 * \param salt_length Size of the \p salt buffer in bytes.
2786 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002787 * \param[out] output Buffer where the decrypted message is to
2788 * be written.
2789 * \param output_size Size of the \c output buffer in bytes.
2790 * \param[out] output_length On success, the number of bytes
2791 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002792 *
Gilles Peskine28538492018-07-11 17:34:00 +02002793 * \retval #PSA_SUCCESS
2794 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002795 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002796 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002797 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002798 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002799 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002800 * \retval #PSA_ERROR_NOT_SUPPORTED
2801 * \retval #PSA_ERROR_INVALID_ARGUMENT
2802 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2803 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2804 * \retval #PSA_ERROR_HARDWARE_FAILURE
2805 * \retval #PSA_ERROR_TAMPERING_DETECTED
2806 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2807 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002808 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002809 * The library has not been previously initialized by psa_crypto_init().
2810 * It is implementation-dependent whether a failure to initialize
2811 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002812 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002813psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002814 psa_algorithm_t alg,
2815 const uint8_t *input,
2816 size_t input_length,
2817 const uint8_t *salt,
2818 size_t salt_length,
2819 uint8_t *output,
2820 size_t output_size,
2821 size_t *output_length);
2822
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002823/**@}*/
2824
Gilles Peskineedd76872018-07-20 17:42:05 +02002825/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002826 * @{
2827 */
2828
2829/** The type of the state data structure for generators.
2830 *
2831 * Before calling any function on a generator, the application must
2832 * initialize it by any of the following means:
2833 * - Set the structure to all-bits-zero, for example:
2834 * \code
2835 * psa_crypto_generator_t generator;
2836 * memset(&generator, 0, sizeof(generator));
2837 * \endcode
2838 * - Initialize the structure to logical zero values, for example:
2839 * \code
2840 * psa_crypto_generator_t generator = {0};
2841 * \endcode
2842 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2843 * for example:
2844 * \code
2845 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2846 * \endcode
2847 * - Assign the result of the function psa_crypto_generator_init()
2848 * to the structure, for example:
2849 * \code
2850 * psa_crypto_generator_t generator;
2851 * generator = psa_crypto_generator_init();
2852 * \endcode
2853 *
2854 * This is an implementation-defined \c struct. Applications should not
2855 * make any assumptions about the content of this structure except
2856 * as directed by the documentation of a specific implementation.
2857 */
2858typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2859
2860/** \def PSA_CRYPTO_GENERATOR_INIT
2861 *
2862 * This macro returns a suitable initializer for a generator object
2863 * of type #psa_crypto_generator_t.
2864 */
2865#ifdef __DOXYGEN_ONLY__
2866/* This is an example definition for documentation purposes.
2867 * Implementations should define a suitable value in `crypto_struct.h`.
2868 */
2869#define PSA_CRYPTO_GENERATOR_INIT {0}
2870#endif
2871
2872/** Return an initial value for a generator object.
2873 */
2874static psa_crypto_generator_t psa_crypto_generator_init(void);
2875
2876/** Retrieve the current capacity of a generator.
2877 *
2878 * The capacity of a generator is the maximum number of bytes that it can
2879 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2880 *
2881 * \param[in] generator The generator to query.
2882 * \param[out] capacity On success, the capacity of the generator.
2883 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002884 * \retval #PSA_SUCCESS
2885 * \retval #PSA_ERROR_BAD_STATE
2886 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002887 */
2888psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2889 size_t *capacity);
2890
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002891/** Set the maximum capacity of a generator.
2892 *
2893 * \param[in,out] generator The generator object to modify.
2894 * \param capacity The new capacity of the generator.
2895 * It must be less or equal to the generator's
2896 * current capacity.
2897 *
2898 * \retval #PSA_SUCCESS
2899 * \retval #PSA_ERROR_INVALID_ARGUMENT
2900 * \p capacity is larger than the generator's current capacity.
2901 * \retval #PSA_ERROR_BAD_STATE
2902 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2903 */
2904psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2905 size_t capacity);
2906
Gilles Peskineeab56e42018-07-12 17:12:33 +02002907/** Read some data from a generator.
2908 *
2909 * This function reads and returns a sequence of bytes from a generator.
2910 * The data that is read is discarded from the generator. The generator's
2911 * capacity is decreased by the number of bytes read.
2912 *
2913 * \param[in,out] generator The generator object to read from.
2914 * \param[out] output Buffer where the generator output will be
2915 * written.
2916 * \param output_length Number of bytes to output.
2917 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002918 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02002919 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02002920 * There were fewer than \p output_length bytes
2921 * in the generator. Note that in this case, no
2922 * output is written to the output buffer.
2923 * The generator's capacity is set to 0, thus
2924 * subsequent calls to this function will not
2925 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002926 * \retval #PSA_ERROR_BAD_STATE
2927 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2928 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2929 * \retval #PSA_ERROR_HARDWARE_FAILURE
2930 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002931 */
2932psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2933 uint8_t *output,
2934 size_t output_length);
2935
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002936/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002937 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002938 * This function uses the output of a generator to derive a key.
2939 * How much output it consumes and how the key is derived depends on the
2940 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002941 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002942 * - For key types for which the key is an arbitrary sequence of bytes
2943 * of a given size,
2944 * this function is functionally equivalent to calling #psa_generator_read
2945 * and passing the resulting output to #psa_import_key.
2946 * However, this function has a security benefit:
2947 * if the implementation provides an isolation boundary then
2948 * the key material is not exposed outside the isolation boundary.
2949 * As a consequence, for these key types, this function always consumes
2950 * exactly (\p bits / 8) bytes from the generator.
2951 * The following key types defined in this specification follow this scheme:
2952 *
2953 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002954 * - #PSA_KEY_TYPE_ARC4;
2955 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002956 * - #PSA_KEY_TYPE_DERIVE;
2957 * - #PSA_KEY_TYPE_HMAC.
2958 *
2959 * - For ECC keys on a Montgomery elliptic curve
2960 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
2961 * Montgomery curve), this function always draws a byte string whose
2962 * length is determined by the curve, and sets the mandatory bits
2963 * accordingly. That is:
2964 *
2965 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
2966 * and process it as specified in RFC 7748 &sect;5.
2967 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
2968 * and process it as specified in RFC 7748 &sect;5.
2969 *
2970 * - For key types for which the key is represented by a single sequence of
2971 * \p bits bits with constraints as to which bit sequences are acceptable,
2972 * this function draws a byte string of length (\p bits / 8) bytes rounded
2973 * up to the nearest whole number of bytes. If the resulting byte string
2974 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
2975 * This process is repeated until an acceptable byte string is drawn.
2976 * The byte string drawn from the generator is interpreted as specified
2977 * for the output produced by psa_export_key().
2978 * The following key types defined in this specification follow this scheme:
2979 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01002980 * - #PSA_KEY_TYPE_DES.
2981 * Force-set the parity bits, but discard forbidden weak keys.
2982 * For 2-key and 3-key triple-DES, the three keys are generated
2983 * successively (for example, for 3-key triple-DES,
2984 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
2985 * discard the first 8 bytes, use the next 8 bytes as the first key,
2986 * and continue reading output from the generator to derive the other
2987 * two keys).
2988 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
2989 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
2990 * ECC keys on a Weierstrass elliptic curve
2991 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
2992 * Weierstrass curve).
2993 * For these key types, interpret the byte string as integer
2994 * in big-endian order. Discard it if it is not in the range
2995 * [0, *N* - 2] where *N* is the boundary of the private key domain
2996 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01002997 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01002998 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01002999 * This method allows compliance to NIST standards, specifically
3000 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003001 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3002 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3003 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3004 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003005 *
3006 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
3007 * the way in which the generator output is consumed is
3008 * implementation-defined.
3009 *
3010 * In all cases, the data that is read is discarded from the generator.
3011 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003012 *
Gilles Peskine20628592019-04-19 19:29:50 +02003013 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003014 * \param[out] handle On success, a handle to the newly created key.
3015 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003016 * \param[in,out] generator The generator object to read from.
3017 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003018 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003019 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003020 * If the key is persistent, the key material and the key's metadata
3021 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003022 * \retval #PSA_ERROR_ALREADY_EXISTS
3023 * This is an attempt to create a persistent key, and there is
3024 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003025 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003026 * There was not enough data to create the desired key.
3027 * Note that in this case, no output is written to the output buffer.
3028 * The generator's capacity is set to 0, thus subsequent calls to
3029 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003030 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003031 * The key type or key size is not supported, either by the
3032 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003033 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003034 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3035 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3036 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3037 * \retval #PSA_ERROR_HARDWARE_FAILURE
3038 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003039 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003040 * The library has not been previously initialized by psa_crypto_init().
3041 * It is implementation-dependent whether a failure to initialize
3042 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003043 */
Gilles Peskine87a5e562019-04-17 12:28:25 +02003044psa_status_t psa_generator_import_key(const psa_key_attributes_t *attributes,
3045 psa_key_handle_t *handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003046 psa_crypto_generator_t *generator);
3047
3048/** Abort a generator.
3049 *
3050 * Once a generator has been aborted, its capacity is zero.
3051 * Aborting a generator frees all associated resources except for the
3052 * \c generator structure itself.
3053 *
3054 * This function may be called at any time as long as the generator
3055 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3056 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3057 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3058 * on a generator that has not been set up.
3059 *
3060 * Once aborted, the generator object may be called.
3061 *
3062 * \param[in,out] generator The generator to abort.
3063 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003064 * \retval #PSA_SUCCESS
3065 * \retval #PSA_ERROR_BAD_STATE
3066 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3067 * \retval #PSA_ERROR_HARDWARE_FAILURE
3068 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003069 */
3070psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3071
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003072/** Use the maximum possible capacity for a generator.
3073 *
3074 * Use this value as the capacity argument when setting up a generator
3075 * to indicate that the generator should have the maximum possible capacity.
3076 * The value of the maximum possible capacity depends on the generator
3077 * algorithm.
3078 */
3079#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3080
Gilles Peskineeab56e42018-07-12 17:12:33 +02003081/**@}*/
3082
Gilles Peskineea0fb492018-07-12 17:17:20 +02003083/** \defgroup derivation Key derivation
3084 * @{
3085 */
3086
3087/** Set up a key derivation operation.
3088 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003089 * A key derivation algorithm takes some inputs and uses them to create
3090 * a byte generator which can be used to produce keys and other
3091 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003092 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003093 * To use a generator for key derivation:
3094 * - Start with an initialized object of type #psa_crypto_generator_t.
3095 * - Call psa_key_derivation_setup() to select the algorithm.
3096 * - Provide the inputs for the key derivation by calling
3097 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3098 * as appropriate. Which inputs are needed, in what order, and whether
3099 * they may be keys and if so of what type depends on the algorithm.
3100 * - Optionally set the generator's maximum capacity with
3101 * psa_set_generator_capacity(). You may do this before, in the middle of
3102 * or after providing inputs. For some algorithms, this step is mandatory
3103 * because the output depends on the maximum capacity.
3104 * - Generate output with psa_generator_read() or
3105 * psa_generator_import_key(). Successive calls to these functions
3106 * use successive output bytes from the generator.
3107 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003108 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003109 * \param[in,out] generator The generator object to set up. It must
3110 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003111 * \param alg The key derivation algorithm to compute
3112 * (\c PSA_ALG_XXX value such that
3113 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003114 *
3115 * \retval #PSA_SUCCESS
3116 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003117 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003118 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003119 * \retval #PSA_ERROR_NOT_SUPPORTED
3120 * \c alg is not supported or is not a key derivation algorithm.
3121 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3122 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3123 * \retval #PSA_ERROR_HARDWARE_FAILURE
3124 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003125 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003126 */
3127psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3128 psa_algorithm_t alg);
3129
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003130/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003131 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003132 * Which inputs are required and in what order depends on the algorithm.
3133 * Refer to the documentation of each key derivation or key agreement
3134 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003135 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003136 * This function passes direct inputs. Some inputs must be passed as keys
3137 * using psa_key_derivation_input_key() instead of this function. Refer to
3138 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003139 *
3140 * \param[in,out] generator The generator object to use. It must
3141 * have been set up with
3142 * psa_key_derivation_setup() and must not
3143 * have produced any output yet.
3144 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003145 * \param[in] data Input data to use.
3146 * \param data_length Size of the \p data buffer in bytes.
3147 *
3148 * \retval #PSA_SUCCESS
3149 * Success.
3150 * \retval #PSA_ERROR_INVALID_ARGUMENT
3151 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003152 * \retval #PSA_ERROR_INVALID_ARGUMENT
3153 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003154 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3155 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3156 * \retval #PSA_ERROR_HARDWARE_FAILURE
3157 * \retval #PSA_ERROR_TAMPERING_DETECTED
3158 * \retval #PSA_ERROR_BAD_STATE
3159 * The value of \p step is not valid given the state of \p generator.
3160 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003161 * The library has not been previously initialized by psa_crypto_init().
3162 * It is implementation-dependent whether a failure to initialize
3163 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003164 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003165psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3166 psa_key_derivation_step_t step,
3167 const uint8_t *data,
3168 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003169
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003170/** Provide an input for key derivation in the form of a key.
3171 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003172 * Which inputs are required and in what order depends on the algorithm.
3173 * Refer to the documentation of each key derivation or key agreement
3174 * algorithm for information.
3175 *
3176 * This function passes key inputs. Some inputs must be passed as keys
3177 * of the appropriate type using this function, while others must be
3178 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3179 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003180 *
3181 * \param[in,out] generator The generator object to use. It must
3182 * have been set up with
3183 * psa_key_derivation_setup() and must not
3184 * have produced any output yet.
3185 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003186 * \param handle Handle to the key. It must have an
3187 * appropriate type for \p step and must
3188 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003189 *
3190 * \retval #PSA_SUCCESS
3191 * Success.
3192 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003193 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003194 * \retval #PSA_ERROR_NOT_PERMITTED
3195 * \retval #PSA_ERROR_INVALID_ARGUMENT
3196 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003197 * \retval #PSA_ERROR_INVALID_ARGUMENT
3198 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003199 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3200 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3201 * \retval #PSA_ERROR_HARDWARE_FAILURE
3202 * \retval #PSA_ERROR_TAMPERING_DETECTED
3203 * \retval #PSA_ERROR_BAD_STATE
3204 * The value of \p step is not valid given the state of \p generator.
3205 * \retval #PSA_ERROR_BAD_STATE
3206 * The library has not been previously initialized by psa_crypto_init().
3207 * It is implementation-dependent whether a failure to initialize
3208 * results in this error code.
3209 */
3210psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3211 psa_key_derivation_step_t step,
3212 psa_key_handle_t handle);
3213
Gilles Peskine969c5d62019-01-16 15:53:06 +01003214/** Perform a key agreement and use the shared secret as input to a key
3215 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003216 *
3217 * A key agreement algorithm takes two inputs: a private key \p private_key
3218 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003219 * The result of this function is passed as input to a key derivation.
3220 * The output of this key derivation can be extracted by reading from the
3221 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003222 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003223 * \param[in,out] generator The generator object to use. It must
3224 * have been set up with
3225 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003226 * key agreement and derivation algorithm
3227 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003228 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3229 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003230 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003231 * The generator must be ready for an
3232 * input of the type given by \p step.
3233 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003234 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003235 * \param[in] peer_key Public key of the peer. The peer key must be in the
3236 * same format that psa_import_key() accepts for the
3237 * public key type corresponding to the type of
3238 * private_key. That is, this function performs the
3239 * equivalent of
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003240 * #psa_import_key(`internal_public_key_handle`,
3241 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(`private_key_type`),
3242 * `peer_key`, `peer_key_length`) where
Jaeden Amero8afbff82019-01-14 16:56:20 +00003243 * `private_key_type` is the type of `private_key`.
3244 * For example, for EC keys, this means that peer_key
3245 * is interpreted as a point on the curve that the
3246 * private key is on. The standard formats for public
3247 * keys are documented in the documentation of
3248 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003249 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003250 *
3251 * \retval #PSA_SUCCESS
3252 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003253 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003254 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003255 * \retval #PSA_ERROR_NOT_PERMITTED
3256 * \retval #PSA_ERROR_INVALID_ARGUMENT
3257 * \c private_key is not compatible with \c alg,
3258 * or \p peer_key is not valid for \c alg or not compatible with
3259 * \c private_key.
3260 * \retval #PSA_ERROR_NOT_SUPPORTED
3261 * \c alg is not supported or is not a key derivation algorithm.
3262 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3263 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3264 * \retval #PSA_ERROR_HARDWARE_FAILURE
3265 * \retval #PSA_ERROR_TAMPERING_DETECTED
3266 */
3267psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003268 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003269 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003270 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003271 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003272
Gilles Peskine769c7a62019-01-18 16:42:29 +01003273/** Perform a key agreement and use the shared secret as input to a key
3274 * derivation.
3275 *
3276 * A key agreement algorithm takes two inputs: a private key \p private_key
3277 * a public key \p peer_key.
3278 *
3279 * \warning The raw result of a key agreement algorithm such as finite-field
3280 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3281 * not be used directly as key material. It should instead be passed as
3282 * input to a key derivation algorithm. To chain a key agreement with
3283 * a key derivation, use psa_key_agreement() and other functions from
3284 * the key derivation and generator interface.
3285 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003286 * \param alg The key agreement algorithm to compute
3287 * (\c PSA_ALG_XXX value such that
3288 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3289 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003290 * \param private_key Handle to the private key to use.
3291 * \param[in] peer_key Public key of the peer. It must be
3292 * in the same format that psa_import_key()
3293 * accepts. The standard formats for public
3294 * keys are documented in the documentation
3295 * of psa_export_public_key().
3296 * \param peer_key_length Size of \p peer_key in bytes.
3297 * \param[out] output Buffer where the decrypted message is to
3298 * be written.
3299 * \param output_size Size of the \c output buffer in bytes.
3300 * \param[out] output_length On success, the number of bytes
3301 * that make up the returned output.
3302 *
3303 * \retval #PSA_SUCCESS
3304 * Success.
3305 * \retval #PSA_ERROR_INVALID_HANDLE
3306 * \retval #PSA_ERROR_EMPTY_SLOT
3307 * \retval #PSA_ERROR_NOT_PERMITTED
3308 * \retval #PSA_ERROR_INVALID_ARGUMENT
3309 * \p alg is not a key agreement algorithm
3310 * \retval #PSA_ERROR_INVALID_ARGUMENT
3311 * \p private_key is not compatible with \p alg,
3312 * or \p peer_key is not valid for \p alg or not compatible with
3313 * \p private_key.
3314 * \retval #PSA_ERROR_NOT_SUPPORTED
3315 * \p alg is not a supported key agreement algorithm.
3316 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3317 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3318 * \retval #PSA_ERROR_HARDWARE_FAILURE
3319 * \retval #PSA_ERROR_TAMPERING_DETECTED
3320 */
3321psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3322 psa_key_handle_t private_key,
3323 const uint8_t *peer_key,
3324 size_t peer_key_length,
3325 uint8_t *output,
3326 size_t output_size,
3327 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003328
3329/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003330
3331/** \defgroup random Random generation
3332 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003333 */
3334
3335/**
3336 * \brief Generate random bytes.
3337 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003338 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003339 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003340 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003341 *
3342 * \note To generate a key, use psa_generate_key() instead.
3343 *
3344 * \param[out] output Output buffer for the generated data.
3345 * \param output_size Number of bytes to generate and output.
3346 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003347 * \retval #PSA_SUCCESS
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003348 * \retval #PSA_ERROR_NOT_SUPPORTED
3349 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003350 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003351 * \retval #PSA_ERROR_HARDWARE_FAILURE
3352 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003353 * \retval #PSA_ERROR_BAD_STATE
3354 * The library has not been previously initialized by psa_crypto_init().
3355 * It is implementation-dependent whether a failure to initialize
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003356 * results in this error code.
3357 */
3358psa_status_t psa_generate_random(uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003359 size_t output_size);
3360
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003361/**
3362 * \brief Generate a key or key pair.
3363 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003364 * The key is generated randomly.
3365 * Its location, policy, type and size are taken from \p attributes.
3366 *
3367 * If the type requires additional domain parameters, these are taken
3368 * from \p attributes as well. The following types use domain parameters:
3369 * - When generating an RSA key (#PSA_KEY_TYPE_RSA_KEYPAIR),
3370 * the default public exponent is 65537. This value is used if
3371 * \p attributes was set with psa_set_key_type() or by passing an empty
3372 * byte string as domain parameters to psa_set_key_domain_parameters().
3373 * If psa_set_key_domain_parameters() was used to set a non-empty
3374 * domain parameter string in \p attributes, this string is read as
3375 * a big-endian integer which is used as the public exponent.
3376 * - When generating a DSA key (#PSA_KEY_TYPE_DSA_KEYPAIR) or a
3377 * Diffie-Hellman key (#PSA_KEY_TYPE_DH_KEYPAIR), the domain parameters
3378 * from \p attributes are interpreted as described for
3379 * psa_set_key_domain_parameters().
3380 *
Gilles Peskine20628592019-04-19 19:29:50 +02003381 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003382 * \param[out] handle On success, a handle to the newly created key.
3383 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003384 *
Gilles Peskine28538492018-07-11 17:34:00 +02003385 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003386 * Success.
3387 * If the key is persistent, the key material and the key's metadata
3388 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003389 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003390 * This is an attempt to create a persistent key, and there is
3391 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003392 * \retval #PSA_ERROR_NOT_SUPPORTED
3393 * \retval #PSA_ERROR_INVALID_ARGUMENT
3394 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3395 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3396 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3397 * \retval #PSA_ERROR_HARDWARE_FAILURE
3398 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003399 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003400 * The library has not been previously initialized by psa_crypto_init().
3401 * It is implementation-dependent whether a failure to initialize
3402 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003403 */
Gilles Peskine87a5e562019-04-17 12:28:25 +02003404psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003405 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003406
3407/**@}*/
3408
Gilles Peskinee59236f2018-01-27 23:32:46 +01003409#ifdef __cplusplus
3410}
3411#endif
3412
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003413/* The file "crypto_sizes.h" contains definitions for size calculation
3414 * macros whose definitions are implementation-specific. */
3415#include "crypto_sizes.h"
3416
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003417/* The file "crypto_struct.h" contains definitions for
3418 * implementation-specific structs that are declared above. */
3419#include "crypto_struct.h"
3420
3421/* The file "crypto_extra.h" contains vendor-specific definitions. This
3422 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003423#include "crypto_extra.h"
3424
3425#endif /* PSA_CRYPTO_H */