blob: 2c3288ef84df4ba361094f617a912caac1aaa680 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
89 * \retval #PSA_ERROR_TAMPERING_DETECTED
90 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine87a5e562019-04-17 12:28:25 +020096/** \defgroup attributes Key attributes
97 * @{
98 */
99
100/** The type of a structure containing key attributes.
101 *
102 * This is an opaque structure that can represent the metadata of a key
103 * object, including the key type and size, domain parameters, usage policies,
104 * location in storage, and any other similar information.
105 *
106 * The actual key material is not considered an attribute of a key.
107 * Key attributes do not contain information that is generally considered
108 * highly confidential.
Gilles Peskine20628592019-04-19 19:29:50 +0200109 *
110 * Before calling any function on a key attribute structure, the application
111 * must initialize it by any of the following means:
112 * - Set the structure to all-bits-zero, for example:
113 * \code
114 * psa_key_attributes_t attributes;
115 * memset(&attributes, 0, sizeof(attributes));
116 * \endcode
117 * - Initialize the structure to logical zero values, for example:
118 * \code
119 * psa_key_attributes_t attributes = {0};
120 * \endcode
121 * - Initialize the structure to the initializer #PSA_KEY_ATTRIBUTES_INIT,
122 * for example:
123 * \code
124 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
125 * \endcode
126 * - Assign the result of the function psa_key_attributes_init()
127 * to the structure, for example:
128 * \code
129 * psa_key_attributes_t attributes;
130 * attributes = psa_key_attributes_init();
131 * \endcode
132 *
133 * A freshly initialized attribute structure contains the following
134 * values:
135 *
136 * - lifetime: #PSA_KEY_LIFETIME_VOLATILE.
137 * - key identifier: unspecified.
138 * - type: \c 0, with no domain parameters.
139 * - key size: \c 0.
140 * - usage flags: \c 0.
141 * - algorithm: \c 0.
142 *
143 * A freshly initialized attribute structure does not own any auxiliary
144 * resources such as pointers to allocated memory, and therefore can be
145 * freed simply by freeing the memory allocated for the structure itself.
146 * This property still holds if the structure has only been modified
147 * by the following functions:
148 * - psa_make_key_persistent()
149 * - psa_set_key_type()
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200150 * - psa_set_key_bits()
Gilles Peskine20628592019-04-19 19:29:50 +0200151 * - psa_set_key_usage_flags()
152 * - psa_set_key_algorithm()
153 * - psa_reset_key_attributes()
154 * - psa_get_key_attributes() on a key which has been created with
155 * attribute structure that itself did not contain auxiliary resources
156 *
157 * If the attribute structure has been modified with other functions,
158 * you must free auxiliary resources by calling psa_reset_key_attributes().
159 * The following functions may create auxiliary resouces:
160 * - psa_set_key_domain_parameters()
Gilles Peskine87a5e562019-04-17 12:28:25 +0200161 */
162typedef struct psa_key_attributes_s psa_key_attributes_t;
163
Gilles Peskine20628592019-04-19 19:29:50 +0200164/** Declare a key as persistent.
165 *
166 * This function does not access storage, it merely fills the attribute
167 * structure with given values. The persistent key will be written to
168 * storage when the attribute structure is passed to a key creation
169 * function such as psa_import_key(), psa_generate_key(),
170 * psa_generator_import_key() or psa_copy_key().
171 *
172 * This function overwrites any identifier and lifetime values
173 * previously set in \p attributes.
174 *
175 * This function may be declared as `static` (i.e. without external
176 * linkage). This function may be provided as a function-like macro,
177 * but in this case it must evaluate each of its arguments exactly once.
178 *
179 * \param[out] attributes The attribute structure to write to.
180 * \param id The persistent identifier for the key.
181 * \param lifetime The lifetime for the key.
182 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
183 * key will be volatile, and \p id is ignored.
184 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200185static void psa_make_key_persistent(psa_key_attributes_t *attributes,
186 psa_key_id_t id,
187 psa_key_lifetime_t lifetime);
188
Gilles Peskine20628592019-04-19 19:29:50 +0200189/** Retrieve the key identifier from key attributes.
190 *
191 * This function may be declared as `static` (i.e. without external
192 * linkage). This function may be provided as a function-like macro,
193 * but in this case it must evaluate its argument exactly once.
194 *
195 * \param[in] attributes The key attribute structure to query.
196 *
197 * \return The persistent identifier stored in the attribute structure.
198 * This value is unspecified if the attribute structure declares
199 * the key as volatile.
200 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200201static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
202
Gilles Peskine20628592019-04-19 19:29:50 +0200203/** Retrieve the lifetime from key attributes.
204 *
205 * This function may be declared as `static` (i.e. without external
206 * linkage). This function may be provided as a function-like macro,
207 * but in this case it must evaluate its argument exactly once.
208 *
209 * \param[in] attributes The key attribute structure to query.
210 *
211 * \return The lifetime value stored in the attribute structure.
212 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200213static psa_key_lifetime_t psa_get_key_lifetime(
214 const psa_key_attributes_t *attributes);
215
Gilles Peskine20628592019-04-19 19:29:50 +0200216/** Declare usage flags for a key.
217 *
218 * Usage flags are part of a key's usage policy. They encode what
219 * kind of operations are permitted on the key. For more details,
220 * refer to the documentation of the type #psa_key_usage_t.
221 *
222 * This function overwrites any usage flags
223 * previously set in \p attributes.
224 *
225 * This function may be declared as `static` (i.e. without external
226 * linkage). This function may be provided as a function-like macro,
227 * but in this case it must evaluate each of its arguments exactly once.
228 *
229 * \param[out] attributes The attribute structure to write to.
230 * \param usage_flags The usage flags to write.
231 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200232static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
233 psa_key_usage_t usage_flags);
234
Gilles Peskine20628592019-04-19 19:29:50 +0200235/** Retrieve the usage flags from key attributes.
236 *
237 * This function may be declared as `static` (i.e. without external
238 * linkage). This function may be provided as a function-like macro,
239 * but in this case it must evaluate its argument exactly once.
240 *
241 * \param[in] attributes The key attribute structure to query.
242 *
243 * \return The usage flags stored in the attribute structure.
244 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200245static psa_key_usage_t psa_get_key_usage_flags(
246 const psa_key_attributes_t *attributes);
247
Gilles Peskine20628592019-04-19 19:29:50 +0200248/** Declare the permitted algorithm policy for a key.
249 *
250 * The permitted algorithm policy of a key encodes which algorithm or
251 * algorithms are permitted to be used with this key.
252 *
253 * This function overwrites any algorithm policy
254 * previously set in \p attributes.
255 *
256 * This function may be declared as `static` (i.e. without external
257 * linkage). This function may be provided as a function-like macro,
258 * but in this case it must evaluate each of its arguments exactly once.
259 *
260 * \param[out] attributes The attribute structure to write to.
261 * \param alg The permitted algorithm policy to write.
262 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200263static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
264 psa_algorithm_t alg);
265
Gilles Peskine20628592019-04-19 19:29:50 +0200266/** Retrieve the algorithm policy from key attributes.
267 *
268 * This function may be declared as `static` (i.e. without external
269 * linkage). This function may be provided as a function-like macro,
270 * but in this case it must evaluate its argument exactly once.
271 *
272 * \param[in] attributes The key attribute structure to query.
273 *
274 * \return The algorithm stored in the attribute structure.
275 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200276static psa_algorithm_t psa_get_key_algorithm(
277 const psa_key_attributes_t *attributes);
278
Gilles Peskine20628592019-04-19 19:29:50 +0200279/** Declare the type of a key.
280 *
281 * If a type requires domain parameters, you must call
282 * psa_set_key_domain_parameters() instead of this function.
283 *
284 * This function overwrites any key type and domain parameters
285 * previously set in \p attributes.
286 *
287 * This function may be declared as `static` (i.e. without external
288 * linkage). This function may be provided as a function-like macro,
289 * but in this case it must evaluate each of its arguments exactly once.
290 *
291 * \param[out] attributes The attribute structure to write to.
292 * \param type The key type to write.
293 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200294static void psa_set_key_type(psa_key_attributes_t *attributes,
295 psa_key_type_t type);
296
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200297/** Declare the size of a key.
298 *
299 * This function overwrites any key size previously set in \p attributes.
300 *
301 * This function may be declared as `static` (i.e. without external
302 * linkage). This function may be provided as a function-like macro,
303 * but in this case it must evaluate each of its arguments exactly once.
304 *
305 * \param[out] attributes The attribute structure to write to.
306 * \param bits The key size in bits.
307 */
308static void psa_set_key_bits(psa_key_attributes_t *attributes,
309 size_t bits);
310
Gilles Peskine20628592019-04-19 19:29:50 +0200311/** Retrieve the key type from key attributes.
312 *
313 * This function may be declared as `static` (i.e. without external
314 * linkage). This function may be provided as a function-like macro,
315 * but in this case it must evaluate its argument exactly once.
316 *
317 * \param[in] attributes The key attribute structure to query.
318 *
319 * \return The key type stored in the attribute structure.
320 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200321static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
322
Gilles Peskine20628592019-04-19 19:29:50 +0200323/** Retrieve the key size from key attributes.
324 *
325 * This function may be declared as `static` (i.e. without external
326 * linkage). This function may be provided as a function-like macro,
327 * but in this case it must evaluate its argument exactly once.
328 *
329 * \param[in] attributes The key attribute structure to query.
330 *
331 * \return The key size stored in the attribute structure, in bits.
332 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200333static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
334
Gilles Peskine20628592019-04-19 19:29:50 +0200335/** Retrieve the attributes of a key.
336 *
337 * This function first resets the attribute structure as with
338 * psa_reset_key_attributes(). It then populates the attribute
339 * structure with the attributes of the given key.
340 *
341 * The attributes that were set when creating the key are reported in a
342 * semantically equivalent manner, not necessarily with the same
343 * numerical value or the same bit pattern. In this specification,
344 * all key types, usage flags, algorithms and lifetime values are
345 * equivalent only if they have the same numerical encoding, but this
346 * property may not hold in future versions of this specification or
347 * for implementation-specific values.
348 *
Gilles Peskine20628592019-04-19 19:29:50 +0200349 * \param[in] handle Handle to the key to query.
350 * \param[in,out] attributes On success, the attributes of the key.
351 * On failure, equivalent to a
352 * freshly-initialized structure.
353 *
354 * \retval #PSA_SUCCESS
355 * \retval #PSA_ERROR_INVALID_HANDLE
356 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
357 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
358 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200359psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
360 psa_key_attributes_t *attributes);
361
Gilles Peskine20628592019-04-19 19:29:50 +0200362/** Reset a key attribute structure to a freshly initialized state.
363 *
364 * You must initialize the attribute structure as described in the
365 * documentation of the type #psa_key_attributes_t before calling this
366 * function. Once the structure has been initialized, you may call this
367 * function at any time.
368 *
369 * This function frees any auxiliary resources that the structure
370 * may contain.
371 *
372 * \param[in,out] attributes The attribute structure to reset.
373 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200374void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200375
Gilles Peskine87a5e562019-04-17 12:28:25 +0200376/**@}*/
377
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100378/** \defgroup key_management Key management
379 * @{
380 */
381
Gilles Peskinef535eb22018-11-30 14:08:36 +0100382/** Open a handle to an existing persistent key.
383 *
384 * Open a handle to a key which was previously created with psa_create_key().
385 *
386 * \param lifetime The lifetime of the key. This designates a storage
387 * area where the key material is stored. This must not
388 * be #PSA_KEY_LIFETIME_VOLATILE.
389 * \param id The persistent identifier of the key.
390 * \param[out] handle On success, a handle to a key slot which contains
391 * the data and metadata loaded from the specified
392 * persistent location.
393 *
394 * \retval #PSA_SUCCESS
395 * Success. The application can now use the value of `*handle`
396 * to access the newly allocated key slot.
397 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200398 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100399 * \retval #PSA_ERROR_INVALID_ARGUMENT
400 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
401 * \retval #PSA_ERROR_INVALID_ARGUMENT
402 * \p id is invalid for the specified lifetime.
403 * \retval #PSA_ERROR_NOT_SUPPORTED
404 * \p lifetime is not supported.
405 * \retval #PSA_ERROR_NOT_PERMITTED
406 * The specified key exists, but the application does not have the
407 * permission to access it. Note that this specification does not
408 * define any way to create such a key, but it may be possible
409 * through implementation-specific means.
410 */
411psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
412 psa_key_id_t id,
413 psa_key_handle_t *handle);
414
Gilles Peskinef535eb22018-11-30 14:08:36 +0100415/** Close a key handle.
416 *
417 * If the handle designates a volatile key, destroy the key material and
418 * free all associated resources, just like psa_destroy_key().
419 *
420 * If the handle designates a persistent key, free all resources associated
421 * with the key in volatile memory. The key slot in persistent storage is
422 * not affected and can be opened again later with psa_open_key().
423 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100424 * If the key is currently in use in a multipart operation,
425 * the multipart operation is aborted.
426 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100427 * \param handle The key handle to close.
428 *
429 * \retval #PSA_SUCCESS
430 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100431 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100432 */
433psa_status_t psa_close_key(psa_key_handle_t handle);
434
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100435/**@}*/
436
437/** \defgroup import_export Key import and export
438 * @{
439 */
440
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100441/**
442 * \brief Import a key in binary format.
443 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100444 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100445 * documentation of psa_export_public_key() for the format of public keys
446 * and to the documentation of psa_export_key() for the format for
447 * other key types.
448 *
449 * This specification supports a single format for each key type.
450 * Implementations may support other formats as long as the standard
451 * format is supported. Implementations that support other formats
452 * should ensure that the formats are clearly unambiguous so as to
453 * minimize the risk that an invalid input is accidentally interpreted
454 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100455 *
Gilles Peskine20628592019-04-19 19:29:50 +0200456 * \param[in] attributes The attributes for the new key.
457 * The key size field in \p attributes is
458 * ignored; the actual key size is determined
459 * from the \p data buffer.
460 * \param[out] handle On success, a handle to the newly created key.
461 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100462 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine20628592019-04-19 19:29:50 +0200463 * buffer is interpreted according to the type and,
464 * if applicable, domain parameters declared in
465 * \p attributes.
466 * All implementations must support at least the format
467 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100468 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200469 * the chosen type. Implementations may allow other
470 * formats, but should be conservative: implementations
471 * should err on the side of rejecting content if it
472 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200473 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100474 *
Gilles Peskine28538492018-07-11 17:34:00 +0200475 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100476 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100477 * If the key is persistent, the key material and the key's metadata
478 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200479 * \retval #PSA_ERROR_ALREADY_EXISTS
480 * This is an attempt to create a persistent key, and there is
481 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200482 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200483 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200484 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200485 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200486 * The key attributes, as a whole, are invalid,
Gilles Peskine308b91d2018-02-08 09:47:44 +0100487 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200488 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
489 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
490 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100491 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200492 * \retval #PSA_ERROR_HARDWARE_FAILURE
493 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300494 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300495 * The library has not been previously initialized by psa_crypto_init().
496 * It is implementation-dependent whether a failure to initialize
497 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100498 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200499psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
500 psa_key_handle_t *handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100501 const uint8_t *data,
502 size_t data_length);
503
504/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100505 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200506 *
507 * This function destroys the content of the key slot from both volatile
508 * memory and, if applicable, non-volatile storage. Implementations shall
509 * make a best effort to ensure that any previous content of the slot is
510 * unrecoverable.
511 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100512 * This function also erases any metadata such as policies and frees all
513 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200514 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100515 * If the key is currently in use in a multipart operation,
516 * the multipart operation is aborted.
517 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100518 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100519 *
Gilles Peskine28538492018-07-11 17:34:00 +0200520 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200521 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200522 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200523 * The slot holds content and cannot be erased because it is
524 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100525 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200526 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200527 * There was an failure in communication with the cryptoprocessor.
528 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200529 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200530 * The storage is corrupted. Implementations shall make a best effort
531 * to erase key material even in this stage, however applications
532 * should be aware that it may be impossible to guarantee that the
533 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200534 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200535 * An unexpected condition which is not a storage corruption or
536 * a communication failure occurred. The cryptoprocessor may have
537 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300538 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300539 * The library has not been previously initialized by psa_crypto_init().
540 * It is implementation-dependent whether a failure to initialize
541 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100543psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100544
545/**
Jaeden Amero283dfd12019-01-11 12:06:22 +0000546 * \brief Set domain parameters for a key.
547 *
548 * Some key types require additional domain parameters to be set before import
549 * or generation of the key. The domain parameters can be set with this
550 * function or, for key generation, through the \c extra parameter of
551 * psa_generate_key().
552 *
553 * The format for the required domain parameters varies by the key type.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000554 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
555 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
556 * ```
557 * Dss-Parms ::= SEQUENCE {
558 * p INTEGER,
559 * q INTEGER,
560 * g INTEGER
561 * }
562 * ```
Jaeden Amero8851c402019-01-11 14:20:03 +0000563 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY), the
564 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
565 * ```
566 * DomainParameters ::= SEQUENCE {
567 * p INTEGER, -- odd prime, p=jq +1
568 * g INTEGER, -- generator, g
569 * q INTEGER, -- factor of p-1
570 * j INTEGER OPTIONAL, -- subgroup factor
571 * validationParms ValidationParms OPTIONAL
572 * }
573 * ValidationParms ::= SEQUENCE {
574 * seed BIT STRING,
575 * pgenCounter INTEGER
576 * }
577 * ```
Jaeden Amero283dfd12019-01-11 12:06:22 +0000578 *
Gilles Peskine3a74e002019-01-18 17:11:25 +0100579 * \param handle Handle to the slot where the key will be stored.
580 * This must be a valid slot for a key of the chosen
581 * type: it must have been obtained by calling
582 * psa_allocate_key() or psa_create_key() with the
583 * correct \p type and with a maximum size that is
584 * compatible with \p data. It must not contain
585 * key material yet.
586 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). When
587 * subsequently creating key material into \p handle,
588 * the type must be compatible.
Jaeden Amero283dfd12019-01-11 12:06:22 +0000589 * \param[in] data Buffer containing the key domain parameters. The content
590 * of this buffer is interpreted according to \p type. of
591 * psa_export_key() or psa_export_public_key() for the
592 * chosen type.
593 * \param data_length Size of the \p data buffer in bytes.
594 *
595 * \retval #PSA_SUCCESS
596 * \retval #PSA_ERROR_INVALID_HANDLE
597 * \retval #PSA_ERROR_OCCUPIED_SLOT
598 * There is already a key in the specified slot.
599 * \retval #PSA_ERROR_INVALID_ARGUMENT
600 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
601 * \retval #PSA_ERROR_HARDWARE_FAILURE
602 * \retval #PSA_ERROR_TAMPERING_DETECTED
603 * \retval #PSA_ERROR_BAD_STATE
604 * The library has not been previously initialized by psa_crypto_init().
605 * It is implementation-dependent whether a failure to initialize
606 * results in this error code.
607 */
608psa_status_t psa_set_key_domain_parameters(psa_key_handle_t handle,
Gilles Peskine3a74e002019-01-18 17:11:25 +0100609 psa_key_type_t type,
Jaeden Amero283dfd12019-01-11 12:06:22 +0000610 const uint8_t *data,
611 size_t data_length);
612
613/**
614 * \brief Get domain parameters for a key.
615 *
616 * Get the domain parameters for a key with this function, if any. The format
617 * of the domain parameters written to \p data is specified in the
618 * documentation for psa_set_key_domain_parameters().
619 *
620 * \param handle Handle to the key to get domain parameters from.
621 * \param[out] data On success, the key domain parameters.
622 * \param data_size Size of the \p data buffer in bytes.
623 * \param[out] data_length On success, the number of bytes
624 * that make up the key domain parameters data.
625 *
626 * \retval #PSA_SUCCESS
627 * \retval #PSA_ERROR_INVALID_HANDLE
628 * \retval #PSA_ERROR_EMPTY_SLOT
629 * There is no key in the specified slot.
630 * \retval #PSA_ERROR_INVALID_ARGUMENT
631 * \retval #PSA_ERROR_NOT_SUPPORTED
632 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
633 * \retval #PSA_ERROR_HARDWARE_FAILURE
634 * \retval #PSA_ERROR_TAMPERING_DETECTED
635 * \retval #PSA_ERROR_BAD_STATE
636 * The library has not been previously initialized by psa_crypto_init().
637 * It is implementation-dependent whether a failure to initialize
638 * results in this error code.
639 */
640psa_status_t psa_get_key_domain_parameters(psa_key_handle_t handle,
641 uint8_t *data,
642 size_t data_size,
643 size_t *data_length);
644
645/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646 * \brief Export a key in binary format.
647 *
648 * The output of this function can be passed to psa_import_key() to
649 * create an equivalent object.
650 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100651 * If the implementation of psa_import_key() supports other formats
652 * beyond the format specified here, the output from psa_export_key()
653 * must use the representation specified here, not the original
654 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100655 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100656 * For standard key types, the output format is as follows:
657 *
658 * - For symmetric keys (including MAC keys), the format is the
659 * raw bytes of the key.
660 * - For DES, the key data consists of 8 bytes. The parity bits must be
661 * correct.
662 * - For Triple-DES, the format is the concatenation of the
663 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100664 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200665 * is the non-encrypted DER encoding of the representation defined by
666 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
667 * ```
668 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200669 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200670 * modulus INTEGER, -- n
671 * publicExponent INTEGER, -- e
672 * privateExponent INTEGER, -- d
673 * prime1 INTEGER, -- p
674 * prime2 INTEGER, -- q
675 * exponent1 INTEGER, -- d mod (p-1)
676 * exponent2 INTEGER, -- d mod (q-1)
677 * coefficient INTEGER, -- (inverse of q) mod p
678 * }
679 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000680 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
681 * representation of the private key `x` as a big-endian byte string. The
682 * length of the byte string is the private key size in bytes (leading zeroes
683 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200684 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100685 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100686 * a representation of the private value as a `ceiling(m/8)`-byte string
687 * where `m` is the bit size associated with the curve, i.e. the bit size
688 * of the order of the curve's coordinate field. This byte string is
689 * in little-endian order for Montgomery curves (curve types
690 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
691 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
692 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100693 * This is the content of the `privateKey` field of the `ECPrivateKey`
694 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000695 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
696 * format is the representation of the private key `x` as a big-endian byte
697 * string. The length of the byte string is the private key size in bytes
698 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200699 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
700 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100701 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100702 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200703 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200704 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200705 * \param[out] data_length On success, the number of bytes
706 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100707 *
Gilles Peskine28538492018-07-11 17:34:00 +0200708 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100709 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200710 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200711 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100712 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200713 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
714 * The size of the \p data buffer is too small. You can determine a
715 * sufficient buffer size by calling
716 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
717 * where \c type is the key type
718 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200719 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
720 * \retval #PSA_ERROR_HARDWARE_FAILURE
721 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300722 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300723 * The library has not been previously initialized by psa_crypto_init().
724 * It is implementation-dependent whether a failure to initialize
725 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100726 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100727psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100728 uint8_t *data,
729 size_t data_size,
730 size_t *data_length);
731
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100732/**
733 * \brief Export a public key or the public part of a key pair in binary format.
734 *
735 * The output of this function can be passed to psa_import_key() to
736 * create an object that is equivalent to the public key.
737 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000738 * This specification supports a single format for each key type.
739 * Implementations may support other formats as long as the standard
740 * format is supported. Implementations that support other formats
741 * should ensure that the formats are clearly unambiguous so as to
742 * minimize the risk that an invalid input is accidentally interpreted
743 * according to a different format.
744 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000745 * For standard key types, the output format is as follows:
746 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
747 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
748 * ```
749 * RSAPublicKey ::= SEQUENCE {
750 * modulus INTEGER, -- n
751 * publicExponent INTEGER } -- e
752 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000753 * - For elliptic curve public keys (key types for which
754 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
755 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
756 * Let `m` be the bit size associated with the curve, i.e. the bit size of
757 * `q` for a curve over `F_q`. The representation consists of:
758 * - The byte 0x04;
759 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
760 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000761 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
762 * representation of the public key `y = g^x mod p` as a big-endian byte
763 * string. The length of the byte string is the length of the base prime `p`
764 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000765 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
766 * the format is the representation of the public key `y = g^x mod p` as a
767 * big-endian byte string. The length of the byte string is the length of the
768 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100769 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100770 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200771 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200772 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200773 * \param[out] data_length On success, the number of bytes
774 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100775 *
Gilles Peskine28538492018-07-11 17:34:00 +0200776 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100777 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200778 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200779 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200780 * The key is neither a public key nor a key pair.
781 * \retval #PSA_ERROR_NOT_SUPPORTED
782 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
783 * The size of the \p data buffer is too small. You can determine a
784 * sufficient buffer size by calling
785 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
786 * where \c type is the key type
787 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200788 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
789 * \retval #PSA_ERROR_HARDWARE_FAILURE
790 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300791 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300792 * The library has not been previously initialized by psa_crypto_init().
793 * It is implementation-dependent whether a failure to initialize
794 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100795 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100796psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100797 uint8_t *data,
798 size_t data_size,
799 size_t *data_length);
800
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100801/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100802 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100803 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000804 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100805 * This function is primarily useful to copy a key from one location
806 * to another, since it populates a key using the material from
807 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200808 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100809 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100810 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100811 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100812 *
Gilles Peskine20628592019-04-19 19:29:50 +0200813 * The resulting key may only be used in a way that conforms to
814 * both the policy of the original key and the policy specified in
815 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100816 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200817 * usage flags on the source policy and the usage flags in \p attributes.
818 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100819 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200820 * - If either of the policies allows an algorithm and the other policy
821 * allows a wildcard-based algorithm policy that includes this algorithm,
822 * the resulting key allows the same algorithm.
823 * - If the policies do not allow any algorithm in common, this function
824 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200825 *
Gilles Peskine20628592019-04-19 19:29:50 +0200826 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100827 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200828 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100829 * \param source_handle The key to copy. It must be a handle to an
830 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200831 * \param[in] attributes The attributes for the new key.
832 * They are used as follows:
833 * - The key type, key size and domain parameters
834 * are ignored. This information is copied
835 * from the source key.
836 * - The key location (the lifetime and, for
837 * persistent keys, the key identifier) is
838 * used directly.
839 * - The policy constraints (usage flags and
840 * algorithm policy) are combined from
841 * the source key and \p attributes so that
842 * both sets of restrictions apply, as
843 * described in the documentation of this function.
844 * \param[out] target_handle On success, a handle to the newly created key.
845 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200846 *
847 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100848 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200849 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200850 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200851 * This is an attempt to create a persistent key, and there is
852 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200853 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200854 * The lifetime or identifier in \p attributes are invalid.
855 * \retval #PSA_ERROR_INVALID_ARGUMENT
856 * The policy constraints on the source and specified in
857 * \p attributes are incompatible.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100858 * \retval #PSA_ERROR_NOT_PERMITTED
859 * The source key is not exportable and its lifetime does not
860 * allow copying it to the target's lifetime.
861 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
862 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200863 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
864 * \retval #PSA_ERROR_HARDWARE_FAILURE
865 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100866 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100867psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200868 const psa_key_attributes_t *attributes,
869 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100870
871/**@}*/
872
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100873/** \defgroup hash Message digests
874 * @{
875 */
876
Gilles Peskine69647a42019-01-14 20:18:12 +0100877/** Calculate the hash (digest) of a message.
878 *
879 * \note To verify the hash of a message against an
880 * expected value, use psa_hash_compare() instead.
881 *
882 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
883 * such that #PSA_ALG_IS_HASH(\p alg) is true).
884 * \param[in] input Buffer containing the message to hash.
885 * \param input_length Size of the \p input buffer in bytes.
886 * \param[out] hash Buffer where the hash is to be written.
887 * \param hash_size Size of the \p hash buffer in bytes.
888 * \param[out] hash_length On success, the number of bytes
889 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100890 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100891 *
892 * \retval #PSA_SUCCESS
893 * Success.
894 * \retval #PSA_ERROR_NOT_SUPPORTED
895 * \p alg is not supported or is not a hash algorithm.
896 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
897 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
898 * \retval #PSA_ERROR_HARDWARE_FAILURE
899 * \retval #PSA_ERROR_TAMPERING_DETECTED
900 */
901psa_status_t psa_hash_compute(psa_algorithm_t alg,
902 const uint8_t *input,
903 size_t input_length,
904 uint8_t *hash,
905 size_t hash_size,
906 size_t *hash_length);
907
908/** Calculate the hash (digest) of a message and compare it with a
909 * reference value.
910 *
911 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
912 * such that #PSA_ALG_IS_HASH(\p alg) is true).
913 * \param[in] input Buffer containing the message to hash.
914 * \param input_length Size of the \p input buffer in bytes.
915 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100916 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100917 *
918 * \retval #PSA_SUCCESS
919 * The expected hash is identical to the actual hash of the input.
920 * \retval #PSA_ERROR_INVALID_SIGNATURE
921 * The hash of the message was calculated successfully, but it
922 * differs from the expected hash.
923 * \retval #PSA_ERROR_NOT_SUPPORTED
924 * \p alg is not supported or is not a hash algorithm.
925 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
926 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
927 * \retval #PSA_ERROR_HARDWARE_FAILURE
928 * \retval #PSA_ERROR_TAMPERING_DETECTED
929 */
930psa_status_t psa_hash_compare(psa_algorithm_t alg,
931 const uint8_t *input,
932 size_t input_length,
933 const uint8_t *hash,
934 const size_t hash_length);
935
Gilles Peskine308b91d2018-02-08 09:47:44 +0100936/** The type of the state data structure for multipart hash operations.
937 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000938 * Before calling any function on a hash operation object, the application must
939 * initialize it by any of the following means:
940 * - Set the structure to all-bits-zero, for example:
941 * \code
942 * psa_hash_operation_t operation;
943 * memset(&operation, 0, sizeof(operation));
944 * \endcode
945 * - Initialize the structure to logical zero values, for example:
946 * \code
947 * psa_hash_operation_t operation = {0};
948 * \endcode
949 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
950 * for example:
951 * \code
952 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
953 * \endcode
954 * - Assign the result of the function psa_hash_operation_init()
955 * to the structure, for example:
956 * \code
957 * psa_hash_operation_t operation;
958 * operation = psa_hash_operation_init();
959 * \endcode
960 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100961 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100962 * make any assumptions about the content of this structure except
963 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100964typedef struct psa_hash_operation_s psa_hash_operation_t;
965
Jaeden Amero6a25b412019-01-04 11:47:44 +0000966/** \def PSA_HASH_OPERATION_INIT
967 *
968 * This macro returns a suitable initializer for a hash operation object
969 * of type #psa_hash_operation_t.
970 */
971#ifdef __DOXYGEN_ONLY__
972/* This is an example definition for documentation purposes.
973 * Implementations should define a suitable value in `crypto_struct.h`.
974 */
975#define PSA_HASH_OPERATION_INIT {0}
976#endif
977
978/** Return an initial value for a hash operation object.
979 */
980static psa_hash_operation_t psa_hash_operation_init(void);
981
Gilles Peskinef45adda2019-01-14 18:29:18 +0100982/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100983 *
984 * The sequence of operations to calculate a hash (message digest)
985 * is as follows:
986 * -# Allocate an operation object which will be passed to all the functions
987 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000988 * -# Initialize the operation object with one of the methods described in the
989 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200990 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100991 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100992 * of the message each time. The hash that is calculated is the hash
993 * of the concatenation of these messages in order.
994 * -# To calculate the hash, call psa_hash_finish().
995 * To compare the hash with an expected value, call psa_hash_verify().
996 *
997 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000998 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100999 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001000 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001001 * eventually terminate the operation. The following events terminate an
1002 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001003 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001004 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001005 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001006 * \param[in,out] operation The operation object to set up. It must have
1007 * been initialized as per the documentation for
1008 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001009 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1010 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001011 *
Gilles Peskine28538492018-07-11 17:34:00 +02001012 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001013 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001014 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001015 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001016 * \retval #PSA_ERROR_BAD_STATE
1017 * The operation state is not valid (already set up and not
1018 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001019 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1020 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1021 * \retval #PSA_ERROR_HARDWARE_FAILURE
1022 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001023 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001024psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001025 psa_algorithm_t alg);
1026
Gilles Peskine308b91d2018-02-08 09:47:44 +01001027/** Add a message fragment to a multipart hash operation.
1028 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001029 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001030 *
1031 * If this function returns an error status, the operation becomes inactive.
1032 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001033 * \param[in,out] operation Active hash operation.
1034 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001035 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001036 *
Gilles Peskine28538492018-07-11 17:34:00 +02001037 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001038 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001039 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001040 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001041 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1042 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1043 * \retval #PSA_ERROR_HARDWARE_FAILURE
1044 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001045 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001046psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1047 const uint8_t *input,
1048 size_t input_length);
1049
Gilles Peskine308b91d2018-02-08 09:47:44 +01001050/** Finish the calculation of the hash of a message.
1051 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001052 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001053 * This function calculates the hash of the message formed by concatenating
1054 * the inputs passed to preceding calls to psa_hash_update().
1055 *
1056 * When this function returns, the operation becomes inactive.
1057 *
1058 * \warning Applications should not call this function if they expect
1059 * a specific value for the hash. Call psa_hash_verify() instead.
1060 * Beware that comparing integrity or authenticity data such as
1061 * hash values with a function such as \c memcmp is risky
1062 * because the time taken by the comparison may leak information
1063 * about the hashed data which could allow an attacker to guess
1064 * a valid hash and thereby bypass security controls.
1065 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001066 * \param[in,out] operation Active hash operation.
1067 * \param[out] hash Buffer where the hash is to be written.
1068 * \param hash_size Size of the \p hash buffer in bytes.
1069 * \param[out] hash_length On success, the number of bytes
1070 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001071 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001072 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001073 *
Gilles Peskine28538492018-07-11 17:34:00 +02001074 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001075 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001076 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001077 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001078 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001079 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001080 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001081 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001082 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1083 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1084 * \retval #PSA_ERROR_HARDWARE_FAILURE
1085 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001086 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001087psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1088 uint8_t *hash,
1089 size_t hash_size,
1090 size_t *hash_length);
1091
Gilles Peskine308b91d2018-02-08 09:47:44 +01001092/** Finish the calculation of the hash of a message and compare it with
1093 * an expected value.
1094 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001095 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001096 * This function calculates the hash of the message formed by concatenating
1097 * the inputs passed to preceding calls to psa_hash_update(). It then
1098 * compares the calculated hash with the expected hash passed as a
1099 * parameter to this function.
1100 *
1101 * When this function returns, the operation becomes inactive.
1102 *
Gilles Peskine19067982018-03-20 17:54:53 +01001103 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001104 * comparison between the actual hash and the expected hash is performed
1105 * in constant time.
1106 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001107 * \param[in,out] operation Active hash operation.
1108 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001109 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001110 *
Gilles Peskine28538492018-07-11 17:34:00 +02001111 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001112 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001113 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001114 * The hash of the message was calculated successfully, but it
1115 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001116 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001117 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001118 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1119 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1120 * \retval #PSA_ERROR_HARDWARE_FAILURE
1121 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001122 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001123psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1124 const uint8_t *hash,
1125 size_t hash_length);
1126
Gilles Peskine308b91d2018-02-08 09:47:44 +01001127/** Abort a hash operation.
1128 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001129 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001130 * \p operation structure itself. Once aborted, the operation object
1131 * can be reused for another operation by calling
1132 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001133 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001134 * You may call this function any time after the operation object has
1135 * been initialized by any of the following methods:
1136 * - A call to psa_hash_setup(), whether it succeeds or not.
1137 * - Initializing the \c struct to all-bits-zero.
1138 * - Initializing the \c struct to logical zeros, e.g.
1139 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001140 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001141 * In particular, calling psa_hash_abort() after the operation has been
1142 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1143 * psa_hash_verify() is safe and has no effect.
1144 *
1145 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001146 *
Gilles Peskine28538492018-07-11 17:34:00 +02001147 * \retval #PSA_SUCCESS
1148 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001149 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001150 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1151 * \retval #PSA_ERROR_HARDWARE_FAILURE
1152 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001153 */
1154psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001155
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001156/** Clone a hash operation.
1157 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001158 * This function copies the state of an ongoing hash operation to
1159 * a new operation object. In other words, this function is equivalent
1160 * to calling psa_hash_setup() on \p target_operation with the same
1161 * algorithm that \p source_operation was set up for, then
1162 * psa_hash_update() on \p target_operation with the same input that
1163 * that was passed to \p source_operation. After this function returns, the
1164 * two objects are independent, i.e. subsequent calls involving one of
1165 * the objects do not affect the other object.
1166 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001167 * \param[in] source_operation The active hash operation to clone.
1168 * \param[in,out] target_operation The operation object to set up.
1169 * It must be initialized but not active.
1170 *
1171 * \retval #PSA_SUCCESS
1172 * \retval #PSA_ERROR_BAD_STATE
1173 * \p source_operation is not an active hash operation.
1174 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001175 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001176 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1177 * \retval #PSA_ERROR_HARDWARE_FAILURE
1178 * \retval #PSA_ERROR_TAMPERING_DETECTED
1179 */
1180psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1181 psa_hash_operation_t *target_operation);
1182
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001183/**@}*/
1184
Gilles Peskine8c9def32018-02-08 10:02:12 +01001185/** \defgroup MAC Message authentication codes
1186 * @{
1187 */
1188
Gilles Peskine69647a42019-01-14 20:18:12 +01001189/** Calculate the MAC (message authentication code) of a message.
1190 *
1191 * \note To verify the MAC of a message against an
1192 * expected value, use psa_mac_verify() instead.
1193 * Beware that comparing integrity or authenticity data such as
1194 * MAC values with a function such as \c memcmp is risky
1195 * because the time taken by the comparison may leak information
1196 * about the MAC value which could allow an attacker to guess
1197 * a valid MAC and thereby bypass security controls.
1198 *
1199 * \param handle Handle to the key to use for the operation.
1200 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001201 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001202 * \param[in] input Buffer containing the input message.
1203 * \param input_length Size of the \p input buffer in bytes.
1204 * \param[out] mac Buffer where the MAC value is to be written.
1205 * \param mac_size Size of the \p mac buffer in bytes.
1206 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001207 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001208 *
1209 * \retval #PSA_SUCCESS
1210 * Success.
1211 * \retval #PSA_ERROR_INVALID_HANDLE
1212 * \retval #PSA_ERROR_EMPTY_SLOT
1213 * \retval #PSA_ERROR_NOT_PERMITTED
1214 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001215 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001216 * \retval #PSA_ERROR_NOT_SUPPORTED
1217 * \p alg is not supported or is not a MAC algorithm.
1218 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1219 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1220 * \retval #PSA_ERROR_HARDWARE_FAILURE
1221 * \retval #PSA_ERROR_TAMPERING_DETECTED
1222 * \retval #PSA_ERROR_BAD_STATE
1223 * The library has not been previously initialized by psa_crypto_init().
1224 * It is implementation-dependent whether a failure to initialize
1225 * results in this error code.
1226 */
1227psa_status_t psa_mac_compute(psa_key_handle_t handle,
1228 psa_algorithm_t alg,
1229 const uint8_t *input,
1230 size_t input_length,
1231 uint8_t *mac,
1232 size_t mac_size,
1233 size_t *mac_length);
1234
1235/** Calculate the MAC of a message and compare it with a reference value.
1236 *
1237 * \param handle Handle to the key to use for the operation.
1238 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001239 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001240 * \param[in] input Buffer containing the input message.
1241 * \param input_length Size of the \p input buffer in bytes.
1242 * \param[out] mac Buffer containing the expected MAC value.
1243 * \param mac_length Size of the \p mac buffer in bytes.
1244 *
1245 * \retval #PSA_SUCCESS
1246 * The expected MAC is identical to the actual MAC of the input.
1247 * \retval #PSA_ERROR_INVALID_SIGNATURE
1248 * The MAC of the message was calculated successfully, but it
1249 * differs from the expected value.
1250 * \retval #PSA_ERROR_INVALID_HANDLE
1251 * \retval #PSA_ERROR_EMPTY_SLOT
1252 * \retval #PSA_ERROR_NOT_PERMITTED
1253 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001254 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001255 * \retval #PSA_ERROR_NOT_SUPPORTED
1256 * \p alg is not supported or is not a MAC algorithm.
1257 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1258 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1259 * \retval #PSA_ERROR_HARDWARE_FAILURE
1260 * \retval #PSA_ERROR_TAMPERING_DETECTED
1261 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001262psa_status_t psa_mac_verify(psa_key_handle_t handle,
1263 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001264 const uint8_t *input,
1265 size_t input_length,
1266 const uint8_t *mac,
1267 const size_t mac_length);
1268
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001269/** The type of the state data structure for multipart MAC operations.
1270 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001271 * Before calling any function on a MAC operation object, the application must
1272 * initialize it by any of the following means:
1273 * - Set the structure to all-bits-zero, for example:
1274 * \code
1275 * psa_mac_operation_t operation;
1276 * memset(&operation, 0, sizeof(operation));
1277 * \endcode
1278 * - Initialize the structure to logical zero values, for example:
1279 * \code
1280 * psa_mac_operation_t operation = {0};
1281 * \endcode
1282 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1283 * for example:
1284 * \code
1285 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1286 * \endcode
1287 * - Assign the result of the function psa_mac_operation_init()
1288 * to the structure, for example:
1289 * \code
1290 * psa_mac_operation_t operation;
1291 * operation = psa_mac_operation_init();
1292 * \endcode
1293 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001294 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001295 * make any assumptions about the content of this structure except
1296 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001297typedef struct psa_mac_operation_s psa_mac_operation_t;
1298
Jaeden Amero769ce272019-01-04 11:48:03 +00001299/** \def PSA_MAC_OPERATION_INIT
1300 *
1301 * This macro returns a suitable initializer for a MAC operation object of type
1302 * #psa_mac_operation_t.
1303 */
1304#ifdef __DOXYGEN_ONLY__
1305/* This is an example definition for documentation purposes.
1306 * Implementations should define a suitable value in `crypto_struct.h`.
1307 */
1308#define PSA_MAC_OPERATION_INIT {0}
1309#endif
1310
1311/** Return an initial value for a MAC operation object.
1312 */
1313static psa_mac_operation_t psa_mac_operation_init(void);
1314
Gilles Peskinef45adda2019-01-14 18:29:18 +01001315/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001316 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001317 * This function sets up the calculation of the MAC
1318 * (message authentication code) of a byte string.
1319 * To verify the MAC of a message against an
1320 * expected value, use psa_mac_verify_setup() instead.
1321 *
1322 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001323 * -# Allocate an operation object which will be passed to all the functions
1324 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001325 * -# Initialize the operation object with one of the methods described in the
1326 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001327 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001328 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1329 * of the message each time. The MAC that is calculated is the MAC
1330 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001331 * -# At the end of the message, call psa_mac_sign_finish() to finish
1332 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001333 *
1334 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001335 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001336 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001337 * After a successful call to psa_mac_sign_setup(), the application must
1338 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001339 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001340 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001341 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001342 * \param[in,out] operation The operation object to set up. It must have
1343 * been initialized as per the documentation for
1344 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001345 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001346 * It must remain valid until the operation
1347 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001348 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001349 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001350 *
Gilles Peskine28538492018-07-11 17:34:00 +02001351 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001352 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001353 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001354 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001355 * \retval #PSA_ERROR_NOT_PERMITTED
1356 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001357 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001358 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001359 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001360 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1361 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1362 * \retval #PSA_ERROR_HARDWARE_FAILURE
1363 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001364 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001365 * The operation state is not valid (already set up and not
1366 * subsequently completed).
1367 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001368 * The library has not been previously initialized by psa_crypto_init().
1369 * It is implementation-dependent whether a failure to initialize
1370 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001371 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001372psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001373 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001374 psa_algorithm_t alg);
1375
Gilles Peskinef45adda2019-01-14 18:29:18 +01001376/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001377 *
1378 * This function sets up the verification of the MAC
1379 * (message authentication code) of a byte string against an expected value.
1380 *
1381 * The sequence of operations to verify a MAC is as follows:
1382 * -# Allocate an operation object which will be passed to all the functions
1383 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001384 * -# Initialize the operation object with one of the methods described in the
1385 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001386 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001387 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1388 * of the message each time. The MAC that is calculated is the MAC
1389 * of the concatenation of these messages in order.
1390 * -# At the end of the message, call psa_mac_verify_finish() to finish
1391 * calculating the actual MAC of the message and verify it against
1392 * the expected value.
1393 *
1394 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001395 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001396 *
1397 * After a successful call to psa_mac_verify_setup(), the application must
1398 * eventually terminate the operation through one of the following methods:
1399 * - A failed call to psa_mac_update().
1400 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1401 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001402 * \param[in,out] operation The operation object to set up. It must have
1403 * been initialized as per the documentation for
1404 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001405 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001406 * It must remain valid until the operation
1407 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001408 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1409 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001410 *
Gilles Peskine28538492018-07-11 17:34:00 +02001411 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001412 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001413 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001414 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001415 * \retval #PSA_ERROR_NOT_PERMITTED
1416 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001417 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001418 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001419 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001420 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1421 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1422 * \retval #PSA_ERROR_HARDWARE_FAILURE
1423 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001424 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001425 * The operation state is not valid (already set up and not
1426 * subsequently completed).
1427 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001428 * The library has not been previously initialized by psa_crypto_init().
1429 * It is implementation-dependent whether a failure to initialize
1430 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001431 */
1432psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001433 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001434 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001435
Gilles Peskinedcd14942018-07-12 00:30:52 +02001436/** Add a message fragment to a multipart MAC operation.
1437 *
1438 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1439 * before calling this function.
1440 *
1441 * If this function returns an error status, the operation becomes inactive.
1442 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001443 * \param[in,out] operation Active MAC operation.
1444 * \param[in] input Buffer containing the message fragment to add to
1445 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001446 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001447 *
1448 * \retval #PSA_SUCCESS
1449 * Success.
1450 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001451 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001452 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1453 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1454 * \retval #PSA_ERROR_HARDWARE_FAILURE
1455 * \retval #PSA_ERROR_TAMPERING_DETECTED
1456 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001457psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1458 const uint8_t *input,
1459 size_t input_length);
1460
Gilles Peskinedcd14942018-07-12 00:30:52 +02001461/** Finish the calculation of the MAC of a message.
1462 *
1463 * The application must call psa_mac_sign_setup() before calling this function.
1464 * This function calculates the MAC of the message formed by concatenating
1465 * the inputs passed to preceding calls to psa_mac_update().
1466 *
1467 * When this function returns, the operation becomes inactive.
1468 *
1469 * \warning Applications should not call this function if they expect
1470 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1471 * Beware that comparing integrity or authenticity data such as
1472 * MAC values with a function such as \c memcmp is risky
1473 * because the time taken by the comparison may leak information
1474 * about the MAC value which could allow an attacker to guess
1475 * a valid MAC and thereby bypass security controls.
1476 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001477 * \param[in,out] operation Active MAC operation.
1478 * \param[out] mac Buffer where the MAC value is to be written.
1479 * \param mac_size Size of the \p mac buffer in bytes.
1480 * \param[out] mac_length On success, the number of bytes
1481 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001482 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001483 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001484 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001485 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001486 *
1487 * \retval #PSA_SUCCESS
1488 * Success.
1489 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001490 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001491 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001492 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001493 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1494 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1495 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1496 * \retval #PSA_ERROR_HARDWARE_FAILURE
1497 * \retval #PSA_ERROR_TAMPERING_DETECTED
1498 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001499psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1500 uint8_t *mac,
1501 size_t mac_size,
1502 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001503
Gilles Peskinedcd14942018-07-12 00:30:52 +02001504/** Finish the calculation of the MAC of a message and compare it with
1505 * an expected value.
1506 *
1507 * The application must call psa_mac_verify_setup() before calling this function.
1508 * This function calculates the MAC of the message formed by concatenating
1509 * the inputs passed to preceding calls to psa_mac_update(). It then
1510 * compares the calculated MAC with the expected MAC passed as a
1511 * parameter to this function.
1512 *
1513 * When this function returns, the operation becomes inactive.
1514 *
1515 * \note Implementations shall make the best effort to ensure that the
1516 * comparison between the actual MAC and the expected MAC is performed
1517 * in constant time.
1518 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001519 * \param[in,out] operation Active MAC operation.
1520 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001521 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001522 *
1523 * \retval #PSA_SUCCESS
1524 * The expected MAC is identical to the actual MAC of the message.
1525 * \retval #PSA_ERROR_INVALID_SIGNATURE
1526 * The MAC of the message was calculated successfully, but it
1527 * differs from the expected MAC.
1528 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001529 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001530 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1531 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1532 * \retval #PSA_ERROR_HARDWARE_FAILURE
1533 * \retval #PSA_ERROR_TAMPERING_DETECTED
1534 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001535psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1536 const uint8_t *mac,
1537 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001538
Gilles Peskinedcd14942018-07-12 00:30:52 +02001539/** Abort a MAC operation.
1540 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001541 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001542 * \p operation structure itself. Once aborted, the operation object
1543 * can be reused for another operation by calling
1544 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001545 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001546 * You may call this function any time after the operation object has
1547 * been initialized by any of the following methods:
1548 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1549 * it succeeds or not.
1550 * - Initializing the \c struct to all-bits-zero.
1551 * - Initializing the \c struct to logical zeros, e.g.
1552 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001553 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001554 * In particular, calling psa_mac_abort() after the operation has been
1555 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1556 * psa_mac_verify_finish() is safe and has no effect.
1557 *
1558 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001559 *
1560 * \retval #PSA_SUCCESS
1561 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001562 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001563 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1564 * \retval #PSA_ERROR_HARDWARE_FAILURE
1565 * \retval #PSA_ERROR_TAMPERING_DETECTED
1566 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001567psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1568
1569/**@}*/
1570
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001571/** \defgroup cipher Symmetric ciphers
1572 * @{
1573 */
1574
Gilles Peskine69647a42019-01-14 20:18:12 +01001575/** Encrypt a message using a symmetric cipher.
1576 *
1577 * This function encrypts a message with a random IV (initialization
1578 * vector).
1579 *
1580 * \param handle Handle to the key to use for the operation.
1581 * It must remain valid until the operation
1582 * terminates.
1583 * \param alg The cipher algorithm to compute
1584 * (\c PSA_ALG_XXX value such that
1585 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1586 * \param[in] input Buffer containing the message to encrypt.
1587 * \param input_length Size of the \p input buffer in bytes.
1588 * \param[out] output Buffer where the output is to be written.
1589 * The output contains the IV followed by
1590 * the ciphertext proper.
1591 * \param output_size Size of the \p output buffer in bytes.
1592 * \param[out] output_length On success, the number of bytes
1593 * that make up the output.
1594 *
1595 * \retval #PSA_SUCCESS
1596 * Success.
1597 * \retval #PSA_ERROR_INVALID_HANDLE
1598 * \retval #PSA_ERROR_EMPTY_SLOT
1599 * \retval #PSA_ERROR_NOT_PERMITTED
1600 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001601 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001602 * \retval #PSA_ERROR_NOT_SUPPORTED
1603 * \p alg is not supported or is not a cipher algorithm.
1604 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1605 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1606 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1607 * \retval #PSA_ERROR_HARDWARE_FAILURE
1608 * \retval #PSA_ERROR_TAMPERING_DETECTED
1609 */
1610psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1611 psa_algorithm_t alg,
1612 const uint8_t *input,
1613 size_t input_length,
1614 uint8_t *output,
1615 size_t output_size,
1616 size_t *output_length);
1617
1618/** Decrypt a message using a symmetric cipher.
1619 *
1620 * This function decrypts a message encrypted with a symmetric cipher.
1621 *
1622 * \param handle Handle to the key to use for the operation.
1623 * It must remain valid until the operation
1624 * terminates.
1625 * \param alg The cipher algorithm to compute
1626 * (\c PSA_ALG_XXX value such that
1627 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1628 * \param[in] input Buffer containing the message to decrypt.
1629 * This consists of the IV followed by the
1630 * ciphertext proper.
1631 * \param input_length Size of the \p input buffer in bytes.
1632 * \param[out] output Buffer where the plaintext is to be written.
1633 * \param output_size Size of the \p output buffer in bytes.
1634 * \param[out] output_length On success, the number of bytes
1635 * that make up the output.
1636 *
1637 * \retval #PSA_SUCCESS
1638 * Success.
1639 * \retval #PSA_ERROR_INVALID_HANDLE
1640 * \retval #PSA_ERROR_EMPTY_SLOT
1641 * \retval #PSA_ERROR_NOT_PERMITTED
1642 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001643 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001644 * \retval #PSA_ERROR_NOT_SUPPORTED
1645 * \p alg is not supported or is not a cipher algorithm.
1646 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1647 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1648 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1649 * \retval #PSA_ERROR_HARDWARE_FAILURE
1650 * \retval #PSA_ERROR_TAMPERING_DETECTED
1651 */
1652psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1653 psa_algorithm_t alg,
1654 const uint8_t *input,
1655 size_t input_length,
1656 uint8_t *output,
1657 size_t output_size,
1658 size_t *output_length);
1659
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001660/** The type of the state data structure for multipart cipher operations.
1661 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001662 * Before calling any function on a cipher operation object, the application
1663 * must initialize it by any of the following means:
1664 * - Set the structure to all-bits-zero, for example:
1665 * \code
1666 * psa_cipher_operation_t operation;
1667 * memset(&operation, 0, sizeof(operation));
1668 * \endcode
1669 * - Initialize the structure to logical zero values, for example:
1670 * \code
1671 * psa_cipher_operation_t operation = {0};
1672 * \endcode
1673 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1674 * for example:
1675 * \code
1676 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1677 * \endcode
1678 * - Assign the result of the function psa_cipher_operation_init()
1679 * to the structure, for example:
1680 * \code
1681 * psa_cipher_operation_t operation;
1682 * operation = psa_cipher_operation_init();
1683 * \endcode
1684 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001685 * This is an implementation-defined \c struct. Applications should not
1686 * make any assumptions about the content of this structure except
1687 * as directed by the documentation of a specific implementation. */
1688typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1689
Jaeden Amero5bae2272019-01-04 11:48:27 +00001690/** \def PSA_CIPHER_OPERATION_INIT
1691 *
1692 * This macro returns a suitable initializer for a cipher operation object of
1693 * type #psa_cipher_operation_t.
1694 */
1695#ifdef __DOXYGEN_ONLY__
1696/* This is an example definition for documentation purposes.
1697 * Implementations should define a suitable value in `crypto_struct.h`.
1698 */
1699#define PSA_CIPHER_OPERATION_INIT {0}
1700#endif
1701
1702/** Return an initial value for a cipher operation object.
1703 */
1704static psa_cipher_operation_t psa_cipher_operation_init(void);
1705
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001706/** Set the key for a multipart symmetric encryption operation.
1707 *
1708 * The sequence of operations to encrypt a message with a symmetric cipher
1709 * is as follows:
1710 * -# Allocate an operation object which will be passed to all the functions
1711 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001712 * -# Initialize the operation object with one of the methods described in the
1713 * documentation for #psa_cipher_operation_t, e.g.
1714 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001715 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001716 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001717 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001718 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001719 * requires a specific IV value.
1720 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1721 * of the message each time.
1722 * -# Call psa_cipher_finish().
1723 *
1724 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001725 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001726 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001727 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001728 * eventually terminate the operation. The following events terminate an
1729 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001730 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001731 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001732 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001733 * \param[in,out] operation The operation object to set up. It must have
1734 * been initialized as per the documentation for
1735 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001736 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001737 * It must remain valid until the operation
1738 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001739 * \param alg The cipher algorithm to compute
1740 * (\c PSA_ALG_XXX value such that
1741 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001742 *
Gilles Peskine28538492018-07-11 17:34:00 +02001743 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001744 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001745 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001746 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001747 * \retval #PSA_ERROR_NOT_PERMITTED
1748 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001749 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001750 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001751 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001752 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1753 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1754 * \retval #PSA_ERROR_HARDWARE_FAILURE
1755 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001756 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001757 * The operation state is not valid (already set up and not
1758 * subsequently completed).
1759 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001760 * The library has not been previously initialized by psa_crypto_init().
1761 * It is implementation-dependent whether a failure to initialize
1762 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001763 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001764psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001765 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001766 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001767
1768/** Set the key for a multipart symmetric decryption operation.
1769 *
1770 * The sequence of operations to decrypt a message with a symmetric cipher
1771 * is as follows:
1772 * -# Allocate an operation object which will be passed to all the functions
1773 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001774 * -# Initialize the operation object with one of the methods described in the
1775 * documentation for #psa_cipher_operation_t, e.g.
1776 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001777 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001778 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001779 * decryption. If the IV is prepended to the ciphertext, you can call
1780 * psa_cipher_update() on a buffer containing the IV followed by the
1781 * beginning of the message.
1782 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1783 * of the message each time.
1784 * -# Call psa_cipher_finish().
1785 *
1786 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001787 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001788 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001789 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001790 * eventually terminate the operation. The following events terminate an
1791 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001792 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001793 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001794 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001795 * \param[in,out] operation The operation object to set up. It must have
1796 * been initialized as per the documentation for
1797 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001798 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001799 * It must remain valid until the operation
1800 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001801 * \param alg The cipher algorithm to compute
1802 * (\c PSA_ALG_XXX value such that
1803 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001804 *
Gilles Peskine28538492018-07-11 17:34:00 +02001805 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001806 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001807 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001808 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001809 * \retval #PSA_ERROR_NOT_PERMITTED
1810 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001811 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001812 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001813 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001814 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1815 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1816 * \retval #PSA_ERROR_HARDWARE_FAILURE
1817 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001818 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001819 * The operation state is not valid (already set up and not
1820 * subsequently completed).
1821 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001822 * The library has not been previously initialized by psa_crypto_init().
1823 * It is implementation-dependent whether a failure to initialize
1824 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001825 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001826psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001827 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001828 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001829
Gilles Peskinedcd14942018-07-12 00:30:52 +02001830/** Generate an IV for a symmetric encryption operation.
1831 *
1832 * This function generates a random IV (initialization vector), nonce
1833 * or initial counter value for the encryption operation as appropriate
1834 * for the chosen algorithm, key type and key size.
1835 *
1836 * The application must call psa_cipher_encrypt_setup() before
1837 * calling this function.
1838 *
1839 * If this function returns an error status, the operation becomes inactive.
1840 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001841 * \param[in,out] operation Active cipher operation.
1842 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001843 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001844 * \param[out] iv_length On success, the number of bytes of the
1845 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001846 *
1847 * \retval #PSA_SUCCESS
1848 * Success.
1849 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001850 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001851 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001852 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001853 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1854 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1855 * \retval #PSA_ERROR_HARDWARE_FAILURE
1856 * \retval #PSA_ERROR_TAMPERING_DETECTED
1857 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001858psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1859 unsigned char *iv,
1860 size_t iv_size,
1861 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001862
Gilles Peskinedcd14942018-07-12 00:30:52 +02001863/** Set the IV for a symmetric encryption or decryption operation.
1864 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001865 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001866 * or initial counter value for the encryption or decryption operation.
1867 *
1868 * The application must call psa_cipher_encrypt_setup() before
1869 * calling this function.
1870 *
1871 * If this function returns an error status, the operation becomes inactive.
1872 *
1873 * \note When encrypting, applications should use psa_cipher_generate_iv()
1874 * instead of this function, unless implementing a protocol that requires
1875 * a non-random IV.
1876 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001877 * \param[in,out] operation Active cipher operation.
1878 * \param[in] iv Buffer containing the IV to use.
1879 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001880 *
1881 * \retval #PSA_SUCCESS
1882 * Success.
1883 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001884 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001885 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001886 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001887 * or the chosen algorithm does not use an IV.
1888 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1889 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1890 * \retval #PSA_ERROR_HARDWARE_FAILURE
1891 * \retval #PSA_ERROR_TAMPERING_DETECTED
1892 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001893psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1894 const unsigned char *iv,
1895 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001896
Gilles Peskinedcd14942018-07-12 00:30:52 +02001897/** Encrypt or decrypt a message fragment in an active cipher operation.
1898 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001899 * Before calling this function, you must:
1900 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1901 * The choice of setup function determines whether this function
1902 * encrypts or decrypts its input.
1903 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1904 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001905 *
1906 * If this function returns an error status, the operation becomes inactive.
1907 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001908 * \param[in,out] operation Active cipher operation.
1909 * \param[in] input Buffer containing the message fragment to
1910 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001911 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001912 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001913 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001914 * \param[out] output_length On success, the number of bytes
1915 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001916 *
1917 * \retval #PSA_SUCCESS
1918 * Success.
1919 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001920 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001921 * not set, or already completed).
1922 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1923 * The size of the \p output buffer is too small.
1924 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1925 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1926 * \retval #PSA_ERROR_HARDWARE_FAILURE
1927 * \retval #PSA_ERROR_TAMPERING_DETECTED
1928 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001929psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1930 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001931 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001932 unsigned char *output,
1933 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001934 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001935
Gilles Peskinedcd14942018-07-12 00:30:52 +02001936/** Finish encrypting or decrypting a message in a cipher operation.
1937 *
1938 * The application must call psa_cipher_encrypt_setup() or
1939 * psa_cipher_decrypt_setup() before calling this function. The choice
1940 * of setup function determines whether this function encrypts or
1941 * decrypts its input.
1942 *
1943 * This function finishes the encryption or decryption of the message
1944 * formed by concatenating the inputs passed to preceding calls to
1945 * psa_cipher_update().
1946 *
1947 * When this function returns, the operation becomes inactive.
1948 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001949 * \param[in,out] operation Active cipher operation.
1950 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001951 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001952 * \param[out] output_length On success, the number of bytes
1953 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001954 *
1955 * \retval #PSA_SUCCESS
1956 * Success.
1957 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001958 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001959 * not set, or already completed).
1960 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1961 * The size of the \p output buffer is too small.
1962 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1963 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1964 * \retval #PSA_ERROR_HARDWARE_FAILURE
1965 * \retval #PSA_ERROR_TAMPERING_DETECTED
1966 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001967psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001968 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001969 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001970 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001971
Gilles Peskinedcd14942018-07-12 00:30:52 +02001972/** Abort a cipher operation.
1973 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001974 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001975 * \p operation structure itself. Once aborted, the operation object
1976 * can be reused for another operation by calling
1977 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001978 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001979 * You may call this function any time after the operation object has
1980 * been initialized by any of the following methods:
1981 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1982 * whether it succeeds or not.
1983 * - Initializing the \c struct to all-bits-zero.
1984 * - Initializing the \c struct to logical zeros, e.g.
1985 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001986 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001987 * In particular, calling psa_cipher_abort() after the operation has been
1988 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1989 * is safe and has no effect.
1990 *
1991 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001992 *
1993 * \retval #PSA_SUCCESS
1994 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001995 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001996 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1997 * \retval #PSA_ERROR_HARDWARE_FAILURE
1998 * \retval #PSA_ERROR_TAMPERING_DETECTED
1999 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002000psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2001
2002/**@}*/
2003
Gilles Peskine3b555712018-03-03 21:27:57 +01002004/** \defgroup aead Authenticated encryption with associated data (AEAD)
2005 * @{
2006 */
2007
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002008/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002009 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002010 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002011 * \param alg The AEAD algorithm to compute
2012 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002013 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002014 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002015 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002016 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002017 * but not encrypted.
2018 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002019 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002020 * encrypted.
2021 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002022 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002023 * encrypted data. The additional data is not
2024 * part of this output. For algorithms where the
2025 * encrypted data and the authentication tag
2026 * are defined as separate outputs, the
2027 * authentication tag is appended to the
2028 * encrypted data.
2029 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2030 * This must be at least
2031 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2032 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002033 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002034 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002035 *
Gilles Peskine28538492018-07-11 17:34:00 +02002036 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002037 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002038 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002039 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002040 * \retval #PSA_ERROR_NOT_PERMITTED
2041 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002042 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002043 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002044 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002045 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2046 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2047 * \retval #PSA_ERROR_HARDWARE_FAILURE
2048 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002049 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002050 * The library has not been previously initialized by psa_crypto_init().
2051 * It is implementation-dependent whether a failure to initialize
2052 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002053 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002054psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002055 psa_algorithm_t alg,
2056 const uint8_t *nonce,
2057 size_t nonce_length,
2058 const uint8_t *additional_data,
2059 size_t additional_data_length,
2060 const uint8_t *plaintext,
2061 size_t plaintext_length,
2062 uint8_t *ciphertext,
2063 size_t ciphertext_size,
2064 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002065
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002066/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002067 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002068 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002069 * \param alg The AEAD algorithm to compute
2070 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002071 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002072 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002073 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002074 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002075 * but not encrypted.
2076 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002077 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002078 * encrypted. For algorithms where the
2079 * encrypted data and the authentication tag
2080 * are defined as separate inputs, the buffer
2081 * must contain the encrypted data followed
2082 * by the authentication tag.
2083 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002084 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002085 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2086 * This must be at least
2087 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2088 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002089 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002090 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002091 *
Gilles Peskine28538492018-07-11 17:34:00 +02002092 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002093 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002094 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002095 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002096 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002097 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002098 * \retval #PSA_ERROR_NOT_PERMITTED
2099 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002100 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002101 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002102 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002103 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2104 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2105 * \retval #PSA_ERROR_HARDWARE_FAILURE
2106 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002107 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002108 * The library has not been previously initialized by psa_crypto_init().
2109 * It is implementation-dependent whether a failure to initialize
2110 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002111 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002112psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002113 psa_algorithm_t alg,
2114 const uint8_t *nonce,
2115 size_t nonce_length,
2116 const uint8_t *additional_data,
2117 size_t additional_data_length,
2118 const uint8_t *ciphertext,
2119 size_t ciphertext_length,
2120 uint8_t *plaintext,
2121 size_t plaintext_size,
2122 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002123
Gilles Peskine30a9e412019-01-14 18:36:12 +01002124/** The type of the state data structure for multipart AEAD operations.
2125 *
2126 * Before calling any function on an AEAD operation object, the application
2127 * must initialize it by any of the following means:
2128 * - Set the structure to all-bits-zero, for example:
2129 * \code
2130 * psa_aead_operation_t operation;
2131 * memset(&operation, 0, sizeof(operation));
2132 * \endcode
2133 * - Initialize the structure to logical zero values, for example:
2134 * \code
2135 * psa_aead_operation_t operation = {0};
2136 * \endcode
2137 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2138 * for example:
2139 * \code
2140 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2141 * \endcode
2142 * - Assign the result of the function psa_aead_operation_init()
2143 * to the structure, for example:
2144 * \code
2145 * psa_aead_operation_t operation;
2146 * operation = psa_aead_operation_init();
2147 * \endcode
2148 *
2149 * This is an implementation-defined \c struct. Applications should not
2150 * make any assumptions about the content of this structure except
2151 * as directed by the documentation of a specific implementation. */
2152typedef struct psa_aead_operation_s psa_aead_operation_t;
2153
2154/** \def PSA_AEAD_OPERATION_INIT
2155 *
2156 * This macro returns a suitable initializer for an AEAD operation object of
2157 * type #psa_aead_operation_t.
2158 */
2159#ifdef __DOXYGEN_ONLY__
2160/* This is an example definition for documentation purposes.
2161 * Implementations should define a suitable value in `crypto_struct.h`.
2162 */
2163#define PSA_AEAD_OPERATION_INIT {0}
2164#endif
2165
2166/** Return an initial value for an AEAD operation object.
2167 */
2168static psa_aead_operation_t psa_aead_operation_init(void);
2169
2170/** Set the key for a multipart authenticated encryption operation.
2171 *
2172 * The sequence of operations to encrypt a message with authentication
2173 * is as follows:
2174 * -# Allocate an operation object which will be passed to all the functions
2175 * listed here.
2176 * -# Initialize the operation object with one of the methods described in the
2177 * documentation for #psa_aead_operation_t, e.g.
2178 * PSA_AEAD_OPERATION_INIT.
2179 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002180 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2181 * inputs to the subsequent calls to psa_aead_update_ad() and
2182 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2183 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002184 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2185 * generate or set the nonce. You should use
2186 * psa_aead_generate_nonce() unless the protocol you are implementing
2187 * requires a specific nonce value.
2188 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2189 * of the non-encrypted additional authenticated data each time.
2190 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002191 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002192 * -# Call psa_aead_finish().
2193 *
2194 * The application may call psa_aead_abort() at any time after the operation
2195 * has been initialized.
2196 *
2197 * After a successful call to psa_aead_encrypt_setup(), the application must
2198 * eventually terminate the operation. The following events terminate an
2199 * operation:
2200 * - A failed call to any of the \c psa_aead_xxx functions.
2201 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2202 *
2203 * \param[in,out] operation The operation object to set up. It must have
2204 * been initialized as per the documentation for
2205 * #psa_aead_operation_t and not yet in use.
2206 * \param handle Handle to the key to use for the operation.
2207 * It must remain valid until the operation
2208 * terminates.
2209 * \param alg The AEAD algorithm to compute
2210 * (\c PSA_ALG_XXX value such that
2211 * #PSA_ALG_IS_AEAD(\p alg) is true).
2212 *
2213 * \retval #PSA_SUCCESS
2214 * Success.
2215 * \retval #PSA_ERROR_INVALID_HANDLE
2216 * \retval #PSA_ERROR_EMPTY_SLOT
2217 * \retval #PSA_ERROR_NOT_PERMITTED
2218 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002219 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002220 * \retval #PSA_ERROR_NOT_SUPPORTED
2221 * \p alg is not supported or is not an AEAD algorithm.
2222 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2223 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2224 * \retval #PSA_ERROR_HARDWARE_FAILURE
2225 * \retval #PSA_ERROR_TAMPERING_DETECTED
2226 * \retval #PSA_ERROR_BAD_STATE
2227 * The library has not been previously initialized by psa_crypto_init().
2228 * It is implementation-dependent whether a failure to initialize
2229 * results in this error code.
2230 */
2231psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2232 psa_key_handle_t handle,
2233 psa_algorithm_t alg);
2234
2235/** Set the key for a multipart authenticated decryption operation.
2236 *
2237 * The sequence of operations to decrypt a message with authentication
2238 * is as follows:
2239 * -# Allocate an operation object which will be passed to all the functions
2240 * listed here.
2241 * -# Initialize the operation object with one of the methods described in the
2242 * documentation for #psa_aead_operation_t, e.g.
2243 * PSA_AEAD_OPERATION_INIT.
2244 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002245 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2246 * inputs to the subsequent calls to psa_aead_update_ad() and
2247 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2248 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002249 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2250 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2251 * of the non-encrypted additional authenticated data each time.
2252 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002253 * of the ciphertext to decrypt each time.
2254 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002255 *
2256 * The application may call psa_aead_abort() at any time after the operation
2257 * has been initialized.
2258 *
2259 * After a successful call to psa_aead_decrypt_setup(), the application must
2260 * eventually terminate the operation. The following events terminate an
2261 * operation:
2262 * - A failed call to any of the \c psa_aead_xxx functions.
2263 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2264 *
2265 * \param[in,out] operation The operation object to set up. It must have
2266 * been initialized as per the documentation for
2267 * #psa_aead_operation_t and not yet in use.
2268 * \param handle Handle to the key to use for the operation.
2269 * It must remain valid until the operation
2270 * terminates.
2271 * \param alg The AEAD algorithm to compute
2272 * (\c PSA_ALG_XXX value such that
2273 * #PSA_ALG_IS_AEAD(\p alg) is true).
2274 *
2275 * \retval #PSA_SUCCESS
2276 * Success.
2277 * \retval #PSA_ERROR_INVALID_HANDLE
2278 * \retval #PSA_ERROR_EMPTY_SLOT
2279 * \retval #PSA_ERROR_NOT_PERMITTED
2280 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002281 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002282 * \retval #PSA_ERROR_NOT_SUPPORTED
2283 * \p alg is not supported or is not an AEAD algorithm.
2284 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2285 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2286 * \retval #PSA_ERROR_HARDWARE_FAILURE
2287 * \retval #PSA_ERROR_TAMPERING_DETECTED
2288 * \retval #PSA_ERROR_BAD_STATE
2289 * The library has not been previously initialized by psa_crypto_init().
2290 * It is implementation-dependent whether a failure to initialize
2291 * results in this error code.
2292 */
2293psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2294 psa_key_handle_t handle,
2295 psa_algorithm_t alg);
2296
2297/** Generate a random nonce for an authenticated encryption operation.
2298 *
2299 * This function generates a random nonce for the authenticated encryption
2300 * operation with an appropriate size for the chosen algorithm, key type
2301 * and key size.
2302 *
2303 * The application must call psa_aead_encrypt_setup() before
2304 * calling this function.
2305 *
2306 * If this function returns an error status, the operation becomes inactive.
2307 *
2308 * \param[in,out] operation Active AEAD operation.
2309 * \param[out] nonce Buffer where the generated nonce is to be
2310 * written.
2311 * \param nonce_size Size of the \p nonce buffer in bytes.
2312 * \param[out] nonce_length On success, the number of bytes of the
2313 * generated nonce.
2314 *
2315 * \retval #PSA_SUCCESS
2316 * Success.
2317 * \retval #PSA_ERROR_BAD_STATE
2318 * The operation state is not valid (not set up, or nonce already set).
2319 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2320 * The size of the \p nonce buffer is too small.
2321 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2322 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2323 * \retval #PSA_ERROR_HARDWARE_FAILURE
2324 * \retval #PSA_ERROR_TAMPERING_DETECTED
2325 */
2326psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2327 unsigned char *nonce,
2328 size_t nonce_size,
2329 size_t *nonce_length);
2330
2331/** Set the nonce for an authenticated encryption or decryption operation.
2332 *
2333 * This function sets the nonce for the authenticated
2334 * encryption or decryption operation.
2335 *
2336 * The application must call psa_aead_encrypt_setup() before
2337 * calling this function.
2338 *
2339 * If this function returns an error status, the operation becomes inactive.
2340 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002341 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002342 * instead of this function, unless implementing a protocol that requires
2343 * a non-random IV.
2344 *
2345 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002346 * \param[in] nonce Buffer containing the nonce to use.
2347 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002348 *
2349 * \retval #PSA_SUCCESS
2350 * Success.
2351 * \retval #PSA_ERROR_BAD_STATE
2352 * The operation state is not valid (not set up, or nonce already set).
2353 * \retval #PSA_ERROR_INVALID_ARGUMENT
2354 * The size of \p nonce is not acceptable for the chosen algorithm.
2355 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2356 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2357 * \retval #PSA_ERROR_HARDWARE_FAILURE
2358 * \retval #PSA_ERROR_TAMPERING_DETECTED
2359 */
2360psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2361 const unsigned char *nonce,
2362 size_t nonce_length);
2363
Gilles Peskinebc59c852019-01-17 15:26:08 +01002364/** Declare the lengths of the message and additional data for AEAD.
2365 *
2366 * The application must call this function before calling
2367 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2368 * the operation requires it. If the algorithm does not require it,
2369 * calling this function is optional, but if this function is called
2370 * then the implementation must enforce the lengths.
2371 *
2372 * You may call this function before or after setting the nonce with
2373 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2374 *
2375 * - For #PSA_ALG_CCM, calling this function is required.
2376 * - For the other AEAD algorithms defined in this specification, calling
2377 * this function is not required.
2378 * - For vendor-defined algorithm, refer to the vendor documentation.
2379 *
2380 * \param[in,out] operation Active AEAD operation.
2381 * \param ad_length Size of the non-encrypted additional
2382 * authenticated data in bytes.
2383 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2384 *
2385 * \retval #PSA_SUCCESS
2386 * Success.
2387 * \retval #PSA_ERROR_BAD_STATE
2388 * The operation state is not valid (not set up, already completed,
2389 * or psa_aead_update_ad() or psa_aead_update() already called).
2390 * \retval #PSA_ERROR_INVALID_ARGUMENT
2391 * At least one of the lengths is not acceptable for the chosen
2392 * algorithm.
2393 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2394 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2395 * \retval #PSA_ERROR_HARDWARE_FAILURE
2396 * \retval #PSA_ERROR_TAMPERING_DETECTED
2397 */
2398psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2399 size_t ad_length,
2400 size_t plaintext_length);
2401
Gilles Peskine30a9e412019-01-14 18:36:12 +01002402/** Pass additional data to an active AEAD operation.
2403 *
2404 * Additional data is authenticated, but not encrypted.
2405 *
2406 * You may call this function multiple times to pass successive fragments
2407 * of the additional data. You may not call this function after passing
2408 * data to encrypt or decrypt with psa_aead_update().
2409 *
2410 * Before calling this function, you must:
2411 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2412 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2413 *
2414 * If this function returns an error status, the operation becomes inactive.
2415 *
2416 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2417 * there is no guarantee that the input is valid. Therefore, until
2418 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2419 * treat the input as untrusted and prepare to undo any action that
2420 * depends on the input if psa_aead_verify() returns an error status.
2421 *
2422 * \param[in,out] operation Active AEAD operation.
2423 * \param[in] input Buffer containing the fragment of
2424 * additional data.
2425 * \param input_length Size of the \p input buffer in bytes.
2426 *
2427 * \retval #PSA_SUCCESS
2428 * Success.
2429 * \retval #PSA_ERROR_BAD_STATE
2430 * The operation state is not valid (not set up, nonce not set,
2431 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002432 * \retval #PSA_ERROR_INVALID_ARGUMENT
2433 * The total input length overflows the additional data length that
2434 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002435 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2436 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2437 * \retval #PSA_ERROR_HARDWARE_FAILURE
2438 * \retval #PSA_ERROR_TAMPERING_DETECTED
2439 */
2440psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2441 const uint8_t *input,
2442 size_t input_length);
2443
2444/** Encrypt or decrypt a message fragment in an active AEAD operation.
2445 *
2446 * Before calling this function, you must:
2447 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2448 * The choice of setup function determines whether this function
2449 * encrypts or decrypts its input.
2450 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2451 * 3. Call psa_aead_update_ad() to pass all the additional data.
2452 *
2453 * If this function returns an error status, the operation becomes inactive.
2454 *
2455 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2456 * there is no guarantee that the input is valid. Therefore, until
2457 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2458 * - Do not use the output in any way other than storing it in a
2459 * confidential location. If you take any action that depends
2460 * on the tentative decrypted data, this action will need to be
2461 * undone if the input turns out not to be valid. Furthermore,
2462 * if an adversary can observe that this action took place
2463 * (for example through timing), they may be able to use this
2464 * fact as an oracle to decrypt any message encrypted with the
2465 * same key.
2466 * - In particular, do not copy the output anywhere but to a
2467 * memory or storage space that you have exclusive access to.
2468 *
2469 * \param[in,out] operation Active AEAD operation.
2470 * \param[in] input Buffer containing the message fragment to
2471 * encrypt or decrypt.
2472 * \param input_length Size of the \p input buffer in bytes.
2473 * \param[out] output Buffer where the output is to be written.
2474 * \param output_size Size of the \p output buffer in bytes.
2475 * \param[out] output_length On success, the number of bytes
2476 * that make up the returned output.
2477 *
2478 * \retval #PSA_SUCCESS
2479 * Success.
2480 * \retval #PSA_ERROR_BAD_STATE
2481 * The operation state is not valid (not set up, nonce not set
2482 * or already completed).
2483 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2484 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002485 * \retval #PSA_ERROR_INVALID_ARGUMENT
2486 * The total length of input to psa_aead_update_ad() so far is
2487 * less than the additional data length that was previously
2488 * specified with psa_aead_set_lengths().
2489 * \retval #PSA_ERROR_INVALID_ARGUMENT
2490 * The total input length overflows the plaintext length that
2491 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002492 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2493 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2494 * \retval #PSA_ERROR_HARDWARE_FAILURE
2495 * \retval #PSA_ERROR_TAMPERING_DETECTED
2496 */
2497psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2498 const uint8_t *input,
2499 size_t input_length,
2500 unsigned char *output,
2501 size_t output_size,
2502 size_t *output_length);
2503
2504/** Finish encrypting a message in an AEAD operation.
2505 *
2506 * The operation must have been set up with psa_aead_encrypt_setup().
2507 *
2508 * This function finishes the authentication of the additional data
2509 * formed by concatenating the inputs passed to preceding calls to
2510 * psa_aead_update_ad() with the plaintext formed by concatenating the
2511 * inputs passed to preceding calls to psa_aead_update().
2512 *
2513 * This function has two output buffers:
2514 * - \p ciphertext contains trailing ciphertext that was buffered from
2515 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2516 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2517 * will not contain any output and can be a 0-sized buffer.
2518 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002519 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002520 * that the operation performs.
2521 *
2522 * When this function returns, the operation becomes inactive.
2523 *
2524 * \param[in,out] operation Active AEAD operation.
2525 * \param[out] ciphertext Buffer where the last part of the ciphertext
2526 * is to be written.
2527 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2528 * \param[out] ciphertext_length On success, the number of bytes of
2529 * returned ciphertext.
2530 * \param[out] tag Buffer where the authentication tag is
2531 * to be written.
2532 * \param tag_size Size of the \p tag buffer in bytes.
2533 * \param[out] tag_length On success, the number of bytes
2534 * that make up the returned tag.
2535 *
2536 * \retval #PSA_SUCCESS
2537 * Success.
2538 * \retval #PSA_ERROR_BAD_STATE
2539 * The operation state is not valid (not set up, nonce not set,
2540 * decryption, or already completed).
2541 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002542 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002543 * \retval #PSA_ERROR_INVALID_ARGUMENT
2544 * The total length of input to psa_aead_update_ad() so far is
2545 * less than the additional data length that was previously
2546 * specified with psa_aead_set_lengths().
2547 * \retval #PSA_ERROR_INVALID_ARGUMENT
2548 * The total length of input to psa_aead_update() so far is
2549 * less than the plaintext length that was previously
2550 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002551 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2552 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2553 * \retval #PSA_ERROR_HARDWARE_FAILURE
2554 * \retval #PSA_ERROR_TAMPERING_DETECTED
2555 */
2556psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002557 uint8_t *ciphertext,
2558 size_t ciphertext_size,
2559 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002560 uint8_t *tag,
2561 size_t tag_size,
2562 size_t *tag_length);
2563
2564/** Finish authenticating and decrypting a message in an AEAD operation.
2565 *
2566 * The operation must have been set up with psa_aead_decrypt_setup().
2567 *
2568 * This function finishes the authentication of the additional data
2569 * formed by concatenating the inputs passed to preceding calls to
2570 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2571 * inputs passed to preceding calls to psa_aead_update().
2572 *
2573 * When this function returns, the operation becomes inactive.
2574 *
2575 * \param[in,out] operation Active AEAD operation.
2576 * \param[in] tag Buffer containing the authentication tag.
2577 * \param tag_length Size of the \p tag buffer in bytes.
2578 *
2579 * \retval #PSA_SUCCESS
2580 * Success.
2581 * \retval #PSA_ERROR_BAD_STATE
2582 * The operation state is not valid (not set up, nonce not set,
2583 * encryption, or already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002584 * \retval #PSA_ERROR_INVALID_ARGUMENT
2585 * The total length of input to psa_aead_update_ad() so far is
2586 * less than the additional data length that was previously
2587 * specified with psa_aead_set_lengths().
2588 * \retval #PSA_ERROR_INVALID_ARGUMENT
2589 * The total length of input to psa_aead_update() so far is
2590 * less than the plaintext length that was previously
2591 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002592 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2593 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2594 * \retval #PSA_ERROR_HARDWARE_FAILURE
2595 * \retval #PSA_ERROR_TAMPERING_DETECTED
2596 */
2597psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2598 const uint8_t *tag,
2599 size_t tag_length);
2600
2601/** Abort an AEAD operation.
2602 *
2603 * Aborting an operation frees all associated resources except for the
2604 * \p operation structure itself. Once aborted, the operation object
2605 * can be reused for another operation by calling
2606 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2607 *
2608 * You may call this function any time after the operation object has
2609 * been initialized by any of the following methods:
2610 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2611 * whether it succeeds or not.
2612 * - Initializing the \c struct to all-bits-zero.
2613 * - Initializing the \c struct to logical zeros, e.g.
2614 * `psa_aead_operation_t operation = {0}`.
2615 *
2616 * In particular, calling psa_aead_abort() after the operation has been
2617 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2618 * is safe and has no effect.
2619 *
2620 * \param[in,out] operation Initialized AEAD operation.
2621 *
2622 * \retval #PSA_SUCCESS
2623 * \retval #PSA_ERROR_BAD_STATE
2624 * \p operation is not an active AEAD operation.
2625 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2626 * \retval #PSA_ERROR_HARDWARE_FAILURE
2627 * \retval #PSA_ERROR_TAMPERING_DETECTED
2628 */
2629psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2630
Gilles Peskine3b555712018-03-03 21:27:57 +01002631/**@}*/
2632
Gilles Peskine20035e32018-02-03 22:44:14 +01002633/** \defgroup asymmetric Asymmetric cryptography
2634 * @{
2635 */
2636
2637/**
2638 * \brief Sign a hash or short message with a private key.
2639 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002640 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002641 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002642 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2643 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2644 * to determine the hash algorithm to use.
2645 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002646 * \param handle Handle to the key to use for the operation.
2647 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002648 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002649 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002650 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002651 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002652 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002653 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002654 * \param[out] signature_length On success, the number of bytes
2655 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002656 *
Gilles Peskine28538492018-07-11 17:34:00 +02002657 * \retval #PSA_SUCCESS
2658 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002659 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002660 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002661 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002662 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002663 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002664 * \retval #PSA_ERROR_NOT_SUPPORTED
2665 * \retval #PSA_ERROR_INVALID_ARGUMENT
2666 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2667 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2668 * \retval #PSA_ERROR_HARDWARE_FAILURE
2669 * \retval #PSA_ERROR_TAMPERING_DETECTED
2670 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002671 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002672 * The library has not been previously initialized by psa_crypto_init().
2673 * It is implementation-dependent whether a failure to initialize
2674 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002675 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002676psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002677 psa_algorithm_t alg,
2678 const uint8_t *hash,
2679 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002680 uint8_t *signature,
2681 size_t signature_size,
2682 size_t *signature_length);
2683
2684/**
2685 * \brief Verify the signature a hash or short message using a public key.
2686 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002687 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002688 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002689 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2690 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2691 * to determine the hash algorithm to use.
2692 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002693 * \param handle Handle to the key to use for the operation.
2694 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002695 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002696 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002697 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002698 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002699 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002700 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002701 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002702 *
Gilles Peskine28538492018-07-11 17:34:00 +02002703 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002704 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002705 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002706 * The calculation was perfomed successfully, but the passed
2707 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002708 * \retval #PSA_ERROR_NOT_SUPPORTED
2709 * \retval #PSA_ERROR_INVALID_ARGUMENT
2710 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2711 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2712 * \retval #PSA_ERROR_HARDWARE_FAILURE
2713 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002714 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002715 * The library has not been previously initialized by psa_crypto_init().
2716 * It is implementation-dependent whether a failure to initialize
2717 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002718 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002719psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002720 psa_algorithm_t alg,
2721 const uint8_t *hash,
2722 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002723 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002724 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002725
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002726/**
2727 * \brief Encrypt a short message with a public key.
2728 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002729 * \param handle Handle to the key to use for the operation.
2730 * It must be a public key or an asymmetric
2731 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002732 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002733 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002734 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002735 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002736 * \param[in] salt A salt or label, if supported by the
2737 * encryption algorithm.
2738 * If the algorithm does not support a
2739 * salt, pass \c NULL.
2740 * If the algorithm supports an optional
2741 * salt and you do not want to pass a salt,
2742 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002743 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002744 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2745 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002746 * \param salt_length Size of the \p salt buffer in bytes.
2747 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002748 * \param[out] output Buffer where the encrypted message is to
2749 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002750 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002751 * \param[out] output_length On success, the number of bytes
2752 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002753 *
Gilles Peskine28538492018-07-11 17:34:00 +02002754 * \retval #PSA_SUCCESS
2755 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002756 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002757 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002758 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002759 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002760 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002761 * \retval #PSA_ERROR_NOT_SUPPORTED
2762 * \retval #PSA_ERROR_INVALID_ARGUMENT
2763 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2764 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2765 * \retval #PSA_ERROR_HARDWARE_FAILURE
2766 * \retval #PSA_ERROR_TAMPERING_DETECTED
2767 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002768 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002769 * The library has not been previously initialized by psa_crypto_init().
2770 * It is implementation-dependent whether a failure to initialize
2771 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002772 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002773psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002774 psa_algorithm_t alg,
2775 const uint8_t *input,
2776 size_t input_length,
2777 const uint8_t *salt,
2778 size_t salt_length,
2779 uint8_t *output,
2780 size_t output_size,
2781 size_t *output_length);
2782
2783/**
2784 * \brief Decrypt a short message with a private key.
2785 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002786 * \param handle Handle to the key to use for the operation.
2787 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002788 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002789 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002790 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002791 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002792 * \param[in] salt A salt or label, if supported by the
2793 * encryption algorithm.
2794 * If the algorithm does not support a
2795 * salt, pass \c NULL.
2796 * If the algorithm supports an optional
2797 * salt and you do not want to pass a salt,
2798 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002799 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002800 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2801 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002802 * \param salt_length Size of the \p salt buffer in bytes.
2803 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002804 * \param[out] output Buffer where the decrypted message is to
2805 * be written.
2806 * \param output_size Size of the \c output buffer in bytes.
2807 * \param[out] output_length On success, the number of bytes
2808 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002809 *
Gilles Peskine28538492018-07-11 17:34:00 +02002810 * \retval #PSA_SUCCESS
2811 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002812 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002813 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002814 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002815 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002816 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002817 * \retval #PSA_ERROR_NOT_SUPPORTED
2818 * \retval #PSA_ERROR_INVALID_ARGUMENT
2819 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2820 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2821 * \retval #PSA_ERROR_HARDWARE_FAILURE
2822 * \retval #PSA_ERROR_TAMPERING_DETECTED
2823 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2824 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002825 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002826 * The library has not been previously initialized by psa_crypto_init().
2827 * It is implementation-dependent whether a failure to initialize
2828 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002829 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002830psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002831 psa_algorithm_t alg,
2832 const uint8_t *input,
2833 size_t input_length,
2834 const uint8_t *salt,
2835 size_t salt_length,
2836 uint8_t *output,
2837 size_t output_size,
2838 size_t *output_length);
2839
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002840/**@}*/
2841
Gilles Peskineedd76872018-07-20 17:42:05 +02002842/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002843 * @{
2844 */
2845
2846/** The type of the state data structure for generators.
2847 *
2848 * Before calling any function on a generator, the application must
2849 * initialize it by any of the following means:
2850 * - Set the structure to all-bits-zero, for example:
2851 * \code
2852 * psa_crypto_generator_t generator;
2853 * memset(&generator, 0, sizeof(generator));
2854 * \endcode
2855 * - Initialize the structure to logical zero values, for example:
2856 * \code
2857 * psa_crypto_generator_t generator = {0};
2858 * \endcode
2859 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2860 * for example:
2861 * \code
2862 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2863 * \endcode
2864 * - Assign the result of the function psa_crypto_generator_init()
2865 * to the structure, for example:
2866 * \code
2867 * psa_crypto_generator_t generator;
2868 * generator = psa_crypto_generator_init();
2869 * \endcode
2870 *
2871 * This is an implementation-defined \c struct. Applications should not
2872 * make any assumptions about the content of this structure except
2873 * as directed by the documentation of a specific implementation.
2874 */
2875typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2876
2877/** \def PSA_CRYPTO_GENERATOR_INIT
2878 *
2879 * This macro returns a suitable initializer for a generator object
2880 * of type #psa_crypto_generator_t.
2881 */
2882#ifdef __DOXYGEN_ONLY__
2883/* This is an example definition for documentation purposes.
2884 * Implementations should define a suitable value in `crypto_struct.h`.
2885 */
2886#define PSA_CRYPTO_GENERATOR_INIT {0}
2887#endif
2888
2889/** Return an initial value for a generator object.
2890 */
2891static psa_crypto_generator_t psa_crypto_generator_init(void);
2892
2893/** Retrieve the current capacity of a generator.
2894 *
2895 * The capacity of a generator is the maximum number of bytes that it can
2896 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2897 *
2898 * \param[in] generator The generator to query.
2899 * \param[out] capacity On success, the capacity of the generator.
2900 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002901 * \retval #PSA_SUCCESS
2902 * \retval #PSA_ERROR_BAD_STATE
2903 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002904 */
2905psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2906 size_t *capacity);
2907
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002908/** Set the maximum capacity of a generator.
2909 *
2910 * \param[in,out] generator The generator object to modify.
2911 * \param capacity The new capacity of the generator.
2912 * It must be less or equal to the generator's
2913 * current capacity.
2914 *
2915 * \retval #PSA_SUCCESS
2916 * \retval #PSA_ERROR_INVALID_ARGUMENT
2917 * \p capacity is larger than the generator's current capacity.
2918 * \retval #PSA_ERROR_BAD_STATE
2919 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2920 */
2921psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2922 size_t capacity);
2923
Gilles Peskineeab56e42018-07-12 17:12:33 +02002924/** Read some data from a generator.
2925 *
2926 * This function reads and returns a sequence of bytes from a generator.
2927 * The data that is read is discarded from the generator. The generator's
2928 * capacity is decreased by the number of bytes read.
2929 *
2930 * \param[in,out] generator The generator object to read from.
2931 * \param[out] output Buffer where the generator output will be
2932 * written.
2933 * \param output_length Number of bytes to output.
2934 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002935 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02002936 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02002937 * There were fewer than \p output_length bytes
2938 * in the generator. Note that in this case, no
2939 * output is written to the output buffer.
2940 * The generator's capacity is set to 0, thus
2941 * subsequent calls to this function will not
2942 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002943 * \retval #PSA_ERROR_BAD_STATE
2944 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2945 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2946 * \retval #PSA_ERROR_HARDWARE_FAILURE
2947 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002948 */
2949psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2950 uint8_t *output,
2951 size_t output_length);
2952
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002953/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002954 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002955 * This function uses the output of a generator to derive a key.
2956 * How much output it consumes and how the key is derived depends on the
2957 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002958 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002959 * - For key types for which the key is an arbitrary sequence of bytes
2960 * of a given size,
2961 * this function is functionally equivalent to calling #psa_generator_read
2962 * and passing the resulting output to #psa_import_key.
2963 * However, this function has a security benefit:
2964 * if the implementation provides an isolation boundary then
2965 * the key material is not exposed outside the isolation boundary.
2966 * As a consequence, for these key types, this function always consumes
2967 * exactly (\p bits / 8) bytes from the generator.
2968 * The following key types defined in this specification follow this scheme:
2969 *
2970 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002971 * - #PSA_KEY_TYPE_ARC4;
2972 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002973 * - #PSA_KEY_TYPE_DERIVE;
2974 * - #PSA_KEY_TYPE_HMAC.
2975 *
2976 * - For ECC keys on a Montgomery elliptic curve
2977 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
2978 * Montgomery curve), this function always draws a byte string whose
2979 * length is determined by the curve, and sets the mandatory bits
2980 * accordingly. That is:
2981 *
2982 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
2983 * and process it as specified in RFC 7748 &sect;5.
2984 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
2985 * and process it as specified in RFC 7748 &sect;5.
2986 *
2987 * - For key types for which the key is represented by a single sequence of
2988 * \p bits bits with constraints as to which bit sequences are acceptable,
2989 * this function draws a byte string of length (\p bits / 8) bytes rounded
2990 * up to the nearest whole number of bytes. If the resulting byte string
2991 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
2992 * This process is repeated until an acceptable byte string is drawn.
2993 * The byte string drawn from the generator is interpreted as specified
2994 * for the output produced by psa_export_key().
2995 * The following key types defined in this specification follow this scheme:
2996 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01002997 * - #PSA_KEY_TYPE_DES.
2998 * Force-set the parity bits, but discard forbidden weak keys.
2999 * For 2-key and 3-key triple-DES, the three keys are generated
3000 * successively (for example, for 3-key triple-DES,
3001 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3002 * discard the first 8 bytes, use the next 8 bytes as the first key,
3003 * and continue reading output from the generator to derive the other
3004 * two keys).
3005 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
3006 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
3007 * ECC keys on a Weierstrass elliptic curve
3008 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3009 * Weierstrass curve).
3010 * For these key types, interpret the byte string as integer
3011 * in big-endian order. Discard it if it is not in the range
3012 * [0, *N* - 2] where *N* is the boundary of the private key domain
3013 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003014 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003015 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003016 * This method allows compliance to NIST standards, specifically
3017 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003018 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3019 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3020 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3021 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003022 *
3023 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
3024 * the way in which the generator output is consumed is
3025 * implementation-defined.
3026 *
3027 * In all cases, the data that is read is discarded from the generator.
3028 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003029 *
Gilles Peskine20628592019-04-19 19:29:50 +02003030 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003031 * \param[out] handle On success, a handle to the newly created key.
3032 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003033 * \param[in,out] generator The generator object to read from.
3034 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003035 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003036 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003037 * If the key is persistent, the key material and the key's metadata
3038 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003039 * \retval #PSA_ERROR_ALREADY_EXISTS
3040 * This is an attempt to create a persistent key, and there is
3041 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003042 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003043 * There was not enough data to create the desired key.
3044 * Note that in this case, no output is written to the output buffer.
3045 * The generator's capacity is set to 0, thus subsequent calls to
3046 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003047 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003048 * The key type or key size is not supported, either by the
3049 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003050 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003051 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3052 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3053 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3054 * \retval #PSA_ERROR_HARDWARE_FAILURE
3055 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003056 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003057 * The library has not been previously initialized by psa_crypto_init().
3058 * It is implementation-dependent whether a failure to initialize
3059 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003060 */
Gilles Peskine87a5e562019-04-17 12:28:25 +02003061psa_status_t psa_generator_import_key(const psa_key_attributes_t *attributes,
3062 psa_key_handle_t *handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003063 psa_crypto_generator_t *generator);
3064
3065/** Abort a generator.
3066 *
3067 * Once a generator has been aborted, its capacity is zero.
3068 * Aborting a generator frees all associated resources except for the
3069 * \c generator structure itself.
3070 *
3071 * This function may be called at any time as long as the generator
3072 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3073 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3074 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3075 * on a generator that has not been set up.
3076 *
3077 * Once aborted, the generator object may be called.
3078 *
3079 * \param[in,out] generator The generator to abort.
3080 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003081 * \retval #PSA_SUCCESS
3082 * \retval #PSA_ERROR_BAD_STATE
3083 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3084 * \retval #PSA_ERROR_HARDWARE_FAILURE
3085 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003086 */
3087psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3088
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003089/** Use the maximum possible capacity for a generator.
3090 *
3091 * Use this value as the capacity argument when setting up a generator
3092 * to indicate that the generator should have the maximum possible capacity.
3093 * The value of the maximum possible capacity depends on the generator
3094 * algorithm.
3095 */
3096#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3097
Gilles Peskineeab56e42018-07-12 17:12:33 +02003098/**@}*/
3099
Gilles Peskineea0fb492018-07-12 17:17:20 +02003100/** \defgroup derivation Key derivation
3101 * @{
3102 */
3103
3104/** Set up a key derivation operation.
3105 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003106 * A key derivation algorithm takes some inputs and uses them to create
3107 * a byte generator which can be used to produce keys and other
3108 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003109 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003110 * To use a generator for key derivation:
3111 * - Start with an initialized object of type #psa_crypto_generator_t.
3112 * - Call psa_key_derivation_setup() to select the algorithm.
3113 * - Provide the inputs for the key derivation by calling
3114 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3115 * as appropriate. Which inputs are needed, in what order, and whether
3116 * they may be keys and if so of what type depends on the algorithm.
3117 * - Optionally set the generator's maximum capacity with
3118 * psa_set_generator_capacity(). You may do this before, in the middle of
3119 * or after providing inputs. For some algorithms, this step is mandatory
3120 * because the output depends on the maximum capacity.
3121 * - Generate output with psa_generator_read() or
3122 * psa_generator_import_key(). Successive calls to these functions
3123 * use successive output bytes from the generator.
3124 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003125 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003126 * \param[in,out] generator The generator object to set up. It must
3127 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003128 * \param alg The key derivation algorithm to compute
3129 * (\c PSA_ALG_XXX value such that
3130 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003131 *
3132 * \retval #PSA_SUCCESS
3133 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003134 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003135 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003136 * \retval #PSA_ERROR_NOT_SUPPORTED
3137 * \c alg is not supported or is not a key derivation algorithm.
3138 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3139 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3140 * \retval #PSA_ERROR_HARDWARE_FAILURE
3141 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003142 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003143 */
3144psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3145 psa_algorithm_t alg);
3146
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003147/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003148 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003149 * Which inputs are required and in what order depends on the algorithm.
3150 * Refer to the documentation of each key derivation or key agreement
3151 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003152 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003153 * This function passes direct inputs. Some inputs must be passed as keys
3154 * using psa_key_derivation_input_key() instead of this function. Refer to
3155 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003156 *
3157 * \param[in,out] generator The generator object to use. It must
3158 * have been set up with
3159 * psa_key_derivation_setup() and must not
3160 * have produced any output yet.
3161 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003162 * \param[in] data Input data to use.
3163 * \param data_length Size of the \p data buffer in bytes.
3164 *
3165 * \retval #PSA_SUCCESS
3166 * Success.
3167 * \retval #PSA_ERROR_INVALID_ARGUMENT
3168 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003169 * \retval #PSA_ERROR_INVALID_ARGUMENT
3170 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003171 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3172 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3173 * \retval #PSA_ERROR_HARDWARE_FAILURE
3174 * \retval #PSA_ERROR_TAMPERING_DETECTED
3175 * \retval #PSA_ERROR_BAD_STATE
3176 * The value of \p step is not valid given the state of \p generator.
3177 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003178 * The library has not been previously initialized by psa_crypto_init().
3179 * It is implementation-dependent whether a failure to initialize
3180 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003181 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003182psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3183 psa_key_derivation_step_t step,
3184 const uint8_t *data,
3185 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003186
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003187/** Provide an input for key derivation in the form of a key.
3188 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003189 * Which inputs are required and in what order depends on the algorithm.
3190 * Refer to the documentation of each key derivation or key agreement
3191 * algorithm for information.
3192 *
3193 * This function passes key inputs. Some inputs must be passed as keys
3194 * of the appropriate type using this function, while others must be
3195 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3196 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003197 *
3198 * \param[in,out] generator The generator object to use. It must
3199 * have been set up with
3200 * psa_key_derivation_setup() and must not
3201 * have produced any output yet.
3202 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003203 * \param handle Handle to the key. It must have an
3204 * appropriate type for \p step and must
3205 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003206 *
3207 * \retval #PSA_SUCCESS
3208 * Success.
3209 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003210 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003211 * \retval #PSA_ERROR_NOT_PERMITTED
3212 * \retval #PSA_ERROR_INVALID_ARGUMENT
3213 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003214 * \retval #PSA_ERROR_INVALID_ARGUMENT
3215 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003216 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3217 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3218 * \retval #PSA_ERROR_HARDWARE_FAILURE
3219 * \retval #PSA_ERROR_TAMPERING_DETECTED
3220 * \retval #PSA_ERROR_BAD_STATE
3221 * The value of \p step is not valid given the state of \p generator.
3222 * \retval #PSA_ERROR_BAD_STATE
3223 * The library has not been previously initialized by psa_crypto_init().
3224 * It is implementation-dependent whether a failure to initialize
3225 * results in this error code.
3226 */
3227psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3228 psa_key_derivation_step_t step,
3229 psa_key_handle_t handle);
3230
Gilles Peskine969c5d62019-01-16 15:53:06 +01003231/** Perform a key agreement and use the shared secret as input to a key
3232 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003233 *
3234 * A key agreement algorithm takes two inputs: a private key \p private_key
3235 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003236 * The result of this function is passed as input to a key derivation.
3237 * The output of this key derivation can be extracted by reading from the
3238 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003239 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003240 * \param[in,out] generator The generator object to use. It must
3241 * have been set up with
3242 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003243 * key agreement and derivation algorithm
3244 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003245 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3246 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003247 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003248 * The generator must be ready for an
3249 * input of the type given by \p step.
3250 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003251 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003252 * \param[in] peer_key Public key of the peer. The peer key must be in the
3253 * same format that psa_import_key() accepts for the
3254 * public key type corresponding to the type of
3255 * private_key. That is, this function performs the
3256 * equivalent of
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003257 * #psa_import_key(`internal_public_key_handle`,
3258 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(`private_key_type`),
3259 * `peer_key`, `peer_key_length`) where
Jaeden Amero8afbff82019-01-14 16:56:20 +00003260 * `private_key_type` is the type of `private_key`.
3261 * For example, for EC keys, this means that peer_key
3262 * is interpreted as a point on the curve that the
3263 * private key is on. The standard formats for public
3264 * keys are documented in the documentation of
3265 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003266 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003267 *
3268 * \retval #PSA_SUCCESS
3269 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003270 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003271 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003272 * \retval #PSA_ERROR_NOT_PERMITTED
3273 * \retval #PSA_ERROR_INVALID_ARGUMENT
3274 * \c private_key is not compatible with \c alg,
3275 * or \p peer_key is not valid for \c alg or not compatible with
3276 * \c private_key.
3277 * \retval #PSA_ERROR_NOT_SUPPORTED
3278 * \c alg is not supported or is not a key derivation algorithm.
3279 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3280 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3281 * \retval #PSA_ERROR_HARDWARE_FAILURE
3282 * \retval #PSA_ERROR_TAMPERING_DETECTED
3283 */
3284psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003285 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003286 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003287 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003288 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003289
Gilles Peskine769c7a62019-01-18 16:42:29 +01003290/** Perform a key agreement and use the shared secret as input to a key
3291 * derivation.
3292 *
3293 * A key agreement algorithm takes two inputs: a private key \p private_key
3294 * a public key \p peer_key.
3295 *
3296 * \warning The raw result of a key agreement algorithm such as finite-field
3297 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3298 * not be used directly as key material. It should instead be passed as
3299 * input to a key derivation algorithm. To chain a key agreement with
3300 * a key derivation, use psa_key_agreement() and other functions from
3301 * the key derivation and generator interface.
3302 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003303 * \param alg The key agreement algorithm to compute
3304 * (\c PSA_ALG_XXX value such that
3305 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3306 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003307 * \param private_key Handle to the private key to use.
3308 * \param[in] peer_key Public key of the peer. It must be
3309 * in the same format that psa_import_key()
3310 * accepts. The standard formats for public
3311 * keys are documented in the documentation
3312 * of psa_export_public_key().
3313 * \param peer_key_length Size of \p peer_key in bytes.
3314 * \param[out] output Buffer where the decrypted message is to
3315 * be written.
3316 * \param output_size Size of the \c output buffer in bytes.
3317 * \param[out] output_length On success, the number of bytes
3318 * that make up the returned output.
3319 *
3320 * \retval #PSA_SUCCESS
3321 * Success.
3322 * \retval #PSA_ERROR_INVALID_HANDLE
3323 * \retval #PSA_ERROR_EMPTY_SLOT
3324 * \retval #PSA_ERROR_NOT_PERMITTED
3325 * \retval #PSA_ERROR_INVALID_ARGUMENT
3326 * \p alg is not a key agreement algorithm
3327 * \retval #PSA_ERROR_INVALID_ARGUMENT
3328 * \p private_key is not compatible with \p alg,
3329 * or \p peer_key is not valid for \p alg or not compatible with
3330 * \p private_key.
3331 * \retval #PSA_ERROR_NOT_SUPPORTED
3332 * \p alg is not a supported key agreement algorithm.
3333 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3334 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3335 * \retval #PSA_ERROR_HARDWARE_FAILURE
3336 * \retval #PSA_ERROR_TAMPERING_DETECTED
3337 */
3338psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3339 psa_key_handle_t private_key,
3340 const uint8_t *peer_key,
3341 size_t peer_key_length,
3342 uint8_t *output,
3343 size_t output_size,
3344 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003345
3346/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003347
3348/** \defgroup random Random generation
3349 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003350 */
3351
3352/**
3353 * \brief Generate random bytes.
3354 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003355 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003356 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003357 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003358 *
3359 * \note To generate a key, use psa_generate_key() instead.
3360 *
3361 * \param[out] output Output buffer for the generated data.
3362 * \param output_size Number of bytes to generate and output.
3363 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003364 * \retval #PSA_SUCCESS
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003365 * \retval #PSA_ERROR_NOT_SUPPORTED
3366 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003367 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003368 * \retval #PSA_ERROR_HARDWARE_FAILURE
3369 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003370 * \retval #PSA_ERROR_BAD_STATE
3371 * The library has not been previously initialized by psa_crypto_init().
3372 * It is implementation-dependent whether a failure to initialize
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003373 * results in this error code.
3374 */
3375psa_status_t psa_generate_random(uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003376 size_t output_size);
3377
3378/** Extra parameters for RSA key generation.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003379 *
Gilles Peskine28538492018-07-11 17:34:00 +02003380 * You may pass a pointer to a structure of this type as the \c extra
3381 * parameter to psa_generate_key().
3382 */
3383typedef struct {
3384 uint32_t e; /**< Public exponent value. Default: 65537. */
3385} psa_generate_key_extra_rsa;
3386
3387/**
itayzafrir90d8c7a2018-09-12 11:44:52 +03003388 * \brief Generate a key or key pair.
itayzafrir18617092018-09-16 12:22:41 +03003389 *
Gilles Peskine20628592019-04-19 19:29:50 +02003390 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003391 * \param[out] handle On success, a handle to the newly created key.
3392 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003393 * \param[in] extra Extra parameters for key generation. The
3394 * interpretation of this parameter depends on
Gilles Peskine20628592019-04-19 19:29:50 +02003395 * the key type \c type. All types support \c NULL to
3396 * use default parameters. Implementation that support
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003397 * the generation of vendor-specific key types
3398 * that allow extra parameters shall document
3399 * the format of these extra parameters and
3400 * the default values. For standard parameters,
3401 * the meaning of \p extra is as follows:
3402 * - For a symmetric key type (a type such
Gilles Peskine20628592019-04-19 19:29:50 +02003403 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\c type) is
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003404 * false), \p extra must be \c NULL.
3405 * - For an elliptic curve key type (a type
Gilles Peskine20628592019-04-19 19:29:50 +02003406 * such that #PSA_KEY_TYPE_IS_ECC(\c type) is
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003407 * false), \p extra must be \c NULL.
Gilles Peskine20628592019-04-19 19:29:50 +02003408 * - For an RSA key (\c type is
Gilles Peskine53d991e2018-07-12 01:14:59 +02003409 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3410 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003411 * specifying the public exponent. The
3412 * default public exponent used when \p extra
3413 * is \c NULL is 65537.
Gilles Peskine20628592019-04-19 19:29:50 +02003414 * - For an DSA key (\c type is
Jaeden Amero1308fb52019-01-11 13:50:43 +00003415 * #PSA_KEY_TYPE_DSA_KEYPAIR), \p extra is an
3416 * optional structure specifying the key domain
3417 * parameters. The key domain parameters can also be
3418 * provided by psa_set_key_domain_parameters(),
3419 * which documents the format of the structure.
Gilles Peskine20628592019-04-19 19:29:50 +02003420 * - For a DH key (\c type is
Jaeden Amero8851c402019-01-11 14:20:03 +00003421 * #PSA_KEY_TYPE_DH_KEYPAIR), the \p extra is an
3422 * optional structure specifying the key domain
3423 * parameters. The key domain parameters can also be
3424 * provided by psa_set_key_domain_parameters(),
3425 * which documents the format of the structure.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003426 * \param extra_size Size of the buffer that \p extra
3427 * points to, in bytes. Note that if \p extra is
3428 * \c NULL then \p extra_size must be zero.
3429 *
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003430 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003431 * Success.
3432 * If the key is persistent, the key material and the key's metadata
3433 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003434 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003435 * This is an attempt to create a persistent key, and there is
3436 * already a persistent key with the given identifier.
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003437 * \retval #PSA_ERROR_NOT_SUPPORTED
3438 * \retval #PSA_ERROR_INVALID_ARGUMENT
3439 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003440 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3441 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3442 * \retval #PSA_ERROR_HARDWARE_FAILURE
3443 * \retval #PSA_ERROR_TAMPERING_DETECTED
3444 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003445 * The library has not been previously initialized by psa_crypto_init().
3446 * It is implementation-dependent whether a failure to initialize
3447 * results in this error code.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003448 */
Gilles Peskine87a5e562019-04-17 12:28:25 +02003449psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
3450 psa_key_handle_t *handle,
Gilles Peskinee59236f2018-01-27 23:32:46 +01003451 const void *extra,
3452 size_t extra_size);
3453
3454/**@}*/
3455
3456#ifdef __cplusplus
3457}
3458#endif
3459
3460/* The file "crypto_sizes.h" contains definitions for size calculation
3461 * macros whose definitions are implementation-specific. */
3462#include "crypto_sizes.h"
3463
3464/* The file "crypto_struct.h" contains definitions for
3465 * implementation-specific structs that are declared above. */
3466#include "crypto_struct.h"
3467
3468/* The file "crypto_extra.h" contains vendor-specific definitions. This
3469 * can include vendor-defined algorithms, extra functions, etc. */
3470#include "crypto_extra.h"
3471
3472#endif /* PSA_CRYPTO_H */