blob: e0ac89cade87672e4a0a59a1569db612646c2e64 [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()
150 * - psa_set_key_usage_flags()
151 * - psa_set_key_algorithm()
152 * - psa_reset_key_attributes()
153 * - psa_get_key_attributes() on a key which has been created with
154 * attribute structure that itself did not contain auxiliary resources
155 *
156 * If the attribute structure has been modified with other functions,
157 * you must free auxiliary resources by calling psa_reset_key_attributes().
158 * The following functions may create auxiliary resouces:
159 * - psa_set_key_domain_parameters()
Gilles Peskine87a5e562019-04-17 12:28:25 +0200160 */
161typedef struct psa_key_attributes_s psa_key_attributes_t;
162
Gilles Peskine20628592019-04-19 19:29:50 +0200163/** Declare a key as persistent.
164 *
165 * This function does not access storage, it merely fills the attribute
166 * structure with given values. The persistent key will be written to
167 * storage when the attribute structure is passed to a key creation
168 * function such as psa_import_key(), psa_generate_key(),
169 * psa_generator_import_key() or psa_copy_key().
170 *
171 * This function overwrites any identifier and lifetime values
172 * previously set in \p attributes.
173 *
174 * This function may be declared as `static` (i.e. without external
175 * linkage). This function may be provided as a function-like macro,
176 * but in this case it must evaluate each of its arguments exactly once.
177 *
178 * \param[out] attributes The attribute structure to write to.
179 * \param id The persistent identifier for the key.
180 * \param lifetime The lifetime for the key.
181 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
182 * key will be volatile, and \p id is ignored.
183 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200184static void psa_make_key_persistent(psa_key_attributes_t *attributes,
185 psa_key_id_t id,
186 psa_key_lifetime_t lifetime);
187
Gilles Peskine20628592019-04-19 19:29:50 +0200188/** Retrieve the key identifier from key attributes.
189 *
190 * This function may be declared as `static` (i.e. without external
191 * linkage). This function may be provided as a function-like macro,
192 * but in this case it must evaluate its argument exactly once.
193 *
194 * \param[in] attributes The key attribute structure to query.
195 *
196 * \return The persistent identifier stored in the attribute structure.
197 * This value is unspecified if the attribute structure declares
198 * the key as volatile.
199 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200200static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
201
Gilles Peskine20628592019-04-19 19:29:50 +0200202/** Retrieve the lifetime from key attributes.
203 *
204 * This function may be declared as `static` (i.e. without external
205 * linkage). This function may be provided as a function-like macro,
206 * but in this case it must evaluate its argument exactly once.
207 *
208 * \param[in] attributes The key attribute structure to query.
209 *
210 * \return The lifetime value stored in the attribute structure.
211 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200212static psa_key_lifetime_t psa_get_key_lifetime(
213 const psa_key_attributes_t *attributes);
214
Gilles Peskine20628592019-04-19 19:29:50 +0200215/** Declare usage flags for a key.
216 *
217 * Usage flags are part of a key's usage policy. They encode what
218 * kind of operations are permitted on the key. For more details,
219 * refer to the documentation of the type #psa_key_usage_t.
220 *
221 * This function overwrites any usage flags
222 * previously set in \p attributes.
223 *
224 * This function may be declared as `static` (i.e. without external
225 * linkage). This function may be provided as a function-like macro,
226 * but in this case it must evaluate each of its arguments exactly once.
227 *
228 * \param[out] attributes The attribute structure to write to.
229 * \param usage_flags The usage flags to write.
230 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200231static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
232 psa_key_usage_t usage_flags);
233
Gilles Peskine20628592019-04-19 19:29:50 +0200234/** Retrieve the usage flags from key attributes.
235 *
236 * This function may be declared as `static` (i.e. without external
237 * linkage). This function may be provided as a function-like macro,
238 * but in this case it must evaluate its argument exactly once.
239 *
240 * \param[in] attributes The key attribute structure to query.
241 *
242 * \return The usage flags stored in the attribute structure.
243 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200244static psa_key_usage_t psa_get_key_usage_flags(
245 const psa_key_attributes_t *attributes);
246
Gilles Peskine20628592019-04-19 19:29:50 +0200247/** Declare the permitted algorithm policy for a key.
248 *
249 * The permitted algorithm policy of a key encodes which algorithm or
250 * algorithms are permitted to be used with this key.
251 *
252 * This function overwrites any algorithm policy
253 * previously set in \p attributes.
254 *
255 * This function may be declared as `static` (i.e. without external
256 * linkage). This function may be provided as a function-like macro,
257 * but in this case it must evaluate each of its arguments exactly once.
258 *
259 * \param[out] attributes The attribute structure to write to.
260 * \param alg The permitted algorithm policy to write.
261 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200262static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
263 psa_algorithm_t alg);
264
Gilles Peskine20628592019-04-19 19:29:50 +0200265/** Retrieve the algorithm policy from key attributes.
266 *
267 * This function may be declared as `static` (i.e. without external
268 * linkage). This function may be provided as a function-like macro,
269 * but in this case it must evaluate its argument exactly once.
270 *
271 * \param[in] attributes The key attribute structure to query.
272 *
273 * \return The algorithm stored in the attribute structure.
274 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200275static psa_algorithm_t psa_get_key_algorithm(
276 const psa_key_attributes_t *attributes);
277
Gilles Peskine20628592019-04-19 19:29:50 +0200278/** Declare the type of a key.
279 *
280 * If a type requires domain parameters, you must call
281 * psa_set_key_domain_parameters() instead of this function.
282 *
283 * This function overwrites any key type and domain parameters
284 * previously set in \p attributes.
285 *
286 * This function may be declared as `static` (i.e. without external
287 * linkage). This function may be provided as a function-like macro,
288 * but in this case it must evaluate each of its arguments exactly once.
289 *
290 * \param[out] attributes The attribute structure to write to.
291 * \param type The key type to write.
292 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200293static void psa_set_key_type(psa_key_attributes_t *attributes,
294 psa_key_type_t type);
295
Gilles Peskine20628592019-04-19 19:29:50 +0200296/** Retrieve the key type from key attributes.
297 *
298 * This function may be declared as `static` (i.e. without external
299 * linkage). This function may be provided as a function-like macro,
300 * but in this case it must evaluate its argument exactly once.
301 *
302 * \param[in] attributes The key attribute structure to query.
303 *
304 * \return The key type stored in the attribute structure.
305 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200306static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
307
Gilles Peskine20628592019-04-19 19:29:50 +0200308/** Retrieve the key size from key attributes.
309 *
310 * This function may be declared as `static` (i.e. without external
311 * linkage). This function may be provided as a function-like macro,
312 * but in this case it must evaluate its argument exactly once.
313 *
314 * \param[in] attributes The key attribute structure to query.
315 *
316 * \return The key size stored in the attribute structure, in bits.
317 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200318static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
319
Gilles Peskine20628592019-04-19 19:29:50 +0200320/** Retrieve the attributes of a key.
321 *
322 * This function first resets the attribute structure as with
323 * psa_reset_key_attributes(). It then populates the attribute
324 * structure with the attributes of the given key.
325 *
326 * The attributes that were set when creating the key are reported in a
327 * semantically equivalent manner, not necessarily with the same
328 * numerical value or the same bit pattern. In this specification,
329 * all key types, usage flags, algorithms and lifetime values are
330 * equivalent only if they have the same numerical encoding, but this
331 * property may not hold in future versions of this specification or
332 * for implementation-specific values.
333 *
334 * In addition to the attributes that were set when creating the key,
335 * this function reports the following data:
336 * - The key size in bits, which can be retrieved with
337 * psa_get_key_bits().
338 *
339 * \param[in] handle Handle to the key to query.
340 * \param[in,out] attributes On success, the attributes of the key.
341 * On failure, equivalent to a
342 * freshly-initialized structure.
343 *
344 * \retval #PSA_SUCCESS
345 * \retval #PSA_ERROR_INVALID_HANDLE
346 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
347 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
348 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200349psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
350 psa_key_attributes_t *attributes);
351
Gilles Peskine20628592019-04-19 19:29:50 +0200352/** Reset a key attribute structure to a freshly initialized state.
353 *
354 * You must initialize the attribute structure as described in the
355 * documentation of the type #psa_key_attributes_t before calling this
356 * function. Once the structure has been initialized, you may call this
357 * function at any time.
358 *
359 * This function frees any auxiliary resources that the structure
360 * may contain.
361 *
362 * \param[in,out] attributes The attribute structure to reset.
363 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200364void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200365
Gilles Peskine87a5e562019-04-17 12:28:25 +0200366/**@}*/
367
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100368/** \defgroup policy Key policies
369 * @{
370 */
371
372/** The type of the key policy data structure.
373 *
374 * Before calling any function on a key policy, the application must initialize
375 * it by any of the following means:
376 * - Set the structure to all-bits-zero, for example:
377 * \code
378 * psa_key_policy_t policy;
379 * memset(&policy, 0, sizeof(policy));
380 * \endcode
381 * - Initialize the structure to logical zero values, for example:
382 * \code
383 * psa_key_policy_t policy = {0};
384 * \endcode
385 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
386 * for example:
387 * \code
388 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
389 * \endcode
390 * - Assign the result of the function psa_key_policy_init()
391 * to the structure, for example:
392 * \code
393 * psa_key_policy_t policy;
394 * policy = psa_key_policy_init();
395 * \endcode
396 *
397 * This is an implementation-defined \c struct. Applications should not
398 * make any assumptions about the content of this structure except
399 * as directed by the documentation of a specific implementation. */
400typedef struct psa_key_policy_s psa_key_policy_t;
401
402/** \def PSA_KEY_POLICY_INIT
403 *
404 * This macro returns a suitable initializer for a key policy object of type
405 * #psa_key_policy_t.
406 */
407#ifdef __DOXYGEN_ONLY__
408/* This is an example definition for documentation purposes.
409 * Implementations should define a suitable value in `crypto_struct.h`.
410 */
411#define PSA_KEY_POLICY_INIT {0}
412#endif
413
414/** Return an initial value for a key policy that forbids all usage of the key.
415 */
416static psa_key_policy_t psa_key_policy_init(void);
417
418/** \brief Set the standard fields of a policy structure.
419 *
420 * Note that this function does not make any consistency check of the
421 * parameters. The values are only checked when applying the policy to
422 * a key slot with psa_set_key_policy().
423 *
424 * \param[in,out] policy The key policy to modify. It must have been
425 * initialized as per the documentation for
426 * #psa_key_policy_t.
427 * \param usage The permitted uses for the key.
428 * \param alg The algorithm that the key may be used for.
429 */
430void psa_key_policy_set_usage(psa_key_policy_t *policy,
431 psa_key_usage_t usage,
432 psa_algorithm_t alg);
433
434/** \brief Retrieve the usage field of a policy structure.
435 *
436 * \param[in] policy The policy object to query.
437 *
438 * \return The permitted uses for a key with this policy.
439 */
440psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
441
442/** \brief Retrieve the algorithm field of a policy structure.
443 *
444 * \param[in] policy The policy object to query.
445 *
446 * \return The permitted algorithm for a key with this policy.
447 */
448psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
449
450/** \brief Set the usage policy on a key slot.
451 *
452 * This function must be called on an empty key slot, before importing,
453 * generating or creating a key in the slot. Changing the policy of an
454 * existing key is not permitted.
455 *
456 * Implementations may set restrictions on supported key policies
457 * depending on the key type and the key slot.
458 *
459 * \param handle Handle to the key whose policy is to be changed.
460 * \param[in] policy The policy object to query.
461 *
462 * \retval #PSA_SUCCESS
463 * Success.
464 * If the key is persistent, it is implementation-defined whether
465 * the policy has been saved to persistent storage. Implementations
466 * may defer saving the policy until the key material is created.
467 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200468 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100469 * \retval #PSA_ERROR_NOT_SUPPORTED
470 * \retval #PSA_ERROR_INVALID_ARGUMENT
471 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
472 * \retval #PSA_ERROR_HARDWARE_FAILURE
473 * \retval #PSA_ERROR_TAMPERING_DETECTED
474 * \retval #PSA_ERROR_BAD_STATE
475 * The library has not been previously initialized by psa_crypto_init().
476 * It is implementation-dependent whether a failure to initialize
477 * results in this error code.
478 */
479psa_status_t psa_set_key_policy(psa_key_handle_t handle,
480 const psa_key_policy_t *policy);
481
482/** \brief Get the usage policy for a key slot.
483 *
484 * \param handle Handle to the key slot whose policy is being queried.
485 * \param[out] policy On success, the key's policy.
486 *
487 * \retval #PSA_SUCCESS
488 * \retval #PSA_ERROR_INVALID_HANDLE
489 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
490 * \retval #PSA_ERROR_HARDWARE_FAILURE
491 * \retval #PSA_ERROR_TAMPERING_DETECTED
492 * \retval #PSA_ERROR_BAD_STATE
493 * The library has not been previously initialized by psa_crypto_init().
494 * It is implementation-dependent whether a failure to initialize
495 * results in this error code.
496 */
497psa_status_t psa_get_key_policy(psa_key_handle_t handle,
498 psa_key_policy_t *policy);
499
500/**@}*/
501
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100502/** \defgroup key_management Key management
503 * @{
504 */
505
Gilles Peskinef535eb22018-11-30 14:08:36 +0100506/** Allocate a key slot for a transient key, i.e. a key which is only stored
507 * in volatile memory.
508 *
509 * The allocated key slot and its handle remain valid until the
510 * application calls psa_close_key() or psa_destroy_key() or until the
511 * application terminates.
512 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100513 * \param[out] handle On success, a handle to a volatile key slot.
514 *
515 * \retval #PSA_SUCCESS
516 * Success. The application can now use the value of `*handle`
517 * to access the newly allocated key slot.
518 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
519 * There was not enough memory, or the maximum number of key slots
520 * has been reached.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100521 */
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100522psa_status_t psa_allocate_key(psa_key_handle_t *handle);
Gilles Peskinef535eb22018-11-30 14:08:36 +0100523
524/** Open a handle to an existing persistent key.
525 *
526 * Open a handle to a key which was previously created with psa_create_key().
527 *
528 * \param lifetime The lifetime of the key. This designates a storage
529 * area where the key material is stored. This must not
530 * be #PSA_KEY_LIFETIME_VOLATILE.
531 * \param id The persistent identifier of the key.
532 * \param[out] handle On success, a handle to a key slot which contains
533 * the data and metadata loaded from the specified
534 * persistent location.
535 *
536 * \retval #PSA_SUCCESS
537 * Success. The application can now use the value of `*handle`
538 * to access the newly allocated key slot.
539 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200540 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100541 * \retval #PSA_ERROR_INVALID_ARGUMENT
542 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
543 * \retval #PSA_ERROR_INVALID_ARGUMENT
544 * \p id is invalid for the specified lifetime.
545 * \retval #PSA_ERROR_NOT_SUPPORTED
546 * \p lifetime is not supported.
547 * \retval #PSA_ERROR_NOT_PERMITTED
548 * The specified key exists, but the application does not have the
549 * permission to access it. Note that this specification does not
550 * define any way to create such a key, but it may be possible
551 * through implementation-specific means.
552 */
553psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
554 psa_key_id_t id,
555 psa_key_handle_t *handle);
556
Gilles Peskinef535eb22018-11-30 14:08:36 +0100557/** Close a key handle.
558 *
559 * If the handle designates a volatile key, destroy the key material and
560 * free all associated resources, just like psa_destroy_key().
561 *
562 * If the handle designates a persistent key, free all resources associated
563 * with the key in volatile memory. The key slot in persistent storage is
564 * not affected and can be opened again later with psa_open_key().
565 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100566 * If the key is currently in use in a multipart operation,
567 * the multipart operation is aborted.
568 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100569 * \param handle The key handle to close.
570 *
571 * \retval #PSA_SUCCESS
572 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100573 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100574 */
575psa_status_t psa_close_key(psa_key_handle_t handle);
576
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100577/**@}*/
578
579/** \defgroup import_export Key import and export
580 * @{
581 */
582
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100583/**
584 * \brief Import a key in binary format.
585 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100586 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100587 * documentation of psa_export_public_key() for the format of public keys
588 * and to the documentation of psa_export_key() for the format for
589 * other key types.
590 *
591 * This specification supports a single format for each key type.
592 * Implementations may support other formats as long as the standard
593 * format is supported. Implementations that support other formats
594 * should ensure that the formats are clearly unambiguous so as to
595 * minimize the risk that an invalid input is accidentally interpreted
596 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100597 *
Gilles Peskine20628592019-04-19 19:29:50 +0200598 * \param[in] attributes The attributes for the new key.
599 * The key size field in \p attributes is
600 * ignored; the actual key size is determined
601 * from the \p data buffer.
602 * \param[out] handle On success, a handle to the newly created key.
603 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100604 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine20628592019-04-19 19:29:50 +0200605 * buffer is interpreted according to the type and,
606 * if applicable, domain parameters declared in
607 * \p attributes.
608 * All implementations must support at least the format
609 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100610 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200611 * the chosen type. Implementations may allow other
612 * formats, but should be conservative: implementations
613 * should err on the side of rejecting content if it
614 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200615 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100616 *
Gilles Peskine28538492018-07-11 17:34:00 +0200617 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100618 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100619 * If the key is persistent, the key material and the key's metadata
620 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200621 * \retval #PSA_ERROR_ALREADY_EXISTS
622 * This is an attempt to create a persistent key, and there is
623 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200624 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200625 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200626 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200627 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200628 * The key attributes, as a whole, are invalid,
Gilles Peskine308b91d2018-02-08 09:47:44 +0100629 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200630 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
631 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
632 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100633 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200634 * \retval #PSA_ERROR_HARDWARE_FAILURE
635 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300636 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300637 * The library has not been previously initialized by psa_crypto_init().
638 * It is implementation-dependent whether a failure to initialize
639 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100640 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200641psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
642 psa_key_handle_t *handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100643 const uint8_t *data,
644 size_t data_length);
645
646/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100647 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200648 *
649 * This function destroys the content of the key slot from both volatile
650 * memory and, if applicable, non-volatile storage. Implementations shall
651 * make a best effort to ensure that any previous content of the slot is
652 * unrecoverable.
653 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100654 * This function also erases any metadata such as policies and frees all
655 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200656 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100657 * If the key is currently in use in a multipart operation,
658 * the multipart operation is aborted.
659 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100660 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661 *
Gilles Peskine28538492018-07-11 17:34:00 +0200662 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200663 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200664 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200665 * The slot holds content and cannot be erased because it is
666 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100667 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200668 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200669 * There was an failure in communication with the cryptoprocessor.
670 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200671 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200672 * The storage is corrupted. Implementations shall make a best effort
673 * to erase key material even in this stage, however applications
674 * should be aware that it may be impossible to guarantee that the
675 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200676 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200677 * An unexpected condition which is not a storage corruption or
678 * a communication failure occurred. The cryptoprocessor may have
679 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300680 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300681 * The library has not been previously initialized by psa_crypto_init().
682 * It is implementation-dependent whether a failure to initialize
683 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100685psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686
687/**
688 * \brief Get basic metadata about a key.
689 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100690 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200691 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100692 * This may be a null pointer, in which case the key type
693 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200694 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100695 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100696 * is not written.
697 *
Gilles Peskine28538492018-07-11 17:34:00 +0200698 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100699 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200700 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineae32aac2018-11-30 14:39:32 +0100701 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200702 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
703 * \retval #PSA_ERROR_HARDWARE_FAILURE
704 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300705 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300706 * The library has not been previously initialized by psa_crypto_init().
707 * It is implementation-dependent whether a failure to initialize
708 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100709 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100710psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100711 psa_key_type_t *type,
712 size_t *bits);
713
714/**
Jaeden Amero283dfd12019-01-11 12:06:22 +0000715 * \brief Set domain parameters for a key.
716 *
717 * Some key types require additional domain parameters to be set before import
718 * or generation of the key. The domain parameters can be set with this
719 * function or, for key generation, through the \c extra parameter of
720 * psa_generate_key().
721 *
722 * The format for the required domain parameters varies by the key type.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000723 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
724 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
725 * ```
726 * Dss-Parms ::= SEQUENCE {
727 * p INTEGER,
728 * q INTEGER,
729 * g INTEGER
730 * }
731 * ```
Jaeden Amero8851c402019-01-11 14:20:03 +0000732 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY), the
733 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
734 * ```
735 * DomainParameters ::= SEQUENCE {
736 * p INTEGER, -- odd prime, p=jq +1
737 * g INTEGER, -- generator, g
738 * q INTEGER, -- factor of p-1
739 * j INTEGER OPTIONAL, -- subgroup factor
740 * validationParms ValidationParms OPTIONAL
741 * }
742 * ValidationParms ::= SEQUENCE {
743 * seed BIT STRING,
744 * pgenCounter INTEGER
745 * }
746 * ```
Jaeden Amero283dfd12019-01-11 12:06:22 +0000747 *
Gilles Peskine3a74e002019-01-18 17:11:25 +0100748 * \param handle Handle to the slot where the key will be stored.
749 * This must be a valid slot for a key of the chosen
750 * type: it must have been obtained by calling
751 * psa_allocate_key() or psa_create_key() with the
752 * correct \p type and with a maximum size that is
753 * compatible with \p data. It must not contain
754 * key material yet.
755 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). When
756 * subsequently creating key material into \p handle,
757 * the type must be compatible.
Jaeden Amero283dfd12019-01-11 12:06:22 +0000758 * \param[in] data Buffer containing the key domain parameters. The content
759 * of this buffer is interpreted according to \p type. of
760 * psa_export_key() or psa_export_public_key() for the
761 * chosen type.
762 * \param data_length Size of the \p data buffer in bytes.
763 *
764 * \retval #PSA_SUCCESS
765 * \retval #PSA_ERROR_INVALID_HANDLE
766 * \retval #PSA_ERROR_OCCUPIED_SLOT
767 * There is already a key in the specified slot.
768 * \retval #PSA_ERROR_INVALID_ARGUMENT
769 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
770 * \retval #PSA_ERROR_HARDWARE_FAILURE
771 * \retval #PSA_ERROR_TAMPERING_DETECTED
772 * \retval #PSA_ERROR_BAD_STATE
773 * The library has not been previously initialized by psa_crypto_init().
774 * It is implementation-dependent whether a failure to initialize
775 * results in this error code.
776 */
777psa_status_t psa_set_key_domain_parameters(psa_key_handle_t handle,
Gilles Peskine3a74e002019-01-18 17:11:25 +0100778 psa_key_type_t type,
Jaeden Amero283dfd12019-01-11 12:06:22 +0000779 const uint8_t *data,
780 size_t data_length);
781
782/**
783 * \brief Get domain parameters for a key.
784 *
785 * Get the domain parameters for a key with this function, if any. The format
786 * of the domain parameters written to \p data is specified in the
787 * documentation for psa_set_key_domain_parameters().
788 *
789 * \param handle Handle to the key to get domain parameters from.
790 * \param[out] data On success, the key domain parameters.
791 * \param data_size Size of the \p data buffer in bytes.
792 * \param[out] data_length On success, the number of bytes
793 * that make up the key domain parameters data.
794 *
795 * \retval #PSA_SUCCESS
796 * \retval #PSA_ERROR_INVALID_HANDLE
797 * \retval #PSA_ERROR_EMPTY_SLOT
798 * There is no key in the specified slot.
799 * \retval #PSA_ERROR_INVALID_ARGUMENT
800 * \retval #PSA_ERROR_NOT_SUPPORTED
801 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
802 * \retval #PSA_ERROR_HARDWARE_FAILURE
803 * \retval #PSA_ERROR_TAMPERING_DETECTED
804 * \retval #PSA_ERROR_BAD_STATE
805 * The library has not been previously initialized by psa_crypto_init().
806 * It is implementation-dependent whether a failure to initialize
807 * results in this error code.
808 */
809psa_status_t psa_get_key_domain_parameters(psa_key_handle_t handle,
810 uint8_t *data,
811 size_t data_size,
812 size_t *data_length);
813
814/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100815 * \brief Export a key in binary format.
816 *
817 * The output of this function can be passed to psa_import_key() to
818 * create an equivalent object.
819 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100820 * If the implementation of psa_import_key() supports other formats
821 * beyond the format specified here, the output from psa_export_key()
822 * must use the representation specified here, not the original
823 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100824 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100825 * For standard key types, the output format is as follows:
826 *
827 * - For symmetric keys (including MAC keys), the format is the
828 * raw bytes of the key.
829 * - For DES, the key data consists of 8 bytes. The parity bits must be
830 * correct.
831 * - For Triple-DES, the format is the concatenation of the
832 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100833 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200834 * is the non-encrypted DER encoding of the representation defined by
835 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
836 * ```
837 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200838 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200839 * modulus INTEGER, -- n
840 * publicExponent INTEGER, -- e
841 * privateExponent INTEGER, -- d
842 * prime1 INTEGER, -- p
843 * prime2 INTEGER, -- q
844 * exponent1 INTEGER, -- d mod (p-1)
845 * exponent2 INTEGER, -- d mod (q-1)
846 * coefficient INTEGER, -- (inverse of q) mod p
847 * }
848 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000849 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
850 * representation of the private key `x` as a big-endian byte string. The
851 * length of the byte string is the private key size in bytes (leading zeroes
852 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200853 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100854 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100855 * a representation of the private value as a `ceiling(m/8)`-byte string
856 * where `m` is the bit size associated with the curve, i.e. the bit size
857 * of the order of the curve's coordinate field. This byte string is
858 * in little-endian order for Montgomery curves (curve types
859 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
860 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
861 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100862 * This is the content of the `privateKey` field of the `ECPrivateKey`
863 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000864 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
865 * format is the representation of the private key `x` as a big-endian byte
866 * string. The length of the byte string is the private key size in bytes
867 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200868 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
869 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100870 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100871 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200872 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200873 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200874 * \param[out] data_length On success, the number of bytes
875 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100876 *
Gilles Peskine28538492018-07-11 17:34:00 +0200877 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100878 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200879 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200880 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100881 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200882 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
883 * The size of the \p data buffer is too small. You can determine a
884 * sufficient buffer size by calling
885 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
886 * where \c type is the key type
887 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200888 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
889 * \retval #PSA_ERROR_HARDWARE_FAILURE
890 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300891 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300892 * The library has not been previously initialized by psa_crypto_init().
893 * It is implementation-dependent whether a failure to initialize
894 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100895 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100896psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100897 uint8_t *data,
898 size_t data_size,
899 size_t *data_length);
900
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100901/**
902 * \brief Export a public key or the public part of a key pair in binary format.
903 *
904 * The output of this function can be passed to psa_import_key() to
905 * create an object that is equivalent to the public key.
906 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000907 * This specification supports a single format for each key type.
908 * Implementations may support other formats as long as the standard
909 * format is supported. Implementations that support other formats
910 * should ensure that the formats are clearly unambiguous so as to
911 * minimize the risk that an invalid input is accidentally interpreted
912 * according to a different format.
913 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000914 * For standard key types, the output format is as follows:
915 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
916 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
917 * ```
918 * RSAPublicKey ::= SEQUENCE {
919 * modulus INTEGER, -- n
920 * publicExponent INTEGER } -- e
921 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000922 * - For elliptic curve public keys (key types for which
923 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
924 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
925 * Let `m` be the bit size associated with the curve, i.e. the bit size of
926 * `q` for a curve over `F_q`. The representation consists of:
927 * - The byte 0x04;
928 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
929 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000930 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
931 * representation of the public key `y = g^x mod p` as a big-endian byte
932 * string. The length of the byte string is the length of the base prime `p`
933 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000934 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
935 * the format is the representation of the public key `y = g^x mod p` as a
936 * big-endian byte string. The length of the byte string is the length of the
937 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100938 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100939 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200940 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200941 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200942 * \param[out] data_length On success, the number of bytes
943 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100944 *
Gilles Peskine28538492018-07-11 17:34:00 +0200945 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100946 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200947 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200948 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200949 * The key is neither a public key nor a key pair.
950 * \retval #PSA_ERROR_NOT_SUPPORTED
951 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
952 * The size of the \p data buffer is too small. You can determine a
953 * sufficient buffer size by calling
954 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
955 * where \c type is the key type
956 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200957 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
958 * \retval #PSA_ERROR_HARDWARE_FAILURE
959 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300960 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300961 * The library has not been previously initialized by psa_crypto_init().
962 * It is implementation-dependent whether a failure to initialize
963 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100964 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100965psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100966 uint8_t *data,
967 size_t data_size,
968 size_t *data_length);
969
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100970/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100971 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100972 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000973 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100974 * This function is primarily useful to copy a key from one location
975 * to another, since it populates a key using the material from
976 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200977 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100978 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100979 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100980 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100981 *
Gilles Peskine20628592019-04-19 19:29:50 +0200982 * The resulting key may only be used in a way that conforms to
983 * both the policy of the original key and the policy specified in
984 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100985 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200986 * usage flags on the source policy and the usage flags in \p attributes.
987 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100988 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200989 * - If either of the policies allows an algorithm and the other policy
990 * allows a wildcard-based algorithm policy that includes this algorithm,
991 * the resulting key allows the same algorithm.
992 * - If the policies do not allow any algorithm in common, this function
993 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200994 *
Gilles Peskine20628592019-04-19 19:29:50 +0200995 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100996 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200997 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100998 * \param source_handle The key to copy. It must be a handle to an
999 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +02001000 * \param[in] attributes The attributes for the new key.
1001 * They are used as follows:
1002 * - The key type, key size and domain parameters
1003 * are ignored. This information is copied
1004 * from the source key.
1005 * - The key location (the lifetime and, for
1006 * persistent keys, the key identifier) is
1007 * used directly.
1008 * - The policy constraints (usage flags and
1009 * algorithm policy) are combined from
1010 * the source key and \p attributes so that
1011 * both sets of restrictions apply, as
1012 * described in the documentation of this function.
1013 * \param[out] target_handle On success, a handle to the newly created key.
1014 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001015 *
1016 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +01001017 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +02001018 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +02001019 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02001020 * This is an attempt to create a persistent key, and there is
1021 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001022 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +02001023 * The lifetime or identifier in \p attributes are invalid.
1024 * \retval #PSA_ERROR_INVALID_ARGUMENT
1025 * The policy constraints on the source and specified in
1026 * \p attributes are incompatible.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001027 * \retval #PSA_ERROR_NOT_PERMITTED
1028 * The source key is not exportable and its lifetime does not
1029 * allow copying it to the target's lifetime.
1030 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1031 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001032 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1033 * \retval #PSA_ERROR_HARDWARE_FAILURE
1034 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001035 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001036psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +02001037 const psa_key_attributes_t *attributes,
1038 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +01001039
1040/**@}*/
1041
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001042/** \defgroup hash Message digests
1043 * @{
1044 */
1045
Gilles Peskine69647a42019-01-14 20:18:12 +01001046/** Calculate the hash (digest) of a message.
1047 *
1048 * \note To verify the hash of a message against an
1049 * expected value, use psa_hash_compare() instead.
1050 *
1051 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1052 * such that #PSA_ALG_IS_HASH(\p alg) is true).
1053 * \param[in] input Buffer containing the message to hash.
1054 * \param input_length Size of the \p input buffer in bytes.
1055 * \param[out] hash Buffer where the hash is to be written.
1056 * \param hash_size Size of the \p hash buffer in bytes.
1057 * \param[out] hash_length On success, the number of bytes
1058 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +01001059 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +01001060 *
1061 * \retval #PSA_SUCCESS
1062 * Success.
1063 * \retval #PSA_ERROR_NOT_SUPPORTED
1064 * \p alg is not supported or is not a hash algorithm.
1065 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1066 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1067 * \retval #PSA_ERROR_HARDWARE_FAILURE
1068 * \retval #PSA_ERROR_TAMPERING_DETECTED
1069 */
1070psa_status_t psa_hash_compute(psa_algorithm_t alg,
1071 const uint8_t *input,
1072 size_t input_length,
1073 uint8_t *hash,
1074 size_t hash_size,
1075 size_t *hash_length);
1076
1077/** Calculate the hash (digest) of a message and compare it with a
1078 * reference value.
1079 *
1080 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1081 * such that #PSA_ALG_IS_HASH(\p alg) is true).
1082 * \param[in] input Buffer containing the message to hash.
1083 * \param input_length Size of the \p input buffer in bytes.
1084 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +01001085 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +01001086 *
1087 * \retval #PSA_SUCCESS
1088 * The expected hash is identical to the actual hash of the input.
1089 * \retval #PSA_ERROR_INVALID_SIGNATURE
1090 * The hash of the message was calculated successfully, but it
1091 * differs from the expected hash.
1092 * \retval #PSA_ERROR_NOT_SUPPORTED
1093 * \p alg is not supported or is not a hash algorithm.
1094 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1095 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1096 * \retval #PSA_ERROR_HARDWARE_FAILURE
1097 * \retval #PSA_ERROR_TAMPERING_DETECTED
1098 */
1099psa_status_t psa_hash_compare(psa_algorithm_t alg,
1100 const uint8_t *input,
1101 size_t input_length,
1102 const uint8_t *hash,
1103 const size_t hash_length);
1104
Gilles Peskine308b91d2018-02-08 09:47:44 +01001105/** The type of the state data structure for multipart hash operations.
1106 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001107 * Before calling any function on a hash operation object, the application must
1108 * initialize it by any of the following means:
1109 * - Set the structure to all-bits-zero, for example:
1110 * \code
1111 * psa_hash_operation_t operation;
1112 * memset(&operation, 0, sizeof(operation));
1113 * \endcode
1114 * - Initialize the structure to logical zero values, for example:
1115 * \code
1116 * psa_hash_operation_t operation = {0};
1117 * \endcode
1118 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
1119 * for example:
1120 * \code
1121 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
1122 * \endcode
1123 * - Assign the result of the function psa_hash_operation_init()
1124 * to the structure, for example:
1125 * \code
1126 * psa_hash_operation_t operation;
1127 * operation = psa_hash_operation_init();
1128 * \endcode
1129 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001130 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001131 * make any assumptions about the content of this structure except
1132 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001133typedef struct psa_hash_operation_s psa_hash_operation_t;
1134
Jaeden Amero6a25b412019-01-04 11:47:44 +00001135/** \def PSA_HASH_OPERATION_INIT
1136 *
1137 * This macro returns a suitable initializer for a hash operation object
1138 * of type #psa_hash_operation_t.
1139 */
1140#ifdef __DOXYGEN_ONLY__
1141/* This is an example definition for documentation purposes.
1142 * Implementations should define a suitable value in `crypto_struct.h`.
1143 */
1144#define PSA_HASH_OPERATION_INIT {0}
1145#endif
1146
1147/** Return an initial value for a hash operation object.
1148 */
1149static psa_hash_operation_t psa_hash_operation_init(void);
1150
Gilles Peskinef45adda2019-01-14 18:29:18 +01001151/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001152 *
1153 * The sequence of operations to calculate a hash (message digest)
1154 * is as follows:
1155 * -# Allocate an operation object which will be passed to all the functions
1156 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +00001157 * -# Initialize the operation object with one of the methods described in the
1158 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001159 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001160 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001161 * of the message each time. The hash that is calculated is the hash
1162 * of the concatenation of these messages in order.
1163 * -# To calculate the hash, call psa_hash_finish().
1164 * To compare the hash with an expected value, call psa_hash_verify().
1165 *
1166 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +00001167 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001168 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001169 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001170 * eventually terminate the operation. The following events terminate an
1171 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001172 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001173 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001174 *
Jaeden Amero6a25b412019-01-04 11:47:44 +00001175 * \param[in,out] operation The operation object to set up. It must have
1176 * been initialized as per the documentation for
1177 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001178 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1179 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001180 *
Gilles Peskine28538492018-07-11 17:34:00 +02001181 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001182 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001183 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001184 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001185 * \retval #PSA_ERROR_BAD_STATE
1186 * The operation state is not valid (already set up and not
1187 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001188 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1189 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1190 * \retval #PSA_ERROR_HARDWARE_FAILURE
1191 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001192 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001193psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001194 psa_algorithm_t alg);
1195
Gilles Peskine308b91d2018-02-08 09:47:44 +01001196/** Add a message fragment to a multipart hash operation.
1197 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001198 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001199 *
1200 * If this function returns an error status, the operation becomes inactive.
1201 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001202 * \param[in,out] operation Active hash operation.
1203 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001204 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001205 *
Gilles Peskine28538492018-07-11 17:34:00 +02001206 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001207 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001208 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001209 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001210 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1211 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1212 * \retval #PSA_ERROR_HARDWARE_FAILURE
1213 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001214 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001215psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1216 const uint8_t *input,
1217 size_t input_length);
1218
Gilles Peskine308b91d2018-02-08 09:47:44 +01001219/** Finish the calculation of the hash of a message.
1220 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001221 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001222 * This function calculates the hash of the message formed by concatenating
1223 * the inputs passed to preceding calls to psa_hash_update().
1224 *
1225 * When this function returns, the operation becomes inactive.
1226 *
1227 * \warning Applications should not call this function if they expect
1228 * a specific value for the hash. Call psa_hash_verify() instead.
1229 * Beware that comparing integrity or authenticity data such as
1230 * hash values with a function such as \c memcmp is risky
1231 * because the time taken by the comparison may leak information
1232 * about the hashed data which could allow an attacker to guess
1233 * a valid hash and thereby bypass security controls.
1234 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001235 * \param[in,out] operation Active hash operation.
1236 * \param[out] hash Buffer where the hash is to be written.
1237 * \param hash_size Size of the \p hash buffer in bytes.
1238 * \param[out] hash_length On success, the number of bytes
1239 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001240 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001241 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001242 *
Gilles Peskine28538492018-07-11 17:34:00 +02001243 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001244 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001245 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001246 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001247 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001248 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001249 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001250 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001251 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1252 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1253 * \retval #PSA_ERROR_HARDWARE_FAILURE
1254 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001255 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001256psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1257 uint8_t *hash,
1258 size_t hash_size,
1259 size_t *hash_length);
1260
Gilles Peskine308b91d2018-02-08 09:47:44 +01001261/** Finish the calculation of the hash of a message and compare it with
1262 * an expected value.
1263 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001264 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001265 * This function calculates the hash of the message formed by concatenating
1266 * the inputs passed to preceding calls to psa_hash_update(). It then
1267 * compares the calculated hash with the expected hash passed as a
1268 * parameter to this function.
1269 *
1270 * When this function returns, the operation becomes inactive.
1271 *
Gilles Peskine19067982018-03-20 17:54:53 +01001272 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001273 * comparison between the actual hash and the expected hash is performed
1274 * in constant time.
1275 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001276 * \param[in,out] operation Active hash operation.
1277 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001278 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001279 *
Gilles Peskine28538492018-07-11 17:34:00 +02001280 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001281 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001282 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001283 * The hash of the message was calculated successfully, but it
1284 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001285 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001286 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001287 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1288 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1289 * \retval #PSA_ERROR_HARDWARE_FAILURE
1290 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001291 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001292psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1293 const uint8_t *hash,
1294 size_t hash_length);
1295
Gilles Peskine308b91d2018-02-08 09:47:44 +01001296/** Abort a hash operation.
1297 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001298 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001299 * \p operation structure itself. Once aborted, the operation object
1300 * can be reused for another operation by calling
1301 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001302 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001303 * You may call this function any time after the operation object has
1304 * been initialized by any of the following methods:
1305 * - A call to psa_hash_setup(), whether it succeeds or not.
1306 * - Initializing the \c struct to all-bits-zero.
1307 * - Initializing the \c struct to logical zeros, e.g.
1308 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001309 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001310 * In particular, calling psa_hash_abort() after the operation has been
1311 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1312 * psa_hash_verify() is safe and has no effect.
1313 *
1314 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001315 *
Gilles Peskine28538492018-07-11 17:34:00 +02001316 * \retval #PSA_SUCCESS
1317 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001318 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001319 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1320 * \retval #PSA_ERROR_HARDWARE_FAILURE
1321 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001322 */
1323psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001324
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001325/** Clone a hash operation.
1326 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001327 * This function copies the state of an ongoing hash operation to
1328 * a new operation object. In other words, this function is equivalent
1329 * to calling psa_hash_setup() on \p target_operation with the same
1330 * algorithm that \p source_operation was set up for, then
1331 * psa_hash_update() on \p target_operation with the same input that
1332 * that was passed to \p source_operation. After this function returns, the
1333 * two objects are independent, i.e. subsequent calls involving one of
1334 * the objects do not affect the other object.
1335 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001336 * \param[in] source_operation The active hash operation to clone.
1337 * \param[in,out] target_operation The operation object to set up.
1338 * It must be initialized but not active.
1339 *
1340 * \retval #PSA_SUCCESS
1341 * \retval #PSA_ERROR_BAD_STATE
1342 * \p source_operation is not an active hash operation.
1343 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001344 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001345 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1346 * \retval #PSA_ERROR_HARDWARE_FAILURE
1347 * \retval #PSA_ERROR_TAMPERING_DETECTED
1348 */
1349psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1350 psa_hash_operation_t *target_operation);
1351
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001352/**@}*/
1353
Gilles Peskine8c9def32018-02-08 10:02:12 +01001354/** \defgroup MAC Message authentication codes
1355 * @{
1356 */
1357
Gilles Peskine69647a42019-01-14 20:18:12 +01001358/** Calculate the MAC (message authentication code) of a message.
1359 *
1360 * \note To verify the MAC of a message against an
1361 * expected value, use psa_mac_verify() instead.
1362 * Beware that comparing integrity or authenticity data such as
1363 * MAC values with a function such as \c memcmp is risky
1364 * because the time taken by the comparison may leak information
1365 * about the MAC value which could allow an attacker to guess
1366 * a valid MAC and thereby bypass security controls.
1367 *
1368 * \param handle Handle to the key to use for the operation.
1369 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001370 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001371 * \param[in] input Buffer containing the input message.
1372 * \param input_length Size of the \p input buffer in bytes.
1373 * \param[out] mac Buffer where the MAC value is to be written.
1374 * \param mac_size Size of the \p mac buffer in bytes.
1375 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001376 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001377 *
1378 * \retval #PSA_SUCCESS
1379 * Success.
1380 * \retval #PSA_ERROR_INVALID_HANDLE
1381 * \retval #PSA_ERROR_EMPTY_SLOT
1382 * \retval #PSA_ERROR_NOT_PERMITTED
1383 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001384 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001385 * \retval #PSA_ERROR_NOT_SUPPORTED
1386 * \p alg is not supported or is not a MAC algorithm.
1387 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1388 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1389 * \retval #PSA_ERROR_HARDWARE_FAILURE
1390 * \retval #PSA_ERROR_TAMPERING_DETECTED
1391 * \retval #PSA_ERROR_BAD_STATE
1392 * The library has not been previously initialized by psa_crypto_init().
1393 * It is implementation-dependent whether a failure to initialize
1394 * results in this error code.
1395 */
1396psa_status_t psa_mac_compute(psa_key_handle_t handle,
1397 psa_algorithm_t alg,
1398 const uint8_t *input,
1399 size_t input_length,
1400 uint8_t *mac,
1401 size_t mac_size,
1402 size_t *mac_length);
1403
1404/** Calculate the MAC of a message and compare it with a reference value.
1405 *
1406 * \param handle Handle to the key to use for the operation.
1407 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001408 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001409 * \param[in] input Buffer containing the input message.
1410 * \param input_length Size of the \p input buffer in bytes.
1411 * \param[out] mac Buffer containing the expected MAC value.
1412 * \param mac_length Size of the \p mac buffer in bytes.
1413 *
1414 * \retval #PSA_SUCCESS
1415 * The expected MAC is identical to the actual MAC of the input.
1416 * \retval #PSA_ERROR_INVALID_SIGNATURE
1417 * The MAC of the message was calculated successfully, but it
1418 * differs from the expected value.
1419 * \retval #PSA_ERROR_INVALID_HANDLE
1420 * \retval #PSA_ERROR_EMPTY_SLOT
1421 * \retval #PSA_ERROR_NOT_PERMITTED
1422 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001423 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001424 * \retval #PSA_ERROR_NOT_SUPPORTED
1425 * \p alg is not supported or is not a MAC algorithm.
1426 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1427 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1428 * \retval #PSA_ERROR_HARDWARE_FAILURE
1429 * \retval #PSA_ERROR_TAMPERING_DETECTED
1430 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001431psa_status_t psa_mac_verify(psa_key_handle_t handle,
1432 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001433 const uint8_t *input,
1434 size_t input_length,
1435 const uint8_t *mac,
1436 const size_t mac_length);
1437
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001438/** The type of the state data structure for multipart MAC operations.
1439 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001440 * Before calling any function on a MAC operation object, the application must
1441 * initialize it by any of the following means:
1442 * - Set the structure to all-bits-zero, for example:
1443 * \code
1444 * psa_mac_operation_t operation;
1445 * memset(&operation, 0, sizeof(operation));
1446 * \endcode
1447 * - Initialize the structure to logical zero values, for example:
1448 * \code
1449 * psa_mac_operation_t operation = {0};
1450 * \endcode
1451 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1452 * for example:
1453 * \code
1454 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1455 * \endcode
1456 * - Assign the result of the function psa_mac_operation_init()
1457 * to the structure, for example:
1458 * \code
1459 * psa_mac_operation_t operation;
1460 * operation = psa_mac_operation_init();
1461 * \endcode
1462 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001463 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001464 * make any assumptions about the content of this structure except
1465 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001466typedef struct psa_mac_operation_s psa_mac_operation_t;
1467
Jaeden Amero769ce272019-01-04 11:48:03 +00001468/** \def PSA_MAC_OPERATION_INIT
1469 *
1470 * This macro returns a suitable initializer for a MAC operation object of type
1471 * #psa_mac_operation_t.
1472 */
1473#ifdef __DOXYGEN_ONLY__
1474/* This is an example definition for documentation purposes.
1475 * Implementations should define a suitable value in `crypto_struct.h`.
1476 */
1477#define PSA_MAC_OPERATION_INIT {0}
1478#endif
1479
1480/** Return an initial value for a MAC operation object.
1481 */
1482static psa_mac_operation_t psa_mac_operation_init(void);
1483
Gilles Peskinef45adda2019-01-14 18:29:18 +01001484/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001485 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001486 * This function sets up the calculation of the MAC
1487 * (message authentication code) of a byte string.
1488 * To verify the MAC of a message against an
1489 * expected value, use psa_mac_verify_setup() instead.
1490 *
1491 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001492 * -# Allocate an operation object which will be passed to all the functions
1493 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001494 * -# Initialize the operation object with one of the methods described in the
1495 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001496 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001497 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1498 * of the message each time. The MAC that is calculated is the MAC
1499 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001500 * -# At the end of the message, call psa_mac_sign_finish() to finish
1501 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001502 *
1503 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001504 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001505 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001506 * After a successful call to psa_mac_sign_setup(), the application must
1507 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001508 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001509 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001510 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001511 * \param[in,out] operation The operation object to set up. It must have
1512 * been initialized as per the documentation for
1513 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001514 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001515 * It must remain valid until the operation
1516 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001517 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001518 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001519 *
Gilles Peskine28538492018-07-11 17:34:00 +02001520 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001521 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001522 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001523 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001524 * \retval #PSA_ERROR_NOT_PERMITTED
1525 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001526 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001527 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001528 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001529 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1530 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1531 * \retval #PSA_ERROR_HARDWARE_FAILURE
1532 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001533 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001534 * The operation state is not valid (already set up and not
1535 * subsequently completed).
1536 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001537 * The library has not been previously initialized by psa_crypto_init().
1538 * It is implementation-dependent whether a failure to initialize
1539 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001540 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001541psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001542 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001543 psa_algorithm_t alg);
1544
Gilles Peskinef45adda2019-01-14 18:29:18 +01001545/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001546 *
1547 * This function sets up the verification of the MAC
1548 * (message authentication code) of a byte string against an expected value.
1549 *
1550 * The sequence of operations to verify a MAC is as follows:
1551 * -# Allocate an operation object which will be passed to all the functions
1552 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001553 * -# Initialize the operation object with one of the methods described in the
1554 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001555 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001556 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1557 * of the message each time. The MAC that is calculated is the MAC
1558 * of the concatenation of these messages in order.
1559 * -# At the end of the message, call psa_mac_verify_finish() to finish
1560 * calculating the actual MAC of the message and verify it against
1561 * the expected value.
1562 *
1563 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001564 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001565 *
1566 * After a successful call to psa_mac_verify_setup(), the application must
1567 * eventually terminate the operation through one of the following methods:
1568 * - A failed call to psa_mac_update().
1569 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1570 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001571 * \param[in,out] operation The operation object to set up. It must have
1572 * been initialized as per the documentation for
1573 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001574 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001575 * It must remain valid until the operation
1576 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001577 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1578 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001579 *
Gilles Peskine28538492018-07-11 17:34:00 +02001580 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001581 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001582 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001583 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001584 * \retval #PSA_ERROR_NOT_PERMITTED
1585 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001586 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001587 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001588 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001589 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1590 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1591 * \retval #PSA_ERROR_HARDWARE_FAILURE
1592 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001593 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001594 * The operation state is not valid (already set up and not
1595 * subsequently completed).
1596 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001597 * The library has not been previously initialized by psa_crypto_init().
1598 * It is implementation-dependent whether a failure to initialize
1599 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001600 */
1601psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001602 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001603 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001604
Gilles Peskinedcd14942018-07-12 00:30:52 +02001605/** Add a message fragment to a multipart MAC operation.
1606 *
1607 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1608 * before calling this function.
1609 *
1610 * If this function returns an error status, the operation becomes inactive.
1611 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001612 * \param[in,out] operation Active MAC operation.
1613 * \param[in] input Buffer containing the message fragment to add to
1614 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001615 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001616 *
1617 * \retval #PSA_SUCCESS
1618 * Success.
1619 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001620 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001621 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1622 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1623 * \retval #PSA_ERROR_HARDWARE_FAILURE
1624 * \retval #PSA_ERROR_TAMPERING_DETECTED
1625 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001626psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1627 const uint8_t *input,
1628 size_t input_length);
1629
Gilles Peskinedcd14942018-07-12 00:30:52 +02001630/** Finish the calculation of the MAC of a message.
1631 *
1632 * The application must call psa_mac_sign_setup() before calling this function.
1633 * This function calculates the MAC of the message formed by concatenating
1634 * the inputs passed to preceding calls to psa_mac_update().
1635 *
1636 * When this function returns, the operation becomes inactive.
1637 *
1638 * \warning Applications should not call this function if they expect
1639 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1640 * Beware that comparing integrity or authenticity data such as
1641 * MAC values with a function such as \c memcmp is risky
1642 * because the time taken by the comparison may leak information
1643 * about the MAC value which could allow an attacker to guess
1644 * a valid MAC and thereby bypass security controls.
1645 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001646 * \param[in,out] operation Active MAC operation.
1647 * \param[out] mac Buffer where the MAC value is to be written.
1648 * \param mac_size Size of the \p mac buffer in bytes.
1649 * \param[out] mac_length On success, the number of bytes
1650 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001651 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001652 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001653 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001654 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001655 *
1656 * \retval #PSA_SUCCESS
1657 * Success.
1658 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001659 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001660 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001661 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001662 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1663 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1664 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1665 * \retval #PSA_ERROR_HARDWARE_FAILURE
1666 * \retval #PSA_ERROR_TAMPERING_DETECTED
1667 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001668psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1669 uint8_t *mac,
1670 size_t mac_size,
1671 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001672
Gilles Peskinedcd14942018-07-12 00:30:52 +02001673/** Finish the calculation of the MAC of a message and compare it with
1674 * an expected value.
1675 *
1676 * The application must call psa_mac_verify_setup() before calling this function.
1677 * This function calculates the MAC of the message formed by concatenating
1678 * the inputs passed to preceding calls to psa_mac_update(). It then
1679 * compares the calculated MAC with the expected MAC passed as a
1680 * parameter to this function.
1681 *
1682 * When this function returns, the operation becomes inactive.
1683 *
1684 * \note Implementations shall make the best effort to ensure that the
1685 * comparison between the actual MAC and the expected MAC is performed
1686 * in constant time.
1687 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001688 * \param[in,out] operation Active MAC operation.
1689 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001690 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001691 *
1692 * \retval #PSA_SUCCESS
1693 * The expected MAC is identical to the actual MAC of the message.
1694 * \retval #PSA_ERROR_INVALID_SIGNATURE
1695 * The MAC of the message was calculated successfully, but it
1696 * differs from the expected MAC.
1697 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001698 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001699 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1700 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1701 * \retval #PSA_ERROR_HARDWARE_FAILURE
1702 * \retval #PSA_ERROR_TAMPERING_DETECTED
1703 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001704psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1705 const uint8_t *mac,
1706 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001707
Gilles Peskinedcd14942018-07-12 00:30:52 +02001708/** Abort a MAC operation.
1709 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001710 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001711 * \p operation structure itself. Once aborted, the operation object
1712 * can be reused for another operation by calling
1713 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001714 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001715 * You may call this function any time after the operation object has
1716 * been initialized by any of the following methods:
1717 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1718 * it succeeds or not.
1719 * - Initializing the \c struct to all-bits-zero.
1720 * - Initializing the \c struct to logical zeros, e.g.
1721 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001722 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001723 * In particular, calling psa_mac_abort() after the operation has been
1724 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1725 * psa_mac_verify_finish() is safe and has no effect.
1726 *
1727 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001728 *
1729 * \retval #PSA_SUCCESS
1730 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001731 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001732 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1733 * \retval #PSA_ERROR_HARDWARE_FAILURE
1734 * \retval #PSA_ERROR_TAMPERING_DETECTED
1735 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001736psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1737
1738/**@}*/
1739
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001740/** \defgroup cipher Symmetric ciphers
1741 * @{
1742 */
1743
Gilles Peskine69647a42019-01-14 20:18:12 +01001744/** Encrypt a message using a symmetric cipher.
1745 *
1746 * This function encrypts a message with a random IV (initialization
1747 * vector).
1748 *
1749 * \param handle Handle to the key to use for the operation.
1750 * It must remain valid until the operation
1751 * terminates.
1752 * \param alg The cipher algorithm to compute
1753 * (\c PSA_ALG_XXX value such that
1754 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1755 * \param[in] input Buffer containing the message to encrypt.
1756 * \param input_length Size of the \p input buffer in bytes.
1757 * \param[out] output Buffer where the output is to be written.
1758 * The output contains the IV followed by
1759 * the ciphertext proper.
1760 * \param output_size Size of the \p output buffer in bytes.
1761 * \param[out] output_length On success, the number of bytes
1762 * that make up the output.
1763 *
1764 * \retval #PSA_SUCCESS
1765 * Success.
1766 * \retval #PSA_ERROR_INVALID_HANDLE
1767 * \retval #PSA_ERROR_EMPTY_SLOT
1768 * \retval #PSA_ERROR_NOT_PERMITTED
1769 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001770 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001771 * \retval #PSA_ERROR_NOT_SUPPORTED
1772 * \p alg is not supported or is not a cipher algorithm.
1773 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1774 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1775 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1776 * \retval #PSA_ERROR_HARDWARE_FAILURE
1777 * \retval #PSA_ERROR_TAMPERING_DETECTED
1778 */
1779psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1780 psa_algorithm_t alg,
1781 const uint8_t *input,
1782 size_t input_length,
1783 uint8_t *output,
1784 size_t output_size,
1785 size_t *output_length);
1786
1787/** Decrypt a message using a symmetric cipher.
1788 *
1789 * This function decrypts a message encrypted with a symmetric cipher.
1790 *
1791 * \param handle Handle to the key to use for the operation.
1792 * It must remain valid until the operation
1793 * terminates.
1794 * \param alg The cipher algorithm to compute
1795 * (\c PSA_ALG_XXX value such that
1796 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1797 * \param[in] input Buffer containing the message to decrypt.
1798 * This consists of the IV followed by the
1799 * ciphertext proper.
1800 * \param input_length Size of the \p input buffer in bytes.
1801 * \param[out] output Buffer where the plaintext is to be written.
1802 * \param output_size Size of the \p output buffer in bytes.
1803 * \param[out] output_length On success, the number of bytes
1804 * that make up the output.
1805 *
1806 * \retval #PSA_SUCCESS
1807 * Success.
1808 * \retval #PSA_ERROR_INVALID_HANDLE
1809 * \retval #PSA_ERROR_EMPTY_SLOT
1810 * \retval #PSA_ERROR_NOT_PERMITTED
1811 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001812 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001813 * \retval #PSA_ERROR_NOT_SUPPORTED
1814 * \p alg is not supported or is not a cipher algorithm.
1815 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1816 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1817 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1818 * \retval #PSA_ERROR_HARDWARE_FAILURE
1819 * \retval #PSA_ERROR_TAMPERING_DETECTED
1820 */
1821psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1822 psa_algorithm_t alg,
1823 const uint8_t *input,
1824 size_t input_length,
1825 uint8_t *output,
1826 size_t output_size,
1827 size_t *output_length);
1828
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001829/** The type of the state data structure for multipart cipher operations.
1830 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001831 * Before calling any function on a cipher operation object, the application
1832 * must initialize it by any of the following means:
1833 * - Set the structure to all-bits-zero, for example:
1834 * \code
1835 * psa_cipher_operation_t operation;
1836 * memset(&operation, 0, sizeof(operation));
1837 * \endcode
1838 * - Initialize the structure to logical zero values, for example:
1839 * \code
1840 * psa_cipher_operation_t operation = {0};
1841 * \endcode
1842 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1843 * for example:
1844 * \code
1845 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1846 * \endcode
1847 * - Assign the result of the function psa_cipher_operation_init()
1848 * to the structure, for example:
1849 * \code
1850 * psa_cipher_operation_t operation;
1851 * operation = psa_cipher_operation_init();
1852 * \endcode
1853 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001854 * This is an implementation-defined \c struct. Applications should not
1855 * make any assumptions about the content of this structure except
1856 * as directed by the documentation of a specific implementation. */
1857typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1858
Jaeden Amero5bae2272019-01-04 11:48:27 +00001859/** \def PSA_CIPHER_OPERATION_INIT
1860 *
1861 * This macro returns a suitable initializer for a cipher operation object of
1862 * type #psa_cipher_operation_t.
1863 */
1864#ifdef __DOXYGEN_ONLY__
1865/* This is an example definition for documentation purposes.
1866 * Implementations should define a suitable value in `crypto_struct.h`.
1867 */
1868#define PSA_CIPHER_OPERATION_INIT {0}
1869#endif
1870
1871/** Return an initial value for a cipher operation object.
1872 */
1873static psa_cipher_operation_t psa_cipher_operation_init(void);
1874
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001875/** Set the key for a multipart symmetric encryption operation.
1876 *
1877 * The sequence of operations to encrypt a message with a symmetric cipher
1878 * is as follows:
1879 * -# Allocate an operation object which will be passed to all the functions
1880 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001881 * -# Initialize the operation object with one of the methods described in the
1882 * documentation for #psa_cipher_operation_t, e.g.
1883 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001884 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001885 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001886 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001887 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001888 * requires a specific IV value.
1889 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1890 * of the message each time.
1891 * -# Call psa_cipher_finish().
1892 *
1893 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001894 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001895 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001896 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001897 * eventually terminate the operation. The following events terminate an
1898 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001899 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001900 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001901 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001902 * \param[in,out] operation The operation object to set up. It must have
1903 * been initialized as per the documentation for
1904 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001905 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001906 * It must remain valid until the operation
1907 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001908 * \param alg The cipher algorithm to compute
1909 * (\c PSA_ALG_XXX value such that
1910 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001911 *
Gilles Peskine28538492018-07-11 17:34:00 +02001912 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001913 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001914 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001915 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001916 * \retval #PSA_ERROR_NOT_PERMITTED
1917 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001918 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001919 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001920 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001921 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1922 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1923 * \retval #PSA_ERROR_HARDWARE_FAILURE
1924 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001925 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001926 * The operation state is not valid (already set up and not
1927 * subsequently completed).
1928 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001929 * The library has not been previously initialized by psa_crypto_init().
1930 * It is implementation-dependent whether a failure to initialize
1931 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001932 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001933psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001934 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001935 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001936
1937/** Set the key for a multipart symmetric decryption operation.
1938 *
1939 * The sequence of operations to decrypt a message with a symmetric cipher
1940 * is as follows:
1941 * -# Allocate an operation object which will be passed to all the functions
1942 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001943 * -# Initialize the operation object with one of the methods described in the
1944 * documentation for #psa_cipher_operation_t, e.g.
1945 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001946 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001947 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001948 * decryption. If the IV is prepended to the ciphertext, you can call
1949 * psa_cipher_update() on a buffer containing the IV followed by the
1950 * beginning of the message.
1951 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1952 * of the message each time.
1953 * -# Call psa_cipher_finish().
1954 *
1955 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001956 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001957 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001958 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001959 * eventually terminate the operation. The following events terminate an
1960 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001961 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001962 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001963 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001964 * \param[in,out] operation The operation object to set up. It must have
1965 * been initialized as per the documentation for
1966 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001967 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001968 * It must remain valid until the operation
1969 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001970 * \param alg The cipher algorithm to compute
1971 * (\c PSA_ALG_XXX value such that
1972 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001973 *
Gilles Peskine28538492018-07-11 17:34:00 +02001974 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001975 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001976 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001977 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001978 * \retval #PSA_ERROR_NOT_PERMITTED
1979 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001980 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001981 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001982 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001983 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1984 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1985 * \retval #PSA_ERROR_HARDWARE_FAILURE
1986 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001987 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001988 * The operation state is not valid (already set up and not
1989 * subsequently completed).
1990 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001991 * The library has not been previously initialized by psa_crypto_init().
1992 * It is implementation-dependent whether a failure to initialize
1993 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001994 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001995psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001996 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001997 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001998
Gilles Peskinedcd14942018-07-12 00:30:52 +02001999/** Generate an IV for a symmetric encryption operation.
2000 *
2001 * This function generates a random IV (initialization vector), nonce
2002 * or initial counter value for the encryption operation as appropriate
2003 * for the chosen algorithm, key type and key size.
2004 *
2005 * The application must call psa_cipher_encrypt_setup() before
2006 * calling this function.
2007 *
2008 * If this function returns an error status, the operation becomes inactive.
2009 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002010 * \param[in,out] operation Active cipher operation.
2011 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002012 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002013 * \param[out] iv_length On success, the number of bytes of the
2014 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002015 *
2016 * \retval #PSA_SUCCESS
2017 * Success.
2018 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002019 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002020 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002021 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002022 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2023 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2024 * \retval #PSA_ERROR_HARDWARE_FAILURE
2025 * \retval #PSA_ERROR_TAMPERING_DETECTED
2026 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002027psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
2028 unsigned char *iv,
2029 size_t iv_size,
2030 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002031
Gilles Peskinedcd14942018-07-12 00:30:52 +02002032/** Set the IV for a symmetric encryption or decryption operation.
2033 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01002034 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02002035 * or initial counter value for the encryption or decryption operation.
2036 *
2037 * The application must call psa_cipher_encrypt_setup() before
2038 * calling this function.
2039 *
2040 * If this function returns an error status, the operation becomes inactive.
2041 *
2042 * \note When encrypting, applications should use psa_cipher_generate_iv()
2043 * instead of this function, unless implementing a protocol that requires
2044 * a non-random IV.
2045 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002046 * \param[in,out] operation Active cipher operation.
2047 * \param[in] iv Buffer containing the IV to use.
2048 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002049 *
2050 * \retval #PSA_SUCCESS
2051 * Success.
2052 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002053 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002054 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002055 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02002056 * or the chosen algorithm does not use an IV.
2057 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2058 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2059 * \retval #PSA_ERROR_HARDWARE_FAILURE
2060 * \retval #PSA_ERROR_TAMPERING_DETECTED
2061 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002062psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
2063 const unsigned char *iv,
2064 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002065
Gilles Peskinedcd14942018-07-12 00:30:52 +02002066/** Encrypt or decrypt a message fragment in an active cipher operation.
2067 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02002068 * Before calling this function, you must:
2069 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
2070 * The choice of setup function determines whether this function
2071 * encrypts or decrypts its input.
2072 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
2073 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002074 *
2075 * If this function returns an error status, the operation becomes inactive.
2076 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002077 * \param[in,out] operation Active cipher operation.
2078 * \param[in] input Buffer containing the message fragment to
2079 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002080 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002081 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002082 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002083 * \param[out] output_length On success, the number of bytes
2084 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002085 *
2086 * \retval #PSA_SUCCESS
2087 * Success.
2088 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002089 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002090 * not set, or already completed).
2091 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2092 * The size of the \p output buffer is too small.
2093 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2094 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2095 * \retval #PSA_ERROR_HARDWARE_FAILURE
2096 * \retval #PSA_ERROR_TAMPERING_DETECTED
2097 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002098psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2099 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002100 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002101 unsigned char *output,
2102 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002103 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002104
Gilles Peskinedcd14942018-07-12 00:30:52 +02002105/** Finish encrypting or decrypting a message in a cipher operation.
2106 *
2107 * The application must call psa_cipher_encrypt_setup() or
2108 * psa_cipher_decrypt_setup() before calling this function. The choice
2109 * of setup function determines whether this function encrypts or
2110 * decrypts its input.
2111 *
2112 * This function finishes the encryption or decryption of the message
2113 * formed by concatenating the inputs passed to preceding calls to
2114 * psa_cipher_update().
2115 *
2116 * When this function returns, the operation becomes inactive.
2117 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002118 * \param[in,out] operation Active cipher operation.
2119 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002120 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002121 * \param[out] output_length On success, the number of bytes
2122 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002123 *
2124 * \retval #PSA_SUCCESS
2125 * Success.
2126 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002127 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002128 * not set, or already completed).
2129 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2130 * The size of the \p output buffer is too small.
2131 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2132 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2133 * \retval #PSA_ERROR_HARDWARE_FAILURE
2134 * \retval #PSA_ERROR_TAMPERING_DETECTED
2135 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002136psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002137 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002138 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002139 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002140
Gilles Peskinedcd14942018-07-12 00:30:52 +02002141/** Abort a cipher operation.
2142 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002143 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002144 * \p operation structure itself. Once aborted, the operation object
2145 * can be reused for another operation by calling
2146 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002147 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002148 * You may call this function any time after the operation object has
2149 * been initialized by any of the following methods:
2150 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2151 * whether it succeeds or not.
2152 * - Initializing the \c struct to all-bits-zero.
2153 * - Initializing the \c struct to logical zeros, e.g.
2154 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002155 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002156 * In particular, calling psa_cipher_abort() after the operation has been
2157 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2158 * is safe and has no effect.
2159 *
2160 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002161 *
2162 * \retval #PSA_SUCCESS
2163 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002164 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002165 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2166 * \retval #PSA_ERROR_HARDWARE_FAILURE
2167 * \retval #PSA_ERROR_TAMPERING_DETECTED
2168 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002169psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2170
2171/**@}*/
2172
Gilles Peskine3b555712018-03-03 21:27:57 +01002173/** \defgroup aead Authenticated encryption with associated data (AEAD)
2174 * @{
2175 */
2176
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002177/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002178 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002179 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002180 * \param alg The AEAD algorithm to compute
2181 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002182 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002183 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002184 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002185 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002186 * but not encrypted.
2187 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002188 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002189 * encrypted.
2190 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002191 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002192 * encrypted data. The additional data is not
2193 * part of this output. For algorithms where the
2194 * encrypted data and the authentication tag
2195 * are defined as separate outputs, the
2196 * authentication tag is appended to the
2197 * encrypted data.
2198 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2199 * This must be at least
2200 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2201 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002202 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002203 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002204 *
Gilles Peskine28538492018-07-11 17:34:00 +02002205 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002206 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002207 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002208 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002209 * \retval #PSA_ERROR_NOT_PERMITTED
2210 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002211 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002212 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002213 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002214 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2215 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2216 * \retval #PSA_ERROR_HARDWARE_FAILURE
2217 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002218 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002219 * The library has not been previously initialized by psa_crypto_init().
2220 * It is implementation-dependent whether a failure to initialize
2221 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002222 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002223psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002224 psa_algorithm_t alg,
2225 const uint8_t *nonce,
2226 size_t nonce_length,
2227 const uint8_t *additional_data,
2228 size_t additional_data_length,
2229 const uint8_t *plaintext,
2230 size_t plaintext_length,
2231 uint8_t *ciphertext,
2232 size_t ciphertext_size,
2233 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002234
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002235/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002236 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002237 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002238 * \param alg The AEAD algorithm to compute
2239 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002240 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002241 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002242 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002243 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002244 * but not encrypted.
2245 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002246 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002247 * encrypted. For algorithms where the
2248 * encrypted data and the authentication tag
2249 * are defined as separate inputs, the buffer
2250 * must contain the encrypted data followed
2251 * by the authentication tag.
2252 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002253 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002254 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2255 * This must be at least
2256 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2257 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002258 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002259 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002260 *
Gilles Peskine28538492018-07-11 17:34:00 +02002261 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002262 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002263 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002264 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002265 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002266 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002267 * \retval #PSA_ERROR_NOT_PERMITTED
2268 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002269 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002270 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002271 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002272 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2273 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2274 * \retval #PSA_ERROR_HARDWARE_FAILURE
2275 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002276 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002277 * The library has not been previously initialized by psa_crypto_init().
2278 * It is implementation-dependent whether a failure to initialize
2279 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002280 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002281psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002282 psa_algorithm_t alg,
2283 const uint8_t *nonce,
2284 size_t nonce_length,
2285 const uint8_t *additional_data,
2286 size_t additional_data_length,
2287 const uint8_t *ciphertext,
2288 size_t ciphertext_length,
2289 uint8_t *plaintext,
2290 size_t plaintext_size,
2291 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002292
Gilles Peskine30a9e412019-01-14 18:36:12 +01002293/** The type of the state data structure for multipart AEAD operations.
2294 *
2295 * Before calling any function on an AEAD operation object, the application
2296 * must initialize it by any of the following means:
2297 * - Set the structure to all-bits-zero, for example:
2298 * \code
2299 * psa_aead_operation_t operation;
2300 * memset(&operation, 0, sizeof(operation));
2301 * \endcode
2302 * - Initialize the structure to logical zero values, for example:
2303 * \code
2304 * psa_aead_operation_t operation = {0};
2305 * \endcode
2306 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2307 * for example:
2308 * \code
2309 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2310 * \endcode
2311 * - Assign the result of the function psa_aead_operation_init()
2312 * to the structure, for example:
2313 * \code
2314 * psa_aead_operation_t operation;
2315 * operation = psa_aead_operation_init();
2316 * \endcode
2317 *
2318 * This is an implementation-defined \c struct. Applications should not
2319 * make any assumptions about the content of this structure except
2320 * as directed by the documentation of a specific implementation. */
2321typedef struct psa_aead_operation_s psa_aead_operation_t;
2322
2323/** \def PSA_AEAD_OPERATION_INIT
2324 *
2325 * This macro returns a suitable initializer for an AEAD operation object of
2326 * type #psa_aead_operation_t.
2327 */
2328#ifdef __DOXYGEN_ONLY__
2329/* This is an example definition for documentation purposes.
2330 * Implementations should define a suitable value in `crypto_struct.h`.
2331 */
2332#define PSA_AEAD_OPERATION_INIT {0}
2333#endif
2334
2335/** Return an initial value for an AEAD operation object.
2336 */
2337static psa_aead_operation_t psa_aead_operation_init(void);
2338
2339/** Set the key for a multipart authenticated encryption operation.
2340 *
2341 * The sequence of operations to encrypt a message with authentication
2342 * is as follows:
2343 * -# Allocate an operation object which will be passed to all the functions
2344 * listed here.
2345 * -# Initialize the operation object with one of the methods described in the
2346 * documentation for #psa_aead_operation_t, e.g.
2347 * PSA_AEAD_OPERATION_INIT.
2348 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002349 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2350 * inputs to the subsequent calls to psa_aead_update_ad() and
2351 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2352 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002353 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2354 * generate or set the nonce. You should use
2355 * psa_aead_generate_nonce() unless the protocol you are implementing
2356 * requires a specific nonce value.
2357 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2358 * of the non-encrypted additional authenticated data each time.
2359 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002360 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002361 * -# Call psa_aead_finish().
2362 *
2363 * The application may call psa_aead_abort() at any time after the operation
2364 * has been initialized.
2365 *
2366 * After a successful call to psa_aead_encrypt_setup(), the application must
2367 * eventually terminate the operation. The following events terminate an
2368 * operation:
2369 * - A failed call to any of the \c psa_aead_xxx functions.
2370 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2371 *
2372 * \param[in,out] operation The operation object to set up. It must have
2373 * been initialized as per the documentation for
2374 * #psa_aead_operation_t and not yet in use.
2375 * \param handle Handle to the key to use for the operation.
2376 * It must remain valid until the operation
2377 * terminates.
2378 * \param alg The AEAD algorithm to compute
2379 * (\c PSA_ALG_XXX value such that
2380 * #PSA_ALG_IS_AEAD(\p alg) is true).
2381 *
2382 * \retval #PSA_SUCCESS
2383 * Success.
2384 * \retval #PSA_ERROR_INVALID_HANDLE
2385 * \retval #PSA_ERROR_EMPTY_SLOT
2386 * \retval #PSA_ERROR_NOT_PERMITTED
2387 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002388 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002389 * \retval #PSA_ERROR_NOT_SUPPORTED
2390 * \p alg is not supported or is not an AEAD algorithm.
2391 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2392 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2393 * \retval #PSA_ERROR_HARDWARE_FAILURE
2394 * \retval #PSA_ERROR_TAMPERING_DETECTED
2395 * \retval #PSA_ERROR_BAD_STATE
2396 * The library has not been previously initialized by psa_crypto_init().
2397 * It is implementation-dependent whether a failure to initialize
2398 * results in this error code.
2399 */
2400psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2401 psa_key_handle_t handle,
2402 psa_algorithm_t alg);
2403
2404/** Set the key for a multipart authenticated decryption operation.
2405 *
2406 * The sequence of operations to decrypt a message with authentication
2407 * is as follows:
2408 * -# Allocate an operation object which will be passed to all the functions
2409 * listed here.
2410 * -# Initialize the operation object with one of the methods described in the
2411 * documentation for #psa_aead_operation_t, e.g.
2412 * PSA_AEAD_OPERATION_INIT.
2413 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002414 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2415 * inputs to the subsequent calls to psa_aead_update_ad() and
2416 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2417 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002418 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2419 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2420 * of the non-encrypted additional authenticated data each time.
2421 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002422 * of the ciphertext to decrypt each time.
2423 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002424 *
2425 * The application may call psa_aead_abort() at any time after the operation
2426 * has been initialized.
2427 *
2428 * After a successful call to psa_aead_decrypt_setup(), the application must
2429 * eventually terminate the operation. The following events terminate an
2430 * operation:
2431 * - A failed call to any of the \c psa_aead_xxx functions.
2432 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2433 *
2434 * \param[in,out] operation The operation object to set up. It must have
2435 * been initialized as per the documentation for
2436 * #psa_aead_operation_t and not yet in use.
2437 * \param handle Handle to the key to use for the operation.
2438 * It must remain valid until the operation
2439 * terminates.
2440 * \param alg The AEAD algorithm to compute
2441 * (\c PSA_ALG_XXX value such that
2442 * #PSA_ALG_IS_AEAD(\p alg) is true).
2443 *
2444 * \retval #PSA_SUCCESS
2445 * Success.
2446 * \retval #PSA_ERROR_INVALID_HANDLE
2447 * \retval #PSA_ERROR_EMPTY_SLOT
2448 * \retval #PSA_ERROR_NOT_PERMITTED
2449 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002450 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002451 * \retval #PSA_ERROR_NOT_SUPPORTED
2452 * \p alg is not supported or is not an AEAD algorithm.
2453 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2454 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2455 * \retval #PSA_ERROR_HARDWARE_FAILURE
2456 * \retval #PSA_ERROR_TAMPERING_DETECTED
2457 * \retval #PSA_ERROR_BAD_STATE
2458 * The library has not been previously initialized by psa_crypto_init().
2459 * It is implementation-dependent whether a failure to initialize
2460 * results in this error code.
2461 */
2462psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2463 psa_key_handle_t handle,
2464 psa_algorithm_t alg);
2465
2466/** Generate a random nonce for an authenticated encryption operation.
2467 *
2468 * This function generates a random nonce for the authenticated encryption
2469 * operation with an appropriate size for the chosen algorithm, key type
2470 * and key size.
2471 *
2472 * The application must call psa_aead_encrypt_setup() before
2473 * calling this function.
2474 *
2475 * If this function returns an error status, the operation becomes inactive.
2476 *
2477 * \param[in,out] operation Active AEAD operation.
2478 * \param[out] nonce Buffer where the generated nonce is to be
2479 * written.
2480 * \param nonce_size Size of the \p nonce buffer in bytes.
2481 * \param[out] nonce_length On success, the number of bytes of the
2482 * generated nonce.
2483 *
2484 * \retval #PSA_SUCCESS
2485 * Success.
2486 * \retval #PSA_ERROR_BAD_STATE
2487 * The operation state is not valid (not set up, or nonce already set).
2488 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2489 * The size of the \p nonce buffer is too small.
2490 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2491 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2492 * \retval #PSA_ERROR_HARDWARE_FAILURE
2493 * \retval #PSA_ERROR_TAMPERING_DETECTED
2494 */
2495psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2496 unsigned char *nonce,
2497 size_t nonce_size,
2498 size_t *nonce_length);
2499
2500/** Set the nonce for an authenticated encryption or decryption operation.
2501 *
2502 * This function sets the nonce for the authenticated
2503 * encryption or decryption operation.
2504 *
2505 * The application must call psa_aead_encrypt_setup() before
2506 * calling this function.
2507 *
2508 * If this function returns an error status, the operation becomes inactive.
2509 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002510 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002511 * instead of this function, unless implementing a protocol that requires
2512 * a non-random IV.
2513 *
2514 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002515 * \param[in] nonce Buffer containing the nonce to use.
2516 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002517 *
2518 * \retval #PSA_SUCCESS
2519 * Success.
2520 * \retval #PSA_ERROR_BAD_STATE
2521 * The operation state is not valid (not set up, or nonce already set).
2522 * \retval #PSA_ERROR_INVALID_ARGUMENT
2523 * The size of \p nonce is not acceptable for the chosen algorithm.
2524 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2525 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2526 * \retval #PSA_ERROR_HARDWARE_FAILURE
2527 * \retval #PSA_ERROR_TAMPERING_DETECTED
2528 */
2529psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2530 const unsigned char *nonce,
2531 size_t nonce_length);
2532
Gilles Peskinebc59c852019-01-17 15:26:08 +01002533/** Declare the lengths of the message and additional data for AEAD.
2534 *
2535 * The application must call this function before calling
2536 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2537 * the operation requires it. If the algorithm does not require it,
2538 * calling this function is optional, but if this function is called
2539 * then the implementation must enforce the lengths.
2540 *
2541 * You may call this function before or after setting the nonce with
2542 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2543 *
2544 * - For #PSA_ALG_CCM, calling this function is required.
2545 * - For the other AEAD algorithms defined in this specification, calling
2546 * this function is not required.
2547 * - For vendor-defined algorithm, refer to the vendor documentation.
2548 *
2549 * \param[in,out] operation Active AEAD operation.
2550 * \param ad_length Size of the non-encrypted additional
2551 * authenticated data in bytes.
2552 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2553 *
2554 * \retval #PSA_SUCCESS
2555 * Success.
2556 * \retval #PSA_ERROR_BAD_STATE
2557 * The operation state is not valid (not set up, already completed,
2558 * or psa_aead_update_ad() or psa_aead_update() already called).
2559 * \retval #PSA_ERROR_INVALID_ARGUMENT
2560 * At least one of the lengths is not acceptable for the chosen
2561 * algorithm.
2562 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2563 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2564 * \retval #PSA_ERROR_HARDWARE_FAILURE
2565 * \retval #PSA_ERROR_TAMPERING_DETECTED
2566 */
2567psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2568 size_t ad_length,
2569 size_t plaintext_length);
2570
Gilles Peskine30a9e412019-01-14 18:36:12 +01002571/** Pass additional data to an active AEAD operation.
2572 *
2573 * Additional data is authenticated, but not encrypted.
2574 *
2575 * You may call this function multiple times to pass successive fragments
2576 * of the additional data. You may not call this function after passing
2577 * data to encrypt or decrypt with psa_aead_update().
2578 *
2579 * Before calling this function, you must:
2580 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2581 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2582 *
2583 * If this function returns an error status, the operation becomes inactive.
2584 *
2585 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2586 * there is no guarantee that the input is valid. Therefore, until
2587 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2588 * treat the input as untrusted and prepare to undo any action that
2589 * depends on the input if psa_aead_verify() returns an error status.
2590 *
2591 * \param[in,out] operation Active AEAD operation.
2592 * \param[in] input Buffer containing the fragment of
2593 * additional data.
2594 * \param input_length Size of the \p input buffer in bytes.
2595 *
2596 * \retval #PSA_SUCCESS
2597 * Success.
2598 * \retval #PSA_ERROR_BAD_STATE
2599 * The operation state is not valid (not set up, nonce not set,
2600 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002601 * \retval #PSA_ERROR_INVALID_ARGUMENT
2602 * The total input length overflows the additional data length that
2603 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002604 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2605 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2606 * \retval #PSA_ERROR_HARDWARE_FAILURE
2607 * \retval #PSA_ERROR_TAMPERING_DETECTED
2608 */
2609psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2610 const uint8_t *input,
2611 size_t input_length);
2612
2613/** Encrypt or decrypt a message fragment in an active AEAD operation.
2614 *
2615 * Before calling this function, you must:
2616 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2617 * The choice of setup function determines whether this function
2618 * encrypts or decrypts its input.
2619 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2620 * 3. Call psa_aead_update_ad() to pass all the additional data.
2621 *
2622 * If this function returns an error status, the operation becomes inactive.
2623 *
2624 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2625 * there is no guarantee that the input is valid. Therefore, until
2626 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2627 * - Do not use the output in any way other than storing it in a
2628 * confidential location. If you take any action that depends
2629 * on the tentative decrypted data, this action will need to be
2630 * undone if the input turns out not to be valid. Furthermore,
2631 * if an adversary can observe that this action took place
2632 * (for example through timing), they may be able to use this
2633 * fact as an oracle to decrypt any message encrypted with the
2634 * same key.
2635 * - In particular, do not copy the output anywhere but to a
2636 * memory or storage space that you have exclusive access to.
2637 *
2638 * \param[in,out] operation Active AEAD operation.
2639 * \param[in] input Buffer containing the message fragment to
2640 * encrypt or decrypt.
2641 * \param input_length Size of the \p input buffer in bytes.
2642 * \param[out] output Buffer where the output is to be written.
2643 * \param output_size Size of the \p output buffer in bytes.
2644 * \param[out] output_length On success, the number of bytes
2645 * that make up the returned output.
2646 *
2647 * \retval #PSA_SUCCESS
2648 * Success.
2649 * \retval #PSA_ERROR_BAD_STATE
2650 * The operation state is not valid (not set up, nonce not set
2651 * or already completed).
2652 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2653 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002654 * \retval #PSA_ERROR_INVALID_ARGUMENT
2655 * The total length of input to psa_aead_update_ad() so far is
2656 * less than the additional data length that was previously
2657 * specified with psa_aead_set_lengths().
2658 * \retval #PSA_ERROR_INVALID_ARGUMENT
2659 * The total input length overflows the plaintext length that
2660 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002661 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2662 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2663 * \retval #PSA_ERROR_HARDWARE_FAILURE
2664 * \retval #PSA_ERROR_TAMPERING_DETECTED
2665 */
2666psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2667 const uint8_t *input,
2668 size_t input_length,
2669 unsigned char *output,
2670 size_t output_size,
2671 size_t *output_length);
2672
2673/** Finish encrypting a message in an AEAD operation.
2674 *
2675 * The operation must have been set up with psa_aead_encrypt_setup().
2676 *
2677 * This function finishes the authentication of the additional data
2678 * formed by concatenating the inputs passed to preceding calls to
2679 * psa_aead_update_ad() with the plaintext formed by concatenating the
2680 * inputs passed to preceding calls to psa_aead_update().
2681 *
2682 * This function has two output buffers:
2683 * - \p ciphertext contains trailing ciphertext that was buffered from
2684 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2685 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2686 * will not contain any output and can be a 0-sized buffer.
2687 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002688 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002689 * that the operation performs.
2690 *
2691 * When this function returns, the operation becomes inactive.
2692 *
2693 * \param[in,out] operation Active AEAD operation.
2694 * \param[out] ciphertext Buffer where the last part of the ciphertext
2695 * is to be written.
2696 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2697 * \param[out] ciphertext_length On success, the number of bytes of
2698 * returned ciphertext.
2699 * \param[out] tag Buffer where the authentication tag is
2700 * to be written.
2701 * \param tag_size Size of the \p tag buffer in bytes.
2702 * \param[out] tag_length On success, the number of bytes
2703 * that make up the returned tag.
2704 *
2705 * \retval #PSA_SUCCESS
2706 * Success.
2707 * \retval #PSA_ERROR_BAD_STATE
2708 * The operation state is not valid (not set up, nonce not set,
2709 * decryption, or already completed).
2710 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002711 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002712 * \retval #PSA_ERROR_INVALID_ARGUMENT
2713 * The total length of input to psa_aead_update_ad() so far is
2714 * less than the additional data length that was previously
2715 * specified with psa_aead_set_lengths().
2716 * \retval #PSA_ERROR_INVALID_ARGUMENT
2717 * The total length of input to psa_aead_update() so far is
2718 * less than the plaintext length that was previously
2719 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002720 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2721 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2722 * \retval #PSA_ERROR_HARDWARE_FAILURE
2723 * \retval #PSA_ERROR_TAMPERING_DETECTED
2724 */
2725psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002726 uint8_t *ciphertext,
2727 size_t ciphertext_size,
2728 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002729 uint8_t *tag,
2730 size_t tag_size,
2731 size_t *tag_length);
2732
2733/** Finish authenticating and decrypting a message in an AEAD operation.
2734 *
2735 * The operation must have been set up with psa_aead_decrypt_setup().
2736 *
2737 * This function finishes the authentication of the additional data
2738 * formed by concatenating the inputs passed to preceding calls to
2739 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2740 * inputs passed to preceding calls to psa_aead_update().
2741 *
2742 * When this function returns, the operation becomes inactive.
2743 *
2744 * \param[in,out] operation Active AEAD operation.
2745 * \param[in] tag Buffer containing the authentication tag.
2746 * \param tag_length Size of the \p tag buffer in bytes.
2747 *
2748 * \retval #PSA_SUCCESS
2749 * Success.
2750 * \retval #PSA_ERROR_BAD_STATE
2751 * The operation state is not valid (not set up, nonce not set,
2752 * encryption, or already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002753 * \retval #PSA_ERROR_INVALID_ARGUMENT
2754 * The total length of input to psa_aead_update_ad() so far is
2755 * less than the additional data length that was previously
2756 * specified with psa_aead_set_lengths().
2757 * \retval #PSA_ERROR_INVALID_ARGUMENT
2758 * The total length of input to psa_aead_update() so far is
2759 * less than the plaintext length that was previously
2760 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002761 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2762 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2763 * \retval #PSA_ERROR_HARDWARE_FAILURE
2764 * \retval #PSA_ERROR_TAMPERING_DETECTED
2765 */
2766psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2767 const uint8_t *tag,
2768 size_t tag_length);
2769
2770/** Abort an AEAD operation.
2771 *
2772 * Aborting an operation frees all associated resources except for the
2773 * \p operation structure itself. Once aborted, the operation object
2774 * can be reused for another operation by calling
2775 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2776 *
2777 * You may call this function any time after the operation object has
2778 * been initialized by any of the following methods:
2779 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2780 * whether it succeeds or not.
2781 * - Initializing the \c struct to all-bits-zero.
2782 * - Initializing the \c struct to logical zeros, e.g.
2783 * `psa_aead_operation_t operation = {0}`.
2784 *
2785 * In particular, calling psa_aead_abort() after the operation has been
2786 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2787 * is safe and has no effect.
2788 *
2789 * \param[in,out] operation Initialized AEAD operation.
2790 *
2791 * \retval #PSA_SUCCESS
2792 * \retval #PSA_ERROR_BAD_STATE
2793 * \p operation is not an active AEAD operation.
2794 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2795 * \retval #PSA_ERROR_HARDWARE_FAILURE
2796 * \retval #PSA_ERROR_TAMPERING_DETECTED
2797 */
2798psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2799
Gilles Peskine3b555712018-03-03 21:27:57 +01002800/**@}*/
2801
Gilles Peskine20035e32018-02-03 22:44:14 +01002802/** \defgroup asymmetric Asymmetric cryptography
2803 * @{
2804 */
2805
2806/**
2807 * \brief Sign a hash or short message with a private key.
2808 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002809 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002810 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002811 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2812 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2813 * to determine the hash algorithm to use.
2814 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002815 * \param handle Handle to the key to use for the operation.
2816 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002817 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002818 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002819 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002820 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002821 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002822 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002823 * \param[out] signature_length On success, the number of bytes
2824 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002825 *
Gilles Peskine28538492018-07-11 17:34:00 +02002826 * \retval #PSA_SUCCESS
2827 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002828 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002829 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002830 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002831 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002832 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002833 * \retval #PSA_ERROR_NOT_SUPPORTED
2834 * \retval #PSA_ERROR_INVALID_ARGUMENT
2835 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2836 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2837 * \retval #PSA_ERROR_HARDWARE_FAILURE
2838 * \retval #PSA_ERROR_TAMPERING_DETECTED
2839 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002840 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002841 * The library has not been previously initialized by psa_crypto_init().
2842 * It is implementation-dependent whether a failure to initialize
2843 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002844 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002845psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002846 psa_algorithm_t alg,
2847 const uint8_t *hash,
2848 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002849 uint8_t *signature,
2850 size_t signature_size,
2851 size_t *signature_length);
2852
2853/**
2854 * \brief Verify the signature a hash or short message using a public key.
2855 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002856 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002857 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002858 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2859 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2860 * to determine the hash algorithm to use.
2861 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002862 * \param handle Handle to the key to use for the operation.
2863 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002864 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002865 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002866 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002867 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002868 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002869 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002870 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002871 *
Gilles Peskine28538492018-07-11 17:34:00 +02002872 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002873 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002874 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002875 * The calculation was perfomed successfully, but the passed
2876 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002877 * \retval #PSA_ERROR_NOT_SUPPORTED
2878 * \retval #PSA_ERROR_INVALID_ARGUMENT
2879 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2880 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2881 * \retval #PSA_ERROR_HARDWARE_FAILURE
2882 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002883 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002884 * The library has not been previously initialized by psa_crypto_init().
2885 * It is implementation-dependent whether a failure to initialize
2886 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002887 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002888psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002889 psa_algorithm_t alg,
2890 const uint8_t *hash,
2891 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002892 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002893 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002894
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002895/**
2896 * \brief Encrypt a short message with a public key.
2897 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002898 * \param handle Handle to the key to use for the operation.
2899 * It must be a public key or an asymmetric
2900 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002901 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002902 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002903 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002904 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002905 * \param[in] salt A salt or label, if supported by the
2906 * encryption algorithm.
2907 * If the algorithm does not support a
2908 * salt, pass \c NULL.
2909 * If the algorithm supports an optional
2910 * salt and you do not want to pass a salt,
2911 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002912 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002913 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2914 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002915 * \param salt_length Size of the \p salt buffer in bytes.
2916 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002917 * \param[out] output Buffer where the encrypted message is to
2918 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002919 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002920 * \param[out] output_length On success, the number of bytes
2921 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002922 *
Gilles Peskine28538492018-07-11 17:34:00 +02002923 * \retval #PSA_SUCCESS
2924 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002925 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002926 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002927 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002928 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002929 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002930 * \retval #PSA_ERROR_NOT_SUPPORTED
2931 * \retval #PSA_ERROR_INVALID_ARGUMENT
2932 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2933 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2934 * \retval #PSA_ERROR_HARDWARE_FAILURE
2935 * \retval #PSA_ERROR_TAMPERING_DETECTED
2936 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002937 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002938 * The library has not been previously initialized by psa_crypto_init().
2939 * It is implementation-dependent whether a failure to initialize
2940 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002941 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002942psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002943 psa_algorithm_t alg,
2944 const uint8_t *input,
2945 size_t input_length,
2946 const uint8_t *salt,
2947 size_t salt_length,
2948 uint8_t *output,
2949 size_t output_size,
2950 size_t *output_length);
2951
2952/**
2953 * \brief Decrypt a short message with a private key.
2954 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002955 * \param handle Handle to the key to use for the operation.
2956 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002957 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002958 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002959 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002960 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002961 * \param[in] salt A salt or label, if supported by the
2962 * encryption algorithm.
2963 * If the algorithm does not support a
2964 * salt, pass \c NULL.
2965 * If the algorithm supports an optional
2966 * salt and you do not want to pass a salt,
2967 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002968 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002969 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2970 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002971 * \param salt_length Size of the \p salt buffer in bytes.
2972 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002973 * \param[out] output Buffer where the decrypted message is to
2974 * be written.
2975 * \param output_size Size of the \c output buffer in bytes.
2976 * \param[out] output_length On success, the number of bytes
2977 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002978 *
Gilles Peskine28538492018-07-11 17:34:00 +02002979 * \retval #PSA_SUCCESS
2980 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002981 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002982 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002983 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002984 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002985 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002986 * \retval #PSA_ERROR_NOT_SUPPORTED
2987 * \retval #PSA_ERROR_INVALID_ARGUMENT
2988 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2989 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2990 * \retval #PSA_ERROR_HARDWARE_FAILURE
2991 * \retval #PSA_ERROR_TAMPERING_DETECTED
2992 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2993 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002994 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002995 * The library has not been previously initialized by psa_crypto_init().
2996 * It is implementation-dependent whether a failure to initialize
2997 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002998 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002999psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003000 psa_algorithm_t alg,
3001 const uint8_t *input,
3002 size_t input_length,
3003 const uint8_t *salt,
3004 size_t salt_length,
3005 uint8_t *output,
3006 size_t output_size,
3007 size_t *output_length);
3008
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003009/**@}*/
3010
Gilles Peskineedd76872018-07-20 17:42:05 +02003011/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02003012 * @{
3013 */
3014
3015/** The type of the state data structure for generators.
3016 *
3017 * Before calling any function on a generator, the application must
3018 * initialize it by any of the following means:
3019 * - Set the structure to all-bits-zero, for example:
3020 * \code
3021 * psa_crypto_generator_t generator;
3022 * memset(&generator, 0, sizeof(generator));
3023 * \endcode
3024 * - Initialize the structure to logical zero values, for example:
3025 * \code
3026 * psa_crypto_generator_t generator = {0};
3027 * \endcode
3028 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
3029 * for example:
3030 * \code
3031 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3032 * \endcode
3033 * - Assign the result of the function psa_crypto_generator_init()
3034 * to the structure, for example:
3035 * \code
3036 * psa_crypto_generator_t generator;
3037 * generator = psa_crypto_generator_init();
3038 * \endcode
3039 *
3040 * This is an implementation-defined \c struct. Applications should not
3041 * make any assumptions about the content of this structure except
3042 * as directed by the documentation of a specific implementation.
3043 */
3044typedef struct psa_crypto_generator_s psa_crypto_generator_t;
3045
3046/** \def PSA_CRYPTO_GENERATOR_INIT
3047 *
3048 * This macro returns a suitable initializer for a generator object
3049 * of type #psa_crypto_generator_t.
3050 */
3051#ifdef __DOXYGEN_ONLY__
3052/* This is an example definition for documentation purposes.
3053 * Implementations should define a suitable value in `crypto_struct.h`.
3054 */
3055#define PSA_CRYPTO_GENERATOR_INIT {0}
3056#endif
3057
3058/** Return an initial value for a generator object.
3059 */
3060static psa_crypto_generator_t psa_crypto_generator_init(void);
3061
3062/** Retrieve the current capacity of a generator.
3063 *
3064 * The capacity of a generator is the maximum number of bytes that it can
3065 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
3066 *
3067 * \param[in] generator The generator to query.
3068 * \param[out] capacity On success, the capacity of the generator.
3069 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003070 * \retval #PSA_SUCCESS
3071 * \retval #PSA_ERROR_BAD_STATE
3072 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02003073 */
3074psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3075 size_t *capacity);
3076
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003077/** Set the maximum capacity of a generator.
3078 *
3079 * \param[in,out] generator The generator object to modify.
3080 * \param capacity The new capacity of the generator.
3081 * It must be less or equal to the generator's
3082 * current capacity.
3083 *
3084 * \retval #PSA_SUCCESS
3085 * \retval #PSA_ERROR_INVALID_ARGUMENT
3086 * \p capacity is larger than the generator's current capacity.
3087 * \retval #PSA_ERROR_BAD_STATE
3088 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3089 */
3090psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
3091 size_t capacity);
3092
Gilles Peskineeab56e42018-07-12 17:12:33 +02003093/** Read some data from a generator.
3094 *
3095 * This function reads and returns a sequence of bytes from a generator.
3096 * The data that is read is discarded from the generator. The generator's
3097 * capacity is decreased by the number of bytes read.
3098 *
3099 * \param[in,out] generator The generator object to read from.
3100 * \param[out] output Buffer where the generator output will be
3101 * written.
3102 * \param output_length Number of bytes to output.
3103 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003104 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003105 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02003106 * There were fewer than \p output_length bytes
3107 * in the generator. Note that in this case, no
3108 * output is written to the output buffer.
3109 * The generator's capacity is set to 0, thus
3110 * subsequent calls to this function will not
3111 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003112 * \retval #PSA_ERROR_BAD_STATE
3113 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3114 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3115 * \retval #PSA_ERROR_HARDWARE_FAILURE
3116 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003117 */
3118psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
3119 uint8_t *output,
3120 size_t output_length);
3121
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003122/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003123 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003124 * This function uses the output of a generator to derive a key.
3125 * How much output it consumes and how the key is derived depends on the
3126 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003127 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003128 * - For key types for which the key is an arbitrary sequence of bytes
3129 * of a given size,
3130 * this function is functionally equivalent to calling #psa_generator_read
3131 * and passing the resulting output to #psa_import_key.
3132 * However, this function has a security benefit:
3133 * if the implementation provides an isolation boundary then
3134 * the key material is not exposed outside the isolation boundary.
3135 * As a consequence, for these key types, this function always consumes
3136 * exactly (\p bits / 8) bytes from the generator.
3137 * The following key types defined in this specification follow this scheme:
3138 *
3139 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003140 * - #PSA_KEY_TYPE_ARC4;
3141 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003142 * - #PSA_KEY_TYPE_DERIVE;
3143 * - #PSA_KEY_TYPE_HMAC.
3144 *
3145 * - For ECC keys on a Montgomery elliptic curve
3146 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3147 * Montgomery curve), this function always draws a byte string whose
3148 * length is determined by the curve, and sets the mandatory bits
3149 * accordingly. That is:
3150 *
3151 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3152 * and process it as specified in RFC 7748 &sect;5.
3153 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3154 * and process it as specified in RFC 7748 &sect;5.
3155 *
3156 * - For key types for which the key is represented by a single sequence of
3157 * \p bits bits with constraints as to which bit sequences are acceptable,
3158 * this function draws a byte string of length (\p bits / 8) bytes rounded
3159 * up to the nearest whole number of bytes. If the resulting byte string
3160 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3161 * This process is repeated until an acceptable byte string is drawn.
3162 * The byte string drawn from the generator is interpreted as specified
3163 * for the output produced by psa_export_key().
3164 * The following key types defined in this specification follow this scheme:
3165 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003166 * - #PSA_KEY_TYPE_DES.
3167 * Force-set the parity bits, but discard forbidden weak keys.
3168 * For 2-key and 3-key triple-DES, the three keys are generated
3169 * successively (for example, for 3-key triple-DES,
3170 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3171 * discard the first 8 bytes, use the next 8 bytes as the first key,
3172 * and continue reading output from the generator to derive the other
3173 * two keys).
3174 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
3175 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
3176 * ECC keys on a Weierstrass elliptic curve
3177 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
3178 * Weierstrass curve).
3179 * For these key types, interpret the byte string as integer
3180 * in big-endian order. Discard it if it is not in the range
3181 * [0, *N* - 2] where *N* is the boundary of the private key domain
3182 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003183 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003184 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003185 * This method allows compliance to NIST standards, specifically
3186 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003187 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3188 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3189 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3190 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003191 *
3192 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
3193 * the way in which the generator output is consumed is
3194 * implementation-defined.
3195 *
3196 * In all cases, the data that is read is discarded from the generator.
3197 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003198 *
Gilles Peskine20628592019-04-19 19:29:50 +02003199 * \param[in] attributes The attributes for the new key.
3200 * The key size field in \p attributes is
3201 * ignored; the actual key size is taken
3202 * from the \p bits parameter instead.
3203 * \param[out] handle On success, a handle to the newly created key.
3204 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003205 * \param bits Key size in bits.
3206 * \param[in,out] generator The generator object to read from.
3207 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003208 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003209 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003210 * If the key is persistent, the key material and the key's metadata
3211 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003212 * \retval #PSA_ERROR_ALREADY_EXISTS
3213 * This is an attempt to create a persistent key, and there is
3214 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003215 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003216 * There was not enough data to create the desired key.
3217 * Note that in this case, no output is written to the output buffer.
3218 * The generator's capacity is set to 0, thus subsequent calls to
3219 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003220 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003221 * The key type or key size is not supported, either by the
3222 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003223 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003224 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3225 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3226 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3227 * \retval #PSA_ERROR_HARDWARE_FAILURE
3228 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003229 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003230 * The library has not been previously initialized by psa_crypto_init().
3231 * It is implementation-dependent whether a failure to initialize
3232 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003233 */
Gilles Peskine87a5e562019-04-17 12:28:25 +02003234psa_status_t psa_generator_import_key(const psa_key_attributes_t *attributes,
3235 psa_key_handle_t *handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003236 size_t bits,
3237 psa_crypto_generator_t *generator);
3238
3239/** Abort a generator.
3240 *
3241 * Once a generator has been aborted, its capacity is zero.
3242 * Aborting a generator frees all associated resources except for the
3243 * \c generator structure itself.
3244 *
3245 * This function may be called at any time as long as the generator
3246 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3247 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3248 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3249 * on a generator that has not been set up.
3250 *
3251 * Once aborted, the generator object may be called.
3252 *
3253 * \param[in,out] generator The generator to abort.
3254 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003255 * \retval #PSA_SUCCESS
3256 * \retval #PSA_ERROR_BAD_STATE
3257 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3258 * \retval #PSA_ERROR_HARDWARE_FAILURE
3259 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003260 */
3261psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3262
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003263/** Use the maximum possible capacity for a generator.
3264 *
3265 * Use this value as the capacity argument when setting up a generator
3266 * to indicate that the generator should have the maximum possible capacity.
3267 * The value of the maximum possible capacity depends on the generator
3268 * algorithm.
3269 */
3270#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3271
Gilles Peskineeab56e42018-07-12 17:12:33 +02003272/**@}*/
3273
Gilles Peskineea0fb492018-07-12 17:17:20 +02003274/** \defgroup derivation Key derivation
3275 * @{
3276 */
3277
3278/** Set up a key derivation operation.
3279 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003280 * A key derivation algorithm takes some inputs and uses them to create
3281 * a byte generator which can be used to produce keys and other
3282 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003283 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003284 * To use a generator for key derivation:
3285 * - Start with an initialized object of type #psa_crypto_generator_t.
3286 * - Call psa_key_derivation_setup() to select the algorithm.
3287 * - Provide the inputs for the key derivation by calling
3288 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3289 * as appropriate. Which inputs are needed, in what order, and whether
3290 * they may be keys and if so of what type depends on the algorithm.
3291 * - Optionally set the generator's maximum capacity with
3292 * psa_set_generator_capacity(). You may do this before, in the middle of
3293 * or after providing inputs. For some algorithms, this step is mandatory
3294 * because the output depends on the maximum capacity.
3295 * - Generate output with psa_generator_read() or
3296 * psa_generator_import_key(). Successive calls to these functions
3297 * use successive output bytes from the generator.
3298 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003299 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003300 * \param[in,out] generator The generator object to set up. It must
3301 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003302 * \param alg The key derivation algorithm to compute
3303 * (\c PSA_ALG_XXX value such that
3304 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003305 *
3306 * \retval #PSA_SUCCESS
3307 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003308 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003309 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003310 * \retval #PSA_ERROR_NOT_SUPPORTED
3311 * \c alg is not supported or is not a key derivation algorithm.
3312 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3313 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3314 * \retval #PSA_ERROR_HARDWARE_FAILURE
3315 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003316 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003317 */
3318psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3319 psa_algorithm_t alg);
3320
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003321/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003322 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003323 * Which inputs are required and in what order depends on the algorithm.
3324 * Refer to the documentation of each key derivation or key agreement
3325 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003326 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003327 * This function passes direct inputs. Some inputs must be passed as keys
3328 * using psa_key_derivation_input_key() instead of this function. Refer to
3329 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003330 *
3331 * \param[in,out] generator The generator object to use. It must
3332 * have been set up with
3333 * psa_key_derivation_setup() and must not
3334 * have produced any output yet.
3335 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003336 * \param[in] data Input data to use.
3337 * \param data_length Size of the \p data buffer in bytes.
3338 *
3339 * \retval #PSA_SUCCESS
3340 * Success.
3341 * \retval #PSA_ERROR_INVALID_ARGUMENT
3342 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003343 * \retval #PSA_ERROR_INVALID_ARGUMENT
3344 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003345 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3346 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3347 * \retval #PSA_ERROR_HARDWARE_FAILURE
3348 * \retval #PSA_ERROR_TAMPERING_DETECTED
3349 * \retval #PSA_ERROR_BAD_STATE
3350 * The value of \p step is not valid given the state of \p generator.
3351 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003352 * The library has not been previously initialized by psa_crypto_init().
3353 * It is implementation-dependent whether a failure to initialize
3354 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003355 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003356psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3357 psa_key_derivation_step_t step,
3358 const uint8_t *data,
3359 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003360
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003361/** Provide an input for key derivation in the form of a key.
3362 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003363 * Which inputs are required and in what order depends on the algorithm.
3364 * Refer to the documentation of each key derivation or key agreement
3365 * algorithm for information.
3366 *
3367 * This function passes key inputs. Some inputs must be passed as keys
3368 * of the appropriate type using this function, while others must be
3369 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3370 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003371 *
3372 * \param[in,out] generator The generator object to use. It must
3373 * have been set up with
3374 * psa_key_derivation_setup() and must not
3375 * have produced any output yet.
3376 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003377 * \param handle Handle to the key. It must have an
3378 * appropriate type for \p step and must
3379 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003380 *
3381 * \retval #PSA_SUCCESS
3382 * Success.
3383 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003384 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003385 * \retval #PSA_ERROR_NOT_PERMITTED
3386 * \retval #PSA_ERROR_INVALID_ARGUMENT
3387 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003388 * \retval #PSA_ERROR_INVALID_ARGUMENT
3389 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003390 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3391 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3392 * \retval #PSA_ERROR_HARDWARE_FAILURE
3393 * \retval #PSA_ERROR_TAMPERING_DETECTED
3394 * \retval #PSA_ERROR_BAD_STATE
3395 * The value of \p step is not valid given the state of \p generator.
3396 * \retval #PSA_ERROR_BAD_STATE
3397 * The library has not been previously initialized by psa_crypto_init().
3398 * It is implementation-dependent whether a failure to initialize
3399 * results in this error code.
3400 */
3401psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3402 psa_key_derivation_step_t step,
3403 psa_key_handle_t handle);
3404
Gilles Peskine969c5d62019-01-16 15:53:06 +01003405/** Perform a key agreement and use the shared secret as input to a key
3406 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003407 *
3408 * A key agreement algorithm takes two inputs: a private key \p private_key
3409 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003410 * The result of this function is passed as input to a key derivation.
3411 * The output of this key derivation can be extracted by reading from the
3412 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003413 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003414 * \param[in,out] generator The generator object to use. It must
3415 * have been set up with
3416 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003417 * key agreement and derivation algorithm
3418 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003419 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3420 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003421 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003422 * The generator must be ready for an
3423 * input of the type given by \p step.
3424 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003425 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003426 * \param[in] peer_key Public key of the peer. The peer key must be in the
3427 * same format that psa_import_key() accepts for the
3428 * public key type corresponding to the type of
3429 * private_key. That is, this function performs the
3430 * equivalent of
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003431 * #psa_import_key(`internal_public_key_handle`,
3432 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(`private_key_type`),
3433 * `peer_key`, `peer_key_length`) where
Jaeden Amero8afbff82019-01-14 16:56:20 +00003434 * `private_key_type` is the type of `private_key`.
3435 * For example, for EC keys, this means that peer_key
3436 * is interpreted as a point on the curve that the
3437 * private key is on. The standard formats for public
3438 * keys are documented in the documentation of
3439 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003440 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003441 *
3442 * \retval #PSA_SUCCESS
3443 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003444 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003445 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003446 * \retval #PSA_ERROR_NOT_PERMITTED
3447 * \retval #PSA_ERROR_INVALID_ARGUMENT
3448 * \c private_key is not compatible with \c alg,
3449 * or \p peer_key is not valid for \c alg or not compatible with
3450 * \c private_key.
3451 * \retval #PSA_ERROR_NOT_SUPPORTED
3452 * \c alg is not supported or is not a key derivation algorithm.
3453 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3454 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3455 * \retval #PSA_ERROR_HARDWARE_FAILURE
3456 * \retval #PSA_ERROR_TAMPERING_DETECTED
3457 */
3458psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003459 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003460 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003461 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003462 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003463
Gilles Peskine769c7a62019-01-18 16:42:29 +01003464/** Perform a key agreement and use the shared secret as input to a key
3465 * derivation.
3466 *
3467 * A key agreement algorithm takes two inputs: a private key \p private_key
3468 * a public key \p peer_key.
3469 *
3470 * \warning The raw result of a key agreement algorithm such as finite-field
3471 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3472 * not be used directly as key material. It should instead be passed as
3473 * input to a key derivation algorithm. To chain a key agreement with
3474 * a key derivation, use psa_key_agreement() and other functions from
3475 * the key derivation and generator interface.
3476 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003477 * \param alg The key agreement algorithm to compute
3478 * (\c PSA_ALG_XXX value such that
3479 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3480 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003481 * \param private_key Handle to the private key to use.
3482 * \param[in] peer_key Public key of the peer. It must be
3483 * in the same format that psa_import_key()
3484 * accepts. The standard formats for public
3485 * keys are documented in the documentation
3486 * of psa_export_public_key().
3487 * \param peer_key_length Size of \p peer_key in bytes.
3488 * \param[out] output Buffer where the decrypted message is to
3489 * be written.
3490 * \param output_size Size of the \c output buffer in bytes.
3491 * \param[out] output_length On success, the number of bytes
3492 * that make up the returned output.
3493 *
3494 * \retval #PSA_SUCCESS
3495 * Success.
3496 * \retval #PSA_ERROR_INVALID_HANDLE
3497 * \retval #PSA_ERROR_EMPTY_SLOT
3498 * \retval #PSA_ERROR_NOT_PERMITTED
3499 * \retval #PSA_ERROR_INVALID_ARGUMENT
3500 * \p alg is not a key agreement algorithm
3501 * \retval #PSA_ERROR_INVALID_ARGUMENT
3502 * \p private_key is not compatible with \p alg,
3503 * or \p peer_key is not valid for \p alg or not compatible with
3504 * \p private_key.
3505 * \retval #PSA_ERROR_NOT_SUPPORTED
3506 * \p alg is not a supported key agreement algorithm.
3507 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3508 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3509 * \retval #PSA_ERROR_HARDWARE_FAILURE
3510 * \retval #PSA_ERROR_TAMPERING_DETECTED
3511 */
3512psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3513 psa_key_handle_t private_key,
3514 const uint8_t *peer_key,
3515 size_t peer_key_length,
3516 uint8_t *output,
3517 size_t output_size,
3518 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003519
3520/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003521
3522/** \defgroup random Random generation
3523 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003524 */
3525
3526/**
3527 * \brief Generate random bytes.
3528 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003529 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003530 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003531 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003532 *
3533 * \note To generate a key, use psa_generate_key() instead.
3534 *
3535 * \param[out] output Output buffer for the generated data.
3536 * \param output_size Number of bytes to generate and output.
3537 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003538 * \retval #PSA_SUCCESS
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003539 * \retval #PSA_ERROR_NOT_SUPPORTED
3540 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003541 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003542 * \retval #PSA_ERROR_HARDWARE_FAILURE
3543 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003544 * \retval #PSA_ERROR_BAD_STATE
3545 * The library has not been previously initialized by psa_crypto_init().
3546 * It is implementation-dependent whether a failure to initialize
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003547 * results in this error code.
3548 */
3549psa_status_t psa_generate_random(uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003550 size_t output_size);
3551
3552/** Extra parameters for RSA key generation.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003553 *
Gilles Peskine28538492018-07-11 17:34:00 +02003554 * You may pass a pointer to a structure of this type as the \c extra
3555 * parameter to psa_generate_key().
3556 */
3557typedef struct {
3558 uint32_t e; /**< Public exponent value. Default: 65537. */
3559} psa_generate_key_extra_rsa;
3560
3561/**
itayzafrir90d8c7a2018-09-12 11:44:52 +03003562 * \brief Generate a key or key pair.
itayzafrir18617092018-09-16 12:22:41 +03003563 *
Gilles Peskine20628592019-04-19 19:29:50 +02003564 * \param[in] attributes The attributes for the new key.
3565 * The key size field in \p attributes is
3566 * ignored; the actual key size is taken
3567 * from the \p bits parameter instead.
3568 * \param[out] handle On success, a handle to the newly created key.
3569 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003570 * \param bits Key size in bits.
3571 * \param[in] extra Extra parameters for key generation. The
3572 * interpretation of this parameter depends on
Gilles Peskine20628592019-04-19 19:29:50 +02003573 * the key type \c type. All types support \c NULL to
3574 * use default parameters. Implementation that support
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003575 * the generation of vendor-specific key types
3576 * that allow extra parameters shall document
3577 * the format of these extra parameters and
3578 * the default values. For standard parameters,
3579 * the meaning of \p extra is as follows:
3580 * - For a symmetric key type (a type such
Gilles Peskine20628592019-04-19 19:29:50 +02003581 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\c type) is
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003582 * false), \p extra must be \c NULL.
3583 * - For an elliptic curve key type (a type
Gilles Peskine20628592019-04-19 19:29:50 +02003584 * such that #PSA_KEY_TYPE_IS_ECC(\c type) is
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003585 * false), \p extra must be \c NULL.
Gilles Peskine20628592019-04-19 19:29:50 +02003586 * - For an RSA key (\c type is
Gilles Peskine53d991e2018-07-12 01:14:59 +02003587 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3588 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003589 * specifying the public exponent. The
3590 * default public exponent used when \p extra
3591 * is \c NULL is 65537.
Gilles Peskine20628592019-04-19 19:29:50 +02003592 * - For an DSA key (\c type is
Jaeden Amero1308fb52019-01-11 13:50:43 +00003593 * #PSA_KEY_TYPE_DSA_KEYPAIR), \p extra is an
3594 * optional structure specifying the key domain
3595 * parameters. The key domain parameters can also be
3596 * provided by psa_set_key_domain_parameters(),
3597 * which documents the format of the structure.
Gilles Peskine20628592019-04-19 19:29:50 +02003598 * - For a DH key (\c type is
Jaeden Amero8851c402019-01-11 14:20:03 +00003599 * #PSA_KEY_TYPE_DH_KEYPAIR), the \p extra is an
3600 * optional structure specifying the key domain
3601 * parameters. The key domain parameters can also be
3602 * provided by psa_set_key_domain_parameters(),
3603 * which documents the format of the structure.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003604 * \param extra_size Size of the buffer that \p extra
3605 * points to, in bytes. Note that if \p extra is
3606 * \c NULL then \p extra_size must be zero.
3607 *
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003608 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003609 * Success.
3610 * If the key is persistent, the key material and the key's metadata
3611 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003612 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003613 * This is an attempt to create a persistent key, and there is
3614 * already a persistent key with the given identifier.
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003615 * \retval #PSA_ERROR_NOT_SUPPORTED
3616 * \retval #PSA_ERROR_INVALID_ARGUMENT
3617 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003618 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3619 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3620 * \retval #PSA_ERROR_HARDWARE_FAILURE
3621 * \retval #PSA_ERROR_TAMPERING_DETECTED
3622 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003623 * The library has not been previously initialized by psa_crypto_init().
3624 * It is implementation-dependent whether a failure to initialize
3625 * results in this error code.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003626 */
Gilles Peskine87a5e562019-04-17 12:28:25 +02003627psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
3628 psa_key_handle_t *handle,
Gilles Peskinee59236f2018-01-27 23:32:46 +01003629 size_t bits,
3630 const void *extra,
3631 size_t extra_size);
3632
3633/**@}*/
3634
3635#ifdef __cplusplus
3636}
3637#endif
3638
3639/* The file "crypto_sizes.h" contains definitions for size calculation
3640 * macros whose definitions are implementation-specific. */
3641#include "crypto_sizes.h"
3642
3643/* The file "crypto_struct.h" contains definitions for
3644 * implementation-specific structs that are declared above. */
3645#include "crypto_struct.h"
3646
3647/* The file "crypto_extra.h" contains vendor-specific definitions. This
3648 * can include vendor-defined algorithms, extra functions, etc. */
3649#include "crypto_extra.h"
3650
3651#endif /* PSA_CRYPTO_H */