blob: 705f2ca37dc20bf3546721f964bcfdc951a898b3 [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()
Gilles Peskine20628592019-04-19 19:29:50 +0200154 *
155 * If the attribute structure has been modified with other functions,
156 * you must free auxiliary resources by calling psa_reset_key_attributes().
157 * The following functions may create auxiliary resouces:
158 * - psa_set_key_domain_parameters()
Gilles Peskine06af0cd2019-04-28 11:32:30 +0200159 * - psa_get_key_attributes()
Gilles Peskine87a5e562019-04-17 12:28:25 +0200160 */
161typedef struct psa_key_attributes_s psa_key_attributes_t;
162
Gilles Peskine20628592019-04-19 19:29:50 +0200163/** Declare a key as persistent.
164 *
165 * This function does not access storage, it merely fills the attribute
166 * structure with given values. The persistent key will be written to
167 * storage when the attribute structure is passed to a key creation
168 * function such as psa_import_key(), psa_generate_key(),
169 * psa_generator_import_key() or psa_copy_key().
170 *
171 * This function overwrites any identifier and lifetime values
172 * previously set in \p attributes.
173 *
174 * This function may be declared as `static` (i.e. without external
175 * linkage). This function may be provided as a function-like macro,
176 * but in this case it must evaluate each of its arguments exactly once.
177 *
178 * \param[out] attributes The attribute structure to write to.
179 * \param id The persistent identifier for the key.
180 * \param lifetime The lifetime for the key.
181 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
182 * key will be volatile, and \p id is ignored.
183 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200184static void psa_make_key_persistent(psa_key_attributes_t *attributes,
185 psa_key_id_t id,
186 psa_key_lifetime_t lifetime);
187
Gilles Peskine20628592019-04-19 19:29:50 +0200188/** Retrieve the key identifier from key attributes.
189 *
190 * This function may be declared as `static` (i.e. without external
191 * linkage). This function may be provided as a function-like macro,
192 * but in this case it must evaluate its argument exactly once.
193 *
194 * \param[in] attributes The key attribute structure to query.
195 *
196 * \return The persistent identifier stored in the attribute structure.
197 * This value is unspecified if the attribute structure declares
198 * the key as volatile.
199 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200200static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
201
Gilles Peskine20628592019-04-19 19:29:50 +0200202/** Retrieve the lifetime from key attributes.
203 *
204 * This function may be declared as `static` (i.e. without external
205 * linkage). This function may be provided as a function-like macro,
206 * but in this case it must evaluate its argument exactly once.
207 *
208 * \param[in] attributes The key attribute structure to query.
209 *
210 * \return The lifetime value stored in the attribute structure.
211 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200212static psa_key_lifetime_t psa_get_key_lifetime(
213 const psa_key_attributes_t *attributes);
214
Gilles Peskine20628592019-04-19 19:29:50 +0200215/** Declare usage flags for a key.
216 *
217 * Usage flags are part of a key's usage policy. They encode what
218 * kind of operations are permitted on the key. For more details,
219 * refer to the documentation of the type #psa_key_usage_t.
220 *
221 * This function overwrites any usage flags
222 * previously set in \p attributes.
223 *
224 * This function may be declared as `static` (i.e. without external
225 * linkage). This function may be provided as a function-like macro,
226 * but in this case it must evaluate each of its arguments exactly once.
227 *
228 * \param[out] attributes The attribute structure to write to.
229 * \param usage_flags The usage flags to write.
230 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200231static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
232 psa_key_usage_t usage_flags);
233
Gilles Peskine20628592019-04-19 19:29:50 +0200234/** Retrieve the usage flags from key attributes.
235 *
236 * This function may be declared as `static` (i.e. without external
237 * linkage). This function may be provided as a function-like macro,
238 * but in this case it must evaluate its argument exactly once.
239 *
240 * \param[in] attributes The key attribute structure to query.
241 *
242 * \return The usage flags stored in the attribute structure.
243 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200244static psa_key_usage_t psa_get_key_usage_flags(
245 const psa_key_attributes_t *attributes);
246
Gilles Peskine20628592019-04-19 19:29:50 +0200247/** Declare the permitted algorithm policy for a key.
248 *
249 * The permitted algorithm policy of a key encodes which algorithm or
250 * algorithms are permitted to be used with this key.
251 *
252 * This function overwrites any algorithm policy
253 * previously set in \p attributes.
254 *
255 * This function may be declared as `static` (i.e. without external
256 * linkage). This function may be provided as a function-like macro,
257 * but in this case it must evaluate each of its arguments exactly once.
258 *
259 * \param[out] attributes The attribute structure to write to.
260 * \param alg The permitted algorithm policy to write.
261 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200262static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
263 psa_algorithm_t alg);
264
Gilles Peskine20628592019-04-19 19:29:50 +0200265/** Retrieve the algorithm policy from key attributes.
266 *
267 * This function may be declared as `static` (i.e. without external
268 * linkage). This function may be provided as a function-like macro,
269 * but in this case it must evaluate its argument exactly once.
270 *
271 * \param[in] attributes The key attribute structure to query.
272 *
273 * \return The algorithm stored in the attribute structure.
274 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200275static psa_algorithm_t psa_get_key_algorithm(
276 const psa_key_attributes_t *attributes);
277
Gilles Peskine20628592019-04-19 19:29:50 +0200278/** Declare the type of a key.
279 *
280 * If a type requires domain parameters, you must call
281 * psa_set_key_domain_parameters() instead of this function.
282 *
283 * This function overwrites any key type and domain parameters
284 * previously set in \p attributes.
285 *
286 * This function may be declared as `static` (i.e. without external
287 * linkage). This function may be provided as a function-like macro,
288 * but in this case it must evaluate each of its arguments exactly once.
289 *
290 * \param[out] attributes The attribute structure to write to.
291 * \param type The key type to write.
292 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200293static void psa_set_key_type(psa_key_attributes_t *attributes,
294 psa_key_type_t type);
295
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200296/** Declare the size of a key.
297 *
298 * This function overwrites any key size previously set in \p attributes.
299 *
300 * This function may be declared as `static` (i.e. without external
301 * linkage). This function may be provided as a function-like macro,
302 * but in this case it must evaluate each of its arguments exactly once.
303 *
304 * \param[out] attributes The attribute structure to write to.
305 * \param bits The key size in bits.
306 */
307static void psa_set_key_bits(psa_key_attributes_t *attributes,
308 size_t bits);
309
Gilles Peskine20628592019-04-19 19:29:50 +0200310/** Retrieve the key type from key attributes.
311 *
312 * This function may be declared as `static` (i.e. without external
313 * linkage). This function may be provided as a function-like macro,
314 * but in this case it must evaluate its argument exactly once.
315 *
316 * \param[in] attributes The key attribute structure to query.
317 *
318 * \return The key type stored in the attribute structure.
319 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200320static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
321
Gilles Peskine20628592019-04-19 19:29:50 +0200322/** Retrieve the key size from key attributes.
323 *
324 * This function may be declared as `static` (i.e. without external
325 * linkage). This function may be provided as a function-like macro,
326 * but in this case it must evaluate its argument exactly once.
327 *
328 * \param[in] attributes The key attribute structure to query.
329 *
330 * \return The key size stored in the attribute structure, in bits.
331 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200332static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
333
Gilles Peskineb699f072019-04-26 16:06:02 +0200334/**
335 * \brief Set domain parameters for a key.
336 *
337 * Some key types require additional domain parameters in addition to
338 * the key type identifier and the key size.
339 * The format for the required domain parameters varies by the key type.
340 *
Gilles Peskinee56e8782019-04-26 17:34:02 +0200341 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEYPAIR),
342 * the domain parameter data consists of the public exponent,
Gilles Peskineb699f072019-04-26 16:06:02 +0200343 * represented as a big-endian integer with no leading zeros.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200344 * This information is used when generating an RSA key pair.
Gilles Peskineb699f072019-04-26 16:06:02 +0200345 * When importing a key, the public exponent is read from the imported
346 * key data and the exponent recorded in the attribute structure is ignored.
Gilles Peskinee56e8782019-04-26 17:34:02 +0200347 * As an exception, the public exponent 65537 is represented by an empty
348 * byte string.
349 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEYPAIR),
Gilles Peskineb699f072019-04-26 16:06:02 +0200350 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
351 * ```
352 * Dss-Parms ::= SEQUENCE {
353 * p INTEGER,
354 * q INTEGER,
355 * g INTEGER
356 * }
357 * ```
Gilles Peskinee56e8782019-04-26 17:34:02 +0200358 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY or
359 * #PSA_KEY_TYPE_DH_KEYPAIR), the
Gilles Peskineb699f072019-04-26 16:06:02 +0200360 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
361 * ```
362 * DomainParameters ::= SEQUENCE {
363 * p INTEGER, -- odd prime, p=jq +1
364 * g INTEGER, -- generator, g
365 * q INTEGER, -- factor of p-1
366 * j INTEGER OPTIONAL, -- subgroup factor
367 * validationParms ValidationParms OPTIONAL
368 * }
369 * ValidationParms ::= SEQUENCE {
370 * seed BIT STRING,
371 * pgenCounter INTEGER
372 * }
373 * ```
374 *
375 * \param[in,out] attributes Attribute structure where the specified domain
376 * parameters will be stored.
377 * If this function fails, the content of
378 * \p attributes is not modified.
379 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
380 * \param[in] data Buffer containing the key domain parameters.
381 * The content of this buffer is interpreted
382 * according to \p type as described above.
383 * \param data_length Size of the \p data buffer in bytes.
384 *
385 * \retval #PSA_SUCCESS
386 * \retval #PSA_ERROR_INVALID_ARGUMENT
387 * \retval #PSA_ERROR_NOT_SUPPORTED
388 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
389 */
390psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
391 psa_key_type_t type,
392 const uint8_t *data,
393 size_t data_length);
394
395/**
396 * \brief Get domain parameters for a key.
397 *
398 * Get the domain parameters for a key with this function, if any. The format
399 * of the domain parameters written to \p data is specified in the
400 * documentation for psa_set_key_domain_parameters().
401 *
402 * \param[in] attributes The key attribute structure to query.
403 * \param[out] data On success, the key domain parameters.
404 * \param data_size Size of the \p data buffer in bytes.
405 * \param[out] data_length On success, the number of bytes
406 * that make up the key domain parameters data.
407 *
408 * \retval #PSA_SUCCESS
409 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
410 */
411psa_status_t psa_get_key_domain_parameters(
412 const psa_key_attributes_t *attributes,
413 uint8_t *data,
414 size_t data_size,
415 size_t *data_length);
416
Gilles Peskine20628592019-04-19 19:29:50 +0200417/** Retrieve the attributes of a key.
418 *
419 * This function first resets the attribute structure as with
420 * psa_reset_key_attributes(). It then populates the attribute
421 * structure with the attributes of the given key.
422 *
423 * The attributes that were set when creating the key are reported in a
424 * semantically equivalent manner, not necessarily with the same
425 * numerical value or the same bit pattern. In this specification,
426 * all key types, usage flags, algorithms and lifetime values are
427 * equivalent only if they have the same numerical encoding, but this
428 * property may not hold in future versions of this specification or
429 * for implementation-specific values.
430 *
Gilles Peskine20628592019-04-19 19:29:50 +0200431 * \param[in] handle Handle to the key to query.
432 * \param[in,out] attributes On success, the attributes of the key.
433 * On failure, equivalent to a
434 * freshly-initialized structure.
435 *
436 * \retval #PSA_SUCCESS
437 * \retval #PSA_ERROR_INVALID_HANDLE
438 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
439 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
440 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200441psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
442 psa_key_attributes_t *attributes);
443
Gilles Peskine20628592019-04-19 19:29:50 +0200444/** Reset a key attribute structure to a freshly initialized state.
445 *
446 * You must initialize the attribute structure as described in the
447 * documentation of the type #psa_key_attributes_t before calling this
448 * function. Once the structure has been initialized, you may call this
449 * function at any time.
450 *
451 * This function frees any auxiliary resources that the structure
452 * may contain.
453 *
454 * \param[in,out] attributes The attribute structure to reset.
455 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200456void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200457
Gilles Peskine87a5e562019-04-17 12:28:25 +0200458/**@}*/
459
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100460/** \defgroup key_management Key management
461 * @{
462 */
463
Gilles Peskinef535eb22018-11-30 14:08:36 +0100464/** Open a handle to an existing persistent key.
465 *
466 * Open a handle to a key which was previously created with psa_create_key().
467 *
468 * \param lifetime The lifetime of the key. This designates a storage
469 * area where the key material is stored. This must not
470 * be #PSA_KEY_LIFETIME_VOLATILE.
471 * \param id The persistent identifier of the key.
472 * \param[out] handle On success, a handle to a key slot which contains
473 * the data and metadata loaded from the specified
474 * persistent location.
475 *
476 * \retval #PSA_SUCCESS
477 * Success. The application can now use the value of `*handle`
478 * to access the newly allocated key slot.
479 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200480 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100481 * \retval #PSA_ERROR_INVALID_ARGUMENT
482 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
483 * \retval #PSA_ERROR_INVALID_ARGUMENT
484 * \p id is invalid for the specified lifetime.
485 * \retval #PSA_ERROR_NOT_SUPPORTED
486 * \p lifetime is not supported.
487 * \retval #PSA_ERROR_NOT_PERMITTED
488 * The specified key exists, but the application does not have the
489 * permission to access it. Note that this specification does not
490 * define any way to create such a key, but it may be possible
491 * through implementation-specific means.
492 */
493psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
494 psa_key_id_t id,
495 psa_key_handle_t *handle);
496
Gilles Peskinef535eb22018-11-30 14:08:36 +0100497/** Close a key handle.
498 *
499 * If the handle designates a volatile key, destroy the key material and
500 * free all associated resources, just like psa_destroy_key().
501 *
502 * If the handle designates a persistent key, free all resources associated
503 * with the key in volatile memory. The key slot in persistent storage is
504 * not affected and can be opened again later with psa_open_key().
505 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100506 * If the key is currently in use in a multipart operation,
507 * the multipart operation is aborted.
508 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100509 * \param handle The key handle to close.
510 *
511 * \retval #PSA_SUCCESS
512 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100513 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100514 */
515psa_status_t psa_close_key(psa_key_handle_t handle);
516
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100517/**@}*/
518
519/** \defgroup import_export Key import and export
520 * @{
521 */
522
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100523/**
524 * \brief Import a key in binary format.
525 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100526 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100527 * documentation of psa_export_public_key() for the format of public keys
528 * and to the documentation of psa_export_key() for the format for
529 * other key types.
530 *
531 * This specification supports a single format for each key type.
532 * Implementations may support other formats as long as the standard
533 * format is supported. Implementations that support other formats
534 * should ensure that the formats are clearly unambiguous so as to
535 * minimize the risk that an invalid input is accidentally interpreted
536 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100537 *
Gilles Peskine20628592019-04-19 19:29:50 +0200538 * \param[in] attributes The attributes for the new key.
539 * The key size field in \p attributes is
540 * ignored; the actual key size is determined
541 * from the \p data buffer.
542 * \param[out] handle On success, a handle to the newly created key.
543 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100544 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine20628592019-04-19 19:29:50 +0200545 * buffer is interpreted according to the type and,
546 * if applicable, domain parameters declared in
547 * \p attributes.
548 * All implementations must support at least the format
549 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100550 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200551 * the chosen type. Implementations may allow other
552 * formats, but should be conservative: implementations
553 * should err on the side of rejecting content if it
554 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200555 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100556 *
Gilles Peskine28538492018-07-11 17:34:00 +0200557 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100558 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100559 * If the key is persistent, the key material and the key's metadata
560 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200561 * \retval #PSA_ERROR_ALREADY_EXISTS
562 * This is an attempt to create a persistent key, and there is
563 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200564 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200565 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200566 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200567 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200568 * The key attributes, as a whole, are invalid,
Gilles Peskine308b91d2018-02-08 09:47:44 +0100569 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200570 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
571 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
572 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100573 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200574 * \retval #PSA_ERROR_HARDWARE_FAILURE
575 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300576 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300577 * The library has not been previously initialized by psa_crypto_init().
578 * It is implementation-dependent whether a failure to initialize
579 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100580 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200581psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
582 psa_key_handle_t *handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100583 const uint8_t *data,
584 size_t data_length);
585
586/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100587 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200588 *
589 * This function destroys the content of the key slot from both volatile
590 * memory and, if applicable, non-volatile storage. Implementations shall
591 * make a best effort to ensure that any previous content of the slot is
592 * unrecoverable.
593 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100594 * This function also erases any metadata such as policies and frees all
595 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200596 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100597 * If the key is currently in use in a multipart operation,
598 * the multipart operation is aborted.
599 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100600 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601 *
Gilles Peskine28538492018-07-11 17:34:00 +0200602 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200603 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200604 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200605 * The slot holds content and cannot be erased because it is
606 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100607 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200608 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200609 * There was an failure in communication with the cryptoprocessor.
610 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200611 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200612 * The storage is corrupted. Implementations shall make a best effort
613 * to erase key material even in this stage, however applications
614 * should be aware that it may be impossible to guarantee that the
615 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200616 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200617 * An unexpected condition which is not a storage corruption or
618 * a communication failure occurred. The cryptoprocessor may have
619 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300620 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300621 * The library has not been previously initialized by psa_crypto_init().
622 * It is implementation-dependent whether a failure to initialize
623 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100625psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100626
627/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100628 * \brief Export a key in binary format.
629 *
630 * The output of this function can be passed to psa_import_key() to
631 * create an equivalent object.
632 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100633 * If the implementation of psa_import_key() supports other formats
634 * beyond the format specified here, the output from psa_export_key()
635 * must use the representation specified here, not the original
636 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100637 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100638 * For standard key types, the output format is as follows:
639 *
640 * - For symmetric keys (including MAC keys), the format is the
641 * raw bytes of the key.
642 * - For DES, the key data consists of 8 bytes. The parity bits must be
643 * correct.
644 * - For Triple-DES, the format is the concatenation of the
645 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100646 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200647 * is the non-encrypted DER encoding of the representation defined by
648 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
649 * ```
650 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200651 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200652 * modulus INTEGER, -- n
653 * publicExponent INTEGER, -- e
654 * privateExponent INTEGER, -- d
655 * prime1 INTEGER, -- p
656 * prime2 INTEGER, -- q
657 * exponent1 INTEGER, -- d mod (p-1)
658 * exponent2 INTEGER, -- d mod (q-1)
659 * coefficient INTEGER, -- (inverse of q) mod p
660 * }
661 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000662 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
663 * representation of the private key `x` as a big-endian byte string. The
664 * length of the byte string is the private key size in bytes (leading zeroes
665 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200666 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100667 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100668 * a representation of the private value as a `ceiling(m/8)`-byte string
669 * where `m` is the bit size associated with the curve, i.e. the bit size
670 * of the order of the curve's coordinate field. This byte string is
671 * in little-endian order for Montgomery curves (curve types
672 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
673 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
674 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100675 * This is the content of the `privateKey` field of the `ECPrivateKey`
676 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000677 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
678 * format is the representation of the private key `x` as a big-endian byte
679 * string. The length of the byte string is the private key size in bytes
680 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200681 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
682 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100683 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100684 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200685 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200686 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200687 * \param[out] data_length On success, the number of bytes
688 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100689 *
Gilles Peskine28538492018-07-11 17:34:00 +0200690 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100691 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200692 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200693 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100694 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200695 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
696 * The size of the \p data buffer is too small. You can determine a
697 * sufficient buffer size by calling
698 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
699 * where \c type is the key type
700 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200701 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
702 * \retval #PSA_ERROR_HARDWARE_FAILURE
703 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300704 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300705 * The library has not been previously initialized by psa_crypto_init().
706 * It is implementation-dependent whether a failure to initialize
707 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100708 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100709psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710 uint8_t *data,
711 size_t data_size,
712 size_t *data_length);
713
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100714/**
715 * \brief Export a public key or the public part of a key pair in binary format.
716 *
717 * The output of this function can be passed to psa_import_key() to
718 * create an object that is equivalent to the public key.
719 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000720 * This specification supports a single format for each key type.
721 * Implementations may support other formats as long as the standard
722 * format is supported. Implementations that support other formats
723 * should ensure that the formats are clearly unambiguous so as to
724 * minimize the risk that an invalid input is accidentally interpreted
725 * according to a different format.
726 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000727 * For standard key types, the output format is as follows:
728 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
729 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
730 * ```
731 * RSAPublicKey ::= SEQUENCE {
732 * modulus INTEGER, -- n
733 * publicExponent INTEGER } -- e
734 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000735 * - For elliptic curve public keys (key types for which
736 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
737 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
738 * Let `m` be the bit size associated with the curve, i.e. the bit size of
739 * `q` for a curve over `F_q`. The representation consists of:
740 * - The byte 0x04;
741 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
742 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000743 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
744 * representation of the public key `y = g^x mod p` as a big-endian byte
745 * string. The length of the byte string is the length of the base prime `p`
746 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000747 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
748 * the format is the representation of the public key `y = g^x mod p` as a
749 * big-endian byte string. The length of the byte string is the length of the
750 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100751 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100752 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200753 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200754 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200755 * \param[out] data_length On success, the number of bytes
756 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100757 *
Gilles Peskine28538492018-07-11 17:34:00 +0200758 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100759 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200760 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200761 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200762 * The key is neither a public key nor a key pair.
763 * \retval #PSA_ERROR_NOT_SUPPORTED
764 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
765 * The size of the \p data buffer is too small. You can determine a
766 * sufficient buffer size by calling
767 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
768 * where \c type is the key type
769 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200770 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
771 * \retval #PSA_ERROR_HARDWARE_FAILURE
772 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300773 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300774 * The library has not been previously initialized by psa_crypto_init().
775 * It is implementation-dependent whether a failure to initialize
776 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100777 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100778psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100779 uint8_t *data,
780 size_t data_size,
781 size_t *data_length);
782
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100783/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100784 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100785 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000786 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100787 * This function is primarily useful to copy a key from one location
788 * to another, since it populates a key using the material from
789 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200790 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100791 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100792 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100793 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100794 *
Gilles Peskine20628592019-04-19 19:29:50 +0200795 * The resulting key may only be used in a way that conforms to
796 * both the policy of the original key and the policy specified in
797 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100798 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200799 * usage flags on the source policy and the usage flags in \p attributes.
800 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100801 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200802 * - If either of the policies allows an algorithm and the other policy
803 * allows a wildcard-based algorithm policy that includes this algorithm,
804 * the resulting key allows the same algorithm.
805 * - If the policies do not allow any algorithm in common, this function
806 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200807 *
Gilles Peskine20628592019-04-19 19:29:50 +0200808 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100809 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200810 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100811 * \param source_handle The key to copy. It must be a handle to an
812 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200813 * \param[in] attributes The attributes for the new key.
814 * They are used as follows:
815 * - The key type, key size and domain parameters
816 * are ignored. This information is copied
817 * from the source key.
818 * - The key location (the lifetime and, for
819 * persistent keys, the key identifier) is
820 * used directly.
821 * - The policy constraints (usage flags and
822 * algorithm policy) are combined from
823 * the source key and \p attributes so that
824 * both sets of restrictions apply, as
825 * described in the documentation of this function.
826 * \param[out] target_handle On success, a handle to the newly created key.
827 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200828 *
829 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100830 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200831 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200832 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200833 * This is an attempt to create a persistent key, and there is
834 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200835 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200836 * The lifetime or identifier in \p attributes are invalid.
837 * \retval #PSA_ERROR_INVALID_ARGUMENT
838 * The policy constraints on the source and specified in
839 * \p attributes are incompatible.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100840 * \retval #PSA_ERROR_NOT_PERMITTED
841 * The source key is not exportable and its lifetime does not
842 * allow copying it to the target's lifetime.
843 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
844 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200845 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
846 * \retval #PSA_ERROR_HARDWARE_FAILURE
847 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100848 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100849psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200850 const psa_key_attributes_t *attributes,
851 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100852
853/**@}*/
854
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100855/** \defgroup hash Message digests
856 * @{
857 */
858
Gilles Peskine69647a42019-01-14 20:18:12 +0100859/** Calculate the hash (digest) of a message.
860 *
861 * \note To verify the hash of a message against an
862 * expected value, use psa_hash_compare() instead.
863 *
864 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
865 * such that #PSA_ALG_IS_HASH(\p alg) is true).
866 * \param[in] input Buffer containing the message to hash.
867 * \param input_length Size of the \p input buffer in bytes.
868 * \param[out] hash Buffer where the hash is to be written.
869 * \param hash_size Size of the \p hash buffer in bytes.
870 * \param[out] hash_length On success, the number of bytes
871 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100872 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100873 *
874 * \retval #PSA_SUCCESS
875 * Success.
876 * \retval #PSA_ERROR_NOT_SUPPORTED
877 * \p alg is not supported or is not a hash algorithm.
878 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
879 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
880 * \retval #PSA_ERROR_HARDWARE_FAILURE
881 * \retval #PSA_ERROR_TAMPERING_DETECTED
882 */
883psa_status_t psa_hash_compute(psa_algorithm_t alg,
884 const uint8_t *input,
885 size_t input_length,
886 uint8_t *hash,
887 size_t hash_size,
888 size_t *hash_length);
889
890/** Calculate the hash (digest) of a message and compare it with a
891 * reference value.
892 *
893 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
894 * such that #PSA_ALG_IS_HASH(\p alg) is true).
895 * \param[in] input Buffer containing the message to hash.
896 * \param input_length Size of the \p input buffer in bytes.
897 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100898 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100899 *
900 * \retval #PSA_SUCCESS
901 * The expected hash is identical to the actual hash of the input.
902 * \retval #PSA_ERROR_INVALID_SIGNATURE
903 * The hash of the message was calculated successfully, but it
904 * differs from the expected hash.
905 * \retval #PSA_ERROR_NOT_SUPPORTED
906 * \p alg is not supported or is not a hash algorithm.
907 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
908 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
909 * \retval #PSA_ERROR_HARDWARE_FAILURE
910 * \retval #PSA_ERROR_TAMPERING_DETECTED
911 */
912psa_status_t psa_hash_compare(psa_algorithm_t alg,
913 const uint8_t *input,
914 size_t input_length,
915 const uint8_t *hash,
916 const size_t hash_length);
917
Gilles Peskine308b91d2018-02-08 09:47:44 +0100918/** The type of the state data structure for multipart hash operations.
919 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000920 * Before calling any function on a hash operation object, the application must
921 * initialize it by any of the following means:
922 * - Set the structure to all-bits-zero, for example:
923 * \code
924 * psa_hash_operation_t operation;
925 * memset(&operation, 0, sizeof(operation));
926 * \endcode
927 * - Initialize the structure to logical zero values, for example:
928 * \code
929 * psa_hash_operation_t operation = {0};
930 * \endcode
931 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
932 * for example:
933 * \code
934 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
935 * \endcode
936 * - Assign the result of the function psa_hash_operation_init()
937 * to the structure, for example:
938 * \code
939 * psa_hash_operation_t operation;
940 * operation = psa_hash_operation_init();
941 * \endcode
942 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100943 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100944 * make any assumptions about the content of this structure except
945 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100946typedef struct psa_hash_operation_s psa_hash_operation_t;
947
Jaeden Amero6a25b412019-01-04 11:47:44 +0000948/** \def PSA_HASH_OPERATION_INIT
949 *
950 * This macro returns a suitable initializer for a hash operation object
951 * of type #psa_hash_operation_t.
952 */
953#ifdef __DOXYGEN_ONLY__
954/* This is an example definition for documentation purposes.
955 * Implementations should define a suitable value in `crypto_struct.h`.
956 */
957#define PSA_HASH_OPERATION_INIT {0}
958#endif
959
960/** Return an initial value for a hash operation object.
961 */
962static psa_hash_operation_t psa_hash_operation_init(void);
963
Gilles Peskinef45adda2019-01-14 18:29:18 +0100964/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100965 *
966 * The sequence of operations to calculate a hash (message digest)
967 * is as follows:
968 * -# Allocate an operation object which will be passed to all the functions
969 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000970 * -# Initialize the operation object with one of the methods described in the
971 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200972 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100973 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100974 * of the message each time. The hash that is calculated is the hash
975 * of the concatenation of these messages in order.
976 * -# To calculate the hash, call psa_hash_finish().
977 * To compare the hash with an expected value, call psa_hash_verify().
978 *
979 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000980 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100981 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200982 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100983 * eventually terminate the operation. The following events terminate an
984 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100985 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100986 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100987 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000988 * \param[in,out] operation The operation object to set up. It must have
989 * been initialized as per the documentation for
990 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200991 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
992 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100993 *
Gilles Peskine28538492018-07-11 17:34:00 +0200994 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100995 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200996 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200997 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100998 * \retval #PSA_ERROR_BAD_STATE
999 * The operation state is not valid (already set up and not
1000 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001001 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1002 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1003 * \retval #PSA_ERROR_HARDWARE_FAILURE
1004 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001005 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001006psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001007 psa_algorithm_t alg);
1008
Gilles Peskine308b91d2018-02-08 09:47:44 +01001009/** Add a message fragment to a multipart hash operation.
1010 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001011 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001012 *
1013 * If this function returns an error status, the operation becomes inactive.
1014 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001015 * \param[in,out] operation Active hash operation.
1016 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001017 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001018 *
Gilles Peskine28538492018-07-11 17:34:00 +02001019 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001020 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001021 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001022 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001023 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1024 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1025 * \retval #PSA_ERROR_HARDWARE_FAILURE
1026 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001027 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001028psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1029 const uint8_t *input,
1030 size_t input_length);
1031
Gilles Peskine308b91d2018-02-08 09:47:44 +01001032/** Finish the calculation of the hash of a message.
1033 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001034 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001035 * This function calculates the hash of the message formed by concatenating
1036 * the inputs passed to preceding calls to psa_hash_update().
1037 *
1038 * When this function returns, the operation becomes inactive.
1039 *
1040 * \warning Applications should not call this function if they expect
1041 * a specific value for the hash. Call psa_hash_verify() instead.
1042 * Beware that comparing integrity or authenticity data such as
1043 * hash values with a function such as \c memcmp is risky
1044 * because the time taken by the comparison may leak information
1045 * about the hashed data which could allow an attacker to guess
1046 * a valid hash and thereby bypass security controls.
1047 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001048 * \param[in,out] operation Active hash operation.
1049 * \param[out] hash Buffer where the hash is to be written.
1050 * \param hash_size Size of the \p hash buffer in bytes.
1051 * \param[out] hash_length On success, the number of bytes
1052 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001053 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001054 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001055 *
Gilles Peskine28538492018-07-11 17:34:00 +02001056 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001057 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001058 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001059 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001060 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001061 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001062 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001063 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001064 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1065 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1066 * \retval #PSA_ERROR_HARDWARE_FAILURE
1067 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001068 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001069psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1070 uint8_t *hash,
1071 size_t hash_size,
1072 size_t *hash_length);
1073
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074/** Finish the calculation of the hash of a message and compare it with
1075 * an expected value.
1076 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001077 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001078 * This function calculates the hash of the message formed by concatenating
1079 * the inputs passed to preceding calls to psa_hash_update(). It then
1080 * compares the calculated hash with the expected hash passed as a
1081 * parameter to this function.
1082 *
1083 * When this function returns, the operation becomes inactive.
1084 *
Gilles Peskine19067982018-03-20 17:54:53 +01001085 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001086 * comparison between the actual hash and the expected hash is performed
1087 * in constant time.
1088 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001089 * \param[in,out] operation Active hash operation.
1090 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001091 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001092 *
Gilles Peskine28538492018-07-11 17:34:00 +02001093 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001095 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001096 * The hash of the message was calculated successfully, but it
1097 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001098 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001099 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001100 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1101 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1102 * \retval #PSA_ERROR_HARDWARE_FAILURE
1103 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001104 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001105psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1106 const uint8_t *hash,
1107 size_t hash_length);
1108
Gilles Peskine308b91d2018-02-08 09:47:44 +01001109/** Abort a hash operation.
1110 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001111 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001112 * \p operation structure itself. Once aborted, the operation object
1113 * can be reused for another operation by calling
1114 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001115 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001116 * You may call this function any time after the operation object has
1117 * been initialized by any of the following methods:
1118 * - A call to psa_hash_setup(), whether it succeeds or not.
1119 * - Initializing the \c struct to all-bits-zero.
1120 * - Initializing the \c struct to logical zeros, e.g.
1121 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001122 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001123 * In particular, calling psa_hash_abort() after the operation has been
1124 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1125 * psa_hash_verify() is safe and has no effect.
1126 *
1127 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001128 *
Gilles Peskine28538492018-07-11 17:34:00 +02001129 * \retval #PSA_SUCCESS
1130 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001131 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001132 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1133 * \retval #PSA_ERROR_HARDWARE_FAILURE
1134 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001135 */
1136psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001137
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001138/** Clone a hash operation.
1139 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001140 * This function copies the state of an ongoing hash operation to
1141 * a new operation object. In other words, this function is equivalent
1142 * to calling psa_hash_setup() on \p target_operation with the same
1143 * algorithm that \p source_operation was set up for, then
1144 * psa_hash_update() on \p target_operation with the same input that
1145 * that was passed to \p source_operation. After this function returns, the
1146 * two objects are independent, i.e. subsequent calls involving one of
1147 * the objects do not affect the other object.
1148 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001149 * \param[in] source_operation The active hash operation to clone.
1150 * \param[in,out] target_operation The operation object to set up.
1151 * It must be initialized but not active.
1152 *
1153 * \retval #PSA_SUCCESS
1154 * \retval #PSA_ERROR_BAD_STATE
1155 * \p source_operation is not an active hash operation.
1156 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001157 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001158 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1159 * \retval #PSA_ERROR_HARDWARE_FAILURE
1160 * \retval #PSA_ERROR_TAMPERING_DETECTED
1161 */
1162psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1163 psa_hash_operation_t *target_operation);
1164
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001165/**@}*/
1166
Gilles Peskine8c9def32018-02-08 10:02:12 +01001167/** \defgroup MAC Message authentication codes
1168 * @{
1169 */
1170
Gilles Peskine69647a42019-01-14 20:18:12 +01001171/** Calculate the MAC (message authentication code) of a message.
1172 *
1173 * \note To verify the MAC of a message against an
1174 * expected value, use psa_mac_verify() instead.
1175 * Beware that comparing integrity or authenticity data such as
1176 * MAC values with a function such as \c memcmp is risky
1177 * because the time taken by the comparison may leak information
1178 * about the MAC value which could allow an attacker to guess
1179 * a valid MAC and thereby bypass security controls.
1180 *
1181 * \param handle Handle to the key to use for the operation.
1182 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001183 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001184 * \param[in] input Buffer containing the input message.
1185 * \param input_length Size of the \p input buffer in bytes.
1186 * \param[out] mac Buffer where the MAC value is to be written.
1187 * \param mac_size Size of the \p mac buffer in bytes.
1188 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001189 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001190 *
1191 * \retval #PSA_SUCCESS
1192 * Success.
1193 * \retval #PSA_ERROR_INVALID_HANDLE
1194 * \retval #PSA_ERROR_EMPTY_SLOT
1195 * \retval #PSA_ERROR_NOT_PERMITTED
1196 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001197 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001198 * \retval #PSA_ERROR_NOT_SUPPORTED
1199 * \p alg is not supported or is not a MAC algorithm.
1200 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1201 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1202 * \retval #PSA_ERROR_HARDWARE_FAILURE
1203 * \retval #PSA_ERROR_TAMPERING_DETECTED
1204 * \retval #PSA_ERROR_BAD_STATE
1205 * The library has not been previously initialized by psa_crypto_init().
1206 * It is implementation-dependent whether a failure to initialize
1207 * results in this error code.
1208 */
1209psa_status_t psa_mac_compute(psa_key_handle_t handle,
1210 psa_algorithm_t alg,
1211 const uint8_t *input,
1212 size_t input_length,
1213 uint8_t *mac,
1214 size_t mac_size,
1215 size_t *mac_length);
1216
1217/** Calculate the MAC of a message and compare it with a reference value.
1218 *
1219 * \param handle Handle to the key to use for the operation.
1220 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001221 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001222 * \param[in] input Buffer containing the input message.
1223 * \param input_length Size of the \p input buffer in bytes.
1224 * \param[out] mac Buffer containing the expected MAC value.
1225 * \param mac_length Size of the \p mac buffer in bytes.
1226 *
1227 * \retval #PSA_SUCCESS
1228 * The expected MAC is identical to the actual MAC of the input.
1229 * \retval #PSA_ERROR_INVALID_SIGNATURE
1230 * The MAC of the message was calculated successfully, but it
1231 * differs from the expected value.
1232 * \retval #PSA_ERROR_INVALID_HANDLE
1233 * \retval #PSA_ERROR_EMPTY_SLOT
1234 * \retval #PSA_ERROR_NOT_PERMITTED
1235 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001236 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001237 * \retval #PSA_ERROR_NOT_SUPPORTED
1238 * \p alg is not supported or is not a MAC algorithm.
1239 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1240 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1241 * \retval #PSA_ERROR_HARDWARE_FAILURE
1242 * \retval #PSA_ERROR_TAMPERING_DETECTED
1243 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001244psa_status_t psa_mac_verify(psa_key_handle_t handle,
1245 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001246 const uint8_t *input,
1247 size_t input_length,
1248 const uint8_t *mac,
1249 const size_t mac_length);
1250
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001251/** The type of the state data structure for multipart MAC operations.
1252 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001253 * Before calling any function on a MAC operation object, the application must
1254 * initialize it by any of the following means:
1255 * - Set the structure to all-bits-zero, for example:
1256 * \code
1257 * psa_mac_operation_t operation;
1258 * memset(&operation, 0, sizeof(operation));
1259 * \endcode
1260 * - Initialize the structure to logical zero values, for example:
1261 * \code
1262 * psa_mac_operation_t operation = {0};
1263 * \endcode
1264 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1265 * for example:
1266 * \code
1267 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1268 * \endcode
1269 * - Assign the result of the function psa_mac_operation_init()
1270 * to the structure, for example:
1271 * \code
1272 * psa_mac_operation_t operation;
1273 * operation = psa_mac_operation_init();
1274 * \endcode
1275 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001276 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001277 * make any assumptions about the content of this structure except
1278 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001279typedef struct psa_mac_operation_s psa_mac_operation_t;
1280
Jaeden Amero769ce272019-01-04 11:48:03 +00001281/** \def PSA_MAC_OPERATION_INIT
1282 *
1283 * This macro returns a suitable initializer for a MAC operation object of type
1284 * #psa_mac_operation_t.
1285 */
1286#ifdef __DOXYGEN_ONLY__
1287/* This is an example definition for documentation purposes.
1288 * Implementations should define a suitable value in `crypto_struct.h`.
1289 */
1290#define PSA_MAC_OPERATION_INIT {0}
1291#endif
1292
1293/** Return an initial value for a MAC operation object.
1294 */
1295static psa_mac_operation_t psa_mac_operation_init(void);
1296
Gilles Peskinef45adda2019-01-14 18:29:18 +01001297/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001298 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001299 * This function sets up the calculation of the MAC
1300 * (message authentication code) of a byte string.
1301 * To verify the MAC of a message against an
1302 * expected value, use psa_mac_verify_setup() instead.
1303 *
1304 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001305 * -# Allocate an operation object which will be passed to all the functions
1306 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001307 * -# Initialize the operation object with one of the methods described in the
1308 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001309 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001310 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1311 * of the message each time. The MAC that is calculated is the MAC
1312 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001313 * -# At the end of the message, call psa_mac_sign_finish() to finish
1314 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001315 *
1316 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001317 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001318 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001319 * After a successful call to psa_mac_sign_setup(), the application must
1320 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001321 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001322 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001323 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001324 * \param[in,out] operation The operation object to set up. It must have
1325 * been initialized as per the documentation for
1326 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001327 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001328 * It must remain valid until the operation
1329 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001330 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001331 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001332 *
Gilles Peskine28538492018-07-11 17:34:00 +02001333 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001334 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001335 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001336 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001337 * \retval #PSA_ERROR_NOT_PERMITTED
1338 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001339 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001340 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001341 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001342 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1343 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1344 * \retval #PSA_ERROR_HARDWARE_FAILURE
1345 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001346 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001347 * The operation state is not valid (already set up and not
1348 * subsequently completed).
1349 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001350 * The library has not been previously initialized by psa_crypto_init().
1351 * It is implementation-dependent whether a failure to initialize
1352 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001353 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001354psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001355 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001356 psa_algorithm_t alg);
1357
Gilles Peskinef45adda2019-01-14 18:29:18 +01001358/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001359 *
1360 * This function sets up the verification of the MAC
1361 * (message authentication code) of a byte string against an expected value.
1362 *
1363 * The sequence of operations to verify a MAC is as follows:
1364 * -# Allocate an operation object which will be passed to all the functions
1365 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001366 * -# Initialize the operation object with one of the methods described in the
1367 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001368 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001369 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1370 * of the message each time. The MAC that is calculated is the MAC
1371 * of the concatenation of these messages in order.
1372 * -# At the end of the message, call psa_mac_verify_finish() to finish
1373 * calculating the actual MAC of the message and verify it against
1374 * the expected value.
1375 *
1376 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001377 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001378 *
1379 * After a successful call to psa_mac_verify_setup(), the application must
1380 * eventually terminate the operation through one of the following methods:
1381 * - A failed call to psa_mac_update().
1382 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1383 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001384 * \param[in,out] operation The operation object to set up. It must have
1385 * been initialized as per the documentation for
1386 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001387 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001388 * It must remain valid until the operation
1389 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001390 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1391 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001392 *
Gilles Peskine28538492018-07-11 17:34:00 +02001393 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001394 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001395 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001396 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001397 * \retval #PSA_ERROR_NOT_PERMITTED
1398 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001399 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001400 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001401 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001402 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1403 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1404 * \retval #PSA_ERROR_HARDWARE_FAILURE
1405 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001406 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001407 * The operation state is not valid (already set up and not
1408 * subsequently completed).
1409 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001410 * The library has not been previously initialized by psa_crypto_init().
1411 * It is implementation-dependent whether a failure to initialize
1412 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001413 */
1414psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001415 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001416 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001417
Gilles Peskinedcd14942018-07-12 00:30:52 +02001418/** Add a message fragment to a multipart MAC operation.
1419 *
1420 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1421 * before calling this function.
1422 *
1423 * If this function returns an error status, the operation becomes inactive.
1424 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001425 * \param[in,out] operation Active MAC operation.
1426 * \param[in] input Buffer containing the message fragment to add to
1427 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001428 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001429 *
1430 * \retval #PSA_SUCCESS
1431 * Success.
1432 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001433 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001434 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1435 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1436 * \retval #PSA_ERROR_HARDWARE_FAILURE
1437 * \retval #PSA_ERROR_TAMPERING_DETECTED
1438 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001439psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1440 const uint8_t *input,
1441 size_t input_length);
1442
Gilles Peskinedcd14942018-07-12 00:30:52 +02001443/** Finish the calculation of the MAC of a message.
1444 *
1445 * The application must call psa_mac_sign_setup() before calling this function.
1446 * This function calculates the MAC of the message formed by concatenating
1447 * the inputs passed to preceding calls to psa_mac_update().
1448 *
1449 * When this function returns, the operation becomes inactive.
1450 *
1451 * \warning Applications should not call this function if they expect
1452 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1453 * Beware that comparing integrity or authenticity data such as
1454 * MAC values with a function such as \c memcmp is risky
1455 * because the time taken by the comparison may leak information
1456 * about the MAC value which could allow an attacker to guess
1457 * a valid MAC and thereby bypass security controls.
1458 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001459 * \param[in,out] operation Active MAC operation.
1460 * \param[out] mac Buffer where the MAC value is to be written.
1461 * \param mac_size Size of the \p mac buffer in bytes.
1462 * \param[out] mac_length On success, the number of bytes
1463 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001464 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001465 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001466 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001467 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001468 *
1469 * \retval #PSA_SUCCESS
1470 * Success.
1471 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001472 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001473 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001474 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001475 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1476 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1477 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1478 * \retval #PSA_ERROR_HARDWARE_FAILURE
1479 * \retval #PSA_ERROR_TAMPERING_DETECTED
1480 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001481psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1482 uint8_t *mac,
1483 size_t mac_size,
1484 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001485
Gilles Peskinedcd14942018-07-12 00:30:52 +02001486/** Finish the calculation of the MAC of a message and compare it with
1487 * an expected value.
1488 *
1489 * The application must call psa_mac_verify_setup() before calling this function.
1490 * This function calculates the MAC of the message formed by concatenating
1491 * the inputs passed to preceding calls to psa_mac_update(). It then
1492 * compares the calculated MAC with the expected MAC passed as a
1493 * parameter to this function.
1494 *
1495 * When this function returns, the operation becomes inactive.
1496 *
1497 * \note Implementations shall make the best effort to ensure that the
1498 * comparison between the actual MAC and the expected MAC is performed
1499 * in constant time.
1500 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001501 * \param[in,out] operation Active MAC operation.
1502 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001503 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001504 *
1505 * \retval #PSA_SUCCESS
1506 * The expected MAC is identical to the actual MAC of the message.
1507 * \retval #PSA_ERROR_INVALID_SIGNATURE
1508 * The MAC of the message was calculated successfully, but it
1509 * differs from the expected MAC.
1510 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001511 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001512 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1513 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1514 * \retval #PSA_ERROR_HARDWARE_FAILURE
1515 * \retval #PSA_ERROR_TAMPERING_DETECTED
1516 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001517psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1518 const uint8_t *mac,
1519 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001520
Gilles Peskinedcd14942018-07-12 00:30:52 +02001521/** Abort a MAC operation.
1522 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001523 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001524 * \p operation structure itself. Once aborted, the operation object
1525 * can be reused for another operation by calling
1526 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001527 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001528 * You may call this function any time after the operation object has
1529 * been initialized by any of the following methods:
1530 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1531 * it succeeds or not.
1532 * - Initializing the \c struct to all-bits-zero.
1533 * - Initializing the \c struct to logical zeros, e.g.
1534 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001535 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001536 * In particular, calling psa_mac_abort() after the operation has been
1537 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1538 * psa_mac_verify_finish() is safe and has no effect.
1539 *
1540 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001541 *
1542 * \retval #PSA_SUCCESS
1543 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001544 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001545 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1546 * \retval #PSA_ERROR_HARDWARE_FAILURE
1547 * \retval #PSA_ERROR_TAMPERING_DETECTED
1548 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001549psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1550
1551/**@}*/
1552
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001553/** \defgroup cipher Symmetric ciphers
1554 * @{
1555 */
1556
Gilles Peskine69647a42019-01-14 20:18:12 +01001557/** Encrypt a message using a symmetric cipher.
1558 *
1559 * This function encrypts a message with a random IV (initialization
1560 * vector).
1561 *
1562 * \param handle Handle to the key to use for the operation.
1563 * It must remain valid until the operation
1564 * terminates.
1565 * \param alg The cipher algorithm to compute
1566 * (\c PSA_ALG_XXX value such that
1567 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1568 * \param[in] input Buffer containing the message to encrypt.
1569 * \param input_length Size of the \p input buffer in bytes.
1570 * \param[out] output Buffer where the output is to be written.
1571 * The output contains the IV followed by
1572 * the ciphertext proper.
1573 * \param output_size Size of the \p output buffer in bytes.
1574 * \param[out] output_length On success, the number of bytes
1575 * that make up the output.
1576 *
1577 * \retval #PSA_SUCCESS
1578 * Success.
1579 * \retval #PSA_ERROR_INVALID_HANDLE
1580 * \retval #PSA_ERROR_EMPTY_SLOT
1581 * \retval #PSA_ERROR_NOT_PERMITTED
1582 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001583 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001584 * \retval #PSA_ERROR_NOT_SUPPORTED
1585 * \p alg is not supported or is not a cipher algorithm.
1586 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1587 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1588 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1589 * \retval #PSA_ERROR_HARDWARE_FAILURE
1590 * \retval #PSA_ERROR_TAMPERING_DETECTED
1591 */
1592psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1593 psa_algorithm_t alg,
1594 const uint8_t *input,
1595 size_t input_length,
1596 uint8_t *output,
1597 size_t output_size,
1598 size_t *output_length);
1599
1600/** Decrypt a message using a symmetric cipher.
1601 *
1602 * This function decrypts a message encrypted with a symmetric cipher.
1603 *
1604 * \param handle Handle to the key to use for the operation.
1605 * It must remain valid until the operation
1606 * terminates.
1607 * \param alg The cipher algorithm to compute
1608 * (\c PSA_ALG_XXX value such that
1609 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1610 * \param[in] input Buffer containing the message to decrypt.
1611 * This consists of the IV followed by the
1612 * ciphertext proper.
1613 * \param input_length Size of the \p input buffer in bytes.
1614 * \param[out] output Buffer where the plaintext is to be written.
1615 * \param output_size Size of the \p output buffer in bytes.
1616 * \param[out] output_length On success, the number of bytes
1617 * that make up the output.
1618 *
1619 * \retval #PSA_SUCCESS
1620 * Success.
1621 * \retval #PSA_ERROR_INVALID_HANDLE
1622 * \retval #PSA_ERROR_EMPTY_SLOT
1623 * \retval #PSA_ERROR_NOT_PERMITTED
1624 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001625 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001626 * \retval #PSA_ERROR_NOT_SUPPORTED
1627 * \p alg is not supported or is not a cipher algorithm.
1628 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1629 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1630 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1631 * \retval #PSA_ERROR_HARDWARE_FAILURE
1632 * \retval #PSA_ERROR_TAMPERING_DETECTED
1633 */
1634psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1635 psa_algorithm_t alg,
1636 const uint8_t *input,
1637 size_t input_length,
1638 uint8_t *output,
1639 size_t output_size,
1640 size_t *output_length);
1641
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001642/** The type of the state data structure for multipart cipher operations.
1643 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001644 * Before calling any function on a cipher operation object, the application
1645 * must initialize it by any of the following means:
1646 * - Set the structure to all-bits-zero, for example:
1647 * \code
1648 * psa_cipher_operation_t operation;
1649 * memset(&operation, 0, sizeof(operation));
1650 * \endcode
1651 * - Initialize the structure to logical zero values, for example:
1652 * \code
1653 * psa_cipher_operation_t operation = {0};
1654 * \endcode
1655 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1656 * for example:
1657 * \code
1658 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1659 * \endcode
1660 * - Assign the result of the function psa_cipher_operation_init()
1661 * to the structure, for example:
1662 * \code
1663 * psa_cipher_operation_t operation;
1664 * operation = psa_cipher_operation_init();
1665 * \endcode
1666 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001667 * This is an implementation-defined \c struct. Applications should not
1668 * make any assumptions about the content of this structure except
1669 * as directed by the documentation of a specific implementation. */
1670typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1671
Jaeden Amero5bae2272019-01-04 11:48:27 +00001672/** \def PSA_CIPHER_OPERATION_INIT
1673 *
1674 * This macro returns a suitable initializer for a cipher operation object of
1675 * type #psa_cipher_operation_t.
1676 */
1677#ifdef __DOXYGEN_ONLY__
1678/* This is an example definition for documentation purposes.
1679 * Implementations should define a suitable value in `crypto_struct.h`.
1680 */
1681#define PSA_CIPHER_OPERATION_INIT {0}
1682#endif
1683
1684/** Return an initial value for a cipher operation object.
1685 */
1686static psa_cipher_operation_t psa_cipher_operation_init(void);
1687
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001688/** Set the key for a multipart symmetric encryption operation.
1689 *
1690 * The sequence of operations to encrypt a message with a symmetric cipher
1691 * is as follows:
1692 * -# Allocate an operation object which will be passed to all the functions
1693 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001694 * -# Initialize the operation object with one of the methods described in the
1695 * documentation for #psa_cipher_operation_t, e.g.
1696 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001697 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001698 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001699 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001700 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001701 * requires a specific IV value.
1702 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1703 * of the message each time.
1704 * -# Call psa_cipher_finish().
1705 *
1706 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001707 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001708 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001709 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001710 * eventually terminate the operation. The following events terminate an
1711 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001712 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001713 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001714 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001715 * \param[in,out] operation The operation object to set up. It must have
1716 * been initialized as per the documentation for
1717 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001718 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001719 * It must remain valid until the operation
1720 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001721 * \param alg The cipher algorithm to compute
1722 * (\c PSA_ALG_XXX value such that
1723 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001724 *
Gilles Peskine28538492018-07-11 17:34:00 +02001725 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001726 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001727 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001728 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001729 * \retval #PSA_ERROR_NOT_PERMITTED
1730 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001731 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001732 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001733 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001734 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1735 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1736 * \retval #PSA_ERROR_HARDWARE_FAILURE
1737 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001738 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001739 * The operation state is not valid (already set up and not
1740 * subsequently completed).
1741 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001742 * The library has not been previously initialized by psa_crypto_init().
1743 * It is implementation-dependent whether a failure to initialize
1744 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001745 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001746psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001747 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001748 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001749
1750/** Set the key for a multipart symmetric decryption operation.
1751 *
1752 * The sequence of operations to decrypt a message with a symmetric cipher
1753 * is as follows:
1754 * -# Allocate an operation object which will be passed to all the functions
1755 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001756 * -# Initialize the operation object with one of the methods described in the
1757 * documentation for #psa_cipher_operation_t, e.g.
1758 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001759 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001760 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001761 * decryption. If the IV is prepended to the ciphertext, you can call
1762 * psa_cipher_update() on a buffer containing the IV followed by the
1763 * beginning of the message.
1764 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1765 * of the message each time.
1766 * -# Call psa_cipher_finish().
1767 *
1768 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001769 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001770 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001771 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001772 * eventually terminate the operation. The following events terminate an
1773 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001774 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001775 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001776 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001777 * \param[in,out] operation The operation object to set up. It must have
1778 * been initialized as per the documentation for
1779 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001780 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001781 * It must remain valid until the operation
1782 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001783 * \param alg The cipher algorithm to compute
1784 * (\c PSA_ALG_XXX value such that
1785 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001786 *
Gilles Peskine28538492018-07-11 17:34:00 +02001787 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001788 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001789 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001790 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001791 * \retval #PSA_ERROR_NOT_PERMITTED
1792 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001793 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001794 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001795 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001796 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1797 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1798 * \retval #PSA_ERROR_HARDWARE_FAILURE
1799 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001800 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001801 * The operation state is not valid (already set up and not
1802 * subsequently completed).
1803 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001804 * The library has not been previously initialized by psa_crypto_init().
1805 * It is implementation-dependent whether a failure to initialize
1806 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001807 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001808psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001809 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001810 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001811
Gilles Peskinedcd14942018-07-12 00:30:52 +02001812/** Generate an IV for a symmetric encryption operation.
1813 *
1814 * This function generates a random IV (initialization vector), nonce
1815 * or initial counter value for the encryption operation as appropriate
1816 * for the chosen algorithm, key type and key size.
1817 *
1818 * The application must call psa_cipher_encrypt_setup() before
1819 * calling this function.
1820 *
1821 * If this function returns an error status, the operation becomes inactive.
1822 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001823 * \param[in,out] operation Active cipher operation.
1824 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001825 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001826 * \param[out] iv_length On success, the number of bytes of the
1827 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001828 *
1829 * \retval #PSA_SUCCESS
1830 * Success.
1831 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001832 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001833 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001834 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001835 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1836 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1837 * \retval #PSA_ERROR_HARDWARE_FAILURE
1838 * \retval #PSA_ERROR_TAMPERING_DETECTED
1839 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001840psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1841 unsigned char *iv,
1842 size_t iv_size,
1843 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001844
Gilles Peskinedcd14942018-07-12 00:30:52 +02001845/** Set the IV for a symmetric encryption or decryption operation.
1846 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001847 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001848 * or initial counter value for the encryption or decryption operation.
1849 *
1850 * The application must call psa_cipher_encrypt_setup() before
1851 * calling this function.
1852 *
1853 * If this function returns an error status, the operation becomes inactive.
1854 *
1855 * \note When encrypting, applications should use psa_cipher_generate_iv()
1856 * instead of this function, unless implementing a protocol that requires
1857 * a non-random IV.
1858 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001859 * \param[in,out] operation Active cipher operation.
1860 * \param[in] iv Buffer containing the IV to use.
1861 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001862 *
1863 * \retval #PSA_SUCCESS
1864 * Success.
1865 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001866 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001867 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001868 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001869 * or the chosen algorithm does not use an IV.
1870 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1871 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1872 * \retval #PSA_ERROR_HARDWARE_FAILURE
1873 * \retval #PSA_ERROR_TAMPERING_DETECTED
1874 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001875psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1876 const unsigned char *iv,
1877 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001878
Gilles Peskinedcd14942018-07-12 00:30:52 +02001879/** Encrypt or decrypt a message fragment in an active cipher operation.
1880 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001881 * Before calling this function, you must:
1882 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1883 * The choice of setup function determines whether this function
1884 * encrypts or decrypts its input.
1885 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1886 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001887 *
1888 * If this function returns an error status, the operation becomes inactive.
1889 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001890 * \param[in,out] operation Active cipher operation.
1891 * \param[in] input Buffer containing the message fragment to
1892 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001893 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001894 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001895 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001896 * \param[out] output_length On success, the number of bytes
1897 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001898 *
1899 * \retval #PSA_SUCCESS
1900 * Success.
1901 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001902 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001903 * not set, or already completed).
1904 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1905 * The size of the \p output buffer is too small.
1906 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1907 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1908 * \retval #PSA_ERROR_HARDWARE_FAILURE
1909 * \retval #PSA_ERROR_TAMPERING_DETECTED
1910 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001911psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1912 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001913 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001914 unsigned char *output,
1915 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001916 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001917
Gilles Peskinedcd14942018-07-12 00:30:52 +02001918/** Finish encrypting or decrypting a message in a cipher operation.
1919 *
1920 * The application must call psa_cipher_encrypt_setup() or
1921 * psa_cipher_decrypt_setup() before calling this function. The choice
1922 * of setup function determines whether this function encrypts or
1923 * decrypts its input.
1924 *
1925 * This function finishes the encryption or decryption of the message
1926 * formed by concatenating the inputs passed to preceding calls to
1927 * psa_cipher_update().
1928 *
1929 * When this function returns, the operation becomes inactive.
1930 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001931 * \param[in,out] operation Active cipher operation.
1932 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001933 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001934 * \param[out] output_length On success, the number of bytes
1935 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001936 *
1937 * \retval #PSA_SUCCESS
1938 * Success.
1939 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001940 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001941 * not set, or already completed).
1942 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1943 * The size of the \p output buffer is too small.
1944 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1945 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1946 * \retval #PSA_ERROR_HARDWARE_FAILURE
1947 * \retval #PSA_ERROR_TAMPERING_DETECTED
1948 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001949psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001950 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001951 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001952 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001953
Gilles Peskinedcd14942018-07-12 00:30:52 +02001954/** Abort a cipher operation.
1955 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001956 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001957 * \p operation structure itself. Once aborted, the operation object
1958 * can be reused for another operation by calling
1959 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001960 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001961 * You may call this function any time after the operation object has
1962 * been initialized by any of the following methods:
1963 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1964 * whether it succeeds or not.
1965 * - Initializing the \c struct to all-bits-zero.
1966 * - Initializing the \c struct to logical zeros, e.g.
1967 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001968 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001969 * In particular, calling psa_cipher_abort() after the operation has been
1970 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1971 * is safe and has no effect.
1972 *
1973 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001974 *
1975 * \retval #PSA_SUCCESS
1976 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001977 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001978 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1979 * \retval #PSA_ERROR_HARDWARE_FAILURE
1980 * \retval #PSA_ERROR_TAMPERING_DETECTED
1981 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001982psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1983
1984/**@}*/
1985
Gilles Peskine3b555712018-03-03 21:27:57 +01001986/** \defgroup aead Authenticated encryption with associated data (AEAD)
1987 * @{
1988 */
1989
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001990/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001991 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001992 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001993 * \param alg The AEAD algorithm to compute
1994 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001995 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001996 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001997 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001998 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001999 * but not encrypted.
2000 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002001 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002002 * encrypted.
2003 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002004 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002005 * encrypted data. The additional data is not
2006 * part of this output. For algorithms where the
2007 * encrypted data and the authentication tag
2008 * are defined as separate outputs, the
2009 * authentication tag is appended to the
2010 * encrypted data.
2011 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2012 * This must be at least
2013 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2014 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002015 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002016 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002017 *
Gilles Peskine28538492018-07-11 17:34:00 +02002018 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002019 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002020 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002021 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002022 * \retval #PSA_ERROR_NOT_PERMITTED
2023 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002024 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002025 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002026 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002027 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2028 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2029 * \retval #PSA_ERROR_HARDWARE_FAILURE
2030 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002031 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002032 * The library has not been previously initialized by psa_crypto_init().
2033 * It is implementation-dependent whether a failure to initialize
2034 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002035 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002036psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002037 psa_algorithm_t alg,
2038 const uint8_t *nonce,
2039 size_t nonce_length,
2040 const uint8_t *additional_data,
2041 size_t additional_data_length,
2042 const uint8_t *plaintext,
2043 size_t plaintext_length,
2044 uint8_t *ciphertext,
2045 size_t ciphertext_size,
2046 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002047
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002048/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002049 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002050 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002051 * \param alg The AEAD algorithm to compute
2052 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002053 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002054 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002055 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002056 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002057 * but not encrypted.
2058 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002059 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002060 * encrypted. For algorithms where the
2061 * encrypted data and the authentication tag
2062 * are defined as separate inputs, the buffer
2063 * must contain the encrypted data followed
2064 * by the authentication tag.
2065 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002066 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002067 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2068 * This must be at least
2069 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2070 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002071 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002072 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002073 *
Gilles Peskine28538492018-07-11 17:34:00 +02002074 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002075 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002076 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002077 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002078 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002079 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002080 * \retval #PSA_ERROR_NOT_PERMITTED
2081 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002082 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002083 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002084 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002085 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2086 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2087 * \retval #PSA_ERROR_HARDWARE_FAILURE
2088 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002089 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002090 * The library has not been previously initialized by psa_crypto_init().
2091 * It is implementation-dependent whether a failure to initialize
2092 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002093 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002094psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002095 psa_algorithm_t alg,
2096 const uint8_t *nonce,
2097 size_t nonce_length,
2098 const uint8_t *additional_data,
2099 size_t additional_data_length,
2100 const uint8_t *ciphertext,
2101 size_t ciphertext_length,
2102 uint8_t *plaintext,
2103 size_t plaintext_size,
2104 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002105
Gilles Peskine30a9e412019-01-14 18:36:12 +01002106/** The type of the state data structure for multipart AEAD operations.
2107 *
2108 * Before calling any function on an AEAD operation object, the application
2109 * must initialize it by any of the following means:
2110 * - Set the structure to all-bits-zero, for example:
2111 * \code
2112 * psa_aead_operation_t operation;
2113 * memset(&operation, 0, sizeof(operation));
2114 * \endcode
2115 * - Initialize the structure to logical zero values, for example:
2116 * \code
2117 * psa_aead_operation_t operation = {0};
2118 * \endcode
2119 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2120 * for example:
2121 * \code
2122 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2123 * \endcode
2124 * - Assign the result of the function psa_aead_operation_init()
2125 * to the structure, for example:
2126 * \code
2127 * psa_aead_operation_t operation;
2128 * operation = psa_aead_operation_init();
2129 * \endcode
2130 *
2131 * This is an implementation-defined \c struct. Applications should not
2132 * make any assumptions about the content of this structure except
2133 * as directed by the documentation of a specific implementation. */
2134typedef struct psa_aead_operation_s psa_aead_operation_t;
2135
2136/** \def PSA_AEAD_OPERATION_INIT
2137 *
2138 * This macro returns a suitable initializer for an AEAD operation object of
2139 * type #psa_aead_operation_t.
2140 */
2141#ifdef __DOXYGEN_ONLY__
2142/* This is an example definition for documentation purposes.
2143 * Implementations should define a suitable value in `crypto_struct.h`.
2144 */
2145#define PSA_AEAD_OPERATION_INIT {0}
2146#endif
2147
2148/** Return an initial value for an AEAD operation object.
2149 */
2150static psa_aead_operation_t psa_aead_operation_init(void);
2151
2152/** Set the key for a multipart authenticated encryption operation.
2153 *
2154 * The sequence of operations to encrypt a message with authentication
2155 * is as follows:
2156 * -# Allocate an operation object which will be passed to all the functions
2157 * listed here.
2158 * -# Initialize the operation object with one of the methods described in the
2159 * documentation for #psa_aead_operation_t, e.g.
2160 * PSA_AEAD_OPERATION_INIT.
2161 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002162 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2163 * inputs to the subsequent calls to psa_aead_update_ad() and
2164 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2165 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002166 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2167 * generate or set the nonce. You should use
2168 * psa_aead_generate_nonce() unless the protocol you are implementing
2169 * requires a specific nonce value.
2170 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2171 * of the non-encrypted additional authenticated data each time.
2172 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002173 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002174 * -# Call psa_aead_finish().
2175 *
2176 * The application may call psa_aead_abort() at any time after the operation
2177 * has been initialized.
2178 *
2179 * After a successful call to psa_aead_encrypt_setup(), the application must
2180 * eventually terminate the operation. The following events terminate an
2181 * operation:
2182 * - A failed call to any of the \c psa_aead_xxx functions.
2183 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2184 *
2185 * \param[in,out] operation The operation object to set up. It must have
2186 * been initialized as per the documentation for
2187 * #psa_aead_operation_t and not yet in use.
2188 * \param handle Handle to the key to use for the operation.
2189 * It must remain valid until the operation
2190 * terminates.
2191 * \param alg The AEAD algorithm to compute
2192 * (\c PSA_ALG_XXX value such that
2193 * #PSA_ALG_IS_AEAD(\p alg) is true).
2194 *
2195 * \retval #PSA_SUCCESS
2196 * Success.
2197 * \retval #PSA_ERROR_INVALID_HANDLE
2198 * \retval #PSA_ERROR_EMPTY_SLOT
2199 * \retval #PSA_ERROR_NOT_PERMITTED
2200 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002201 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002202 * \retval #PSA_ERROR_NOT_SUPPORTED
2203 * \p alg is not supported or is not an AEAD algorithm.
2204 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2205 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2206 * \retval #PSA_ERROR_HARDWARE_FAILURE
2207 * \retval #PSA_ERROR_TAMPERING_DETECTED
2208 * \retval #PSA_ERROR_BAD_STATE
2209 * The library has not been previously initialized by psa_crypto_init().
2210 * It is implementation-dependent whether a failure to initialize
2211 * results in this error code.
2212 */
2213psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2214 psa_key_handle_t handle,
2215 psa_algorithm_t alg);
2216
2217/** Set the key for a multipart authenticated decryption operation.
2218 *
2219 * The sequence of operations to decrypt a message with authentication
2220 * is as follows:
2221 * -# Allocate an operation object which will be passed to all the functions
2222 * listed here.
2223 * -# Initialize the operation object with one of the methods described in the
2224 * documentation for #psa_aead_operation_t, e.g.
2225 * PSA_AEAD_OPERATION_INIT.
2226 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002227 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2228 * inputs to the subsequent calls to psa_aead_update_ad() and
2229 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2230 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002231 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2232 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2233 * of the non-encrypted additional authenticated data each time.
2234 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002235 * of the ciphertext to decrypt each time.
2236 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002237 *
2238 * The application may call psa_aead_abort() at any time after the operation
2239 * has been initialized.
2240 *
2241 * After a successful call to psa_aead_decrypt_setup(), the application must
2242 * eventually terminate the operation. The following events terminate an
2243 * operation:
2244 * - A failed call to any of the \c psa_aead_xxx functions.
2245 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2246 *
2247 * \param[in,out] operation The operation object to set up. It must have
2248 * been initialized as per the documentation for
2249 * #psa_aead_operation_t and not yet in use.
2250 * \param handle Handle to the key to use for the operation.
2251 * It must remain valid until the operation
2252 * terminates.
2253 * \param alg The AEAD algorithm to compute
2254 * (\c PSA_ALG_XXX value such that
2255 * #PSA_ALG_IS_AEAD(\p alg) is true).
2256 *
2257 * \retval #PSA_SUCCESS
2258 * Success.
2259 * \retval #PSA_ERROR_INVALID_HANDLE
2260 * \retval #PSA_ERROR_EMPTY_SLOT
2261 * \retval #PSA_ERROR_NOT_PERMITTED
2262 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002263 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002264 * \retval #PSA_ERROR_NOT_SUPPORTED
2265 * \p alg is not supported or is not an AEAD algorithm.
2266 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2267 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2268 * \retval #PSA_ERROR_HARDWARE_FAILURE
2269 * \retval #PSA_ERROR_TAMPERING_DETECTED
2270 * \retval #PSA_ERROR_BAD_STATE
2271 * The library has not been previously initialized by psa_crypto_init().
2272 * It is implementation-dependent whether a failure to initialize
2273 * results in this error code.
2274 */
2275psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2276 psa_key_handle_t handle,
2277 psa_algorithm_t alg);
2278
2279/** Generate a random nonce for an authenticated encryption operation.
2280 *
2281 * This function generates a random nonce for the authenticated encryption
2282 * operation with an appropriate size for the chosen algorithm, key type
2283 * and key size.
2284 *
2285 * The application must call psa_aead_encrypt_setup() before
2286 * calling this function.
2287 *
2288 * If this function returns an error status, the operation becomes inactive.
2289 *
2290 * \param[in,out] operation Active AEAD operation.
2291 * \param[out] nonce Buffer where the generated nonce is to be
2292 * written.
2293 * \param nonce_size Size of the \p nonce buffer in bytes.
2294 * \param[out] nonce_length On success, the number of bytes of the
2295 * generated nonce.
2296 *
2297 * \retval #PSA_SUCCESS
2298 * Success.
2299 * \retval #PSA_ERROR_BAD_STATE
2300 * The operation state is not valid (not set up, or nonce already set).
2301 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2302 * The size of the \p nonce buffer is too small.
2303 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2304 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2305 * \retval #PSA_ERROR_HARDWARE_FAILURE
2306 * \retval #PSA_ERROR_TAMPERING_DETECTED
2307 */
2308psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2309 unsigned char *nonce,
2310 size_t nonce_size,
2311 size_t *nonce_length);
2312
2313/** Set the nonce for an authenticated encryption or decryption operation.
2314 *
2315 * This function sets the nonce for the authenticated
2316 * encryption or decryption operation.
2317 *
2318 * The application must call psa_aead_encrypt_setup() before
2319 * calling this function.
2320 *
2321 * If this function returns an error status, the operation becomes inactive.
2322 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002323 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002324 * instead of this function, unless implementing a protocol that requires
2325 * a non-random IV.
2326 *
2327 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002328 * \param[in] nonce Buffer containing the nonce to use.
2329 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002330 *
2331 * \retval #PSA_SUCCESS
2332 * Success.
2333 * \retval #PSA_ERROR_BAD_STATE
2334 * The operation state is not valid (not set up, or nonce already set).
2335 * \retval #PSA_ERROR_INVALID_ARGUMENT
2336 * The size of \p nonce is not acceptable for the chosen algorithm.
2337 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2338 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2339 * \retval #PSA_ERROR_HARDWARE_FAILURE
2340 * \retval #PSA_ERROR_TAMPERING_DETECTED
2341 */
2342psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2343 const unsigned char *nonce,
2344 size_t nonce_length);
2345
Gilles Peskinebc59c852019-01-17 15:26:08 +01002346/** Declare the lengths of the message and additional data for AEAD.
2347 *
2348 * The application must call this function before calling
2349 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2350 * the operation requires it. If the algorithm does not require it,
2351 * calling this function is optional, but if this function is called
2352 * then the implementation must enforce the lengths.
2353 *
2354 * You may call this function before or after setting the nonce with
2355 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2356 *
2357 * - For #PSA_ALG_CCM, calling this function is required.
2358 * - For the other AEAD algorithms defined in this specification, calling
2359 * this function is not required.
2360 * - For vendor-defined algorithm, refer to the vendor documentation.
2361 *
2362 * \param[in,out] operation Active AEAD operation.
2363 * \param ad_length Size of the non-encrypted additional
2364 * authenticated data in bytes.
2365 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2366 *
2367 * \retval #PSA_SUCCESS
2368 * Success.
2369 * \retval #PSA_ERROR_BAD_STATE
2370 * The operation state is not valid (not set up, already completed,
2371 * or psa_aead_update_ad() or psa_aead_update() already called).
2372 * \retval #PSA_ERROR_INVALID_ARGUMENT
2373 * At least one of the lengths is not acceptable for the chosen
2374 * algorithm.
2375 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2376 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2377 * \retval #PSA_ERROR_HARDWARE_FAILURE
2378 * \retval #PSA_ERROR_TAMPERING_DETECTED
2379 */
2380psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2381 size_t ad_length,
2382 size_t plaintext_length);
2383
Gilles Peskine30a9e412019-01-14 18:36:12 +01002384/** Pass additional data to an active AEAD operation.
2385 *
2386 * Additional data is authenticated, but not encrypted.
2387 *
2388 * You may call this function multiple times to pass successive fragments
2389 * of the additional data. You may not call this function after passing
2390 * data to encrypt or decrypt with psa_aead_update().
2391 *
2392 * Before calling this function, you must:
2393 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2394 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2395 *
2396 * If this function returns an error status, the operation becomes inactive.
2397 *
2398 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2399 * there is no guarantee that the input is valid. Therefore, until
2400 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2401 * treat the input as untrusted and prepare to undo any action that
2402 * depends on the input if psa_aead_verify() returns an error status.
2403 *
2404 * \param[in,out] operation Active AEAD operation.
2405 * \param[in] input Buffer containing the fragment of
2406 * additional data.
2407 * \param input_length Size of the \p input buffer in bytes.
2408 *
2409 * \retval #PSA_SUCCESS
2410 * Success.
2411 * \retval #PSA_ERROR_BAD_STATE
2412 * The operation state is not valid (not set up, nonce not set,
2413 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002414 * \retval #PSA_ERROR_INVALID_ARGUMENT
2415 * The total input length overflows the additional data length that
2416 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002417 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2418 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2419 * \retval #PSA_ERROR_HARDWARE_FAILURE
2420 * \retval #PSA_ERROR_TAMPERING_DETECTED
2421 */
2422psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2423 const uint8_t *input,
2424 size_t input_length);
2425
2426/** Encrypt or decrypt a message fragment in an active AEAD operation.
2427 *
2428 * Before calling this function, you must:
2429 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2430 * The choice of setup function determines whether this function
2431 * encrypts or decrypts its input.
2432 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2433 * 3. Call psa_aead_update_ad() to pass all the additional data.
2434 *
2435 * If this function returns an error status, the operation becomes inactive.
2436 *
2437 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2438 * there is no guarantee that the input is valid. Therefore, until
2439 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2440 * - Do not use the output in any way other than storing it in a
2441 * confidential location. If you take any action that depends
2442 * on the tentative decrypted data, this action will need to be
2443 * undone if the input turns out not to be valid. Furthermore,
2444 * if an adversary can observe that this action took place
2445 * (for example through timing), they may be able to use this
2446 * fact as an oracle to decrypt any message encrypted with the
2447 * same key.
2448 * - In particular, do not copy the output anywhere but to a
2449 * memory or storage space that you have exclusive access to.
2450 *
2451 * \param[in,out] operation Active AEAD operation.
2452 * \param[in] input Buffer containing the message fragment to
2453 * encrypt or decrypt.
2454 * \param input_length Size of the \p input buffer in bytes.
2455 * \param[out] output Buffer where the output is to be written.
2456 * \param output_size Size of the \p output buffer in bytes.
2457 * \param[out] output_length On success, the number of bytes
2458 * that make up the returned output.
2459 *
2460 * \retval #PSA_SUCCESS
2461 * Success.
2462 * \retval #PSA_ERROR_BAD_STATE
2463 * The operation state is not valid (not set up, nonce not set
2464 * or already completed).
2465 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2466 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002467 * \retval #PSA_ERROR_INVALID_ARGUMENT
2468 * The total length of input to psa_aead_update_ad() so far is
2469 * less than the additional data length that was previously
2470 * specified with psa_aead_set_lengths().
2471 * \retval #PSA_ERROR_INVALID_ARGUMENT
2472 * The total input length overflows the plaintext length that
2473 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002474 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2475 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2476 * \retval #PSA_ERROR_HARDWARE_FAILURE
2477 * \retval #PSA_ERROR_TAMPERING_DETECTED
2478 */
2479psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2480 const uint8_t *input,
2481 size_t input_length,
2482 unsigned char *output,
2483 size_t output_size,
2484 size_t *output_length);
2485
2486/** Finish encrypting a message in an AEAD operation.
2487 *
2488 * The operation must have been set up with psa_aead_encrypt_setup().
2489 *
2490 * This function finishes the authentication of the additional data
2491 * formed by concatenating the inputs passed to preceding calls to
2492 * psa_aead_update_ad() with the plaintext formed by concatenating the
2493 * inputs passed to preceding calls to psa_aead_update().
2494 *
2495 * This function has two output buffers:
2496 * - \p ciphertext contains trailing ciphertext that was buffered from
2497 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2498 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2499 * will not contain any output and can be a 0-sized buffer.
2500 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002501 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002502 * that the operation performs.
2503 *
2504 * When this function returns, the operation becomes inactive.
2505 *
2506 * \param[in,out] operation Active AEAD operation.
2507 * \param[out] ciphertext Buffer where the last part of the ciphertext
2508 * is to be written.
2509 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2510 * \param[out] ciphertext_length On success, the number of bytes of
2511 * returned ciphertext.
2512 * \param[out] tag Buffer where the authentication tag is
2513 * to be written.
2514 * \param tag_size Size of the \p tag buffer in bytes.
2515 * \param[out] tag_length On success, the number of bytes
2516 * that make up the returned tag.
2517 *
2518 * \retval #PSA_SUCCESS
2519 * Success.
2520 * \retval #PSA_ERROR_BAD_STATE
2521 * The operation state is not valid (not set up, nonce not set,
2522 * decryption, or already completed).
2523 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002524 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002525 * \retval #PSA_ERROR_INVALID_ARGUMENT
2526 * The total length of input to psa_aead_update_ad() so far is
2527 * less than the additional data length that was previously
2528 * specified with psa_aead_set_lengths().
2529 * \retval #PSA_ERROR_INVALID_ARGUMENT
2530 * The total length of input to psa_aead_update() so far is
2531 * less than the plaintext length that was previously
2532 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002533 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2534 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2535 * \retval #PSA_ERROR_HARDWARE_FAILURE
2536 * \retval #PSA_ERROR_TAMPERING_DETECTED
2537 */
2538psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002539 uint8_t *ciphertext,
2540 size_t ciphertext_size,
2541 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002542 uint8_t *tag,
2543 size_t tag_size,
2544 size_t *tag_length);
2545
2546/** Finish authenticating and decrypting a message in an AEAD operation.
2547 *
2548 * The operation must have been set up with psa_aead_decrypt_setup().
2549 *
2550 * This function finishes the authentication of the additional data
2551 * formed by concatenating the inputs passed to preceding calls to
2552 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2553 * inputs passed to preceding calls to psa_aead_update().
2554 *
2555 * When this function returns, the operation becomes inactive.
2556 *
2557 * \param[in,out] operation Active AEAD operation.
2558 * \param[in] tag Buffer containing the authentication tag.
2559 * \param tag_length Size of the \p tag buffer in bytes.
2560 *
2561 * \retval #PSA_SUCCESS
2562 * Success.
2563 * \retval #PSA_ERROR_BAD_STATE
2564 * The operation state is not valid (not set up, nonce not set,
2565 * encryption, or already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002566 * \retval #PSA_ERROR_INVALID_ARGUMENT
2567 * The total length of input to psa_aead_update_ad() so far is
2568 * less than the additional data length that was previously
2569 * specified with psa_aead_set_lengths().
2570 * \retval #PSA_ERROR_INVALID_ARGUMENT
2571 * The total length of input to psa_aead_update() so far is
2572 * less than the plaintext length that was previously
2573 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002574 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2575 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2576 * \retval #PSA_ERROR_HARDWARE_FAILURE
2577 * \retval #PSA_ERROR_TAMPERING_DETECTED
2578 */
2579psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2580 const uint8_t *tag,
2581 size_t tag_length);
2582
2583/** Abort an AEAD operation.
2584 *
2585 * Aborting an operation frees all associated resources except for the
2586 * \p operation structure itself. Once aborted, the operation object
2587 * can be reused for another operation by calling
2588 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2589 *
2590 * You may call this function any time after the operation object has
2591 * been initialized by any of the following methods:
2592 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2593 * whether it succeeds or not.
2594 * - Initializing the \c struct to all-bits-zero.
2595 * - Initializing the \c struct to logical zeros, e.g.
2596 * `psa_aead_operation_t operation = {0}`.
2597 *
2598 * In particular, calling psa_aead_abort() after the operation has been
2599 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2600 * is safe and has no effect.
2601 *
2602 * \param[in,out] operation Initialized AEAD operation.
2603 *
2604 * \retval #PSA_SUCCESS
2605 * \retval #PSA_ERROR_BAD_STATE
2606 * \p operation is not an active AEAD operation.
2607 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2608 * \retval #PSA_ERROR_HARDWARE_FAILURE
2609 * \retval #PSA_ERROR_TAMPERING_DETECTED
2610 */
2611psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2612
Gilles Peskine3b555712018-03-03 21:27:57 +01002613/**@}*/
2614
Gilles Peskine20035e32018-02-03 22:44:14 +01002615/** \defgroup asymmetric Asymmetric cryptography
2616 * @{
2617 */
2618
2619/**
2620 * \brief Sign a hash or short message with a private key.
2621 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002622 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002623 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002624 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2625 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2626 * to determine the hash algorithm to use.
2627 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002628 * \param handle Handle to the key to use for the operation.
2629 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002630 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002631 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002632 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002633 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002634 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002635 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002636 * \param[out] signature_length On success, the number of bytes
2637 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002638 *
Gilles Peskine28538492018-07-11 17:34:00 +02002639 * \retval #PSA_SUCCESS
2640 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002641 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002642 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002643 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002644 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002645 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002646 * \retval #PSA_ERROR_NOT_SUPPORTED
2647 * \retval #PSA_ERROR_INVALID_ARGUMENT
2648 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2649 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2650 * \retval #PSA_ERROR_HARDWARE_FAILURE
2651 * \retval #PSA_ERROR_TAMPERING_DETECTED
2652 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002653 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002654 * The library has not been previously initialized by psa_crypto_init().
2655 * It is implementation-dependent whether a failure to initialize
2656 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002657 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002658psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002659 psa_algorithm_t alg,
2660 const uint8_t *hash,
2661 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002662 uint8_t *signature,
2663 size_t signature_size,
2664 size_t *signature_length);
2665
2666/**
2667 * \brief Verify the signature a hash or short message using a public key.
2668 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002669 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002670 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002671 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2672 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2673 * to determine the hash algorithm to use.
2674 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002675 * \param handle Handle to the key to use for the operation.
2676 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002677 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002678 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002679 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002680 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002681 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002682 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002683 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002684 *
Gilles Peskine28538492018-07-11 17:34:00 +02002685 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002686 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002687 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002688 * The calculation was perfomed successfully, but the passed
2689 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002690 * \retval #PSA_ERROR_NOT_SUPPORTED
2691 * \retval #PSA_ERROR_INVALID_ARGUMENT
2692 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2693 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2694 * \retval #PSA_ERROR_HARDWARE_FAILURE
2695 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002696 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002697 * The library has not been previously initialized by psa_crypto_init().
2698 * It is implementation-dependent whether a failure to initialize
2699 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002700 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002701psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002702 psa_algorithm_t alg,
2703 const uint8_t *hash,
2704 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002705 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002706 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002707
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002708/**
2709 * \brief Encrypt a short message with a public key.
2710 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002711 * \param handle Handle to the key to use for the operation.
2712 * It must be a public key or an asymmetric
2713 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002714 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002715 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002716 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002717 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002718 * \param[in] salt A salt or label, if supported by the
2719 * encryption algorithm.
2720 * If the algorithm does not support a
2721 * salt, pass \c NULL.
2722 * If the algorithm supports an optional
2723 * salt and you do not want to pass a salt,
2724 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002725 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002726 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2727 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002728 * \param salt_length Size of the \p salt buffer in bytes.
2729 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002730 * \param[out] output Buffer where the encrypted message is to
2731 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002732 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002733 * \param[out] output_length On success, the number of bytes
2734 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002735 *
Gilles Peskine28538492018-07-11 17:34:00 +02002736 * \retval #PSA_SUCCESS
2737 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002738 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002739 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002740 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002741 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002742 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002743 * \retval #PSA_ERROR_NOT_SUPPORTED
2744 * \retval #PSA_ERROR_INVALID_ARGUMENT
2745 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2746 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2747 * \retval #PSA_ERROR_HARDWARE_FAILURE
2748 * \retval #PSA_ERROR_TAMPERING_DETECTED
2749 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002750 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002751 * The library has not been previously initialized by psa_crypto_init().
2752 * It is implementation-dependent whether a failure to initialize
2753 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002754 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002755psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002756 psa_algorithm_t alg,
2757 const uint8_t *input,
2758 size_t input_length,
2759 const uint8_t *salt,
2760 size_t salt_length,
2761 uint8_t *output,
2762 size_t output_size,
2763 size_t *output_length);
2764
2765/**
2766 * \brief Decrypt a short message with a private key.
2767 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002768 * \param handle Handle to the key to use for the operation.
2769 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002770 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002771 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002772 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002773 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002774 * \param[in] salt A salt or label, if supported by the
2775 * encryption algorithm.
2776 * If the algorithm does not support a
2777 * salt, pass \c NULL.
2778 * If the algorithm supports an optional
2779 * salt and you do not want to pass a salt,
2780 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002781 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002782 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2783 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002784 * \param salt_length Size of the \p salt buffer in bytes.
2785 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002786 * \param[out] output Buffer where the decrypted message is to
2787 * be written.
2788 * \param output_size Size of the \c output buffer in bytes.
2789 * \param[out] output_length On success, the number of bytes
2790 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002791 *
Gilles Peskine28538492018-07-11 17:34:00 +02002792 * \retval #PSA_SUCCESS
2793 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002794 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002795 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002796 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002797 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002798 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002799 * \retval #PSA_ERROR_NOT_SUPPORTED
2800 * \retval #PSA_ERROR_INVALID_ARGUMENT
2801 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2802 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2803 * \retval #PSA_ERROR_HARDWARE_FAILURE
2804 * \retval #PSA_ERROR_TAMPERING_DETECTED
2805 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2806 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002807 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002808 * The library has not been previously initialized by psa_crypto_init().
2809 * It is implementation-dependent whether a failure to initialize
2810 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002811 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002812psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002813 psa_algorithm_t alg,
2814 const uint8_t *input,
2815 size_t input_length,
2816 const uint8_t *salt,
2817 size_t salt_length,
2818 uint8_t *output,
2819 size_t output_size,
2820 size_t *output_length);
2821
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002822/**@}*/
2823
Gilles Peskineedd76872018-07-20 17:42:05 +02002824/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002825 * @{
2826 */
2827
2828/** The type of the state data structure for generators.
2829 *
2830 * Before calling any function on a generator, the application must
2831 * initialize it by any of the following means:
2832 * - Set the structure to all-bits-zero, for example:
2833 * \code
2834 * psa_crypto_generator_t generator;
2835 * memset(&generator, 0, sizeof(generator));
2836 * \endcode
2837 * - Initialize the structure to logical zero values, for example:
2838 * \code
2839 * psa_crypto_generator_t generator = {0};
2840 * \endcode
2841 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2842 * for example:
2843 * \code
2844 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2845 * \endcode
2846 * - Assign the result of the function psa_crypto_generator_init()
2847 * to the structure, for example:
2848 * \code
2849 * psa_crypto_generator_t generator;
2850 * generator = psa_crypto_generator_init();
2851 * \endcode
2852 *
2853 * This is an implementation-defined \c struct. Applications should not
2854 * make any assumptions about the content of this structure except
2855 * as directed by the documentation of a specific implementation.
2856 */
2857typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2858
2859/** \def PSA_CRYPTO_GENERATOR_INIT
2860 *
2861 * This macro returns a suitable initializer for a generator object
2862 * of type #psa_crypto_generator_t.
2863 */
2864#ifdef __DOXYGEN_ONLY__
2865/* This is an example definition for documentation purposes.
2866 * Implementations should define a suitable value in `crypto_struct.h`.
2867 */
2868#define PSA_CRYPTO_GENERATOR_INIT {0}
2869#endif
2870
2871/** Return an initial value for a generator object.
2872 */
2873static psa_crypto_generator_t psa_crypto_generator_init(void);
2874
2875/** Retrieve the current capacity of a generator.
2876 *
2877 * The capacity of a generator is the maximum number of bytes that it can
2878 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2879 *
2880 * \param[in] generator The generator to query.
2881 * \param[out] capacity On success, the capacity of the generator.
2882 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002883 * \retval #PSA_SUCCESS
2884 * \retval #PSA_ERROR_BAD_STATE
2885 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002886 */
2887psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2888 size_t *capacity);
2889
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002890/** Set the maximum capacity of a generator.
2891 *
2892 * \param[in,out] generator The generator object to modify.
2893 * \param capacity The new capacity of the generator.
2894 * It must be less or equal to the generator's
2895 * current capacity.
2896 *
2897 * \retval #PSA_SUCCESS
2898 * \retval #PSA_ERROR_INVALID_ARGUMENT
2899 * \p capacity is larger than the generator's current capacity.
2900 * \retval #PSA_ERROR_BAD_STATE
2901 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2902 */
2903psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2904 size_t capacity);
2905
Gilles Peskineeab56e42018-07-12 17:12:33 +02002906/** Read some data from a generator.
2907 *
2908 * This function reads and returns a sequence of bytes from a generator.
2909 * The data that is read is discarded from the generator. The generator's
2910 * capacity is decreased by the number of bytes read.
2911 *
2912 * \param[in,out] generator The generator object to read from.
2913 * \param[out] output Buffer where the generator output will be
2914 * written.
2915 * \param output_length Number of bytes to output.
2916 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002917 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02002918 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02002919 * There were fewer than \p output_length bytes
2920 * in the generator. Note that in this case, no
2921 * output is written to the output buffer.
2922 * The generator's capacity is set to 0, thus
2923 * subsequent calls to this function will not
2924 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002925 * \retval #PSA_ERROR_BAD_STATE
2926 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2927 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2928 * \retval #PSA_ERROR_HARDWARE_FAILURE
2929 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002930 */
2931psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2932 uint8_t *output,
2933 size_t output_length);
2934
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002935/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002936 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002937 * This function uses the output of a generator to derive a key.
2938 * How much output it consumes and how the key is derived depends on the
2939 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002940 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002941 * - For key types for which the key is an arbitrary sequence of bytes
2942 * of a given size,
2943 * this function is functionally equivalent to calling #psa_generator_read
2944 * and passing the resulting output to #psa_import_key.
2945 * However, this function has a security benefit:
2946 * if the implementation provides an isolation boundary then
2947 * the key material is not exposed outside the isolation boundary.
2948 * As a consequence, for these key types, this function always consumes
2949 * exactly (\p bits / 8) bytes from the generator.
2950 * The following key types defined in this specification follow this scheme:
2951 *
2952 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002953 * - #PSA_KEY_TYPE_ARC4;
2954 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002955 * - #PSA_KEY_TYPE_DERIVE;
2956 * - #PSA_KEY_TYPE_HMAC.
2957 *
2958 * - For ECC keys on a Montgomery elliptic curve
2959 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
2960 * Montgomery curve), this function always draws a byte string whose
2961 * length is determined by the curve, and sets the mandatory bits
2962 * accordingly. That is:
2963 *
2964 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
2965 * and process it as specified in RFC 7748 &sect;5.
2966 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
2967 * and process it as specified in RFC 7748 &sect;5.
2968 *
2969 * - For key types for which the key is represented by a single sequence of
2970 * \p bits bits with constraints as to which bit sequences are acceptable,
2971 * this function draws a byte string of length (\p bits / 8) bytes rounded
2972 * up to the nearest whole number of bytes. If the resulting byte string
2973 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
2974 * This process is repeated until an acceptable byte string is drawn.
2975 * The byte string drawn from the generator is interpreted as specified
2976 * for the output produced by psa_export_key().
2977 * The following key types defined in this specification follow this scheme:
2978 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01002979 * - #PSA_KEY_TYPE_DES.
2980 * Force-set the parity bits, but discard forbidden weak keys.
2981 * For 2-key and 3-key triple-DES, the three keys are generated
2982 * successively (for example, for 3-key triple-DES,
2983 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
2984 * discard the first 8 bytes, use the next 8 bytes as the first key,
2985 * and continue reading output from the generator to derive the other
2986 * two keys).
2987 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
2988 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
2989 * ECC keys on a Weierstrass elliptic curve
2990 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
2991 * Weierstrass curve).
2992 * For these key types, interpret the byte string as integer
2993 * in big-endian order. Discard it if it is not in the range
2994 * [0, *N* - 2] where *N* is the boundary of the private key domain
2995 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01002996 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01002997 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01002998 * This method allows compliance to NIST standards, specifically
2999 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003000 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3001 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3002 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3003 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003004 *
3005 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
3006 * the way in which the generator output is consumed is
3007 * implementation-defined.
3008 *
3009 * In all cases, the data that is read is discarded from the generator.
3010 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003011 *
Gilles Peskine20628592019-04-19 19:29:50 +02003012 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003013 * \param[out] handle On success, a handle to the newly created key.
3014 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003015 * \param[in,out] generator The generator object to read from.
3016 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003017 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003018 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003019 * If the key is persistent, the key material and the key's metadata
3020 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003021 * \retval #PSA_ERROR_ALREADY_EXISTS
3022 * This is an attempt to create a persistent key, and there is
3023 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003024 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003025 * There was not enough data to create the desired key.
3026 * Note that in this case, no output is written to the output buffer.
3027 * The generator's capacity is set to 0, thus subsequent calls to
3028 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003029 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003030 * The key type or key size is not supported, either by the
3031 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003032 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003033 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3034 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3035 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3036 * \retval #PSA_ERROR_HARDWARE_FAILURE
3037 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003038 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003039 * The library has not been previously initialized by psa_crypto_init().
3040 * It is implementation-dependent whether a failure to initialize
3041 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003042 */
Gilles Peskine87a5e562019-04-17 12:28:25 +02003043psa_status_t psa_generator_import_key(const psa_key_attributes_t *attributes,
3044 psa_key_handle_t *handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003045 psa_crypto_generator_t *generator);
3046
3047/** Abort a generator.
3048 *
3049 * Once a generator has been aborted, its capacity is zero.
3050 * Aborting a generator frees all associated resources except for the
3051 * \c generator structure itself.
3052 *
3053 * This function may be called at any time as long as the generator
3054 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3055 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3056 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3057 * on a generator that has not been set up.
3058 *
3059 * Once aborted, the generator object may be called.
3060 *
3061 * \param[in,out] generator The generator to abort.
3062 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003063 * \retval #PSA_SUCCESS
3064 * \retval #PSA_ERROR_BAD_STATE
3065 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3066 * \retval #PSA_ERROR_HARDWARE_FAILURE
3067 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003068 */
3069psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3070
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003071/** Use the maximum possible capacity for a generator.
3072 *
3073 * Use this value as the capacity argument when setting up a generator
3074 * to indicate that the generator should have the maximum possible capacity.
3075 * The value of the maximum possible capacity depends on the generator
3076 * algorithm.
3077 */
3078#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3079
Gilles Peskineeab56e42018-07-12 17:12:33 +02003080/**@}*/
3081
Gilles Peskineea0fb492018-07-12 17:17:20 +02003082/** \defgroup derivation Key derivation
3083 * @{
3084 */
3085
3086/** Set up a key derivation operation.
3087 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003088 * A key derivation algorithm takes some inputs and uses them to create
3089 * a byte generator which can be used to produce keys and other
3090 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003091 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003092 * To use a generator for key derivation:
3093 * - Start with an initialized object of type #psa_crypto_generator_t.
3094 * - Call psa_key_derivation_setup() to select the algorithm.
3095 * - Provide the inputs for the key derivation by calling
3096 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3097 * as appropriate. Which inputs are needed, in what order, and whether
3098 * they may be keys and if so of what type depends on the algorithm.
3099 * - Optionally set the generator's maximum capacity with
3100 * psa_set_generator_capacity(). You may do this before, in the middle of
3101 * or after providing inputs. For some algorithms, this step is mandatory
3102 * because the output depends on the maximum capacity.
3103 * - Generate output with psa_generator_read() or
3104 * psa_generator_import_key(). Successive calls to these functions
3105 * use successive output bytes from the generator.
3106 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003107 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003108 * \param[in,out] generator The generator object to set up. It must
3109 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003110 * \param alg The key derivation algorithm to compute
3111 * (\c PSA_ALG_XXX value such that
3112 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003113 *
3114 * \retval #PSA_SUCCESS
3115 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003116 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003117 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003118 * \retval #PSA_ERROR_NOT_SUPPORTED
3119 * \c alg is not supported or is not a key derivation algorithm.
3120 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3121 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3122 * \retval #PSA_ERROR_HARDWARE_FAILURE
3123 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003124 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003125 */
3126psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3127 psa_algorithm_t alg);
3128
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003129/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003130 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003131 * Which inputs are required and in what order depends on the algorithm.
3132 * Refer to the documentation of each key derivation or key agreement
3133 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003134 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003135 * This function passes direct inputs. Some inputs must be passed as keys
3136 * using psa_key_derivation_input_key() instead of this function. Refer to
3137 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003138 *
3139 * \param[in,out] generator The generator object to use. It must
3140 * have been set up with
3141 * psa_key_derivation_setup() and must not
3142 * have produced any output yet.
3143 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003144 * \param[in] data Input data to use.
3145 * \param data_length Size of the \p data buffer in bytes.
3146 *
3147 * \retval #PSA_SUCCESS
3148 * Success.
3149 * \retval #PSA_ERROR_INVALID_ARGUMENT
3150 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003151 * \retval #PSA_ERROR_INVALID_ARGUMENT
3152 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003153 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3154 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3155 * \retval #PSA_ERROR_HARDWARE_FAILURE
3156 * \retval #PSA_ERROR_TAMPERING_DETECTED
3157 * \retval #PSA_ERROR_BAD_STATE
3158 * The value of \p step is not valid given the state of \p generator.
3159 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003160 * The library has not been previously initialized by psa_crypto_init().
3161 * It is implementation-dependent whether a failure to initialize
3162 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003163 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003164psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3165 psa_key_derivation_step_t step,
3166 const uint8_t *data,
3167 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003168
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003169/** Provide an input for key derivation in the form of a key.
3170 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003171 * Which inputs are required and in what order depends on the algorithm.
3172 * Refer to the documentation of each key derivation or key agreement
3173 * algorithm for information.
3174 *
3175 * This function passes key inputs. Some inputs must be passed as keys
3176 * of the appropriate type using this function, while others must be
3177 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3178 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003179 *
3180 * \param[in,out] generator The generator object to use. It must
3181 * have been set up with
3182 * psa_key_derivation_setup() and must not
3183 * have produced any output yet.
3184 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003185 * \param handle Handle to the key. It must have an
3186 * appropriate type for \p step and must
3187 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003188 *
3189 * \retval #PSA_SUCCESS
3190 * Success.
3191 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003192 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003193 * \retval #PSA_ERROR_NOT_PERMITTED
3194 * \retval #PSA_ERROR_INVALID_ARGUMENT
3195 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003196 * \retval #PSA_ERROR_INVALID_ARGUMENT
3197 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003198 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3199 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3200 * \retval #PSA_ERROR_HARDWARE_FAILURE
3201 * \retval #PSA_ERROR_TAMPERING_DETECTED
3202 * \retval #PSA_ERROR_BAD_STATE
3203 * The value of \p step is not valid given the state of \p generator.
3204 * \retval #PSA_ERROR_BAD_STATE
3205 * The library has not been previously initialized by psa_crypto_init().
3206 * It is implementation-dependent whether a failure to initialize
3207 * results in this error code.
3208 */
3209psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3210 psa_key_derivation_step_t step,
3211 psa_key_handle_t handle);
3212
Gilles Peskine969c5d62019-01-16 15:53:06 +01003213/** Perform a key agreement and use the shared secret as input to a key
3214 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003215 *
3216 * A key agreement algorithm takes two inputs: a private key \p private_key
3217 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003218 * The result of this function is passed as input to a key derivation.
3219 * The output of this key derivation can be extracted by reading from the
3220 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003221 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003222 * \param[in,out] generator The generator object to use. It must
3223 * have been set up with
3224 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003225 * key agreement and derivation algorithm
3226 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003227 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3228 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003229 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003230 * The generator must be ready for an
3231 * input of the type given by \p step.
3232 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003233 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003234 * \param[in] peer_key Public key of the peer. The peer key must be in the
3235 * same format that psa_import_key() accepts for the
3236 * public key type corresponding to the type of
3237 * private_key. That is, this function performs the
3238 * equivalent of
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003239 * #psa_import_key(`internal_public_key_handle`,
3240 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(`private_key_type`),
3241 * `peer_key`, `peer_key_length`) where
Jaeden Amero8afbff82019-01-14 16:56:20 +00003242 * `private_key_type` is the type of `private_key`.
3243 * For example, for EC keys, this means that peer_key
3244 * is interpreted as a point on the curve that the
3245 * private key is on. The standard formats for public
3246 * keys are documented in the documentation of
3247 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003248 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003249 *
3250 * \retval #PSA_SUCCESS
3251 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003252 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003253 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003254 * \retval #PSA_ERROR_NOT_PERMITTED
3255 * \retval #PSA_ERROR_INVALID_ARGUMENT
3256 * \c private_key is not compatible with \c alg,
3257 * or \p peer_key is not valid for \c alg or not compatible with
3258 * \c private_key.
3259 * \retval #PSA_ERROR_NOT_SUPPORTED
3260 * \c alg is not supported or is not a key derivation algorithm.
3261 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3262 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3263 * \retval #PSA_ERROR_HARDWARE_FAILURE
3264 * \retval #PSA_ERROR_TAMPERING_DETECTED
3265 */
3266psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003267 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003268 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003269 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003270 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003271
Gilles Peskine769c7a62019-01-18 16:42:29 +01003272/** Perform a key agreement and use the shared secret as input to a key
3273 * derivation.
3274 *
3275 * A key agreement algorithm takes two inputs: a private key \p private_key
3276 * a public key \p peer_key.
3277 *
3278 * \warning The raw result of a key agreement algorithm such as finite-field
3279 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3280 * not be used directly as key material. It should instead be passed as
3281 * input to a key derivation algorithm. To chain a key agreement with
3282 * a key derivation, use psa_key_agreement() and other functions from
3283 * the key derivation and generator interface.
3284 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003285 * \param alg The key agreement algorithm to compute
3286 * (\c PSA_ALG_XXX value such that
3287 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3288 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003289 * \param private_key Handle to the private key to use.
3290 * \param[in] peer_key Public key of the peer. It must be
3291 * in the same format that psa_import_key()
3292 * accepts. The standard formats for public
3293 * keys are documented in the documentation
3294 * of psa_export_public_key().
3295 * \param peer_key_length Size of \p peer_key in bytes.
3296 * \param[out] output Buffer where the decrypted message is to
3297 * be written.
3298 * \param output_size Size of the \c output buffer in bytes.
3299 * \param[out] output_length On success, the number of bytes
3300 * that make up the returned output.
3301 *
3302 * \retval #PSA_SUCCESS
3303 * Success.
3304 * \retval #PSA_ERROR_INVALID_HANDLE
3305 * \retval #PSA_ERROR_EMPTY_SLOT
3306 * \retval #PSA_ERROR_NOT_PERMITTED
3307 * \retval #PSA_ERROR_INVALID_ARGUMENT
3308 * \p alg is not a key agreement algorithm
3309 * \retval #PSA_ERROR_INVALID_ARGUMENT
3310 * \p private_key is not compatible with \p alg,
3311 * or \p peer_key is not valid for \p alg or not compatible with
3312 * \p private_key.
3313 * \retval #PSA_ERROR_NOT_SUPPORTED
3314 * \p alg is not a supported key agreement algorithm.
3315 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3316 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3317 * \retval #PSA_ERROR_HARDWARE_FAILURE
3318 * \retval #PSA_ERROR_TAMPERING_DETECTED
3319 */
3320psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3321 psa_key_handle_t private_key,
3322 const uint8_t *peer_key,
3323 size_t peer_key_length,
3324 uint8_t *output,
3325 size_t output_size,
3326 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003327
3328/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003329
3330/** \defgroup random Random generation
3331 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003332 */
3333
3334/**
3335 * \brief Generate random bytes.
3336 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003337 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003338 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003339 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003340 *
3341 * \note To generate a key, use psa_generate_key() instead.
3342 *
3343 * \param[out] output Output buffer for the generated data.
3344 * \param output_size Number of bytes to generate and output.
3345 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003346 * \retval #PSA_SUCCESS
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003347 * \retval #PSA_ERROR_NOT_SUPPORTED
3348 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003349 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003350 * \retval #PSA_ERROR_HARDWARE_FAILURE
3351 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003352 * \retval #PSA_ERROR_BAD_STATE
3353 * The library has not been previously initialized by psa_crypto_init().
3354 * It is implementation-dependent whether a failure to initialize
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003355 * results in this error code.
3356 */
3357psa_status_t psa_generate_random(uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003358 size_t output_size);
3359
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003360/**
3361 * \brief Generate a key or key pair.
3362 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003363 * The key is generated randomly.
3364 * Its location, policy, type and size are taken from \p attributes.
3365 *
3366 * If the type requires additional domain parameters, these are taken
3367 * from \p attributes as well. The following types use domain parameters:
3368 * - When generating an RSA key (#PSA_KEY_TYPE_RSA_KEYPAIR),
3369 * the default public exponent is 65537. This value is used if
3370 * \p attributes was set with psa_set_key_type() or by passing an empty
3371 * byte string as domain parameters to psa_set_key_domain_parameters().
3372 * If psa_set_key_domain_parameters() was used to set a non-empty
3373 * domain parameter string in \p attributes, this string is read as
3374 * a big-endian integer which is used as the public exponent.
3375 * - When generating a DSA key (#PSA_KEY_TYPE_DSA_KEYPAIR) or a
3376 * Diffie-Hellman key (#PSA_KEY_TYPE_DH_KEYPAIR), the domain parameters
3377 * from \p attributes are interpreted as described for
3378 * psa_set_key_domain_parameters().
3379 *
Gilles Peskine20628592019-04-19 19:29:50 +02003380 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003381 * \param[out] handle On success, a handle to the newly created key.
3382 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003383 *
Gilles Peskine28538492018-07-11 17:34:00 +02003384 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003385 * Success.
3386 * If the key is persistent, the key material and the key's metadata
3387 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003388 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003389 * This is an attempt to create a persistent key, and there is
3390 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003391 * \retval #PSA_ERROR_NOT_SUPPORTED
3392 * \retval #PSA_ERROR_INVALID_ARGUMENT
3393 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3394 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3395 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3396 * \retval #PSA_ERROR_HARDWARE_FAILURE
3397 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003398 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003399 * The library has not been previously initialized by psa_crypto_init().
3400 * It is implementation-dependent whether a failure to initialize
3401 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003402 */
Gilles Peskine87a5e562019-04-17 12:28:25 +02003403psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003404 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003405
3406/**@}*/
3407
Gilles Peskinee59236f2018-01-27 23:32:46 +01003408#ifdef __cplusplus
3409}
3410#endif
3411
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003412/* The file "crypto_sizes.h" contains definitions for size calculation
3413 * macros whose definitions are implementation-specific. */
3414#include "crypto_sizes.h"
3415
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003416/* The file "crypto_struct.h" contains definitions for
3417 * implementation-specific structs that are declared above. */
3418#include "crypto_struct.h"
3419
3420/* The file "crypto_extra.h" contains vendor-specific definitions. This
3421 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003422#include "crypto_extra.h"
3423
3424#endif /* PSA_CRYPTO_H */