blob: f787b13696025fbfd2cf5e400037a63ac408ff32 [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
Gilles Peskine4b3eb692019-05-16 21:35:18 +020089 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020090 * \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 Peskine105f67f2019-07-23 18:16:05 +020096/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +020097 * @{
98 */
99
Gilles Peskinea0c06552019-05-21 15:54:54 +0200100/** \def PSA_KEY_ATTRIBUTES_INIT
101 *
102 * This macro returns a suitable initializer for a key attribute structure
103 * of type #psa_key_attributes_t.
104 */
105#ifdef __DOXYGEN_ONLY__
106/* This is an example definition for documentation purposes.
107 * Implementations should define a suitable value in `crypto_struct.h`.
108 */
109#define PSA_KEY_ATTRIBUTES_INIT {0}
110#endif
111
112/** Return an initial value for a key attributes structure.
113 */
114static psa_key_attributes_t psa_key_attributes_init(void);
115
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200116/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200117 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200118 * If the attribute structure currently declares the key as volatile (which
119 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200120 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200121 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200122 * This function does not access storage, it merely stores the given
123 * value in the structure.
124 * The persistent key will be written to storage when the attribute
125 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200126 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200127 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200128 *
Gilles Peskine20628592019-04-19 19:29:50 +0200129 * This function may be declared as `static` (i.e. without external
130 * linkage). This function may be provided as a function-like macro,
131 * but in this case it must evaluate each of its arguments exactly once.
132 *
133 * \param[out] attributes The attribute structure to write to.
134 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200135 */
136static void psa_set_key_id(psa_key_attributes_t *attributes,
137 psa_key_id_t id);
138
139/** Set the location of a persistent key.
140 *
141 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200142 * with psa_set_key_id(). By default, a key that has a persistent identifier
143 * is stored in the default storage area identifier by
144 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
145 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200146 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200147 * This function does not access storage, it merely stores the given
148 * value in the structure.
149 * The persistent key will be written to storage when the attribute
150 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200151 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200152 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200153 *
154 * This function may be declared as `static` (i.e. without external
155 * linkage). This function may be provided as a function-like macro,
156 * but in this case it must evaluate each of its arguments exactly once.
157 *
158 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200159 * \param lifetime The lifetime for the key.
160 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200161 * key will be volatile, and the key identifier
162 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200163 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200164static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
165 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200166
Gilles Peskine20628592019-04-19 19:29:50 +0200167/** Retrieve the key identifier from key attributes.
168 *
169 * This function may be declared as `static` (i.e. without external
170 * linkage). This function may be provided as a function-like macro,
171 * but in this case it must evaluate its argument exactly once.
172 *
173 * \param[in] attributes The key attribute structure to query.
174 *
175 * \return The persistent identifier stored in the attribute structure.
176 * This value is unspecified if the attribute structure declares
177 * the key as volatile.
178 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200179static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
180
Gilles Peskine20628592019-04-19 19:29:50 +0200181/** Retrieve the lifetime from key attributes.
182 *
183 * This function may be declared as `static` (i.e. without external
184 * linkage). This function may be provided as a function-like macro,
185 * but in this case it must evaluate its argument exactly once.
186 *
187 * \param[in] attributes The key attribute structure to query.
188 *
189 * \return The lifetime value stored in the attribute structure.
190 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200191static psa_key_lifetime_t psa_get_key_lifetime(
192 const psa_key_attributes_t *attributes);
193
Gilles Peskine20628592019-04-19 19:29:50 +0200194/** Declare usage flags for a key.
195 *
196 * Usage flags are part of a key's usage policy. They encode what
197 * kind of operations are permitted on the key. For more details,
198 * refer to the documentation of the type #psa_key_usage_t.
199 *
200 * This function overwrites any usage flags
201 * previously set in \p attributes.
202 *
203 * This function may be declared as `static` (i.e. without external
204 * linkage). This function may be provided as a function-like macro,
205 * but in this case it must evaluate each of its arguments exactly once.
206 *
207 * \param[out] attributes The attribute structure to write to.
208 * \param usage_flags The usage flags to write.
209 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200210static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
211 psa_key_usage_t usage_flags);
212
Gilles Peskine20628592019-04-19 19:29:50 +0200213/** Retrieve the usage flags from key attributes.
214 *
215 * This function may be declared as `static` (i.e. without external
216 * linkage). This function may be provided as a function-like macro,
217 * but in this case it must evaluate its argument exactly once.
218 *
219 * \param[in] attributes The key attribute structure to query.
220 *
221 * \return The usage flags stored in the attribute structure.
222 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200223static psa_key_usage_t psa_get_key_usage_flags(
224 const psa_key_attributes_t *attributes);
225
Gilles Peskine20628592019-04-19 19:29:50 +0200226/** Declare the permitted algorithm policy for a key.
227 *
228 * The permitted algorithm policy of a key encodes which algorithm or
229 * algorithms are permitted to be used with this key.
230 *
231 * This function overwrites any algorithm policy
232 * previously set in \p attributes.
233 *
234 * This function may be declared as `static` (i.e. without external
235 * linkage). This function may be provided as a function-like macro,
236 * but in this case it must evaluate each of its arguments exactly once.
237 *
238 * \param[out] attributes The attribute structure to write to.
239 * \param alg The permitted algorithm policy to write.
240 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200241static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
242 psa_algorithm_t alg);
243
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100244
Gilles Peskine20628592019-04-19 19:29:50 +0200245/** Retrieve the algorithm policy from key attributes.
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate its argument exactly once.
250 *
251 * \param[in] attributes The key attribute structure to query.
252 *
253 * \return The algorithm stored in the attribute structure.
254 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200255static psa_algorithm_t psa_get_key_algorithm(
256 const psa_key_attributes_t *attributes);
257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Declare the type of a key.
259 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200260 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200261 * previously set in \p attributes.
262 *
263 * This function may be declared as `static` (i.e. without external
264 * linkage). This function may be provided as a function-like macro,
265 * but in this case it must evaluate each of its arguments exactly once.
266 *
267 * \param[out] attributes The attribute structure to write to.
268 * \param type The key type to write.
269 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200270static void psa_set_key_type(psa_key_attributes_t *attributes,
271 psa_key_type_t type);
272
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100273
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200274/** Declare the size of a key.
275 *
276 * This function overwrites any key size previously set in \p attributes.
277 *
278 * This function may be declared as `static` (i.e. without external
279 * linkage). This function may be provided as a function-like macro,
280 * but in this case it must evaluate each of its arguments exactly once.
281 *
282 * \param[out] attributes The attribute structure to write to.
283 * \param bits The key size in bits.
284 */
285static void psa_set_key_bits(psa_key_attributes_t *attributes,
286 size_t bits);
287
Gilles Peskine20628592019-04-19 19:29:50 +0200288/** Retrieve the key type from key attributes.
289 *
290 * This function may be declared as `static` (i.e. without external
291 * linkage). This function may be provided as a function-like macro,
292 * but in this case it must evaluate its argument exactly once.
293 *
294 * \param[in] attributes The key attribute structure to query.
295 *
296 * \return The key type stored in the attribute structure.
297 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200298static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
299
Gilles Peskine20628592019-04-19 19:29:50 +0200300/** Retrieve the key size from key attributes.
301 *
302 * This function may be declared as `static` (i.e. without external
303 * linkage). This function may be provided as a function-like macro,
304 * but in this case it must evaluate its argument exactly once.
305 *
306 * \param[in] attributes The key attribute structure to query.
307 *
308 * \return The key size stored in the attribute structure, in bits.
309 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200310static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
311
Gilles Peskine20628592019-04-19 19:29:50 +0200312/** Retrieve the attributes of a key.
313 *
314 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200315 * psa_reset_key_attributes(). It then copies the attributes of
316 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200317 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200318 * \note This function may allocate memory or other resources.
319 * Once you have called this function on an attribute structure,
320 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200321 *
Gilles Peskine20628592019-04-19 19:29:50 +0200322 * \param[in] handle Handle to the key to query.
323 * \param[in,out] attributes On success, the attributes of the key.
324 * On failure, equivalent to a
325 * freshly-initialized structure.
326 *
327 * \retval #PSA_SUCCESS
328 * \retval #PSA_ERROR_INVALID_HANDLE
329 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
330 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100331 * \retval #PSA_ERROR_CORRUPTION_DETECTED
332 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine20628592019-04-19 19:29:50 +0200333 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200334psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
335 psa_key_attributes_t *attributes);
336
Gilles Peskine20628592019-04-19 19:29:50 +0200337/** Reset a key attribute structure to a freshly initialized state.
338 *
339 * You must initialize the attribute structure as described in the
340 * documentation of the type #psa_key_attributes_t before calling this
341 * function. Once the structure has been initialized, you may call this
342 * function at any time.
343 *
344 * This function frees any auxiliary resources that the structure
345 * may contain.
346 *
347 * \param[in,out] attributes The attribute structure to reset.
348 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200349void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200350
Gilles Peskine87a5e562019-04-17 12:28:25 +0200351/**@}*/
352
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100353/** \defgroup key_management Key management
354 * @{
355 */
356
Gilles Peskinef535eb22018-11-30 14:08:36 +0100357/** Open a handle to an existing persistent key.
358 *
Gilles Peskine4754cde2019-05-21 15:56:29 +0200359 * Open a handle to a persistent key. A key is persistent if it was created
360 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
361 * always has a nonzero key identifier, set with psa_set_key_id() when
362 * creating the key. Implementations may provide additional pre-provisioned
Andrew Thoelke203491c2019-08-21 17:55:30 +0100363 * keys that can be opened with psa_open_key(). Such keys have a key identifier
364 * in the vendor range, as documented in the description of #psa_key_id_t.
Gilles Peskine4754cde2019-05-21 15:56:29 +0200365 *
366 * The application must eventually close the handle with psa_close_key()
367 * to release associated resources. If the application dies without calling
368 * psa_close_key(), the implementation should perform the equivalent of a
369 * call to psa_close_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100370 *
Andrew Thoelke9741b112019-08-21 18:20:41 +0100371 * Some implementations permit an application to open the same key multiple
372 * times. Applications that rely on this behavior will not be portable to
373 * implementations that only permit a single key handle to be opened. See
374 * also :ref:\`key-handles\`.
375 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100376 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100377 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100378 *
379 * \retval #PSA_SUCCESS
380 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100381 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100382 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100383 * The implementation does not have sufficient resources to open the
384 * key. This can be due to reaching an implementation limit on the
385 * number of open keys, the number of open key handles, or available
386 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200387 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100388 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100389 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100390 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100391 * \retval #PSA_ERROR_NOT_PERMITTED
392 * The specified key exists, but the application does not have the
393 * permission to access it. Note that this specification does not
394 * define any way to create such a key, but it may be possible
395 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200396 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
397 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100398 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200399psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100400 psa_key_handle_t *handle);
401
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100402
Gilles Peskinef535eb22018-11-30 14:08:36 +0100403/** Close a key handle.
404 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100405 * If the handle designates a volatile key, this will destroy the key material
406 * and free all associated resources, just like psa_destroy_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100407 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100408 * If this is the last open handle to a persistent key, then closing the handle
409 * will free all resources associated with the key in volatile memory. The key
410 * data in persistent storage is not affected and can be opened again later
411 * with a call to psa_open_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100412 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100413 * Closing the key handle makes the handle invalid, and the key handle
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100414 * must not be used again by the application.
Andrew Thoelke3daba812019-08-21 22:46:56 +0100415 *
416 * If the key is currently in use in a multipart operation, then closing the
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100417 * last remaining handle to the key will abort the multipart operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100418 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100419 * \param handle The key handle to close.
420 *
421 * \retval #PSA_SUCCESS
422 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100423 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100424 */
425psa_status_t psa_close_key(psa_key_handle_t handle);
426
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100427/**@}*/
428
429/** \defgroup import_export Key import and export
430 * @{
431 */
432
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100433/**
434 * \brief Import a key in binary format.
435 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100436 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100437 * documentation of psa_export_public_key() for the format of public keys
438 * and to the documentation of psa_export_key() for the format for
439 * other key types.
440 *
441 * This specification supports a single format for each key type.
442 * Implementations may support other formats as long as the standard
443 * format is supported. Implementations that support other formats
444 * should ensure that the formats are clearly unambiguous so as to
445 * minimize the risk that an invalid input is accidentally interpreted
446 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100447 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100448
Gilles Peskine20628592019-04-19 19:29:50 +0200449 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200450 * The key size is always determined from the
451 * \p data buffer.
452 * If the key size in \p attributes is nonzero,
453 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200454 * \param[out] handle On success, a handle to the newly created key.
455 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100456 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200457 * buffer is interpreted according to the type declared
458 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200459 * All implementations must support at least the format
460 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100461 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200462 * the chosen type. Implementations may allow other
463 * formats, but should be conservative: implementations
464 * should err on the side of rejecting content if it
465 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200466 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100467 *
Gilles Peskine28538492018-07-11 17:34:00 +0200468 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100469 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100470 * If the key is persistent, the key material and the key's metadata
471 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200472 * \retval #PSA_ERROR_ALREADY_EXISTS
473 * This is an attempt to create a persistent key, and there is
474 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200475 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200476 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200477 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200478 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200479 * The key attributes, as a whole, are invalid.
480 * \retval #PSA_ERROR_INVALID_ARGUMENT
481 * The key data is not correctly formatted.
482 * \retval #PSA_ERROR_INVALID_ARGUMENT
483 * The size in \p attributes is nonzero and does not match the size
484 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200485 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
486 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
487 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100488 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200489 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200490 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300491 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300492 * The library has not been previously initialized by psa_crypto_init().
493 * It is implementation-dependent whether a failure to initialize
494 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100495 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200496psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100497 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200498 size_t data_length,
499 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100500
501/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100502 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200503 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100504 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200505 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100506 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200507 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100508 * This function also erases any metadata such as policies and frees all
509 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200510 *
Andrew Thoelke07f16b72019-08-21 22:48:47 +0100511 * Destroying a key will invalidate all existing handles to the key.
512 *
513 * If the key is currently in use in a multipart operation, then destroying the
514 * key will abort the multipart operation.
515 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100516 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100517 *
Gilles Peskine28538492018-07-11 17:34:00 +0200518 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100519 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200520 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100521 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200522 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100523 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200524 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200525 * There was an failure in communication with the cryptoprocessor.
526 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200527 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200528 * The storage is corrupted. Implementations shall make a best effort
529 * to erase key material even in this stage, however applications
530 * should be aware that it may be impossible to guarantee that the
531 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200532 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200533 * An unexpected condition which is not a storage corruption or
534 * a communication failure occurred. The cryptoprocessor may have
535 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300536 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300537 * The library has not been previously initialized by psa_crypto_init().
538 * It is implementation-dependent whether a failure to initialize
539 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100540 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100541psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542
543/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100544 * \brief Export a key in binary format.
545 *
546 * The output of this function can be passed to psa_import_key() to
547 * create an equivalent object.
548 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100549 * If the implementation of psa_import_key() supports other formats
550 * beyond the format specified here, the output from psa_export_key()
551 * must use the representation specified here, not the original
552 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100553 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100554 * For standard key types, the output format is as follows:
555 *
556 * - For symmetric keys (including MAC keys), the format is the
557 * raw bytes of the key.
558 * - For DES, the key data consists of 8 bytes. The parity bits must be
559 * correct.
560 * - For Triple-DES, the format is the concatenation of the
561 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200562 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200563 * is the non-encrypted DER encoding of the representation defined by
564 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
565 * ```
566 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200567 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200568 * modulus INTEGER, -- n
569 * publicExponent INTEGER, -- e
570 * privateExponent INTEGER, -- d
571 * prime1 INTEGER, -- p
572 * prime2 INTEGER, -- q
573 * exponent1 INTEGER, -- d mod (p-1)
574 * exponent2 INTEGER, -- d mod (q-1)
575 * coefficient INTEGER, -- (inverse of q) mod p
576 * }
577 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200578 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200579 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100580 * a representation of the private value as a `ceiling(m/8)`-byte string
581 * where `m` is the bit size associated with the curve, i.e. the bit size
582 * of the order of the curve's coordinate field. This byte string is
583 * in little-endian order for Montgomery curves (curve types
584 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
585 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
586 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100587 * This is the content of the `privateKey` field of the `ECPrivateKey`
588 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200589 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200590 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000591 * format is the representation of the private key `x` as a big-endian byte
592 * string. The length of the byte string is the private key size in bytes
593 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200594 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
595 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100596 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200597 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
598 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100599 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200600 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200601 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200602 * \param[out] data_length On success, the number of bytes
603 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100604 *
Gilles Peskine28538492018-07-11 17:34:00 +0200605 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100606 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200607 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200608 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100609 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200610 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
611 * The size of the \p data buffer is too small. You can determine a
612 * sufficient buffer size by calling
613 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
614 * where \c type is the key type
615 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200616 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
617 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200618 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100619 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100620 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300621 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300622 * The library has not been previously initialized by psa_crypto_init().
623 * It is implementation-dependent whether a failure to initialize
624 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100625 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100626psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100627 uint8_t *data,
628 size_t data_size,
629 size_t *data_length);
630
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100631/**
632 * \brief Export a public key or the public part of a key pair in binary format.
633 *
634 * The output of this function can be passed to psa_import_key() to
635 * create an object that is equivalent to the public key.
636 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000637 * This specification supports a single format for each key type.
638 * Implementations may support other formats as long as the standard
639 * format is supported. Implementations that support other formats
640 * should ensure that the formats are clearly unambiguous so as to
641 * minimize the risk that an invalid input is accidentally interpreted
642 * according to a different format.
643 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000644 * For standard key types, the output format is as follows:
645 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
646 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
647 * ```
648 * RSAPublicKey ::= SEQUENCE {
649 * modulus INTEGER, -- n
650 * publicExponent INTEGER } -- e
651 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000652 * - For elliptic curve public keys (key types for which
653 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
654 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
655 * Let `m` be the bit size associated with the curve, i.e. the bit size of
656 * `q` for a curve over `F_q`. The representation consists of:
657 * - The byte 0x04;
658 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
659 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200660 * - For Diffie-Hellman key exchange public keys (key types for which
661 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000662 * the format is the representation of the public key `y = g^x mod p` as a
663 * big-endian byte string. The length of the byte string is the length of the
664 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100665 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200666 * Exporting a public key object or the public part of a key pair is
667 * always permitted, regardless of the key's usage flags.
668 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100669 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200670 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200671 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200672 * \param[out] data_length On success, the number of bytes
673 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100674 *
Gilles Peskine28538492018-07-11 17:34:00 +0200675 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100676 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200677 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200678 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200679 * The key is neither a public key nor a key pair.
680 * \retval #PSA_ERROR_NOT_SUPPORTED
681 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
682 * The size of the \p data buffer is too small. You can determine a
683 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200684 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200685 * where \c type is the key type
686 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200687 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
688 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200689 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300690 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300691 * The library has not been previously initialized by psa_crypto_init().
692 * It is implementation-dependent whether a failure to initialize
693 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100694 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100695psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100696 uint8_t *data,
697 size_t data_size,
698 size_t *data_length);
699
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100700/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100701 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100702 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000703 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100704 * This function is primarily useful to copy a key from one location
705 * to another, since it populates a key using the material from
706 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200707 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100708 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100709 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100710 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200711 * The policy on the source key must have the usage flag
712 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200713 * This flag is sufficient to permit the copy if the key has the lifetime
714 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
715 * Some secure elements do not provide a way to copy a key without
716 * making it extractable from the secure element. If a key is located
717 * in such a secure element, then the key must have both usage flags
718 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
719 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200720 *
Gilles Peskine20628592019-04-19 19:29:50 +0200721 * The resulting key may only be used in a way that conforms to
722 * both the policy of the original key and the policy specified in
723 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100724 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200725 * usage flags on the source policy and the usage flags in \p attributes.
726 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100727 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200728 * - If either of the policies allows an algorithm and the other policy
729 * allows a wildcard-based algorithm policy that includes this algorithm,
730 * the resulting key allows the same algorithm.
731 * - If the policies do not allow any algorithm in common, this function
732 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200733 *
Gilles Peskine20628592019-04-19 19:29:50 +0200734 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100735 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200736 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100737 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200738 * \param[in] attributes The attributes for the new key.
739 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200740 * - The key type and size may be 0. If either is
741 * nonzero, it must match the corresponding
742 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200743 * - The key location (the lifetime and, for
744 * persistent keys, the key identifier) is
745 * used directly.
746 * - The policy constraints (usage flags and
747 * algorithm policy) are combined from
748 * the source key and \p attributes so that
749 * both sets of restrictions apply, as
750 * described in the documentation of this function.
751 * \param[out] target_handle On success, a handle to the newly created key.
752 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200753 *
754 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100755 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200756 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200757 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200758 * This is an attempt to create a persistent key, and there is
759 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200760 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200761 * The lifetime or identifier in \p attributes are invalid.
762 * \retval #PSA_ERROR_INVALID_ARGUMENT
763 * The policy constraints on the source and specified in
764 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200765 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200766 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200767 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100768 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200769 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
770 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100771 * The source key is not exportable and its lifetime does not
772 * allow copying it to the target's lifetime.
773 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
774 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200775 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
776 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200777 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100778 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100779psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200780 const psa_key_attributes_t *attributes,
781 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100782
783/**@}*/
784
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100785/** \defgroup hash Message digests
786 * @{
787 */
788
Gilles Peskine69647a42019-01-14 20:18:12 +0100789/** Calculate the hash (digest) of a message.
790 *
791 * \note To verify the hash of a message against an
792 * expected value, use psa_hash_compare() instead.
793 *
794 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
795 * such that #PSA_ALG_IS_HASH(\p alg) is true).
796 * \param[in] input Buffer containing the message to hash.
797 * \param input_length Size of the \p input buffer in bytes.
798 * \param[out] hash Buffer where the hash is to be written.
799 * \param hash_size Size of the \p hash buffer in bytes.
800 * \param[out] hash_length On success, the number of bytes
801 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100802 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100803 *
804 * \retval #PSA_SUCCESS
805 * Success.
806 * \retval #PSA_ERROR_NOT_SUPPORTED
807 * \p alg is not supported or is not a hash algorithm.
808 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
809 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
810 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200811 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100812 */
813psa_status_t psa_hash_compute(psa_algorithm_t alg,
814 const uint8_t *input,
815 size_t input_length,
816 uint8_t *hash,
817 size_t hash_size,
818 size_t *hash_length);
819
820/** Calculate the hash (digest) of a message and compare it with a
821 * reference value.
822 *
823 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
824 * such that #PSA_ALG_IS_HASH(\p alg) is true).
825 * \param[in] input Buffer containing the message to hash.
826 * \param input_length Size of the \p input buffer in bytes.
827 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100828 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100829 *
830 * \retval #PSA_SUCCESS
831 * The expected hash is identical to the actual hash of the input.
832 * \retval #PSA_ERROR_INVALID_SIGNATURE
833 * The hash of the message was calculated successfully, but it
834 * differs from the expected hash.
835 * \retval #PSA_ERROR_NOT_SUPPORTED
836 * \p alg is not supported or is not a hash algorithm.
837 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
838 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
839 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200840 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100841 */
842psa_status_t psa_hash_compare(psa_algorithm_t alg,
843 const uint8_t *input,
844 size_t input_length,
845 const uint8_t *hash,
846 const size_t hash_length);
847
Gilles Peskine308b91d2018-02-08 09:47:44 +0100848/** The type of the state data structure for multipart hash operations.
849 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000850 * Before calling any function on a hash operation object, the application must
851 * initialize it by any of the following means:
852 * - Set the structure to all-bits-zero, for example:
853 * \code
854 * psa_hash_operation_t operation;
855 * memset(&operation, 0, sizeof(operation));
856 * \endcode
857 * - Initialize the structure to logical zero values, for example:
858 * \code
859 * psa_hash_operation_t operation = {0};
860 * \endcode
861 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
862 * for example:
863 * \code
864 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
865 * \endcode
866 * - Assign the result of the function psa_hash_operation_init()
867 * to the structure, for example:
868 * \code
869 * psa_hash_operation_t operation;
870 * operation = psa_hash_operation_init();
871 * \endcode
872 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100873 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100874 * make any assumptions about the content of this structure except
875 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100876typedef struct psa_hash_operation_s psa_hash_operation_t;
877
Jaeden Amero6a25b412019-01-04 11:47:44 +0000878/** \def PSA_HASH_OPERATION_INIT
879 *
880 * This macro returns a suitable initializer for a hash operation object
881 * of type #psa_hash_operation_t.
882 */
883#ifdef __DOXYGEN_ONLY__
884/* This is an example definition for documentation purposes.
885 * Implementations should define a suitable value in `crypto_struct.h`.
886 */
887#define PSA_HASH_OPERATION_INIT {0}
888#endif
889
890/** Return an initial value for a hash operation object.
891 */
892static psa_hash_operation_t psa_hash_operation_init(void);
893
Gilles Peskinef45adda2019-01-14 18:29:18 +0100894/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100895 *
896 * The sequence of operations to calculate a hash (message digest)
897 * is as follows:
898 * -# Allocate an operation object which will be passed to all the functions
899 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000900 * -# Initialize the operation object with one of the methods described in the
901 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200902 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100903 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100904 * of the message each time. The hash that is calculated is the hash
905 * of the concatenation of these messages in order.
906 * -# To calculate the hash, call psa_hash_finish().
907 * To compare the hash with an expected value, call psa_hash_verify().
908 *
909 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000910 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100911 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200912 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100913 * eventually terminate the operation. The following events terminate an
914 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100915 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100916 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100917 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000918 * \param[in,out] operation The operation object to set up. It must have
919 * been initialized as per the documentation for
920 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200921 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
922 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100923 *
Gilles Peskine28538492018-07-11 17:34:00 +0200924 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100925 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200926 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200927 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100928 * \retval #PSA_ERROR_BAD_STATE
929 * The operation state is not valid (already set up and not
930 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200931 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
932 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
933 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200934 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100935 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200936psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100937 psa_algorithm_t alg);
938
Gilles Peskine308b91d2018-02-08 09:47:44 +0100939/** Add a message fragment to a multipart hash operation.
940 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200941 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100942 *
943 * If this function returns an error status, the operation becomes inactive.
944 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200945 * \param[in,out] operation Active hash operation.
946 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200947 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100948 *
Gilles Peskine28538492018-07-11 17:34:00 +0200949 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100950 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200951 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100952 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200953 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
954 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
955 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200956 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100957 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100958psa_status_t psa_hash_update(psa_hash_operation_t *operation,
959 const uint8_t *input,
960 size_t input_length);
961
Gilles Peskine308b91d2018-02-08 09:47:44 +0100962/** Finish the calculation of the hash of a message.
963 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200964 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100965 * This function calculates the hash of the message formed by concatenating
966 * the inputs passed to preceding calls to psa_hash_update().
967 *
968 * When this function returns, the operation becomes inactive.
969 *
970 * \warning Applications should not call this function if they expect
971 * a specific value for the hash. Call psa_hash_verify() instead.
972 * Beware that comparing integrity or authenticity data such as
973 * hash values with a function such as \c memcmp is risky
974 * because the time taken by the comparison may leak information
975 * about the hashed data which could allow an attacker to guess
976 * a valid hash and thereby bypass security controls.
977 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200978 * \param[in,out] operation Active hash operation.
979 * \param[out] hash Buffer where the hash is to be written.
980 * \param hash_size Size of the \p hash buffer in bytes.
981 * \param[out] hash_length On success, the number of bytes
982 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200983 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200984 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100985 *
Gilles Peskine28538492018-07-11 17:34:00 +0200986 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100987 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200988 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100989 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200990 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200991 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200992 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100993 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200994 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
995 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
996 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200997 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100998 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100999psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1000 uint8_t *hash,
1001 size_t hash_size,
1002 size_t *hash_length);
1003
Gilles Peskine308b91d2018-02-08 09:47:44 +01001004/** Finish the calculation of the hash of a message and compare it with
1005 * an expected value.
1006 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001007 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001008 * This function calculates the hash of the message formed by concatenating
1009 * the inputs passed to preceding calls to psa_hash_update(). It then
1010 * compares the calculated hash with the expected hash passed as a
1011 * parameter to this function.
1012 *
1013 * When this function returns, the operation becomes inactive.
1014 *
Gilles Peskine19067982018-03-20 17:54:53 +01001015 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001016 * comparison between the actual hash and the expected hash is performed
1017 * in constant time.
1018 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001019 * \param[in,out] operation Active hash operation.
1020 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001021 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001022 *
Gilles Peskine28538492018-07-11 17:34:00 +02001023 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001024 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001025 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001026 * The hash of the message was calculated successfully, but it
1027 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001028 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001029 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001030 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1031 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1032 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001033 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001034 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001035psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1036 const uint8_t *hash,
1037 size_t hash_length);
1038
Gilles Peskine308b91d2018-02-08 09:47:44 +01001039/** Abort a hash operation.
1040 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001041 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001042 * \p operation structure itself. Once aborted, the operation object
1043 * can be reused for another operation by calling
1044 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001045 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001046 * You may call this function any time after the operation object has
1047 * been initialized by any of the following methods:
1048 * - A call to psa_hash_setup(), whether it succeeds or not.
1049 * - Initializing the \c struct to all-bits-zero.
1050 * - Initializing the \c struct to logical zeros, e.g.
1051 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001052 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001053 * In particular, calling psa_hash_abort() after the operation has been
1054 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1055 * psa_hash_verify() is safe and has no effect.
1056 *
1057 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001058 *
Gilles Peskine28538492018-07-11 17:34:00 +02001059 * \retval #PSA_SUCCESS
1060 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001061 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001062 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1063 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001064 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001065 */
1066psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001067
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001068/** Clone a hash operation.
1069 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001070 * This function copies the state of an ongoing hash operation to
1071 * a new operation object. In other words, this function is equivalent
1072 * to calling psa_hash_setup() on \p target_operation with the same
1073 * algorithm that \p source_operation was set up for, then
1074 * psa_hash_update() on \p target_operation with the same input that
1075 * that was passed to \p source_operation. After this function returns, the
1076 * two objects are independent, i.e. subsequent calls involving one of
1077 * the objects do not affect the other object.
1078 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001079 * \param[in] source_operation The active hash operation to clone.
1080 * \param[in,out] target_operation The operation object to set up.
1081 * It must be initialized but not active.
1082 *
1083 * \retval #PSA_SUCCESS
1084 * \retval #PSA_ERROR_BAD_STATE
1085 * \p source_operation is not an active hash operation.
1086 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001087 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001088 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1089 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001090 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001091 */
1092psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1093 psa_hash_operation_t *target_operation);
1094
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001095/**@}*/
1096
Gilles Peskine8c9def32018-02-08 10:02:12 +01001097/** \defgroup MAC Message authentication codes
1098 * @{
1099 */
1100
Gilles Peskine69647a42019-01-14 20:18:12 +01001101/** Calculate the MAC (message authentication code) of a message.
1102 *
1103 * \note To verify the MAC of a message against an
1104 * expected value, use psa_mac_verify() instead.
1105 * Beware that comparing integrity or authenticity data such as
1106 * MAC values with a function such as \c memcmp is risky
1107 * because the time taken by the comparison may leak information
1108 * about the MAC value which could allow an attacker to guess
1109 * a valid MAC and thereby bypass security controls.
1110 *
1111 * \param handle Handle to the key to use for the operation.
1112 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001113 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001114 * \param[in] input Buffer containing the input message.
1115 * \param input_length Size of the \p input buffer in bytes.
1116 * \param[out] mac Buffer where the MAC value is to be written.
1117 * \param mac_size Size of the \p mac buffer in bytes.
1118 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001119 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001120 *
1121 * \retval #PSA_SUCCESS
1122 * Success.
1123 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001124 * \retval #PSA_ERROR_NOT_PERMITTED
1125 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001126 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001127 * \retval #PSA_ERROR_NOT_SUPPORTED
1128 * \p alg is not supported or is not a MAC algorithm.
1129 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1130 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1131 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001132 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001133 * \retval #PSA_ERROR_BAD_STATE
1134 * The library has not been previously initialized by psa_crypto_init().
1135 * It is implementation-dependent whether a failure to initialize
1136 * results in this error code.
1137 */
1138psa_status_t psa_mac_compute(psa_key_handle_t handle,
1139 psa_algorithm_t alg,
1140 const uint8_t *input,
1141 size_t input_length,
1142 uint8_t *mac,
1143 size_t mac_size,
1144 size_t *mac_length);
1145
1146/** Calculate the MAC of a message and compare it with a reference value.
1147 *
1148 * \param handle Handle to the key to use for the operation.
1149 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001150 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001151 * \param[in] input Buffer containing the input message.
1152 * \param input_length Size of the \p input buffer in bytes.
1153 * \param[out] mac Buffer containing the expected MAC value.
1154 * \param mac_length Size of the \p mac buffer in bytes.
1155 *
1156 * \retval #PSA_SUCCESS
1157 * The expected MAC is identical to the actual MAC of the input.
1158 * \retval #PSA_ERROR_INVALID_SIGNATURE
1159 * The MAC of the message was calculated successfully, but it
1160 * differs from the expected value.
1161 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001162 * \retval #PSA_ERROR_NOT_PERMITTED
1163 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001164 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001165 * \retval #PSA_ERROR_NOT_SUPPORTED
1166 * \p alg is not supported or is not a MAC algorithm.
1167 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1168 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1169 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001170 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001171 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001172psa_status_t psa_mac_verify(psa_key_handle_t handle,
1173 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001174 const uint8_t *input,
1175 size_t input_length,
1176 const uint8_t *mac,
1177 const size_t mac_length);
1178
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001179/** The type of the state data structure for multipart MAC operations.
1180 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001181 * Before calling any function on a MAC operation object, the application must
1182 * initialize it by any of the following means:
1183 * - Set the structure to all-bits-zero, for example:
1184 * \code
1185 * psa_mac_operation_t operation;
1186 * memset(&operation, 0, sizeof(operation));
1187 * \endcode
1188 * - Initialize the structure to logical zero values, for example:
1189 * \code
1190 * psa_mac_operation_t operation = {0};
1191 * \endcode
1192 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1193 * for example:
1194 * \code
1195 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1196 * \endcode
1197 * - Assign the result of the function psa_mac_operation_init()
1198 * to the structure, for example:
1199 * \code
1200 * psa_mac_operation_t operation;
1201 * operation = psa_mac_operation_init();
1202 * \endcode
1203 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001204 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001205 * make any assumptions about the content of this structure except
1206 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001207typedef struct psa_mac_operation_s psa_mac_operation_t;
1208
Jaeden Amero769ce272019-01-04 11:48:03 +00001209/** \def PSA_MAC_OPERATION_INIT
1210 *
1211 * This macro returns a suitable initializer for a MAC operation object of type
1212 * #psa_mac_operation_t.
1213 */
1214#ifdef __DOXYGEN_ONLY__
1215/* This is an example definition for documentation purposes.
1216 * Implementations should define a suitable value in `crypto_struct.h`.
1217 */
1218#define PSA_MAC_OPERATION_INIT {0}
1219#endif
1220
1221/** Return an initial value for a MAC operation object.
1222 */
1223static psa_mac_operation_t psa_mac_operation_init(void);
1224
Gilles Peskinef45adda2019-01-14 18:29:18 +01001225/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001226 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001227 * This function sets up the calculation of the MAC
1228 * (message authentication code) of a byte string.
1229 * To verify the MAC of a message against an
1230 * expected value, use psa_mac_verify_setup() instead.
1231 *
1232 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001233 * -# Allocate an operation object which will be passed to all the functions
1234 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001235 * -# Initialize the operation object with one of the methods described in the
1236 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001237 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001238 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1239 * of the message each time. The MAC that is calculated is the MAC
1240 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001241 * -# At the end of the message, call psa_mac_sign_finish() to finish
1242 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001243 *
1244 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001245 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001246 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001247 * After a successful call to psa_mac_sign_setup(), the application must
1248 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001249 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001250 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001251 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001252 * \param[in,out] operation The operation object to set up. It must have
1253 * been initialized as per the documentation for
1254 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001255 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001256 * It must remain valid until the operation
1257 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001258 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001259 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001260 *
Gilles Peskine28538492018-07-11 17:34:00 +02001261 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001262 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001263 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001264 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001265 * \retval #PSA_ERROR_NOT_PERMITTED
1266 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001267 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001268 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001269 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001270 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1271 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1272 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001273 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001274 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001275 * The operation state is not valid (already set up and not
1276 * subsequently completed).
1277 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001278 * The library has not been previously initialized by psa_crypto_init().
1279 * It is implementation-dependent whether a failure to initialize
1280 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001281 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001282psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001283 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001284 psa_algorithm_t alg);
1285
Gilles Peskinef45adda2019-01-14 18:29:18 +01001286/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001287 *
1288 * This function sets up the verification of the MAC
1289 * (message authentication code) of a byte string against an expected value.
1290 *
1291 * The sequence of operations to verify a MAC is as follows:
1292 * -# Allocate an operation object which will be passed to all the functions
1293 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001294 * -# Initialize the operation object with one of the methods described in the
1295 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001296 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001297 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1298 * of the message each time. The MAC that is calculated is the MAC
1299 * of the concatenation of these messages in order.
1300 * -# At the end of the message, call psa_mac_verify_finish() to finish
1301 * calculating the actual MAC of the message and verify it against
1302 * the expected value.
1303 *
1304 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001305 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001306 *
1307 * After a successful call to psa_mac_verify_setup(), the application must
1308 * eventually terminate the operation through one of the following methods:
1309 * - A failed call to psa_mac_update().
1310 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1311 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001312 * \param[in,out] operation The operation object to set up. It must have
1313 * been initialized as per the documentation for
1314 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001315 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001316 * It must remain valid until the operation
1317 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001318 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1319 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001320 *
Gilles Peskine28538492018-07-11 17:34:00 +02001321 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001322 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001323 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001324 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001325 * \retval #PSA_ERROR_NOT_PERMITTED
1326 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001327 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001328 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001329 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001330 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1331 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1332 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001333 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001334 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001335 * The operation state is not valid (already set up and not
1336 * subsequently completed).
1337 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001338 * The library has not been previously initialized by psa_crypto_init().
1339 * It is implementation-dependent whether a failure to initialize
1340 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001341 */
1342psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001343 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001344 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001345
Gilles Peskinedcd14942018-07-12 00:30:52 +02001346/** Add a message fragment to a multipart MAC operation.
1347 *
1348 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1349 * before calling this function.
1350 *
1351 * If this function returns an error status, the operation becomes inactive.
1352 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001353 * \param[in,out] operation Active MAC operation.
1354 * \param[in] input Buffer containing the message fragment to add to
1355 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001356 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001357 *
1358 * \retval #PSA_SUCCESS
1359 * Success.
1360 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001361 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001362 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1363 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1364 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001365 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001366 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001367psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1368 const uint8_t *input,
1369 size_t input_length);
1370
Gilles Peskinedcd14942018-07-12 00:30:52 +02001371/** Finish the calculation of the MAC of a message.
1372 *
1373 * The application must call psa_mac_sign_setup() before calling this function.
1374 * This function calculates the MAC of the message formed by concatenating
1375 * the inputs passed to preceding calls to psa_mac_update().
1376 *
1377 * When this function returns, the operation becomes inactive.
1378 *
1379 * \warning Applications should not call this function if they expect
1380 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1381 * Beware that comparing integrity or authenticity data such as
1382 * MAC values with a function such as \c memcmp is risky
1383 * because the time taken by the comparison may leak information
1384 * about the MAC value which could allow an attacker to guess
1385 * a valid MAC and thereby bypass security controls.
1386 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001387 * \param[in,out] operation Active MAC operation.
1388 * \param[out] mac Buffer where the MAC value is to be written.
1389 * \param mac_size Size of the \p mac buffer in bytes.
1390 * \param[out] mac_length On success, the number of bytes
1391 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001392 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001393 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001394 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001395 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001396 *
1397 * \retval #PSA_SUCCESS
1398 * Success.
1399 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001400 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001401 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001402 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001403 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1404 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1405 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1406 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001407 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001408 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001409psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1410 uint8_t *mac,
1411 size_t mac_size,
1412 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001413
Gilles Peskinedcd14942018-07-12 00:30:52 +02001414/** Finish the calculation of the MAC of a message and compare it with
1415 * an expected value.
1416 *
1417 * The application must call psa_mac_verify_setup() before calling this function.
1418 * This function calculates the MAC of the message formed by concatenating
1419 * the inputs passed to preceding calls to psa_mac_update(). It then
1420 * compares the calculated MAC with the expected MAC passed as a
1421 * parameter to this function.
1422 *
1423 * When this function returns, the operation becomes inactive.
1424 *
1425 * \note Implementations shall make the best effort to ensure that the
1426 * comparison between the actual MAC and the expected MAC is performed
1427 * in constant time.
1428 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001429 * \param[in,out] operation Active MAC operation.
1430 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001431 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001432 *
1433 * \retval #PSA_SUCCESS
1434 * The expected MAC is identical to the actual MAC of the message.
1435 * \retval #PSA_ERROR_INVALID_SIGNATURE
1436 * The MAC of the message was calculated successfully, but it
1437 * differs from the expected MAC.
1438 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001439 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001440 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1441 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1442 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001443 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001444 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001445psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1446 const uint8_t *mac,
1447 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001448
Gilles Peskinedcd14942018-07-12 00:30:52 +02001449/** Abort a MAC operation.
1450 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001451 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001452 * \p operation structure itself. Once aborted, the operation object
1453 * can be reused for another operation by calling
1454 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001455 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001456 * You may call this function any time after the operation object has
1457 * been initialized by any of the following methods:
1458 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1459 * it succeeds or not.
1460 * - Initializing the \c struct to all-bits-zero.
1461 * - Initializing the \c struct to logical zeros, e.g.
1462 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001463 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001464 * In particular, calling psa_mac_abort() after the operation has been
1465 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1466 * psa_mac_verify_finish() is safe and has no effect.
1467 *
1468 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001469 *
1470 * \retval #PSA_SUCCESS
1471 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001472 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001473 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1474 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001475 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001476 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001477psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1478
1479/**@}*/
1480
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001481/** \defgroup cipher Symmetric ciphers
1482 * @{
1483 */
1484
Gilles Peskine69647a42019-01-14 20:18:12 +01001485/** Encrypt a message using a symmetric cipher.
1486 *
1487 * This function encrypts a message with a random IV (initialization
1488 * vector).
1489 *
1490 * \param handle Handle to the key to use for the operation.
1491 * It must remain valid until the operation
1492 * terminates.
1493 * \param alg The cipher algorithm to compute
1494 * (\c PSA_ALG_XXX value such that
1495 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1496 * \param[in] input Buffer containing the message to encrypt.
1497 * \param input_length Size of the \p input buffer in bytes.
1498 * \param[out] output Buffer where the output is to be written.
1499 * The output contains the IV followed by
1500 * the ciphertext proper.
1501 * \param output_size Size of the \p output buffer in bytes.
1502 * \param[out] output_length On success, the number of bytes
1503 * that make up the output.
1504 *
1505 * \retval #PSA_SUCCESS
1506 * Success.
1507 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001508 * \retval #PSA_ERROR_NOT_PERMITTED
1509 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001510 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001511 * \retval #PSA_ERROR_NOT_SUPPORTED
1512 * \p alg is not supported or is not a cipher algorithm.
1513 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1514 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1515 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1516 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001517 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001518 */
1519psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1520 psa_algorithm_t alg,
1521 const uint8_t *input,
1522 size_t input_length,
1523 uint8_t *output,
1524 size_t output_size,
1525 size_t *output_length);
1526
1527/** Decrypt a message using a symmetric cipher.
1528 *
1529 * This function decrypts a message encrypted with a symmetric cipher.
1530 *
1531 * \param handle Handle to the key to use for the operation.
1532 * It must remain valid until the operation
1533 * terminates.
1534 * \param alg The cipher algorithm to compute
1535 * (\c PSA_ALG_XXX value such that
1536 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1537 * \param[in] input Buffer containing the message to decrypt.
1538 * This consists of the IV followed by the
1539 * ciphertext proper.
1540 * \param input_length Size of the \p input buffer in bytes.
1541 * \param[out] output Buffer where the plaintext is to be written.
1542 * \param output_size Size of the \p output buffer in bytes.
1543 * \param[out] output_length On success, the number of bytes
1544 * that make up the output.
1545 *
1546 * \retval #PSA_SUCCESS
1547 * Success.
1548 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001549 * \retval #PSA_ERROR_NOT_PERMITTED
1550 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001551 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001552 * \retval #PSA_ERROR_NOT_SUPPORTED
1553 * \p alg is not supported or is not a cipher algorithm.
1554 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1555 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1556 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1557 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001558 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001559 */
1560psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1561 psa_algorithm_t alg,
1562 const uint8_t *input,
1563 size_t input_length,
1564 uint8_t *output,
1565 size_t output_size,
1566 size_t *output_length);
1567
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001568/** The type of the state data structure for multipart cipher operations.
1569 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001570 * Before calling any function on a cipher operation object, the application
1571 * must initialize it by any of the following means:
1572 * - Set the structure to all-bits-zero, for example:
1573 * \code
1574 * psa_cipher_operation_t operation;
1575 * memset(&operation, 0, sizeof(operation));
1576 * \endcode
1577 * - Initialize the structure to logical zero values, for example:
1578 * \code
1579 * psa_cipher_operation_t operation = {0};
1580 * \endcode
1581 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1582 * for example:
1583 * \code
1584 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1585 * \endcode
1586 * - Assign the result of the function psa_cipher_operation_init()
1587 * to the structure, for example:
1588 * \code
1589 * psa_cipher_operation_t operation;
1590 * operation = psa_cipher_operation_init();
1591 * \endcode
1592 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001593 * This is an implementation-defined \c struct. Applications should not
1594 * make any assumptions about the content of this structure except
1595 * as directed by the documentation of a specific implementation. */
1596typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1597
Jaeden Amero5bae2272019-01-04 11:48:27 +00001598/** \def PSA_CIPHER_OPERATION_INIT
1599 *
1600 * This macro returns a suitable initializer for a cipher operation object of
1601 * type #psa_cipher_operation_t.
1602 */
1603#ifdef __DOXYGEN_ONLY__
1604/* This is an example definition for documentation purposes.
1605 * Implementations should define a suitable value in `crypto_struct.h`.
1606 */
1607#define PSA_CIPHER_OPERATION_INIT {0}
1608#endif
1609
1610/** Return an initial value for a cipher operation object.
1611 */
1612static psa_cipher_operation_t psa_cipher_operation_init(void);
1613
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001614/** Set the key for a multipart symmetric encryption operation.
1615 *
1616 * The sequence of operations to encrypt a message with a symmetric cipher
1617 * is as follows:
1618 * -# Allocate an operation object which will be passed to all the functions
1619 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001620 * -# Initialize the operation object with one of the methods described in the
1621 * documentation for #psa_cipher_operation_t, e.g.
1622 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001623 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001624 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001625 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001626 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001627 * requires a specific IV value.
1628 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1629 * of the message each time.
1630 * -# Call psa_cipher_finish().
1631 *
1632 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001633 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001634 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001635 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001636 * eventually terminate the operation. The following events terminate an
1637 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001638 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001639 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001640 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001641 * \param[in,out] operation The operation object to set up. It must have
1642 * been initialized as per the documentation for
1643 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001644 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001645 * It must remain valid until the operation
1646 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001647 * \param alg The cipher algorithm to compute
1648 * (\c PSA_ALG_XXX value such that
1649 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001650 *
Gilles Peskine28538492018-07-11 17:34:00 +02001651 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001652 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001653 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001654 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001655 * \retval #PSA_ERROR_NOT_PERMITTED
1656 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001657 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001658 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001659 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001660 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1661 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1662 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001663 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001664 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001665 * The operation state is not valid (already set up and not
1666 * subsequently completed).
1667 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001668 * The library has not been previously initialized by psa_crypto_init().
1669 * It is implementation-dependent whether a failure to initialize
1670 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001671 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001672psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001673 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001674 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001675
1676/** Set the key for a multipart symmetric decryption operation.
1677 *
1678 * The sequence of operations to decrypt a message with a symmetric cipher
1679 * is as follows:
1680 * -# Allocate an operation object which will be passed to all the functions
1681 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001682 * -# Initialize the operation object with one of the methods described in the
1683 * documentation for #psa_cipher_operation_t, e.g.
1684 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001685 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001686 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001687 * decryption. If the IV is prepended to the ciphertext, you can call
1688 * psa_cipher_update() on a buffer containing the IV followed by the
1689 * beginning of the message.
1690 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1691 * of the message each time.
1692 * -# Call psa_cipher_finish().
1693 *
1694 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001695 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001696 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001697 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001698 * eventually terminate the operation. The following events terminate an
1699 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001700 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001701 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001702 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001703 * \param[in,out] operation The operation object to set up. It must have
1704 * been initialized as per the documentation for
1705 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001706 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001707 * It must remain valid until the operation
1708 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001709 * \param alg The cipher algorithm to compute
1710 * (\c PSA_ALG_XXX value such that
1711 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001712 *
Gilles Peskine28538492018-07-11 17:34:00 +02001713 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001714 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001715 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001716 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001717 * \retval #PSA_ERROR_NOT_PERMITTED
1718 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001719 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001720 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001721 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001722 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1723 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1724 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001725 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001726 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001727 * The operation state is not valid (already set up and not
1728 * subsequently completed).
1729 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001730 * The library has not been previously initialized by psa_crypto_init().
1731 * It is implementation-dependent whether a failure to initialize
1732 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001733 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001734psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001735 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001736 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001737
Gilles Peskinedcd14942018-07-12 00:30:52 +02001738/** Generate an IV for a symmetric encryption operation.
1739 *
1740 * This function generates a random IV (initialization vector), nonce
1741 * or initial counter value for the encryption operation as appropriate
1742 * for the chosen algorithm, key type and key size.
1743 *
1744 * The application must call psa_cipher_encrypt_setup() before
1745 * calling this function.
1746 *
1747 * If this function returns an error status, the operation becomes inactive.
1748 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001749 * \param[in,out] operation Active cipher operation.
1750 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001751 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001752 * \param[out] iv_length On success, the number of bytes of the
1753 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001754 *
1755 * \retval #PSA_SUCCESS
1756 * Success.
1757 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001758 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001759 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001760 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001761 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1762 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1763 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001764 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001765 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001766psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001767 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001768 size_t iv_size,
1769 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001770
Gilles Peskinedcd14942018-07-12 00:30:52 +02001771/** Set the IV for a symmetric encryption or decryption operation.
1772 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001773 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001774 * or initial counter value for the encryption or decryption operation.
1775 *
1776 * The application must call psa_cipher_encrypt_setup() before
1777 * calling this function.
1778 *
1779 * If this function returns an error status, the operation becomes inactive.
1780 *
1781 * \note When encrypting, applications should use psa_cipher_generate_iv()
1782 * instead of this function, unless implementing a protocol that requires
1783 * a non-random IV.
1784 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001785 * \param[in,out] operation Active cipher operation.
1786 * \param[in] iv Buffer containing the IV to use.
1787 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001788 *
1789 * \retval #PSA_SUCCESS
1790 * Success.
1791 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001792 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001793 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001794 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001795 * or the chosen algorithm does not use an IV.
1796 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1797 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1798 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001799 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001800 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001801psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001802 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001803 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001804
Gilles Peskinedcd14942018-07-12 00:30:52 +02001805/** Encrypt or decrypt a message fragment in an active cipher operation.
1806 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001807 * Before calling this function, you must:
1808 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1809 * The choice of setup function determines whether this function
1810 * encrypts or decrypts its input.
1811 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1812 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001813 *
1814 * If this function returns an error status, the operation becomes inactive.
1815 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001816 * \param[in,out] operation Active cipher operation.
1817 * \param[in] input Buffer containing the message fragment to
1818 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001819 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001820 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001821 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001822 * \param[out] output_length On success, the number of bytes
1823 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001824 *
1825 * \retval #PSA_SUCCESS
1826 * Success.
1827 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001828 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001829 * not set, or already completed).
1830 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1831 * The size of the \p output buffer is too small.
1832 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1833 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1834 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001835 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001836 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001837psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1838 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001839 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001840 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001841 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001842 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001843
Gilles Peskinedcd14942018-07-12 00:30:52 +02001844/** Finish encrypting or decrypting a message in a cipher operation.
1845 *
1846 * The application must call psa_cipher_encrypt_setup() or
1847 * psa_cipher_decrypt_setup() before calling this function. The choice
1848 * of setup function determines whether this function encrypts or
1849 * decrypts its input.
1850 *
1851 * This function finishes the encryption or decryption of the message
1852 * formed by concatenating the inputs passed to preceding calls to
1853 * psa_cipher_update().
1854 *
1855 * When this function returns, the operation becomes inactive.
1856 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001857 * \param[in,out] operation Active cipher operation.
1858 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001859 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001860 * \param[out] output_length On success, the number of bytes
1861 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001862 *
1863 * \retval #PSA_SUCCESS
1864 * Success.
1865 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001866 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001867 * not set, or already completed).
1868 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1869 * The size of the \p output buffer is too small.
1870 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1871 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1872 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001873 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001874 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001875psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001876 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001877 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001878 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001879
Gilles Peskinedcd14942018-07-12 00:30:52 +02001880/** Abort a cipher operation.
1881 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001882 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001883 * \p operation structure itself. Once aborted, the operation object
1884 * can be reused for another operation by calling
1885 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001886 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001887 * You may call this function any time after the operation object has
1888 * been initialized by any of the following methods:
1889 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1890 * whether it succeeds or not.
1891 * - Initializing the \c struct to all-bits-zero.
1892 * - Initializing the \c struct to logical zeros, e.g.
1893 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001894 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001895 * In particular, calling psa_cipher_abort() after the operation has been
1896 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1897 * is safe and has no effect.
1898 *
1899 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001900 *
1901 * \retval #PSA_SUCCESS
1902 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001903 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001904 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1905 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001906 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001907 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001908psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1909
1910/**@}*/
1911
Gilles Peskine3b555712018-03-03 21:27:57 +01001912/** \defgroup aead Authenticated encryption with associated data (AEAD)
1913 * @{
1914 */
1915
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001916/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001917 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001918 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001919 * \param alg The AEAD algorithm to compute
1920 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001921 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001922 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001923 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001924 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001925 * but not encrypted.
1926 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001927 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001928 * encrypted.
1929 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001930 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001931 * encrypted data. The additional data is not
1932 * part of this output. For algorithms where the
1933 * encrypted data and the authentication tag
1934 * are defined as separate outputs, the
1935 * authentication tag is appended to the
1936 * encrypted data.
1937 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1938 * This must be at least
1939 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1940 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001941 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001942 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001943 *
Gilles Peskine28538492018-07-11 17:34:00 +02001944 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001945 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001946 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001947 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001948 * \retval #PSA_ERROR_NOT_PERMITTED
1949 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001950 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001951 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001952 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001953 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1954 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1955 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001956 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001957 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001958 * The library has not been previously initialized by psa_crypto_init().
1959 * It is implementation-dependent whether a failure to initialize
1960 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001961 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001962psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001963 psa_algorithm_t alg,
1964 const uint8_t *nonce,
1965 size_t nonce_length,
1966 const uint8_t *additional_data,
1967 size_t additional_data_length,
1968 const uint8_t *plaintext,
1969 size_t plaintext_length,
1970 uint8_t *ciphertext,
1971 size_t ciphertext_size,
1972 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001973
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001974/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001975 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001976 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001977 * \param alg The AEAD algorithm to compute
1978 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001979 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001980 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001981 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001982 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001983 * but not encrypted.
1984 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001985 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001986 * encrypted. For algorithms where the
1987 * encrypted data and the authentication tag
1988 * are defined as separate inputs, the buffer
1989 * must contain the encrypted data followed
1990 * by the authentication tag.
1991 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001992 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001993 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1994 * This must be at least
1995 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1996 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001997 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001998 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001999 *
Gilles Peskine28538492018-07-11 17:34:00 +02002000 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002001 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002002 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002003 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002004 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002005 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002006 * \retval #PSA_ERROR_NOT_PERMITTED
2007 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002008 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002009 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002010 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002011 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2012 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2013 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002014 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002015 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002016 * The library has not been previously initialized by psa_crypto_init().
2017 * It is implementation-dependent whether a failure to initialize
2018 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002019 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002020psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002021 psa_algorithm_t alg,
2022 const uint8_t *nonce,
2023 size_t nonce_length,
2024 const uint8_t *additional_data,
2025 size_t additional_data_length,
2026 const uint8_t *ciphertext,
2027 size_t ciphertext_length,
2028 uint8_t *plaintext,
2029 size_t plaintext_size,
2030 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002031
Gilles Peskine30a9e412019-01-14 18:36:12 +01002032/** The type of the state data structure for multipart AEAD operations.
2033 *
2034 * Before calling any function on an AEAD operation object, the application
2035 * must initialize it by any of the following means:
2036 * - Set the structure to all-bits-zero, for example:
2037 * \code
2038 * psa_aead_operation_t operation;
2039 * memset(&operation, 0, sizeof(operation));
2040 * \endcode
2041 * - Initialize the structure to logical zero values, for example:
2042 * \code
2043 * psa_aead_operation_t operation = {0};
2044 * \endcode
2045 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2046 * for example:
2047 * \code
2048 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2049 * \endcode
2050 * - Assign the result of the function psa_aead_operation_init()
2051 * to the structure, for example:
2052 * \code
2053 * psa_aead_operation_t operation;
2054 * operation = psa_aead_operation_init();
2055 * \endcode
2056 *
2057 * This is an implementation-defined \c struct. Applications should not
2058 * make any assumptions about the content of this structure except
2059 * as directed by the documentation of a specific implementation. */
2060typedef struct psa_aead_operation_s psa_aead_operation_t;
2061
2062/** \def PSA_AEAD_OPERATION_INIT
2063 *
2064 * This macro returns a suitable initializer for an AEAD operation object of
2065 * type #psa_aead_operation_t.
2066 */
2067#ifdef __DOXYGEN_ONLY__
2068/* This is an example definition for documentation purposes.
2069 * Implementations should define a suitable value in `crypto_struct.h`.
2070 */
2071#define PSA_AEAD_OPERATION_INIT {0}
2072#endif
2073
2074/** Return an initial value for an AEAD operation object.
2075 */
2076static psa_aead_operation_t psa_aead_operation_init(void);
2077
2078/** Set the key for a multipart authenticated encryption operation.
2079 *
2080 * The sequence of operations to encrypt a message with authentication
2081 * is as follows:
2082 * -# Allocate an operation object which will be passed to all the functions
2083 * listed here.
2084 * -# Initialize the operation object with one of the methods described in the
2085 * documentation for #psa_aead_operation_t, e.g.
2086 * PSA_AEAD_OPERATION_INIT.
2087 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002088 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2089 * inputs to the subsequent calls to psa_aead_update_ad() and
2090 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2091 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002092 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2093 * generate or set the nonce. You should use
2094 * psa_aead_generate_nonce() unless the protocol you are implementing
2095 * requires a specific nonce value.
2096 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2097 * of the non-encrypted additional authenticated data each time.
2098 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002099 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002100 * -# Call psa_aead_finish().
2101 *
2102 * The application may call psa_aead_abort() at any time after the operation
2103 * has been initialized.
2104 *
2105 * After a successful call to psa_aead_encrypt_setup(), the application must
2106 * eventually terminate the operation. The following events terminate an
2107 * operation:
2108 * - A failed call to any of the \c psa_aead_xxx functions.
2109 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2110 *
2111 * \param[in,out] operation The operation object to set up. It must have
2112 * been initialized as per the documentation for
2113 * #psa_aead_operation_t and not yet in use.
2114 * \param handle Handle to the key to use for the operation.
2115 * It must remain valid until the operation
2116 * terminates.
2117 * \param alg The AEAD algorithm to compute
2118 * (\c PSA_ALG_XXX value such that
2119 * #PSA_ALG_IS_AEAD(\p alg) is true).
2120 *
2121 * \retval #PSA_SUCCESS
2122 * Success.
2123 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002124 * \retval #PSA_ERROR_NOT_PERMITTED
2125 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002126 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002127 * \retval #PSA_ERROR_NOT_SUPPORTED
2128 * \p alg is not supported or is not an AEAD algorithm.
2129 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2130 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2131 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002132 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002133 * \retval #PSA_ERROR_BAD_STATE
2134 * The library has not been previously initialized by psa_crypto_init().
2135 * It is implementation-dependent whether a failure to initialize
2136 * results in this error code.
2137 */
2138psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2139 psa_key_handle_t handle,
2140 psa_algorithm_t alg);
2141
2142/** Set the key for a multipart authenticated decryption operation.
2143 *
2144 * The sequence of operations to decrypt a message with authentication
2145 * is as follows:
2146 * -# Allocate an operation object which will be passed to all the functions
2147 * listed here.
2148 * -# Initialize the operation object with one of the methods described in the
2149 * documentation for #psa_aead_operation_t, e.g.
2150 * PSA_AEAD_OPERATION_INIT.
2151 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002152 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2153 * inputs to the subsequent calls to psa_aead_update_ad() and
2154 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2155 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002156 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2157 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2158 * of the non-encrypted additional authenticated data each time.
2159 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002160 * of the ciphertext to decrypt each time.
2161 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002162 *
2163 * The application may call psa_aead_abort() at any time after the operation
2164 * has been initialized.
2165 *
2166 * After a successful call to psa_aead_decrypt_setup(), the application must
2167 * eventually terminate the operation. The following events terminate an
2168 * operation:
2169 * - A failed call to any of the \c psa_aead_xxx functions.
2170 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2171 *
2172 * \param[in,out] operation The operation object to set up. It must have
2173 * been initialized as per the documentation for
2174 * #psa_aead_operation_t and not yet in use.
2175 * \param handle Handle to the key to use for the operation.
2176 * It must remain valid until the operation
2177 * terminates.
2178 * \param alg The AEAD algorithm to compute
2179 * (\c PSA_ALG_XXX value such that
2180 * #PSA_ALG_IS_AEAD(\p alg) is true).
2181 *
2182 * \retval #PSA_SUCCESS
2183 * Success.
2184 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002185 * \retval #PSA_ERROR_NOT_PERMITTED
2186 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002187 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002188 * \retval #PSA_ERROR_NOT_SUPPORTED
2189 * \p alg is not supported or is not an AEAD algorithm.
2190 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2191 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2192 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002193 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002194 * \retval #PSA_ERROR_BAD_STATE
2195 * The library has not been previously initialized by psa_crypto_init().
2196 * It is implementation-dependent whether a failure to initialize
2197 * results in this error code.
2198 */
2199psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2200 psa_key_handle_t handle,
2201 psa_algorithm_t alg);
2202
2203/** Generate a random nonce for an authenticated encryption operation.
2204 *
2205 * This function generates a random nonce for the authenticated encryption
2206 * operation with an appropriate size for the chosen algorithm, key type
2207 * and key size.
2208 *
2209 * The application must call psa_aead_encrypt_setup() before
2210 * calling this function.
2211 *
2212 * If this function returns an error status, the operation becomes inactive.
2213 *
2214 * \param[in,out] operation Active AEAD operation.
2215 * \param[out] nonce Buffer where the generated nonce is to be
2216 * written.
2217 * \param nonce_size Size of the \p nonce buffer in bytes.
2218 * \param[out] nonce_length On success, the number of bytes of the
2219 * generated nonce.
2220 *
2221 * \retval #PSA_SUCCESS
2222 * Success.
2223 * \retval #PSA_ERROR_BAD_STATE
2224 * The operation state is not valid (not set up, or nonce already set).
2225 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2226 * The size of the \p nonce buffer is too small.
2227 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2228 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2229 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002230 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002231 */
2232psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002233 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002234 size_t nonce_size,
2235 size_t *nonce_length);
2236
2237/** Set the nonce for an authenticated encryption or decryption operation.
2238 *
2239 * This function sets the nonce for the authenticated
2240 * encryption or decryption operation.
2241 *
2242 * The application must call psa_aead_encrypt_setup() before
2243 * calling this function.
2244 *
2245 * If this function returns an error status, the operation becomes inactive.
2246 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002247 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002248 * instead of this function, unless implementing a protocol that requires
2249 * a non-random IV.
2250 *
2251 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002252 * \param[in] nonce Buffer containing the nonce to use.
2253 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002254 *
2255 * \retval #PSA_SUCCESS
2256 * Success.
2257 * \retval #PSA_ERROR_BAD_STATE
2258 * The operation state is not valid (not set up, or nonce already set).
2259 * \retval #PSA_ERROR_INVALID_ARGUMENT
2260 * The size of \p nonce is not acceptable for the chosen algorithm.
2261 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2262 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2263 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002264 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002265 */
2266psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002267 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002268 size_t nonce_length);
2269
Gilles Peskinebc59c852019-01-17 15:26:08 +01002270/** Declare the lengths of the message and additional data for AEAD.
2271 *
2272 * The application must call this function before calling
2273 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2274 * the operation requires it. If the algorithm does not require it,
2275 * calling this function is optional, but if this function is called
2276 * then the implementation must enforce the lengths.
2277 *
2278 * You may call this function before or after setting the nonce with
2279 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2280 *
2281 * - For #PSA_ALG_CCM, calling this function is required.
2282 * - For the other AEAD algorithms defined in this specification, calling
2283 * this function is not required.
2284 * - For vendor-defined algorithm, refer to the vendor documentation.
2285 *
2286 * \param[in,out] operation Active AEAD operation.
2287 * \param ad_length Size of the non-encrypted additional
2288 * authenticated data in bytes.
2289 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2290 *
2291 * \retval #PSA_SUCCESS
2292 * Success.
2293 * \retval #PSA_ERROR_BAD_STATE
2294 * The operation state is not valid (not set up, already completed,
2295 * or psa_aead_update_ad() or psa_aead_update() already called).
2296 * \retval #PSA_ERROR_INVALID_ARGUMENT
2297 * At least one of the lengths is not acceptable for the chosen
2298 * algorithm.
2299 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2300 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2301 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002302 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinebc59c852019-01-17 15:26:08 +01002303 */
2304psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2305 size_t ad_length,
2306 size_t plaintext_length);
2307
Gilles Peskine30a9e412019-01-14 18:36:12 +01002308/** Pass additional data to an active AEAD operation.
2309 *
2310 * Additional data is authenticated, but not encrypted.
2311 *
2312 * You may call this function multiple times to pass successive fragments
2313 * of the additional data. You may not call this function after passing
2314 * data to encrypt or decrypt with psa_aead_update().
2315 *
2316 * Before calling this function, you must:
2317 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2318 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2319 *
2320 * If this function returns an error status, the operation becomes inactive.
2321 *
2322 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2323 * there is no guarantee that the input is valid. Therefore, until
2324 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2325 * treat the input as untrusted and prepare to undo any action that
2326 * depends on the input if psa_aead_verify() returns an error status.
2327 *
2328 * \param[in,out] operation Active AEAD operation.
2329 * \param[in] input Buffer containing the fragment of
2330 * additional data.
2331 * \param input_length Size of the \p input buffer in bytes.
2332 *
2333 * \retval #PSA_SUCCESS
2334 * Success.
2335 * \retval #PSA_ERROR_BAD_STATE
2336 * The operation state is not valid (not set up, nonce not set,
2337 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002338 * \retval #PSA_ERROR_INVALID_ARGUMENT
2339 * The total input length overflows the additional data length that
2340 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002341 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2342 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2343 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002344 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002345 */
2346psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2347 const uint8_t *input,
2348 size_t input_length);
2349
2350/** Encrypt or decrypt a message fragment in an active AEAD operation.
2351 *
2352 * Before calling this function, you must:
2353 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2354 * The choice of setup function determines whether this function
2355 * encrypts or decrypts its input.
2356 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2357 * 3. Call psa_aead_update_ad() to pass all the additional data.
2358 *
2359 * If this function returns an error status, the operation becomes inactive.
2360 *
2361 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2362 * there is no guarantee that the input is valid. Therefore, until
2363 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2364 * - Do not use the output in any way other than storing it in a
2365 * confidential location. If you take any action that depends
2366 * on the tentative decrypted data, this action will need to be
2367 * undone if the input turns out not to be valid. Furthermore,
2368 * if an adversary can observe that this action took place
2369 * (for example through timing), they may be able to use this
2370 * fact as an oracle to decrypt any message encrypted with the
2371 * same key.
2372 * - In particular, do not copy the output anywhere but to a
2373 * memory or storage space that you have exclusive access to.
2374 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002375 * This function does not require the input to be aligned to any
2376 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002377 * a whole block at a time, it must consume all the input provided, but
2378 * it may delay the end of the corresponding output until a subsequent
2379 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2380 * provides sufficient input. The amount of data that can be delayed
2381 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002382 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002383 * \param[in,out] operation Active AEAD operation.
2384 * \param[in] input Buffer containing the message fragment to
2385 * encrypt or decrypt.
2386 * \param input_length Size of the \p input buffer in bytes.
2387 * \param[out] output Buffer where the output is to be written.
2388 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002389 * This must be at least
2390 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2391 * \p input_length) where \c alg is the
2392 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002393 * \param[out] output_length On success, the number of bytes
2394 * that make up the returned output.
2395 *
2396 * \retval #PSA_SUCCESS
2397 * Success.
2398 * \retval #PSA_ERROR_BAD_STATE
2399 * The operation state is not valid (not set up, nonce not set
2400 * or already completed).
2401 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2402 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002403 * You can determine a sufficient buffer size by calling
2404 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2405 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002406 * \retval #PSA_ERROR_INVALID_ARGUMENT
2407 * The total length of input to psa_aead_update_ad() so far is
2408 * less than the additional data length that was previously
2409 * specified with psa_aead_set_lengths().
2410 * \retval #PSA_ERROR_INVALID_ARGUMENT
2411 * The total input length overflows the plaintext length that
2412 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002413 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2414 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2415 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002416 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002417 */
2418psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2419 const uint8_t *input,
2420 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002421 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002422 size_t output_size,
2423 size_t *output_length);
2424
2425/** Finish encrypting a message in an AEAD operation.
2426 *
2427 * The operation must have been set up with psa_aead_encrypt_setup().
2428 *
2429 * This function finishes the authentication of the additional data
2430 * formed by concatenating the inputs passed to preceding calls to
2431 * psa_aead_update_ad() with the plaintext formed by concatenating the
2432 * inputs passed to preceding calls to psa_aead_update().
2433 *
2434 * This function has two output buffers:
2435 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002436 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002437 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002438 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002439 * that the operation performs.
2440 *
2441 * When this function returns, the operation becomes inactive.
2442 *
2443 * \param[in,out] operation Active AEAD operation.
2444 * \param[out] ciphertext Buffer where the last part of the ciphertext
2445 * is to be written.
2446 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002447 * This must be at least
2448 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2449 * \c alg is the algorithm that is being
2450 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002451 * \param[out] ciphertext_length On success, the number of bytes of
2452 * returned ciphertext.
2453 * \param[out] tag Buffer where the authentication tag is
2454 * to be written.
2455 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002456 * This must be at least
2457 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2458 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002459 * \param[out] tag_length On success, the number of bytes
2460 * that make up the returned tag.
2461 *
2462 * \retval #PSA_SUCCESS
2463 * Success.
2464 * \retval #PSA_ERROR_BAD_STATE
2465 * The operation state is not valid (not set up, nonce not set,
2466 * decryption, or already completed).
2467 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002468 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002469 * You can determine a sufficient buffer size for \p ciphertext by
2470 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2471 * where \c alg is the algorithm that is being calculated.
2472 * You can determine a sufficient buffer size for \p tag by
2473 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002474 * \retval #PSA_ERROR_INVALID_ARGUMENT
2475 * The total length of input to psa_aead_update_ad() so far is
2476 * less than the additional data length that was previously
2477 * specified with psa_aead_set_lengths().
2478 * \retval #PSA_ERROR_INVALID_ARGUMENT
2479 * The total length of input to psa_aead_update() so far is
2480 * less than the plaintext length that was previously
2481 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002482 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2483 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2484 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002485 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002486 */
2487psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002488 uint8_t *ciphertext,
2489 size_t ciphertext_size,
2490 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002491 uint8_t *tag,
2492 size_t tag_size,
2493 size_t *tag_length);
2494
2495/** Finish authenticating and decrypting a message in an AEAD operation.
2496 *
2497 * The operation must have been set up with psa_aead_decrypt_setup().
2498 *
2499 * This function finishes the authentication of the additional data
2500 * formed by concatenating the inputs passed to preceding calls to
2501 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2502 * inputs passed to preceding calls to psa_aead_update().
2503 *
2504 * When this function returns, the operation becomes inactive.
2505 *
2506 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002507 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002508 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002509 * from previous calls to psa_aead_update()
2510 * that could not be processed until the end
2511 * of the input.
2512 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002513 * This must be at least
2514 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2515 * \c alg is the algorithm that is being
2516 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002517 * \param[out] plaintext_length On success, the number of bytes of
2518 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002519 * \param[in] tag Buffer containing the authentication tag.
2520 * \param tag_length Size of the \p tag buffer in bytes.
2521 *
2522 * \retval #PSA_SUCCESS
2523 * Success.
2524 * \retval #PSA_ERROR_BAD_STATE
2525 * The operation state is not valid (not set up, nonce not set,
2526 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002527 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2528 * The size of the \p plaintext buffer is too small.
2529 * You can determine a sufficient buffer size for \p plaintext by
2530 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2531 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002532 * \retval #PSA_ERROR_INVALID_ARGUMENT
2533 * The total length of input to psa_aead_update_ad() so far is
2534 * less than the additional data length that was previously
2535 * specified with psa_aead_set_lengths().
2536 * \retval #PSA_ERROR_INVALID_ARGUMENT
2537 * The total length of input to psa_aead_update() so far is
2538 * less than the plaintext length that was previously
2539 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002540 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2541 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2542 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002543 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002544 */
2545psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002546 uint8_t *plaintext,
2547 size_t plaintext_size,
2548 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002549 const uint8_t *tag,
2550 size_t tag_length);
2551
2552/** Abort an AEAD operation.
2553 *
2554 * Aborting an operation frees all associated resources except for the
2555 * \p operation structure itself. Once aborted, the operation object
2556 * can be reused for another operation by calling
2557 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2558 *
2559 * You may call this function any time after the operation object has
2560 * been initialized by any of the following methods:
2561 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2562 * whether it succeeds or not.
2563 * - Initializing the \c struct to all-bits-zero.
2564 * - Initializing the \c struct to logical zeros, e.g.
2565 * `psa_aead_operation_t operation = {0}`.
2566 *
2567 * In particular, calling psa_aead_abort() after the operation has been
2568 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2569 * is safe and has no effect.
2570 *
2571 * \param[in,out] operation Initialized AEAD operation.
2572 *
2573 * \retval #PSA_SUCCESS
2574 * \retval #PSA_ERROR_BAD_STATE
2575 * \p operation is not an active AEAD operation.
2576 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2577 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002578 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002579 */
2580psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2581
Gilles Peskine3b555712018-03-03 21:27:57 +01002582/**@}*/
2583
Gilles Peskine20035e32018-02-03 22:44:14 +01002584/** \defgroup asymmetric Asymmetric cryptography
2585 * @{
2586 */
2587
2588/**
2589 * \brief Sign a hash or short message with a private key.
2590 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002591 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002592 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002593 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2594 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2595 * to determine the hash algorithm to use.
2596 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002597 * \param handle Handle to the key to use for the operation.
2598 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002599 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002600 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002601 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002602 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002603 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002604 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002605 * \param[out] signature_length On success, the number of bytes
2606 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002607 *
Gilles Peskine28538492018-07-11 17:34:00 +02002608 * \retval #PSA_SUCCESS
2609 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002610 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002611 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002612 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002613 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002614 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002615 * \retval #PSA_ERROR_NOT_SUPPORTED
2616 * \retval #PSA_ERROR_INVALID_ARGUMENT
2617 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2618 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2619 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002620 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002621 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002622 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002623 * The library has not been previously initialized by psa_crypto_init().
2624 * It is implementation-dependent whether a failure to initialize
2625 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002626 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002627psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002628 psa_algorithm_t alg,
2629 const uint8_t *hash,
2630 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002631 uint8_t *signature,
2632 size_t signature_size,
2633 size_t *signature_length);
2634
2635/**
2636 * \brief Verify the signature a hash or short message using a public key.
2637 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002638 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002639 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002640 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2641 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2642 * to determine the hash algorithm to use.
2643 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002644 * \param handle Handle to the key to use for the operation.
2645 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002646 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002647 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002648 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002649 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002650 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002651 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002652 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002653 *
Gilles Peskine28538492018-07-11 17:34:00 +02002654 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002655 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002656 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002657 * The calculation was perfomed successfully, but the passed
2658 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002659 * \retval #PSA_ERROR_NOT_SUPPORTED
2660 * \retval #PSA_ERROR_INVALID_ARGUMENT
2661 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2662 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2663 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002664 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002665 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002666 * The library has not been previously initialized by psa_crypto_init().
2667 * It is implementation-dependent whether a failure to initialize
2668 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002669 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002670psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002671 psa_algorithm_t alg,
2672 const uint8_t *hash,
2673 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002674 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002675 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002676
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002677/**
2678 * \brief Encrypt a short message with a public key.
2679 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002680 * \param handle Handle to the key to use for the operation.
2681 * It must be a public key or an asymmetric
2682 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002683 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002684 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002685 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002686 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002687 * \param[in] salt A salt or label, if supported by the
2688 * encryption algorithm.
2689 * If the algorithm does not support a
2690 * salt, pass \c NULL.
2691 * If the algorithm supports an optional
2692 * salt and you do not want to pass a salt,
2693 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002694 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002695 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2696 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002697 * \param salt_length Size of the \p salt buffer in bytes.
2698 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002699 * \param[out] output Buffer where the encrypted message is to
2700 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002701 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002702 * \param[out] output_length On success, the number of bytes
2703 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002704 *
Gilles Peskine28538492018-07-11 17:34:00 +02002705 * \retval #PSA_SUCCESS
2706 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002707 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002708 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002709 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002710 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002711 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002712 * \retval #PSA_ERROR_NOT_SUPPORTED
2713 * \retval #PSA_ERROR_INVALID_ARGUMENT
2714 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2715 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2716 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002717 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002718 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002719 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002720 * The library has not been previously initialized by psa_crypto_init().
2721 * It is implementation-dependent whether a failure to initialize
2722 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002723 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002724psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002725 psa_algorithm_t alg,
2726 const uint8_t *input,
2727 size_t input_length,
2728 const uint8_t *salt,
2729 size_t salt_length,
2730 uint8_t *output,
2731 size_t output_size,
2732 size_t *output_length);
2733
2734/**
2735 * \brief Decrypt a short message with a private key.
2736 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002737 * \param handle Handle to the key to use for the operation.
2738 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002739 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002740 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002741 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002742 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002743 * \param[in] salt A salt or label, if supported by the
2744 * encryption algorithm.
2745 * If the algorithm does not support a
2746 * salt, pass \c NULL.
2747 * If the algorithm supports an optional
2748 * salt and you do not want to pass a salt,
2749 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002750 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002751 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2752 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002753 * \param salt_length Size of the \p salt buffer in bytes.
2754 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002755 * \param[out] output Buffer where the decrypted message is to
2756 * be written.
2757 * \param output_size Size of the \c output buffer in bytes.
2758 * \param[out] output_length On success, the number of bytes
2759 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002760 *
Gilles Peskine28538492018-07-11 17:34:00 +02002761 * \retval #PSA_SUCCESS
2762 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002763 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002764 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002765 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002766 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002767 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002768 * \retval #PSA_ERROR_NOT_SUPPORTED
2769 * \retval #PSA_ERROR_INVALID_ARGUMENT
2770 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2771 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2772 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002773 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002774 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2775 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002776 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002777 * The library has not been previously initialized by psa_crypto_init().
2778 * It is implementation-dependent whether a failure to initialize
2779 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002780 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002781psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002782 psa_algorithm_t alg,
2783 const uint8_t *input,
2784 size_t input_length,
2785 const uint8_t *salt,
2786 size_t salt_length,
2787 uint8_t *output,
2788 size_t output_size,
2789 size_t *output_length);
2790
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002791/**@}*/
2792
Gilles Peskine35675b62019-05-16 17:26:11 +02002793/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002794 * @{
2795 */
2796
Gilles Peskine35675b62019-05-16 17:26:11 +02002797/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002798 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002799 * Before calling any function on a key derivation operation object, the
2800 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002801 * - Set the structure to all-bits-zero, for example:
2802 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002803 * psa_key_derivation_operation_t operation;
2804 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002805 * \endcode
2806 * - Initialize the structure to logical zero values, for example:
2807 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002808 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002809 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002810 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002811 * for example:
2812 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002813 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002814 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002815 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002816 * to the structure, for example:
2817 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002818 * psa_key_derivation_operation_t operation;
2819 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002820 * \endcode
2821 *
2822 * This is an implementation-defined \c struct. Applications should not
2823 * make any assumptions about the content of this structure except
2824 * as directed by the documentation of a specific implementation.
2825 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002826typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002827
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002828/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002829 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002830 * This macro returns a suitable initializer for a key derivation operation
2831 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002832 */
2833#ifdef __DOXYGEN_ONLY__
2834/* This is an example definition for documentation purposes.
2835 * Implementations should define a suitable value in `crypto_struct.h`.
2836 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002837#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02002838#endif
2839
Gilles Peskine35675b62019-05-16 17:26:11 +02002840/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002841 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002842static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002843
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002844/** Set up a key derivation operation.
2845 *
2846 * A key derivation algorithm takes some inputs and uses them to generate
2847 * a byte stream in a deterministic way.
2848 * This byte stream can be used to produce keys and other
2849 * cryptographic material.
2850 *
2851 * To derive a key:
2852 * - Start with an initialized object of type #psa_key_derivation_operation_t.
2853 * - Call psa_key_derivation_setup() to select the algorithm.
2854 * - Provide the inputs for the key derivation by calling
2855 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2856 * as appropriate. Which inputs are needed, in what order, and whether
2857 * they may be keys and if so of what type depends on the algorithm.
2858 * - Optionally set the operation's maximum capacity with
2859 * psa_key_derivation_set_capacity(). You may do this before, in the middle
2860 * of or after providing inputs. For some algorithms, this step is mandatory
2861 * because the output depends on the maximum capacity.
2862 * - To derive a key, call psa_key_derivation_output_key().
2863 * To derive a byte string for a different purpose, call
2864 * - psa_key_derivation_output_bytes().
2865 * Successive calls to these functions use successive output bytes
2866 * calculated by the key derivation algorithm.
2867 * - Clean up the key derivation operation object with
2868 * psa_key_derivation_abort().
2869 *
2870 * \param[in,out] operation The key derivation operation object
2871 * to set up. It must
2872 * have been initialized but not set up yet.
2873 * \param alg The key derivation algorithm to compute
2874 * (\c PSA_ALG_XXX value such that
2875 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2876 *
2877 * \retval #PSA_SUCCESS
2878 * Success.
2879 * \retval #PSA_ERROR_INVALID_ARGUMENT
2880 * \c alg is not a key derivation algorithm.
2881 * \retval #PSA_ERROR_NOT_SUPPORTED
2882 * \c alg is not supported or is not a key derivation algorithm.
2883 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2884 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2885 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002886 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002887 * \retval #PSA_ERROR_BAD_STATE
2888 */
2889psa_status_t psa_key_derivation_setup(
2890 psa_key_derivation_operation_t *operation,
2891 psa_algorithm_t alg);
2892
Gilles Peskine35675b62019-05-16 17:26:11 +02002893/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002894 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002895 * The capacity of a key derivation is the maximum number of bytes that it can
2896 * return. When you get *N* bytes of output from a key derivation operation,
2897 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002898 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002899 * \param[in] operation The operation to query.
2900 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002901 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002902 * \retval #PSA_SUCCESS
2903 * \retval #PSA_ERROR_BAD_STATE
2904 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002905 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002906psa_status_t psa_key_derivation_get_capacity(
2907 const psa_key_derivation_operation_t *operation,
2908 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002909
Gilles Peskine35675b62019-05-16 17:26:11 +02002910/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002911 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002912 * The capacity of a key derivation operation is the maximum number of bytes
2913 * that the key derivation operation can return from this point onwards.
2914 *
2915 * \param[in,out] operation The key derivation operation object to modify.
2916 * \param capacity The new capacity of the operation.
2917 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002918 * current capacity.
2919 *
2920 * \retval #PSA_SUCCESS
2921 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02002922 * \p capacity is larger than the operation's current capacity.
2923 * In this case, the operation object remains valid and its capacity
2924 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002925 * \retval #PSA_ERROR_BAD_STATE
2926 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2927 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002928psa_status_t psa_key_derivation_set_capacity(
2929 psa_key_derivation_operation_t *operation,
2930 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002931
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002932/** Use the maximum possible capacity for a key derivation operation.
2933 *
2934 * Use this value as the capacity argument when setting up a key derivation
2935 * to indicate that the operation should have the maximum possible capacity.
2936 * The value of the maximum possible capacity depends on the key derivation
2937 * algorithm.
2938 */
2939#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
2940
2941/** Provide an input for key derivation or key agreement.
2942 *
2943 * Which inputs are required and in what order depends on the algorithm.
2944 * Refer to the documentation of each key derivation or key agreement
2945 * algorithm for information.
2946 *
2947 * This function passes direct inputs. Some inputs must be passed as keys
2948 * using psa_key_derivation_input_key() instead of this function. Refer to
2949 * the documentation of individual step types for information.
2950 *
2951 * \param[in,out] operation The key derivation operation object to use.
2952 * It must have been set up with
2953 * psa_key_derivation_setup() and must not
2954 * have produced any output yet.
2955 * \param step Which step the input data is for.
2956 * \param[in] data Input data to use.
2957 * \param data_length Size of the \p data buffer in bytes.
2958 *
2959 * \retval #PSA_SUCCESS
2960 * Success.
2961 * \retval #PSA_ERROR_INVALID_ARGUMENT
2962 * \c step is not compatible with the operation's algorithm.
2963 * \retval #PSA_ERROR_INVALID_ARGUMENT
2964 * \c step does not allow direct inputs.
2965 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2966 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2967 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002968 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002969 * \retval #PSA_ERROR_BAD_STATE
2970 * The value of \p step is not valid given the state of \p operation.
2971 * \retval #PSA_ERROR_BAD_STATE
2972 * The library has not been previously initialized by psa_crypto_init().
2973 * It is implementation-dependent whether a failure to initialize
2974 * results in this error code.
2975 */
2976psa_status_t psa_key_derivation_input_bytes(
2977 psa_key_derivation_operation_t *operation,
2978 psa_key_derivation_step_t step,
2979 const uint8_t *data,
2980 size_t data_length);
2981
2982/** Provide an input for key derivation in the form of a key.
2983 *
2984 * Which inputs are required and in what order depends on the algorithm.
2985 * Refer to the documentation of each key derivation or key agreement
2986 * algorithm for information.
2987 *
2988 * This function passes key inputs. Some inputs must be passed as keys
2989 * of the appropriate type using this function, while others must be
2990 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
2991 * the documentation of individual step types for information.
2992 *
2993 * \param[in,out] operation The key derivation operation object to use.
2994 * It must have been set up with
2995 * psa_key_derivation_setup() and must not
2996 * have produced any output yet.
2997 * \param step Which step the input data is for.
2998 * \param handle Handle to the key. It must have an
2999 * appropriate type for \p step and must
3000 * allow the usage #PSA_KEY_USAGE_DERIVE.
3001 *
3002 * \retval #PSA_SUCCESS
3003 * Success.
3004 * \retval #PSA_ERROR_INVALID_HANDLE
3005 * \retval #PSA_ERROR_DOES_NOT_EXIST
3006 * \retval #PSA_ERROR_NOT_PERMITTED
3007 * \retval #PSA_ERROR_INVALID_ARGUMENT
3008 * \c step is not compatible with the operation's algorithm.
3009 * \retval #PSA_ERROR_INVALID_ARGUMENT
3010 * \c step does not allow key inputs.
3011 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3012 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3013 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003014 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003015 * \retval #PSA_ERROR_BAD_STATE
3016 * The value of \p step is not valid given the state of \p operation.
3017 * \retval #PSA_ERROR_BAD_STATE
3018 * The library has not been previously initialized by psa_crypto_init().
3019 * It is implementation-dependent whether a failure to initialize
3020 * results in this error code.
3021 */
3022psa_status_t psa_key_derivation_input_key(
3023 psa_key_derivation_operation_t *operation,
3024 psa_key_derivation_step_t step,
3025 psa_key_handle_t handle);
3026
3027/** Perform a key agreement and use the shared secret as input to a key
3028 * derivation.
3029 *
3030 * A key agreement algorithm takes two inputs: a private key \p private_key
3031 * a public key \p peer_key.
3032 * The result of this function is passed as input to a key derivation.
3033 * The output of this key derivation can be extracted by reading from the
3034 * resulting operation to produce keys and other cryptographic material.
3035 *
3036 * \param[in,out] operation The key derivation operation object to use.
3037 * It must have been set up with
3038 * psa_key_derivation_setup() with a
3039 * key agreement and derivation algorithm
3040 * \c alg (\c PSA_ALG_XXX value such that
3041 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3042 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3043 * is false).
3044 * The operation must be ready for an
3045 * input of the type given by \p step.
3046 * \param step Which step the input data is for.
3047 * \param private_key Handle to the private key to use.
3048 * \param[in] peer_key Public key of the peer. The peer key must be in the
3049 * same format that psa_import_key() accepts for the
3050 * public key type corresponding to the type of
3051 * private_key. That is, this function performs the
3052 * equivalent of
3053 * #psa_import_key(...,
3054 * `peer_key`, `peer_key_length`) where
3055 * with key attributes indicating the public key
3056 * type corresponding to the type of `private_key`.
3057 * For example, for EC keys, this means that peer_key
3058 * is interpreted as a point on the curve that the
3059 * private key is on. The standard formats for public
3060 * keys are documented in the documentation of
3061 * psa_export_public_key().
3062 * \param peer_key_length Size of \p peer_key in bytes.
3063 *
3064 * \retval #PSA_SUCCESS
3065 * Success.
3066 * \retval #PSA_ERROR_INVALID_HANDLE
3067 * \retval #PSA_ERROR_DOES_NOT_EXIST
3068 * \retval #PSA_ERROR_NOT_PERMITTED
3069 * \retval #PSA_ERROR_INVALID_ARGUMENT
3070 * \c private_key is not compatible with \c alg,
3071 * or \p peer_key is not valid for \c alg or not compatible with
3072 * \c private_key.
3073 * \retval #PSA_ERROR_NOT_SUPPORTED
3074 * \c alg is not supported or is not a key derivation algorithm.
3075 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3076 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3077 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003078 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003079 */
3080psa_status_t psa_key_derivation_key_agreement(
3081 psa_key_derivation_operation_t *operation,
3082 psa_key_derivation_step_t step,
3083 psa_key_handle_t private_key,
3084 const uint8_t *peer_key,
3085 size_t peer_key_length);
3086
Gilles Peskine35675b62019-05-16 17:26:11 +02003087/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003088 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003089 * This function calculates output bytes from a key derivation algorithm and
3090 * return those bytes.
3091 * If you view the key derivation's output as a stream of bytes, this
3092 * function destructively reads the requested number of bytes from the
3093 * stream.
3094 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003095 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003096 * \param[in,out] operation The key derivation operation object to read from.
3097 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003098 * \param output_length Number of bytes to output.
3099 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003100 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003101 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003102 * The operation's capacity was less than
3103 * \p output_length bytes. Note that in this case,
3104 * no output is written to the output buffer.
3105 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003106 * subsequent calls to this function will not
3107 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003108 * \retval #PSA_ERROR_BAD_STATE
3109 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3110 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3111 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003112 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003113 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003114psa_status_t psa_key_derivation_output_bytes(
3115 psa_key_derivation_operation_t *operation,
3116 uint8_t *output,
3117 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003118
Gilles Peskine35675b62019-05-16 17:26:11 +02003119/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003120 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003121 * This function calculates output bytes from a key derivation algorithm
3122 * and uses those bytes to generate a key deterministically.
3123 * If you view the key derivation's output as a stream of bytes, this
3124 * function destructively reads as many bytes as required from the
3125 * stream.
3126 * The operation's capacity decreases by the number of bytes read.
3127 *
3128 * How much output is produced and consumed from the operation, and how
3129 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003130 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003131 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003132 * of a given size, this function is functionally equivalent to
3133 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003134 * and passing the resulting output to #psa_import_key.
3135 * However, this function has a security benefit:
3136 * if the implementation provides an isolation boundary then
3137 * the key material is not exposed outside the isolation boundary.
3138 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003139 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003140 * The following key types defined in this specification follow this scheme:
3141 *
3142 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003143 * - #PSA_KEY_TYPE_ARC4;
3144 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003145 * - #PSA_KEY_TYPE_DERIVE;
3146 * - #PSA_KEY_TYPE_HMAC.
3147 *
3148 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003149 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003150 * Montgomery curve), this function always draws a byte string whose
3151 * length is determined by the curve, and sets the mandatory bits
3152 * accordingly. That is:
3153 *
3154 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3155 * and process it as specified in RFC 7748 &sect;5.
3156 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3157 * and process it as specified in RFC 7748 &sect;5.
3158 *
3159 * - For key types for which the key is represented by a single sequence of
3160 * \p bits bits with constraints as to which bit sequences are acceptable,
3161 * this function draws a byte string of length (\p bits / 8) bytes rounded
3162 * up to the nearest whole number of bytes. If the resulting byte string
3163 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3164 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003165 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003166 * for the output produced by psa_export_key().
3167 * The following key types defined in this specification follow this scheme:
3168 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003169 * - #PSA_KEY_TYPE_DES.
3170 * Force-set the parity bits, but discard forbidden weak keys.
3171 * For 2-key and 3-key triple-DES, the three keys are generated
3172 * successively (for example, for 3-key triple-DES,
3173 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3174 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003175 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003176 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003177 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003178 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003179 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003180 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003181 * Weierstrass curve).
3182 * For these key types, interpret the byte string as integer
3183 * in big-endian order. Discard it if it is not in the range
3184 * [0, *N* - 2] where *N* is the boundary of the private key domain
3185 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003186 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003187 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003188 * This method allows compliance to NIST standards, specifically
3189 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003190 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3191 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3192 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3193 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003194 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003195 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003196 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003197 * implementation-defined.
3198 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003199 * In all cases, the data that is read is discarded from the operation.
3200 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003201 *
Gilles Peskine20628592019-04-19 19:29:50 +02003202 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003203 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003204 * \param[out] handle On success, a handle to the newly created key.
3205 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003206 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003207 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003208 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003209 * If the key is persistent, the key material and the key's metadata
3210 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003211 * \retval #PSA_ERROR_ALREADY_EXISTS
3212 * This is an attempt to create a persistent key, and there is
3213 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003214 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003215 * There was not enough data to create the desired key.
3216 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003217 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003218 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003219 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003220 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003221 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003222 * \retval #PSA_ERROR_INVALID_ARGUMENT
3223 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003224 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003225 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3226 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3227 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3228 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003229 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003230 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003231 * The library has not been previously initialized by psa_crypto_init().
3232 * It is implementation-dependent whether a failure to initialize
3233 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003234 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003235psa_status_t psa_key_derivation_output_key(
3236 const psa_key_attributes_t *attributes,
3237 psa_key_derivation_operation_t *operation,
3238 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003239
Gilles Peskine35675b62019-05-16 17:26:11 +02003240/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003241 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003242 * Once a key derivation operation has been aborted, its capacity is zero.
3243 * Aborting an operation frees all associated resources except for the
3244 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003245 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003246 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003247 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003248 * psa_key_derivation_operation_init() or a zero value. In particular,
3249 * it is valid to call psa_key_derivation_abort() twice, or to call
3250 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003251 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003252 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003253 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003254 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003255 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003256 * \retval #PSA_SUCCESS
3257 * \retval #PSA_ERROR_BAD_STATE
3258 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3259 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003260 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003261 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003262psa_status_t psa_key_derivation_abort(
3263 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003264
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003265/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003266 *
3267 * \warning The raw result of a key agreement algorithm such as finite-field
3268 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3269 * not be used directly as key material. It should instead be passed as
3270 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003271 * a key derivation, use psa_key_derivation_key_agreement() and other
3272 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003273 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003274 * \param alg The key agreement algorithm to compute
3275 * (\c PSA_ALG_XXX value such that
3276 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3277 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003278 * \param private_key Handle to the private key to use.
3279 * \param[in] peer_key Public key of the peer. It must be
3280 * in the same format that psa_import_key()
3281 * accepts. The standard formats for public
3282 * keys are documented in the documentation
3283 * of psa_export_public_key().
3284 * \param peer_key_length Size of \p peer_key in bytes.
3285 * \param[out] output Buffer where the decrypted message is to
3286 * be written.
3287 * \param output_size Size of the \c output buffer in bytes.
3288 * \param[out] output_length On success, the number of bytes
3289 * that make up the returned output.
3290 *
3291 * \retval #PSA_SUCCESS
3292 * Success.
3293 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003294 * \retval #PSA_ERROR_NOT_PERMITTED
3295 * \retval #PSA_ERROR_INVALID_ARGUMENT
3296 * \p alg is not a key agreement algorithm
3297 * \retval #PSA_ERROR_INVALID_ARGUMENT
3298 * \p private_key is not compatible with \p alg,
3299 * or \p peer_key is not valid for \p alg or not compatible with
3300 * \p private_key.
3301 * \retval #PSA_ERROR_NOT_SUPPORTED
3302 * \p alg is not a supported key agreement algorithm.
3303 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3304 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3305 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003306 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine769c7a62019-01-18 16:42:29 +01003307 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003308psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3309 psa_key_handle_t private_key,
3310 const uint8_t *peer_key,
3311 size_t peer_key_length,
3312 uint8_t *output,
3313 size_t output_size,
3314 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003315
Gilles Peskineea0fb492018-07-12 17:17:20 +02003316/**@}*/
3317
Gilles Peskineedd76872018-07-20 17:42:05 +02003318/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003319 * @{
3320 */
3321
3322/**
3323 * \brief Generate random bytes.
3324 *
3325 * \warning This function **can** fail! Callers MUST check the return status
3326 * and MUST NOT use the content of the output buffer if the return
3327 * status is not #PSA_SUCCESS.
3328 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003329 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003330 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003331 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003332 * \param output_size Number of bytes to generate and output.
3333 *
Gilles Peskine28538492018-07-11 17:34:00 +02003334 * \retval #PSA_SUCCESS
3335 * \retval #PSA_ERROR_NOT_SUPPORTED
3336 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3337 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3338 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003339 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003340 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003341 * The library has not been previously initialized by psa_crypto_init().
3342 * It is implementation-dependent whether a failure to initialize
3343 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003344 */
3345psa_status_t psa_generate_random(uint8_t *output,
3346 size_t output_size);
3347
3348/**
3349 * \brief Generate a key or key pair.
3350 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003351 * The key is generated randomly.
3352 * Its location, policy, type and size are taken from \p attributes.
3353 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003354 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003355 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003356 * the public exponent is 65537.
3357 * The modulus is a product of two probabilistic primes
3358 * between 2^{n-1} and 2^n where n is the bit size specified in the
3359 * attributes.
3360 *
Gilles Peskine20628592019-04-19 19:29:50 +02003361 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003362 * \param[out] handle On success, a handle to the newly created key.
3363 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003364 *
Gilles Peskine28538492018-07-11 17:34:00 +02003365 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003366 * Success.
3367 * If the key is persistent, the key material and the key's metadata
3368 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003369 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003370 * This is an attempt to create a persistent key, and there is
3371 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003372 * \retval #PSA_ERROR_NOT_SUPPORTED
3373 * \retval #PSA_ERROR_INVALID_ARGUMENT
3374 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3375 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3376 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3377 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003378 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003379 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003380 * The library has not been previously initialized by psa_crypto_init().
3381 * It is implementation-dependent whether a failure to initialize
3382 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003383 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003384psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003385 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003386
3387/**@}*/
3388
Gilles Peskinee59236f2018-01-27 23:32:46 +01003389#ifdef __cplusplus
3390}
3391#endif
3392
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003393/* The file "crypto_sizes.h" contains definitions for size calculation
3394 * macros whose definitions are implementation-specific. */
3395#include "crypto_sizes.h"
3396
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003397/* The file "crypto_struct.h" contains definitions for
3398 * implementation-specific structs that are declared above. */
3399#include "crypto_struct.h"
3400
3401/* The file "crypto_extra.h" contains vendor-specific definitions. This
3402 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003403#include "crypto_extra.h"
3404
3405#endif /* PSA_CRYPTO_H */