blob: d62c2a9ddf506b3645d0b0159f5f93ecb23d0207 [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
David Saadab4ecc272019-02-14 13:48:10 +0200607 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200608 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200609 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100610 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200611 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
612 * The size of the \p data buffer is too small. You can determine a
613 * sufficient buffer size by calling
614 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
615 * where \c type is the key type
616 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200617 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
618 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200619 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100620 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100621 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300622 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300623 * The library has not been previously initialized by psa_crypto_init().
624 * It is implementation-dependent whether a failure to initialize
625 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100626 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100627psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100628 uint8_t *data,
629 size_t data_size,
630 size_t *data_length);
631
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100632/**
633 * \brief Export a public key or the public part of a key pair in binary format.
634 *
635 * The output of this function can be passed to psa_import_key() to
636 * create an object that is equivalent to the public key.
637 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000638 * This specification supports a single format for each key type.
639 * Implementations may support other formats as long as the standard
640 * format is supported. Implementations that support other formats
641 * should ensure that the formats are clearly unambiguous so as to
642 * minimize the risk that an invalid input is accidentally interpreted
643 * according to a different format.
644 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000645 * For standard key types, the output format is as follows:
646 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
647 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
648 * ```
649 * RSAPublicKey ::= SEQUENCE {
650 * modulus INTEGER, -- n
651 * publicExponent INTEGER } -- e
652 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000653 * - For elliptic curve public keys (key types for which
654 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
655 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
656 * Let `m` be the bit size associated with the curve, i.e. the bit size of
657 * `q` for a curve over `F_q`. The representation consists of:
658 * - The byte 0x04;
659 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
660 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200661 * - For Diffie-Hellman key exchange public keys (key types for which
662 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000663 * the format is the representation of the public key `y = g^x mod p` as a
664 * big-endian byte string. The length of the byte string is the length of the
665 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100666 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200667 * Exporting a public key object or the public part of a key pair is
668 * always permitted, regardless of the key's usage flags.
669 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100670 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200671 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200672 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200673 * \param[out] data_length On success, the number of bytes
674 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100675 *
Gilles Peskine28538492018-07-11 17:34:00 +0200676 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100677 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200678 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200679 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200680 * The key is neither a public key nor a key pair.
681 * \retval #PSA_ERROR_NOT_SUPPORTED
682 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
683 * The size of the \p data buffer is too small. You can determine a
684 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200685 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200686 * where \c type is the key type
687 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200688 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
689 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200690 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300691 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300692 * The library has not been previously initialized by psa_crypto_init().
693 * It is implementation-dependent whether a failure to initialize
694 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100695 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100696psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100697 uint8_t *data,
698 size_t data_size,
699 size_t *data_length);
700
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100701/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100702 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100703 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000704 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100705 * This function is primarily useful to copy a key from one location
706 * to another, since it populates a key using the material from
707 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200708 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100709 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100710 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100711 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200712 * The policy on the source key must have the usage flag
713 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200714 * This flag is sufficient to permit the copy if the key has the lifetime
715 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
716 * Some secure elements do not provide a way to copy a key without
717 * making it extractable from the secure element. If a key is located
718 * in such a secure element, then the key must have both usage flags
719 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
720 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200721 *
Gilles Peskine20628592019-04-19 19:29:50 +0200722 * The resulting key may only be used in a way that conforms to
723 * both the policy of the original key and the policy specified in
724 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100725 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200726 * usage flags on the source policy and the usage flags in \p attributes.
727 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100728 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200729 * - If either of the policies allows an algorithm and the other policy
730 * allows a wildcard-based algorithm policy that includes this algorithm,
731 * the resulting key allows the same algorithm.
732 * - If the policies do not allow any algorithm in common, this function
733 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200734 *
Gilles Peskine20628592019-04-19 19:29:50 +0200735 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100736 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200737 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100738 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200739 * \param[in] attributes The attributes for the new key.
740 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200741 * - The key type and size may be 0. If either is
742 * nonzero, it must match the corresponding
743 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200744 * - The key location (the lifetime and, for
745 * persistent keys, the key identifier) is
746 * used directly.
747 * - The policy constraints (usage flags and
748 * algorithm policy) are combined from
749 * the source key and \p attributes so that
750 * both sets of restrictions apply, as
751 * described in the documentation of this function.
752 * \param[out] target_handle On success, a handle to the newly created key.
753 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200754 *
755 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100756 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200757 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200758 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200759 * This is an attempt to create a persistent key, and there is
760 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200761 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200762 * The lifetime or identifier in \p attributes are invalid.
763 * \retval #PSA_ERROR_INVALID_ARGUMENT
764 * The policy constraints on the source and specified in
765 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200766 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200767 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200768 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100769 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200770 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
771 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100772 * The source key is not exportable and its lifetime does not
773 * allow copying it to the target's lifetime.
774 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
775 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200776 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
777 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200778 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100779 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100780psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200781 const psa_key_attributes_t *attributes,
782 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100783
784/**@}*/
785
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100786/** \defgroup hash Message digests
787 * @{
788 */
789
Gilles Peskine69647a42019-01-14 20:18:12 +0100790/** Calculate the hash (digest) of a message.
791 *
792 * \note To verify the hash of a message against an
793 * expected value, use psa_hash_compare() instead.
794 *
795 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
796 * such that #PSA_ALG_IS_HASH(\p alg) is true).
797 * \param[in] input Buffer containing the message to hash.
798 * \param input_length Size of the \p input buffer in bytes.
799 * \param[out] hash Buffer where the hash is to be written.
800 * \param hash_size Size of the \p hash buffer in bytes.
801 * \param[out] hash_length On success, the number of bytes
802 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100803 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100804 *
805 * \retval #PSA_SUCCESS
806 * Success.
807 * \retval #PSA_ERROR_NOT_SUPPORTED
808 * \p alg is not supported or is not a hash algorithm.
809 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
810 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
811 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200812 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100813 */
814psa_status_t psa_hash_compute(psa_algorithm_t alg,
815 const uint8_t *input,
816 size_t input_length,
817 uint8_t *hash,
818 size_t hash_size,
819 size_t *hash_length);
820
821/** Calculate the hash (digest) of a message and compare it with a
822 * reference value.
823 *
824 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
825 * such that #PSA_ALG_IS_HASH(\p alg) is true).
826 * \param[in] input Buffer containing the message to hash.
827 * \param input_length Size of the \p input buffer in bytes.
828 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100829 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100830 *
831 * \retval #PSA_SUCCESS
832 * The expected hash is identical to the actual hash of the input.
833 * \retval #PSA_ERROR_INVALID_SIGNATURE
834 * The hash of the message was calculated successfully, but it
835 * differs from the expected hash.
836 * \retval #PSA_ERROR_NOT_SUPPORTED
837 * \p alg is not supported or is not a hash algorithm.
838 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
839 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
840 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200841 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100842 */
843psa_status_t psa_hash_compare(psa_algorithm_t alg,
844 const uint8_t *input,
845 size_t input_length,
846 const uint8_t *hash,
847 const size_t hash_length);
848
Gilles Peskine308b91d2018-02-08 09:47:44 +0100849/** The type of the state data structure for multipart hash operations.
850 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000851 * Before calling any function on a hash operation object, the application must
852 * initialize it by any of the following means:
853 * - Set the structure to all-bits-zero, for example:
854 * \code
855 * psa_hash_operation_t operation;
856 * memset(&operation, 0, sizeof(operation));
857 * \endcode
858 * - Initialize the structure to logical zero values, for example:
859 * \code
860 * psa_hash_operation_t operation = {0};
861 * \endcode
862 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
863 * for example:
864 * \code
865 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
866 * \endcode
867 * - Assign the result of the function psa_hash_operation_init()
868 * to the structure, for example:
869 * \code
870 * psa_hash_operation_t operation;
871 * operation = psa_hash_operation_init();
872 * \endcode
873 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100874 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100875 * make any assumptions about the content of this structure except
876 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100877typedef struct psa_hash_operation_s psa_hash_operation_t;
878
Jaeden Amero6a25b412019-01-04 11:47:44 +0000879/** \def PSA_HASH_OPERATION_INIT
880 *
881 * This macro returns a suitable initializer for a hash operation object
882 * of type #psa_hash_operation_t.
883 */
884#ifdef __DOXYGEN_ONLY__
885/* This is an example definition for documentation purposes.
886 * Implementations should define a suitable value in `crypto_struct.h`.
887 */
888#define PSA_HASH_OPERATION_INIT {0}
889#endif
890
891/** Return an initial value for a hash operation object.
892 */
893static psa_hash_operation_t psa_hash_operation_init(void);
894
Gilles Peskinef45adda2019-01-14 18:29:18 +0100895/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100896 *
897 * The sequence of operations to calculate a hash (message digest)
898 * is as follows:
899 * -# Allocate an operation object which will be passed to all the functions
900 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000901 * -# Initialize the operation object with one of the methods described in the
902 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200903 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100904 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100905 * of the message each time. The hash that is calculated is the hash
906 * of the concatenation of these messages in order.
907 * -# To calculate the hash, call psa_hash_finish().
908 * To compare the hash with an expected value, call psa_hash_verify().
909 *
910 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000911 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100912 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200913 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100914 * eventually terminate the operation. The following events terminate an
915 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100916 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100917 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100918 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000919 * \param[in,out] operation The operation object to set up. It must have
920 * been initialized as per the documentation for
921 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200922 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
923 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100924 *
Gilles Peskine28538492018-07-11 17:34:00 +0200925 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100926 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200927 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200928 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100929 * \retval #PSA_ERROR_BAD_STATE
930 * The operation state is not valid (already set up and not
931 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200932 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
933 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
934 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200935 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100936 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200937psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100938 psa_algorithm_t alg);
939
Gilles Peskine308b91d2018-02-08 09:47:44 +0100940/** Add a message fragment to a multipart hash operation.
941 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200942 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100943 *
944 * If this function returns an error status, the operation becomes inactive.
945 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200946 * \param[in,out] operation Active hash operation.
947 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200948 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100949 *
Gilles Peskine28538492018-07-11 17:34:00 +0200950 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100951 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200952 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100953 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200954 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
955 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
956 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200957 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100958 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100959psa_status_t psa_hash_update(psa_hash_operation_t *operation,
960 const uint8_t *input,
961 size_t input_length);
962
Gilles Peskine308b91d2018-02-08 09:47:44 +0100963/** Finish the calculation of the hash of a message.
964 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200965 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100966 * This function calculates the hash of the message formed by concatenating
967 * the inputs passed to preceding calls to psa_hash_update().
968 *
969 * When this function returns, the operation becomes inactive.
970 *
971 * \warning Applications should not call this function if they expect
972 * a specific value for the hash. Call psa_hash_verify() instead.
973 * Beware that comparing integrity or authenticity data such as
974 * hash values with a function such as \c memcmp is risky
975 * because the time taken by the comparison may leak information
976 * about the hashed data which could allow an attacker to guess
977 * a valid hash and thereby bypass security controls.
978 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200979 * \param[in,out] operation Active hash operation.
980 * \param[out] hash Buffer where the hash is to be written.
981 * \param hash_size Size of the \p hash buffer in bytes.
982 * \param[out] hash_length On success, the number of bytes
983 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200984 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200985 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100986 *
Gilles Peskine28538492018-07-11 17:34:00 +0200987 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100988 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200989 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100990 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200991 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200992 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200993 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100994 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200995 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
996 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
997 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200998 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100999 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001000psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1001 uint8_t *hash,
1002 size_t hash_size,
1003 size_t *hash_length);
1004
Gilles Peskine308b91d2018-02-08 09:47:44 +01001005/** Finish the calculation of the hash of a message and compare it with
1006 * an expected value.
1007 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001008 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001009 * This function calculates the hash of the message formed by concatenating
1010 * the inputs passed to preceding calls to psa_hash_update(). It then
1011 * compares the calculated hash with the expected hash passed as a
1012 * parameter to this function.
1013 *
1014 * When this function returns, the operation becomes inactive.
1015 *
Gilles Peskine19067982018-03-20 17:54:53 +01001016 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001017 * comparison between the actual hash and the expected hash is performed
1018 * in constant time.
1019 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001020 * \param[in,out] operation Active hash operation.
1021 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001022 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001023 *
Gilles Peskine28538492018-07-11 17:34:00 +02001024 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001025 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001026 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001027 * The hash of the message was calculated successfully, but it
1028 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001029 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001030 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001031 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1032 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1033 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001034 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001035 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001036psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1037 const uint8_t *hash,
1038 size_t hash_length);
1039
Gilles Peskine308b91d2018-02-08 09:47:44 +01001040/** Abort a hash operation.
1041 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001042 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001043 * \p operation structure itself. Once aborted, the operation object
1044 * can be reused for another operation by calling
1045 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001046 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001047 * You may call this function any time after the operation object has
1048 * been initialized by any of the following methods:
1049 * - A call to psa_hash_setup(), whether it succeeds or not.
1050 * - Initializing the \c struct to all-bits-zero.
1051 * - Initializing the \c struct to logical zeros, e.g.
1052 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001053 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001054 * In particular, calling psa_hash_abort() after the operation has been
1055 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1056 * psa_hash_verify() is safe and has no effect.
1057 *
1058 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001059 *
Gilles Peskine28538492018-07-11 17:34:00 +02001060 * \retval #PSA_SUCCESS
1061 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001062 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001063 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1064 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001065 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001066 */
1067psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001068
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001069/** Clone a hash operation.
1070 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001071 * This function copies the state of an ongoing hash operation to
1072 * a new operation object. In other words, this function is equivalent
1073 * to calling psa_hash_setup() on \p target_operation with the same
1074 * algorithm that \p source_operation was set up for, then
1075 * psa_hash_update() on \p target_operation with the same input that
1076 * that was passed to \p source_operation. After this function returns, the
1077 * two objects are independent, i.e. subsequent calls involving one of
1078 * the objects do not affect the other object.
1079 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001080 * \param[in] source_operation The active hash operation to clone.
1081 * \param[in,out] target_operation The operation object to set up.
1082 * It must be initialized but not active.
1083 *
1084 * \retval #PSA_SUCCESS
1085 * \retval #PSA_ERROR_BAD_STATE
1086 * \p source_operation is not an active hash operation.
1087 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001088 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001089 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1090 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001091 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001092 */
1093psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1094 psa_hash_operation_t *target_operation);
1095
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001096/**@}*/
1097
Gilles Peskine8c9def32018-02-08 10:02:12 +01001098/** \defgroup MAC Message authentication codes
1099 * @{
1100 */
1101
Gilles Peskine69647a42019-01-14 20:18:12 +01001102/** Calculate the MAC (message authentication code) of a message.
1103 *
1104 * \note To verify the MAC of a message against an
1105 * expected value, use psa_mac_verify() instead.
1106 * Beware that comparing integrity or authenticity data such as
1107 * MAC values with a function such as \c memcmp is risky
1108 * because the time taken by the comparison may leak information
1109 * about the MAC value which could allow an attacker to guess
1110 * a valid MAC and thereby bypass security controls.
1111 *
1112 * \param handle Handle to the key to use for the operation.
1113 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001114 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001115 * \param[in] input Buffer containing the input message.
1116 * \param input_length Size of the \p input buffer in bytes.
1117 * \param[out] mac Buffer where the MAC value is to be written.
1118 * \param mac_size Size of the \p mac buffer in bytes.
1119 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001120 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001121 *
1122 * \retval #PSA_SUCCESS
1123 * Success.
1124 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001125 * \retval #PSA_ERROR_NOT_PERMITTED
1126 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001127 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001128 * \retval #PSA_ERROR_NOT_SUPPORTED
1129 * \p alg is not supported or is not a MAC algorithm.
1130 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1131 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1132 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001133 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001134 * \retval #PSA_ERROR_BAD_STATE
1135 * The library has not been previously initialized by psa_crypto_init().
1136 * It is implementation-dependent whether a failure to initialize
1137 * results in this error code.
1138 */
1139psa_status_t psa_mac_compute(psa_key_handle_t handle,
1140 psa_algorithm_t alg,
1141 const uint8_t *input,
1142 size_t input_length,
1143 uint8_t *mac,
1144 size_t mac_size,
1145 size_t *mac_length);
1146
1147/** Calculate the MAC of a message and compare it with a reference value.
1148 *
1149 * \param handle Handle to the key to use for the operation.
1150 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001151 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001152 * \param[in] input Buffer containing the input message.
1153 * \param input_length Size of the \p input buffer in bytes.
1154 * \param[out] mac Buffer containing the expected MAC value.
1155 * \param mac_length Size of the \p mac buffer in bytes.
1156 *
1157 * \retval #PSA_SUCCESS
1158 * The expected MAC is identical to the actual MAC of the input.
1159 * \retval #PSA_ERROR_INVALID_SIGNATURE
1160 * The MAC of the message was calculated successfully, but it
1161 * differs from the expected value.
1162 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001163 * \retval #PSA_ERROR_NOT_PERMITTED
1164 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001165 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001166 * \retval #PSA_ERROR_NOT_SUPPORTED
1167 * \p alg is not supported or is not a MAC algorithm.
1168 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1169 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1170 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001171 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001172 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001173psa_status_t psa_mac_verify(psa_key_handle_t handle,
1174 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001175 const uint8_t *input,
1176 size_t input_length,
1177 const uint8_t *mac,
1178 const size_t mac_length);
1179
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001180/** The type of the state data structure for multipart MAC operations.
1181 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001182 * Before calling any function on a MAC operation object, the application must
1183 * initialize it by any of the following means:
1184 * - Set the structure to all-bits-zero, for example:
1185 * \code
1186 * psa_mac_operation_t operation;
1187 * memset(&operation, 0, sizeof(operation));
1188 * \endcode
1189 * - Initialize the structure to logical zero values, for example:
1190 * \code
1191 * psa_mac_operation_t operation = {0};
1192 * \endcode
1193 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1194 * for example:
1195 * \code
1196 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1197 * \endcode
1198 * - Assign the result of the function psa_mac_operation_init()
1199 * to the structure, for example:
1200 * \code
1201 * psa_mac_operation_t operation;
1202 * operation = psa_mac_operation_init();
1203 * \endcode
1204 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001205 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001206 * make any assumptions about the content of this structure except
1207 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001208typedef struct psa_mac_operation_s psa_mac_operation_t;
1209
Jaeden Amero769ce272019-01-04 11:48:03 +00001210/** \def PSA_MAC_OPERATION_INIT
1211 *
1212 * This macro returns a suitable initializer for a MAC operation object of type
1213 * #psa_mac_operation_t.
1214 */
1215#ifdef __DOXYGEN_ONLY__
1216/* This is an example definition for documentation purposes.
1217 * Implementations should define a suitable value in `crypto_struct.h`.
1218 */
1219#define PSA_MAC_OPERATION_INIT {0}
1220#endif
1221
1222/** Return an initial value for a MAC operation object.
1223 */
1224static psa_mac_operation_t psa_mac_operation_init(void);
1225
Gilles Peskinef45adda2019-01-14 18:29:18 +01001226/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001227 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001228 * This function sets up the calculation of the MAC
1229 * (message authentication code) of a byte string.
1230 * To verify the MAC of a message against an
1231 * expected value, use psa_mac_verify_setup() instead.
1232 *
1233 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001234 * -# Allocate an operation object which will be passed to all the functions
1235 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001236 * -# Initialize the operation object with one of the methods described in the
1237 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001238 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001239 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1240 * of the message each time. The MAC that is calculated is the MAC
1241 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001242 * -# At the end of the message, call psa_mac_sign_finish() to finish
1243 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001244 *
1245 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001246 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001247 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001248 * After a successful call to psa_mac_sign_setup(), the application must
1249 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001250 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001251 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001252 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001253 * \param[in,out] operation The operation object to set up. It must have
1254 * been initialized as per the documentation for
1255 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001256 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001257 * It must remain valid until the operation
1258 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001259 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001260 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001261 *
Gilles Peskine28538492018-07-11 17:34:00 +02001262 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001263 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001264 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001265 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001266 * \retval #PSA_ERROR_NOT_PERMITTED
1267 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001268 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001269 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001270 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001271 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1272 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1273 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001274 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001275 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001276 * The operation state is not valid (already set up and not
1277 * subsequently completed).
1278 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001279 * The library has not been previously initialized by psa_crypto_init().
1280 * It is implementation-dependent whether a failure to initialize
1281 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001282 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001283psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001284 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001285 psa_algorithm_t alg);
1286
Gilles Peskinef45adda2019-01-14 18:29:18 +01001287/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001288 *
1289 * This function sets up the verification of the MAC
1290 * (message authentication code) of a byte string against an expected value.
1291 *
1292 * The sequence of operations to verify a MAC is as follows:
1293 * -# Allocate an operation object which will be passed to all the functions
1294 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001295 * -# Initialize the operation object with one of the methods described in the
1296 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001297 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001298 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1299 * of the message each time. The MAC that is calculated is the MAC
1300 * of the concatenation of these messages in order.
1301 * -# At the end of the message, call psa_mac_verify_finish() to finish
1302 * calculating the actual MAC of the message and verify it against
1303 * the expected value.
1304 *
1305 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001306 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001307 *
1308 * After a successful call to psa_mac_verify_setup(), the application must
1309 * eventually terminate the operation through one of the following methods:
1310 * - A failed call to psa_mac_update().
1311 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1312 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001313 * \param[in,out] operation The operation object to set up. It must have
1314 * been initialized as per the documentation for
1315 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001316 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001317 * It must remain valid until the operation
1318 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001319 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1320 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001321 *
Gilles Peskine28538492018-07-11 17:34:00 +02001322 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001323 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001324 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001325 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001326 * \retval #PSA_ERROR_NOT_PERMITTED
1327 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001328 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001329 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001330 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001331 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1332 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1333 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001334 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001335 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001336 * The operation state is not valid (already set up and not
1337 * subsequently completed).
1338 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001339 * The library has not been previously initialized by psa_crypto_init().
1340 * It is implementation-dependent whether a failure to initialize
1341 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001342 */
1343psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001344 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001345 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001346
Gilles Peskinedcd14942018-07-12 00:30:52 +02001347/** Add a message fragment to a multipart MAC operation.
1348 *
1349 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1350 * before calling this function.
1351 *
1352 * If this function returns an error status, the operation becomes inactive.
1353 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001354 * \param[in,out] operation Active MAC operation.
1355 * \param[in] input Buffer containing the message fragment to add to
1356 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001357 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001358 *
1359 * \retval #PSA_SUCCESS
1360 * Success.
1361 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001362 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001363 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1364 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1365 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001366 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001367 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001368psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1369 const uint8_t *input,
1370 size_t input_length);
1371
Gilles Peskinedcd14942018-07-12 00:30:52 +02001372/** Finish the calculation of the MAC of a message.
1373 *
1374 * The application must call psa_mac_sign_setup() before calling this function.
1375 * This function calculates the MAC of the message formed by concatenating
1376 * the inputs passed to preceding calls to psa_mac_update().
1377 *
1378 * When this function returns, the operation becomes inactive.
1379 *
1380 * \warning Applications should not call this function if they expect
1381 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1382 * Beware that comparing integrity or authenticity data such as
1383 * MAC values with a function such as \c memcmp is risky
1384 * because the time taken by the comparison may leak information
1385 * about the MAC value which could allow an attacker to guess
1386 * a valid MAC and thereby bypass security controls.
1387 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001388 * \param[in,out] operation Active MAC operation.
1389 * \param[out] mac Buffer where the MAC value is to be written.
1390 * \param mac_size Size of the \p mac buffer in bytes.
1391 * \param[out] mac_length On success, the number of bytes
1392 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001393 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001394 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001395 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001396 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001397 *
1398 * \retval #PSA_SUCCESS
1399 * Success.
1400 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001401 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001402 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001403 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001404 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1405 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1406 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1407 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001408 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001409 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001410psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1411 uint8_t *mac,
1412 size_t mac_size,
1413 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001414
Gilles Peskinedcd14942018-07-12 00:30:52 +02001415/** Finish the calculation of the MAC of a message and compare it with
1416 * an expected value.
1417 *
1418 * The application must call psa_mac_verify_setup() before calling this function.
1419 * This function calculates the MAC of the message formed by concatenating
1420 * the inputs passed to preceding calls to psa_mac_update(). It then
1421 * compares the calculated MAC with the expected MAC passed as a
1422 * parameter to this function.
1423 *
1424 * When this function returns, the operation becomes inactive.
1425 *
1426 * \note Implementations shall make the best effort to ensure that the
1427 * comparison between the actual MAC and the expected MAC is performed
1428 * in constant time.
1429 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001430 * \param[in,out] operation Active MAC operation.
1431 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001432 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001433 *
1434 * \retval #PSA_SUCCESS
1435 * The expected MAC is identical to the actual MAC of the message.
1436 * \retval #PSA_ERROR_INVALID_SIGNATURE
1437 * The MAC of the message was calculated successfully, but it
1438 * differs from the expected MAC.
1439 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001440 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001441 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1442 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1443 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001444 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001445 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001446psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1447 const uint8_t *mac,
1448 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001449
Gilles Peskinedcd14942018-07-12 00:30:52 +02001450/** Abort a MAC operation.
1451 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001452 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001453 * \p operation structure itself. Once aborted, the operation object
1454 * can be reused for another operation by calling
1455 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001456 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001457 * You may call this function any time after the operation object has
1458 * been initialized by any of the following methods:
1459 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1460 * it succeeds or not.
1461 * - Initializing the \c struct to all-bits-zero.
1462 * - Initializing the \c struct to logical zeros, e.g.
1463 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001464 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001465 * In particular, calling psa_mac_abort() after the operation has been
1466 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1467 * psa_mac_verify_finish() is safe and has no effect.
1468 *
1469 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001470 *
1471 * \retval #PSA_SUCCESS
1472 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001473 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001474 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1475 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001476 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001477 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001478psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1479
1480/**@}*/
1481
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001482/** \defgroup cipher Symmetric ciphers
1483 * @{
1484 */
1485
Gilles Peskine69647a42019-01-14 20:18:12 +01001486/** Encrypt a message using a symmetric cipher.
1487 *
1488 * This function encrypts a message with a random IV (initialization
1489 * vector).
1490 *
1491 * \param handle Handle to the key to use for the operation.
1492 * It must remain valid until the operation
1493 * terminates.
1494 * \param alg The cipher algorithm to compute
1495 * (\c PSA_ALG_XXX value such that
1496 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1497 * \param[in] input Buffer containing the message to encrypt.
1498 * \param input_length Size of the \p input buffer in bytes.
1499 * \param[out] output Buffer where the output is to be written.
1500 * The output contains the IV followed by
1501 * the ciphertext proper.
1502 * \param output_size Size of the \p output buffer in bytes.
1503 * \param[out] output_length On success, the number of bytes
1504 * that make up the output.
1505 *
1506 * \retval #PSA_SUCCESS
1507 * Success.
1508 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001509 * \retval #PSA_ERROR_NOT_PERMITTED
1510 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001511 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001512 * \retval #PSA_ERROR_NOT_SUPPORTED
1513 * \p alg is not supported or is not a cipher algorithm.
1514 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1515 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1516 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1517 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001518 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001519 */
1520psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1521 psa_algorithm_t alg,
1522 const uint8_t *input,
1523 size_t input_length,
1524 uint8_t *output,
1525 size_t output_size,
1526 size_t *output_length);
1527
1528/** Decrypt a message using a symmetric cipher.
1529 *
1530 * This function decrypts a message encrypted with a symmetric cipher.
1531 *
1532 * \param handle Handle to the key to use for the operation.
1533 * It must remain valid until the operation
1534 * terminates.
1535 * \param alg The cipher algorithm to compute
1536 * (\c PSA_ALG_XXX value such that
1537 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1538 * \param[in] input Buffer containing the message to decrypt.
1539 * This consists of the IV followed by the
1540 * ciphertext proper.
1541 * \param input_length Size of the \p input buffer in bytes.
1542 * \param[out] output Buffer where the plaintext is to be written.
1543 * \param output_size Size of the \p output buffer in bytes.
1544 * \param[out] output_length On success, the number of bytes
1545 * that make up the output.
1546 *
1547 * \retval #PSA_SUCCESS
1548 * Success.
1549 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001550 * \retval #PSA_ERROR_NOT_PERMITTED
1551 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001552 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001553 * \retval #PSA_ERROR_NOT_SUPPORTED
1554 * \p alg is not supported or is not a cipher algorithm.
1555 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1556 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1557 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1558 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001559 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001560 */
1561psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1562 psa_algorithm_t alg,
1563 const uint8_t *input,
1564 size_t input_length,
1565 uint8_t *output,
1566 size_t output_size,
1567 size_t *output_length);
1568
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001569/** The type of the state data structure for multipart cipher operations.
1570 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001571 * Before calling any function on a cipher operation object, the application
1572 * must initialize it by any of the following means:
1573 * - Set the structure to all-bits-zero, for example:
1574 * \code
1575 * psa_cipher_operation_t operation;
1576 * memset(&operation, 0, sizeof(operation));
1577 * \endcode
1578 * - Initialize the structure to logical zero values, for example:
1579 * \code
1580 * psa_cipher_operation_t operation = {0};
1581 * \endcode
1582 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1583 * for example:
1584 * \code
1585 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1586 * \endcode
1587 * - Assign the result of the function psa_cipher_operation_init()
1588 * to the structure, for example:
1589 * \code
1590 * psa_cipher_operation_t operation;
1591 * operation = psa_cipher_operation_init();
1592 * \endcode
1593 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001594 * This is an implementation-defined \c struct. Applications should not
1595 * make any assumptions about the content of this structure except
1596 * as directed by the documentation of a specific implementation. */
1597typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1598
Jaeden Amero5bae2272019-01-04 11:48:27 +00001599/** \def PSA_CIPHER_OPERATION_INIT
1600 *
1601 * This macro returns a suitable initializer for a cipher operation object of
1602 * type #psa_cipher_operation_t.
1603 */
1604#ifdef __DOXYGEN_ONLY__
1605/* This is an example definition for documentation purposes.
1606 * Implementations should define a suitable value in `crypto_struct.h`.
1607 */
1608#define PSA_CIPHER_OPERATION_INIT {0}
1609#endif
1610
1611/** Return an initial value for a cipher operation object.
1612 */
1613static psa_cipher_operation_t psa_cipher_operation_init(void);
1614
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001615/** Set the key for a multipart symmetric encryption operation.
1616 *
1617 * The sequence of operations to encrypt a message with a symmetric cipher
1618 * is as follows:
1619 * -# Allocate an operation object which will be passed to all the functions
1620 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001621 * -# Initialize the operation object with one of the methods described in the
1622 * documentation for #psa_cipher_operation_t, e.g.
1623 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001624 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001625 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001626 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001627 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001628 * requires a specific IV value.
1629 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1630 * of the message each time.
1631 * -# Call psa_cipher_finish().
1632 *
1633 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001634 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001635 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001636 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001637 * eventually terminate the operation. The following events terminate an
1638 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001639 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001640 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001641 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001642 * \param[in,out] operation The operation object to set up. It must have
1643 * been initialized as per the documentation for
1644 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001645 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001646 * It must remain valid until the operation
1647 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001648 * \param alg The cipher algorithm to compute
1649 * (\c PSA_ALG_XXX value such that
1650 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001651 *
Gilles Peskine28538492018-07-11 17:34:00 +02001652 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001653 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001654 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001655 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001656 * \retval #PSA_ERROR_NOT_PERMITTED
1657 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001658 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001659 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001660 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001661 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1662 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1663 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001664 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001665 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001666 * The operation state is not valid (already set up and not
1667 * subsequently completed).
1668 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001669 * The library has not been previously initialized by psa_crypto_init().
1670 * It is implementation-dependent whether a failure to initialize
1671 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001672 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001673psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001674 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001675 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001676
1677/** Set the key for a multipart symmetric decryption operation.
1678 *
1679 * The sequence of operations to decrypt a message with a symmetric cipher
1680 * is as follows:
1681 * -# Allocate an operation object which will be passed to all the functions
1682 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001683 * -# Initialize the operation object with one of the methods described in the
1684 * documentation for #psa_cipher_operation_t, e.g.
1685 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001686 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001687 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001688 * decryption. If the IV is prepended to the ciphertext, you can call
1689 * psa_cipher_update() on a buffer containing the IV followed by the
1690 * beginning of the message.
1691 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1692 * of the message each time.
1693 * -# Call psa_cipher_finish().
1694 *
1695 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001696 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001697 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001698 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001699 * eventually terminate the operation. The following events terminate an
1700 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001701 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001702 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001703 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001704 * \param[in,out] operation The operation object to set up. It must have
1705 * been initialized as per the documentation for
1706 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001707 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001708 * It must remain valid until the operation
1709 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001710 * \param alg The cipher algorithm to compute
1711 * (\c PSA_ALG_XXX value such that
1712 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001713 *
Gilles Peskine28538492018-07-11 17:34:00 +02001714 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001715 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001716 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001717 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001718 * \retval #PSA_ERROR_NOT_PERMITTED
1719 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001720 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001721 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001722 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001723 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1724 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1725 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001726 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001727 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001728 * The operation state is not valid (already set up and not
1729 * subsequently completed).
1730 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001731 * The library has not been previously initialized by psa_crypto_init().
1732 * It is implementation-dependent whether a failure to initialize
1733 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001734 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001735psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001736 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001737 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001738
Gilles Peskinedcd14942018-07-12 00:30:52 +02001739/** Generate an IV for a symmetric encryption operation.
1740 *
1741 * This function generates a random IV (initialization vector), nonce
1742 * or initial counter value for the encryption operation as appropriate
1743 * for the chosen algorithm, key type and key size.
1744 *
1745 * The application must call psa_cipher_encrypt_setup() before
1746 * calling this function.
1747 *
1748 * If this function returns an error status, the operation becomes inactive.
1749 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001750 * \param[in,out] operation Active cipher operation.
1751 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001752 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001753 * \param[out] iv_length On success, the number of bytes of the
1754 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001755 *
1756 * \retval #PSA_SUCCESS
1757 * Success.
1758 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001759 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001760 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001761 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001762 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1763 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1764 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001765 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001766 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001767psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001768 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001769 size_t iv_size,
1770 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001771
Gilles Peskinedcd14942018-07-12 00:30:52 +02001772/** Set the IV for a symmetric encryption or decryption operation.
1773 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001774 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001775 * or initial counter value for the encryption or decryption operation.
1776 *
1777 * The application must call psa_cipher_encrypt_setup() before
1778 * calling this function.
1779 *
1780 * If this function returns an error status, the operation becomes inactive.
1781 *
1782 * \note When encrypting, applications should use psa_cipher_generate_iv()
1783 * instead of this function, unless implementing a protocol that requires
1784 * a non-random IV.
1785 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001786 * \param[in,out] operation Active cipher operation.
1787 * \param[in] iv Buffer containing the IV to use.
1788 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001789 *
1790 * \retval #PSA_SUCCESS
1791 * Success.
1792 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001793 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001794 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001795 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001796 * or the chosen algorithm does not use an IV.
1797 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1798 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1799 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001800 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001801 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001802psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001803 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001804 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001805
Gilles Peskinedcd14942018-07-12 00:30:52 +02001806/** Encrypt or decrypt a message fragment in an active cipher operation.
1807 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001808 * Before calling this function, you must:
1809 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1810 * The choice of setup function determines whether this function
1811 * encrypts or decrypts its input.
1812 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1813 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001814 *
1815 * If this function returns an error status, the operation becomes inactive.
1816 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001817 * \param[in,out] operation Active cipher operation.
1818 * \param[in] input Buffer containing the message fragment to
1819 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001820 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001821 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001822 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001823 * \param[out] output_length On success, the number of bytes
1824 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001825 *
1826 * \retval #PSA_SUCCESS
1827 * Success.
1828 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001829 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001830 * not set, or already completed).
1831 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1832 * The size of the \p output buffer is too small.
1833 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1834 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1835 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001836 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001837 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001838psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1839 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001840 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001841 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001842 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001843 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001844
Gilles Peskinedcd14942018-07-12 00:30:52 +02001845/** Finish encrypting or decrypting a message in a cipher operation.
1846 *
1847 * The application must call psa_cipher_encrypt_setup() or
1848 * psa_cipher_decrypt_setup() before calling this function. The choice
1849 * of setup function determines whether this function encrypts or
1850 * decrypts its input.
1851 *
1852 * This function finishes the encryption or decryption of the message
1853 * formed by concatenating the inputs passed to preceding calls to
1854 * psa_cipher_update().
1855 *
1856 * When this function returns, the operation becomes inactive.
1857 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001858 * \param[in,out] operation Active cipher operation.
1859 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001860 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001861 * \param[out] output_length On success, the number of bytes
1862 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001863 *
1864 * \retval #PSA_SUCCESS
1865 * Success.
1866 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001867 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001868 * not set, or already completed).
1869 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1870 * The size of the \p output buffer is too small.
1871 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1872 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1873 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001874 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001875 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001876psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001877 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001878 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001879 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001880
Gilles Peskinedcd14942018-07-12 00:30:52 +02001881/** Abort a cipher operation.
1882 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001883 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001884 * \p operation structure itself. Once aborted, the operation object
1885 * can be reused for another operation by calling
1886 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001887 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001888 * You may call this function any time after the operation object has
1889 * been initialized by any of the following methods:
1890 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1891 * whether it succeeds or not.
1892 * - Initializing the \c struct to all-bits-zero.
1893 * - Initializing the \c struct to logical zeros, e.g.
1894 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001895 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001896 * In particular, calling psa_cipher_abort() after the operation has been
1897 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1898 * is safe and has no effect.
1899 *
1900 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001901 *
1902 * \retval #PSA_SUCCESS
1903 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001904 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001905 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1906 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001907 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001908 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001909psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1910
1911/**@}*/
1912
Gilles Peskine3b555712018-03-03 21:27:57 +01001913/** \defgroup aead Authenticated encryption with associated data (AEAD)
1914 * @{
1915 */
1916
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001917/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001918 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001919 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001920 * \param alg The AEAD algorithm to compute
1921 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001922 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001923 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001924 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001925 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001926 * but not encrypted.
1927 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001928 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001929 * encrypted.
1930 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001931 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001932 * encrypted data. The additional data is not
1933 * part of this output. For algorithms where the
1934 * encrypted data and the authentication tag
1935 * are defined as separate outputs, the
1936 * authentication tag is appended to the
1937 * encrypted data.
1938 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1939 * This must be at least
1940 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1941 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001942 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001943 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001944 *
Gilles Peskine28538492018-07-11 17:34:00 +02001945 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001946 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001947 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001948 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001949 * \retval #PSA_ERROR_NOT_PERMITTED
1950 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001951 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001952 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001953 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001954 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1955 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1956 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001957 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001958 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001959 * The library has not been previously initialized by psa_crypto_init().
1960 * It is implementation-dependent whether a failure to initialize
1961 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001962 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001963psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001964 psa_algorithm_t alg,
1965 const uint8_t *nonce,
1966 size_t nonce_length,
1967 const uint8_t *additional_data,
1968 size_t additional_data_length,
1969 const uint8_t *plaintext,
1970 size_t plaintext_length,
1971 uint8_t *ciphertext,
1972 size_t ciphertext_size,
1973 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001974
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001975/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001976 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001977 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001978 * \param alg The AEAD algorithm to compute
1979 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001980 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001981 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001982 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001983 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001984 * but not encrypted.
1985 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001986 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001987 * encrypted. For algorithms where the
1988 * encrypted data and the authentication tag
1989 * are defined as separate inputs, the buffer
1990 * must contain the encrypted data followed
1991 * by the authentication tag.
1992 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001993 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001994 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1995 * This must be at least
1996 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1997 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001998 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001999 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002000 *
Gilles Peskine28538492018-07-11 17:34:00 +02002001 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002002 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002003 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002004 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002005 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002006 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002007 * \retval #PSA_ERROR_NOT_PERMITTED
2008 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002009 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002010 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002011 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002012 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2013 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2014 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002015 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002016 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002017 * The library has not been previously initialized by psa_crypto_init().
2018 * It is implementation-dependent whether a failure to initialize
2019 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002020 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002021psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002022 psa_algorithm_t alg,
2023 const uint8_t *nonce,
2024 size_t nonce_length,
2025 const uint8_t *additional_data,
2026 size_t additional_data_length,
2027 const uint8_t *ciphertext,
2028 size_t ciphertext_length,
2029 uint8_t *plaintext,
2030 size_t plaintext_size,
2031 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002032
Gilles Peskine30a9e412019-01-14 18:36:12 +01002033/** The type of the state data structure for multipart AEAD operations.
2034 *
2035 * Before calling any function on an AEAD operation object, the application
2036 * must initialize it by any of the following means:
2037 * - Set the structure to all-bits-zero, for example:
2038 * \code
2039 * psa_aead_operation_t operation;
2040 * memset(&operation, 0, sizeof(operation));
2041 * \endcode
2042 * - Initialize the structure to logical zero values, for example:
2043 * \code
2044 * psa_aead_operation_t operation = {0};
2045 * \endcode
2046 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2047 * for example:
2048 * \code
2049 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2050 * \endcode
2051 * - Assign the result of the function psa_aead_operation_init()
2052 * to the structure, for example:
2053 * \code
2054 * psa_aead_operation_t operation;
2055 * operation = psa_aead_operation_init();
2056 * \endcode
2057 *
2058 * This is an implementation-defined \c struct. Applications should not
2059 * make any assumptions about the content of this structure except
2060 * as directed by the documentation of a specific implementation. */
2061typedef struct psa_aead_operation_s psa_aead_operation_t;
2062
2063/** \def PSA_AEAD_OPERATION_INIT
2064 *
2065 * This macro returns a suitable initializer for an AEAD operation object of
2066 * type #psa_aead_operation_t.
2067 */
2068#ifdef __DOXYGEN_ONLY__
2069/* This is an example definition for documentation purposes.
2070 * Implementations should define a suitable value in `crypto_struct.h`.
2071 */
2072#define PSA_AEAD_OPERATION_INIT {0}
2073#endif
2074
2075/** Return an initial value for an AEAD operation object.
2076 */
2077static psa_aead_operation_t psa_aead_operation_init(void);
2078
2079/** Set the key for a multipart authenticated encryption operation.
2080 *
2081 * The sequence of operations to encrypt a message with authentication
2082 * is as follows:
2083 * -# Allocate an operation object which will be passed to all the functions
2084 * listed here.
2085 * -# Initialize the operation object with one of the methods described in the
2086 * documentation for #psa_aead_operation_t, e.g.
2087 * PSA_AEAD_OPERATION_INIT.
2088 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002089 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2090 * inputs to the subsequent calls to psa_aead_update_ad() and
2091 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2092 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002093 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2094 * generate or set the nonce. You should use
2095 * psa_aead_generate_nonce() unless the protocol you are implementing
2096 * requires a specific nonce value.
2097 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2098 * of the non-encrypted additional authenticated data each time.
2099 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002100 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002101 * -# Call psa_aead_finish().
2102 *
2103 * The application may call psa_aead_abort() at any time after the operation
2104 * has been initialized.
2105 *
2106 * After a successful call to psa_aead_encrypt_setup(), the application must
2107 * eventually terminate the operation. The following events terminate an
2108 * operation:
2109 * - A failed call to any of the \c psa_aead_xxx functions.
2110 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2111 *
2112 * \param[in,out] operation The operation object to set up. It must have
2113 * been initialized as per the documentation for
2114 * #psa_aead_operation_t and not yet in use.
2115 * \param handle Handle to the key to use for the operation.
2116 * It must remain valid until the operation
2117 * terminates.
2118 * \param alg The AEAD algorithm to compute
2119 * (\c PSA_ALG_XXX value such that
2120 * #PSA_ALG_IS_AEAD(\p alg) is true).
2121 *
2122 * \retval #PSA_SUCCESS
2123 * Success.
2124 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002125 * \retval #PSA_ERROR_NOT_PERMITTED
2126 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002127 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002128 * \retval #PSA_ERROR_NOT_SUPPORTED
2129 * \p alg is not supported or is not an AEAD algorithm.
2130 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2131 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2132 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002133 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002134 * \retval #PSA_ERROR_BAD_STATE
2135 * The library has not been previously initialized by psa_crypto_init().
2136 * It is implementation-dependent whether a failure to initialize
2137 * results in this error code.
2138 */
2139psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2140 psa_key_handle_t handle,
2141 psa_algorithm_t alg);
2142
2143/** Set the key for a multipart authenticated decryption operation.
2144 *
2145 * The sequence of operations to decrypt a message with authentication
2146 * is as follows:
2147 * -# Allocate an operation object which will be passed to all the functions
2148 * listed here.
2149 * -# Initialize the operation object with one of the methods described in the
2150 * documentation for #psa_aead_operation_t, e.g.
2151 * PSA_AEAD_OPERATION_INIT.
2152 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002153 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2154 * inputs to the subsequent calls to psa_aead_update_ad() and
2155 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2156 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002157 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2158 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2159 * of the non-encrypted additional authenticated data each time.
2160 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002161 * of the ciphertext to decrypt each time.
2162 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002163 *
2164 * The application may call psa_aead_abort() at any time after the operation
2165 * has been initialized.
2166 *
2167 * After a successful call to psa_aead_decrypt_setup(), the application must
2168 * eventually terminate the operation. The following events terminate an
2169 * operation:
2170 * - A failed call to any of the \c psa_aead_xxx functions.
2171 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2172 *
2173 * \param[in,out] operation The operation object to set up. It must have
2174 * been initialized as per the documentation for
2175 * #psa_aead_operation_t and not yet in use.
2176 * \param handle Handle to the key to use for the operation.
2177 * It must remain valid until the operation
2178 * terminates.
2179 * \param alg The AEAD algorithm to compute
2180 * (\c PSA_ALG_XXX value such that
2181 * #PSA_ALG_IS_AEAD(\p alg) is true).
2182 *
2183 * \retval #PSA_SUCCESS
2184 * Success.
2185 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002186 * \retval #PSA_ERROR_NOT_PERMITTED
2187 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002188 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002189 * \retval #PSA_ERROR_NOT_SUPPORTED
2190 * \p alg is not supported or is not an AEAD algorithm.
2191 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2192 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2193 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002194 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002195 * \retval #PSA_ERROR_BAD_STATE
2196 * The library has not been previously initialized by psa_crypto_init().
2197 * It is implementation-dependent whether a failure to initialize
2198 * results in this error code.
2199 */
2200psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2201 psa_key_handle_t handle,
2202 psa_algorithm_t alg);
2203
2204/** Generate a random nonce for an authenticated encryption operation.
2205 *
2206 * This function generates a random nonce for the authenticated encryption
2207 * operation with an appropriate size for the chosen algorithm, key type
2208 * and key size.
2209 *
2210 * The application must call psa_aead_encrypt_setup() before
2211 * calling this function.
2212 *
2213 * If this function returns an error status, the operation becomes inactive.
2214 *
2215 * \param[in,out] operation Active AEAD operation.
2216 * \param[out] nonce Buffer where the generated nonce is to be
2217 * written.
2218 * \param nonce_size Size of the \p nonce buffer in bytes.
2219 * \param[out] nonce_length On success, the number of bytes of the
2220 * generated nonce.
2221 *
2222 * \retval #PSA_SUCCESS
2223 * Success.
2224 * \retval #PSA_ERROR_BAD_STATE
2225 * The operation state is not valid (not set up, or nonce already set).
2226 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2227 * The size of the \p nonce buffer is too small.
2228 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2229 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2230 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002231 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002232 */
2233psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002234 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002235 size_t nonce_size,
2236 size_t *nonce_length);
2237
2238/** Set the nonce for an authenticated encryption or decryption operation.
2239 *
2240 * This function sets the nonce for the authenticated
2241 * encryption or decryption operation.
2242 *
2243 * The application must call psa_aead_encrypt_setup() before
2244 * calling this function.
2245 *
2246 * If this function returns an error status, the operation becomes inactive.
2247 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002248 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002249 * instead of this function, unless implementing a protocol that requires
2250 * a non-random IV.
2251 *
2252 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002253 * \param[in] nonce Buffer containing the nonce to use.
2254 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002255 *
2256 * \retval #PSA_SUCCESS
2257 * Success.
2258 * \retval #PSA_ERROR_BAD_STATE
2259 * The operation state is not valid (not set up, or nonce already set).
2260 * \retval #PSA_ERROR_INVALID_ARGUMENT
2261 * The size of \p nonce is not acceptable for the chosen algorithm.
2262 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2263 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2264 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002265 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002266 */
2267psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002268 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002269 size_t nonce_length);
2270
Gilles Peskinebc59c852019-01-17 15:26:08 +01002271/** Declare the lengths of the message and additional data for AEAD.
2272 *
2273 * The application must call this function before calling
2274 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2275 * the operation requires it. If the algorithm does not require it,
2276 * calling this function is optional, but if this function is called
2277 * then the implementation must enforce the lengths.
2278 *
2279 * You may call this function before or after setting the nonce with
2280 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2281 *
2282 * - For #PSA_ALG_CCM, calling this function is required.
2283 * - For the other AEAD algorithms defined in this specification, calling
2284 * this function is not required.
2285 * - For vendor-defined algorithm, refer to the vendor documentation.
2286 *
2287 * \param[in,out] operation Active AEAD operation.
2288 * \param ad_length Size of the non-encrypted additional
2289 * authenticated data in bytes.
2290 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2291 *
2292 * \retval #PSA_SUCCESS
2293 * Success.
2294 * \retval #PSA_ERROR_BAD_STATE
2295 * The operation state is not valid (not set up, already completed,
2296 * or psa_aead_update_ad() or psa_aead_update() already called).
2297 * \retval #PSA_ERROR_INVALID_ARGUMENT
2298 * At least one of the lengths is not acceptable for the chosen
2299 * algorithm.
2300 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2301 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2302 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002303 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinebc59c852019-01-17 15:26:08 +01002304 */
2305psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2306 size_t ad_length,
2307 size_t plaintext_length);
2308
Gilles Peskine30a9e412019-01-14 18:36:12 +01002309/** Pass additional data to an active AEAD operation.
2310 *
2311 * Additional data is authenticated, but not encrypted.
2312 *
2313 * You may call this function multiple times to pass successive fragments
2314 * of the additional data. You may not call this function after passing
2315 * data to encrypt or decrypt with psa_aead_update().
2316 *
2317 * Before calling this function, you must:
2318 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2319 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2320 *
2321 * If this function returns an error status, the operation becomes inactive.
2322 *
2323 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2324 * there is no guarantee that the input is valid. Therefore, until
2325 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2326 * treat the input as untrusted and prepare to undo any action that
2327 * depends on the input if psa_aead_verify() returns an error status.
2328 *
2329 * \param[in,out] operation Active AEAD operation.
2330 * \param[in] input Buffer containing the fragment of
2331 * additional data.
2332 * \param input_length Size of the \p input buffer in bytes.
2333 *
2334 * \retval #PSA_SUCCESS
2335 * Success.
2336 * \retval #PSA_ERROR_BAD_STATE
2337 * The operation state is not valid (not set up, nonce not set,
2338 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002339 * \retval #PSA_ERROR_INVALID_ARGUMENT
2340 * The total input length overflows the additional data length that
2341 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002342 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2343 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2344 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002345 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002346 */
2347psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2348 const uint8_t *input,
2349 size_t input_length);
2350
2351/** Encrypt or decrypt a message fragment in an active AEAD operation.
2352 *
2353 * Before calling this function, you must:
2354 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2355 * The choice of setup function determines whether this function
2356 * encrypts or decrypts its input.
2357 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2358 * 3. Call psa_aead_update_ad() to pass all the additional data.
2359 *
2360 * If this function returns an error status, the operation becomes inactive.
2361 *
2362 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2363 * there is no guarantee that the input is valid. Therefore, until
2364 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2365 * - Do not use the output in any way other than storing it in a
2366 * confidential location. If you take any action that depends
2367 * on the tentative decrypted data, this action will need to be
2368 * undone if the input turns out not to be valid. Furthermore,
2369 * if an adversary can observe that this action took place
2370 * (for example through timing), they may be able to use this
2371 * fact as an oracle to decrypt any message encrypted with the
2372 * same key.
2373 * - In particular, do not copy the output anywhere but to a
2374 * memory or storage space that you have exclusive access to.
2375 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002376 * This function does not require the input to be aligned to any
2377 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002378 * a whole block at a time, it must consume all the input provided, but
2379 * it may delay the end of the corresponding output until a subsequent
2380 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2381 * provides sufficient input. The amount of data that can be delayed
2382 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002383 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002384 * \param[in,out] operation Active AEAD operation.
2385 * \param[in] input Buffer containing the message fragment to
2386 * encrypt or decrypt.
2387 * \param input_length Size of the \p input buffer in bytes.
2388 * \param[out] output Buffer where the output is to be written.
2389 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002390 * This must be at least
2391 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2392 * \p input_length) where \c alg is the
2393 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002394 * \param[out] output_length On success, the number of bytes
2395 * that make up the returned output.
2396 *
2397 * \retval #PSA_SUCCESS
2398 * Success.
2399 * \retval #PSA_ERROR_BAD_STATE
2400 * The operation state is not valid (not set up, nonce not set
2401 * or already completed).
2402 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2403 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002404 * You can determine a sufficient buffer size by calling
2405 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2406 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002407 * \retval #PSA_ERROR_INVALID_ARGUMENT
2408 * The total length of input to psa_aead_update_ad() so far is
2409 * less than the additional data length that was previously
2410 * specified with psa_aead_set_lengths().
2411 * \retval #PSA_ERROR_INVALID_ARGUMENT
2412 * The total input length overflows the plaintext length that
2413 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002414 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2415 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2416 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002417 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002418 */
2419psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2420 const uint8_t *input,
2421 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002422 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002423 size_t output_size,
2424 size_t *output_length);
2425
2426/** Finish encrypting a message in an AEAD operation.
2427 *
2428 * The operation must have been set up with psa_aead_encrypt_setup().
2429 *
2430 * This function finishes the authentication of the additional data
2431 * formed by concatenating the inputs passed to preceding calls to
2432 * psa_aead_update_ad() with the plaintext formed by concatenating the
2433 * inputs passed to preceding calls to psa_aead_update().
2434 *
2435 * This function has two output buffers:
2436 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002437 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002438 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002439 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002440 * that the operation performs.
2441 *
2442 * When this function returns, the operation becomes inactive.
2443 *
2444 * \param[in,out] operation Active AEAD operation.
2445 * \param[out] ciphertext Buffer where the last part of the ciphertext
2446 * is to be written.
2447 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002448 * This must be at least
2449 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2450 * \c alg is the algorithm that is being
2451 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002452 * \param[out] ciphertext_length On success, the number of bytes of
2453 * returned ciphertext.
2454 * \param[out] tag Buffer where the authentication tag is
2455 * to be written.
2456 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002457 * This must be at least
2458 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2459 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002460 * \param[out] tag_length On success, the number of bytes
2461 * that make up the returned tag.
2462 *
2463 * \retval #PSA_SUCCESS
2464 * Success.
2465 * \retval #PSA_ERROR_BAD_STATE
2466 * The operation state is not valid (not set up, nonce not set,
2467 * decryption, or already completed).
2468 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002469 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002470 * You can determine a sufficient buffer size for \p ciphertext by
2471 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2472 * where \c alg is the algorithm that is being calculated.
2473 * You can determine a sufficient buffer size for \p tag by
2474 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002475 * \retval #PSA_ERROR_INVALID_ARGUMENT
2476 * The total length of input to psa_aead_update_ad() so far is
2477 * less than the additional data length that was previously
2478 * specified with psa_aead_set_lengths().
2479 * \retval #PSA_ERROR_INVALID_ARGUMENT
2480 * The total length of input to psa_aead_update() so far is
2481 * less than the plaintext length that was previously
2482 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002483 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2484 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2485 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002486 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002487 */
2488psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002489 uint8_t *ciphertext,
2490 size_t ciphertext_size,
2491 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002492 uint8_t *tag,
2493 size_t tag_size,
2494 size_t *tag_length);
2495
2496/** Finish authenticating and decrypting a message in an AEAD operation.
2497 *
2498 * The operation must have been set up with psa_aead_decrypt_setup().
2499 *
2500 * This function finishes the authentication of the additional data
2501 * formed by concatenating the inputs passed to preceding calls to
2502 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2503 * inputs passed to preceding calls to psa_aead_update().
2504 *
2505 * When this function returns, the operation becomes inactive.
2506 *
2507 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002508 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002509 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002510 * from previous calls to psa_aead_update()
2511 * that could not be processed until the end
2512 * of the input.
2513 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002514 * This must be at least
2515 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2516 * \c alg is the algorithm that is being
2517 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002518 * \param[out] plaintext_length On success, the number of bytes of
2519 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002520 * \param[in] tag Buffer containing the authentication tag.
2521 * \param tag_length Size of the \p tag buffer in bytes.
2522 *
2523 * \retval #PSA_SUCCESS
2524 * Success.
2525 * \retval #PSA_ERROR_BAD_STATE
2526 * The operation state is not valid (not set up, nonce not set,
2527 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002528 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2529 * The size of the \p plaintext buffer is too small.
2530 * You can determine a sufficient buffer size for \p plaintext by
2531 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2532 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002533 * \retval #PSA_ERROR_INVALID_ARGUMENT
2534 * The total length of input to psa_aead_update_ad() so far is
2535 * less than the additional data length that was previously
2536 * specified with psa_aead_set_lengths().
2537 * \retval #PSA_ERROR_INVALID_ARGUMENT
2538 * The total length of input to psa_aead_update() so far is
2539 * less than the plaintext length that was previously
2540 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002541 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2542 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2543 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002544 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002545 */
2546psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002547 uint8_t *plaintext,
2548 size_t plaintext_size,
2549 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002550 const uint8_t *tag,
2551 size_t tag_length);
2552
2553/** Abort an AEAD operation.
2554 *
2555 * Aborting an operation frees all associated resources except for the
2556 * \p operation structure itself. Once aborted, the operation object
2557 * can be reused for another operation by calling
2558 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2559 *
2560 * You may call this function any time after the operation object has
2561 * been initialized by any of the following methods:
2562 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2563 * whether it succeeds or not.
2564 * - Initializing the \c struct to all-bits-zero.
2565 * - Initializing the \c struct to logical zeros, e.g.
2566 * `psa_aead_operation_t operation = {0}`.
2567 *
2568 * In particular, calling psa_aead_abort() after the operation has been
2569 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2570 * is safe and has no effect.
2571 *
2572 * \param[in,out] operation Initialized AEAD operation.
2573 *
2574 * \retval #PSA_SUCCESS
2575 * \retval #PSA_ERROR_BAD_STATE
2576 * \p operation is not an active AEAD operation.
2577 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2578 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002579 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002580 */
2581psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2582
Gilles Peskine3b555712018-03-03 21:27:57 +01002583/**@}*/
2584
Gilles Peskine20035e32018-02-03 22:44:14 +01002585/** \defgroup asymmetric Asymmetric cryptography
2586 * @{
2587 */
2588
2589/**
2590 * \brief Sign a hash or short message with a private key.
2591 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002592 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002593 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002594 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2595 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2596 * to determine the hash algorithm to use.
2597 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002598 * \param handle Handle to the key to use for the operation.
2599 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002600 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002601 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002602 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002603 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002604 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002605 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002606 * \param[out] signature_length On success, the number of bytes
2607 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002608 *
Gilles Peskine28538492018-07-11 17:34:00 +02002609 * \retval #PSA_SUCCESS
2610 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002611 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002612 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002613 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002614 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002615 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002616 * \retval #PSA_ERROR_NOT_SUPPORTED
2617 * \retval #PSA_ERROR_INVALID_ARGUMENT
2618 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2619 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2620 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002621 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002622 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002623 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002624 * The library has not been previously initialized by psa_crypto_init().
2625 * It is implementation-dependent whether a failure to initialize
2626 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002627 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002628psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002629 psa_algorithm_t alg,
2630 const uint8_t *hash,
2631 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002632 uint8_t *signature,
2633 size_t signature_size,
2634 size_t *signature_length);
2635
2636/**
2637 * \brief Verify the signature a hash or short message using a public key.
2638 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002639 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002640 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002641 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2642 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2643 * to determine the hash algorithm to use.
2644 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002645 * \param handle Handle to the key to use for the operation.
2646 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002647 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002648 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002649 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002650 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002651 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002652 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002653 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002654 *
Gilles Peskine28538492018-07-11 17:34:00 +02002655 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002656 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002657 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002658 * The calculation was perfomed successfully, but the passed
2659 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002660 * \retval #PSA_ERROR_NOT_SUPPORTED
2661 * \retval #PSA_ERROR_INVALID_ARGUMENT
2662 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2663 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2664 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002665 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002666 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002667 * The library has not been previously initialized by psa_crypto_init().
2668 * It is implementation-dependent whether a failure to initialize
2669 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002670 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002671psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002672 psa_algorithm_t alg,
2673 const uint8_t *hash,
2674 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002675 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002676 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002677
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002678/**
2679 * \brief Encrypt a short message with a public key.
2680 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002681 * \param handle Handle to the key to use for the operation.
2682 * It must be a public key or an asymmetric
2683 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002684 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002685 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002686 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002687 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002688 * \param[in] salt A salt or label, if supported by the
2689 * encryption algorithm.
2690 * If the algorithm does not support a
2691 * salt, pass \c NULL.
2692 * If the algorithm supports an optional
2693 * salt and you do not want to pass a salt,
2694 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002695 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002696 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2697 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002698 * \param salt_length Size of the \p salt buffer in bytes.
2699 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002700 * \param[out] output Buffer where the encrypted message is to
2701 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002702 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002703 * \param[out] output_length On success, the number of bytes
2704 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002705 *
Gilles Peskine28538492018-07-11 17:34:00 +02002706 * \retval #PSA_SUCCESS
2707 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002708 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002709 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002710 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002711 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002712 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002713 * \retval #PSA_ERROR_NOT_SUPPORTED
2714 * \retval #PSA_ERROR_INVALID_ARGUMENT
2715 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2716 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2717 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002718 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002719 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002720 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002721 * The library has not been previously initialized by psa_crypto_init().
2722 * It is implementation-dependent whether a failure to initialize
2723 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002724 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002725psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002726 psa_algorithm_t alg,
2727 const uint8_t *input,
2728 size_t input_length,
2729 const uint8_t *salt,
2730 size_t salt_length,
2731 uint8_t *output,
2732 size_t output_size,
2733 size_t *output_length);
2734
2735/**
2736 * \brief Decrypt a short message with a private key.
2737 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002738 * \param handle Handle to the key to use for the operation.
2739 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002740 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002741 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002742 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002743 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002744 * \param[in] salt A salt or label, if supported by the
2745 * encryption algorithm.
2746 * If the algorithm does not support a
2747 * salt, pass \c NULL.
2748 * If the algorithm supports an optional
2749 * salt and you do not want to pass a salt,
2750 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002751 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002752 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2753 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002754 * \param salt_length Size of the \p salt buffer in bytes.
2755 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002756 * \param[out] output Buffer where the decrypted message is to
2757 * be written.
2758 * \param output_size Size of the \c output buffer in bytes.
2759 * \param[out] output_length On success, the number of bytes
2760 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002761 *
Gilles Peskine28538492018-07-11 17:34:00 +02002762 * \retval #PSA_SUCCESS
2763 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002764 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002765 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002766 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002767 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002768 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002769 * \retval #PSA_ERROR_NOT_SUPPORTED
2770 * \retval #PSA_ERROR_INVALID_ARGUMENT
2771 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2772 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2773 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002774 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002775 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2776 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002777 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002778 * The library has not been previously initialized by psa_crypto_init().
2779 * It is implementation-dependent whether a failure to initialize
2780 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002781 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002782psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002783 psa_algorithm_t alg,
2784 const uint8_t *input,
2785 size_t input_length,
2786 const uint8_t *salt,
2787 size_t salt_length,
2788 uint8_t *output,
2789 size_t output_size,
2790 size_t *output_length);
2791
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002792/**@}*/
2793
Gilles Peskine35675b62019-05-16 17:26:11 +02002794/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002795 * @{
2796 */
2797
Gilles Peskine35675b62019-05-16 17:26:11 +02002798/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002799 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002800 * Before calling any function on a key derivation operation object, the
2801 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002802 * - Set the structure to all-bits-zero, for example:
2803 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002804 * psa_key_derivation_operation_t operation;
2805 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002806 * \endcode
2807 * - Initialize the structure to logical zero values, for example:
2808 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002809 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002810 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002811 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002812 * for example:
2813 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002814 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002815 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002816 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002817 * to the structure, for example:
2818 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002819 * psa_key_derivation_operation_t operation;
2820 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002821 * \endcode
2822 *
2823 * This is an implementation-defined \c struct. Applications should not
2824 * make any assumptions about the content of this structure except
2825 * as directed by the documentation of a specific implementation.
2826 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002827typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002828
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002829/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002830 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002831 * This macro returns a suitable initializer for a key derivation operation
2832 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002833 */
2834#ifdef __DOXYGEN_ONLY__
2835/* This is an example definition for documentation purposes.
2836 * Implementations should define a suitable value in `crypto_struct.h`.
2837 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002838#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02002839#endif
2840
Gilles Peskine35675b62019-05-16 17:26:11 +02002841/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002842 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002843static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002844
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002845/** Set up a key derivation operation.
2846 *
2847 * A key derivation algorithm takes some inputs and uses them to generate
2848 * a byte stream in a deterministic way.
2849 * This byte stream can be used to produce keys and other
2850 * cryptographic material.
2851 *
2852 * To derive a key:
2853 * - Start with an initialized object of type #psa_key_derivation_operation_t.
2854 * - Call psa_key_derivation_setup() to select the algorithm.
2855 * - Provide the inputs for the key derivation by calling
2856 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2857 * as appropriate. Which inputs are needed, in what order, and whether
2858 * they may be keys and if so of what type depends on the algorithm.
2859 * - Optionally set the operation's maximum capacity with
2860 * psa_key_derivation_set_capacity(). You may do this before, in the middle
2861 * of or after providing inputs. For some algorithms, this step is mandatory
2862 * because the output depends on the maximum capacity.
2863 * - To derive a key, call psa_key_derivation_output_key().
2864 * To derive a byte string for a different purpose, call
2865 * - psa_key_derivation_output_bytes().
2866 * Successive calls to these functions use successive output bytes
2867 * calculated by the key derivation algorithm.
2868 * - Clean up the key derivation operation object with
2869 * psa_key_derivation_abort().
2870 *
2871 * \param[in,out] operation The key derivation operation object
2872 * to set up. It must
2873 * have been initialized but not set up yet.
2874 * \param alg The key derivation algorithm to compute
2875 * (\c PSA_ALG_XXX value such that
2876 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2877 *
2878 * \retval #PSA_SUCCESS
2879 * Success.
2880 * \retval #PSA_ERROR_INVALID_ARGUMENT
2881 * \c alg is not a key derivation algorithm.
2882 * \retval #PSA_ERROR_NOT_SUPPORTED
2883 * \c alg is not supported or is not a key derivation algorithm.
2884 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2885 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2886 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002887 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002888 * \retval #PSA_ERROR_BAD_STATE
2889 */
2890psa_status_t psa_key_derivation_setup(
2891 psa_key_derivation_operation_t *operation,
2892 psa_algorithm_t alg);
2893
Gilles Peskine35675b62019-05-16 17:26:11 +02002894/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002895 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002896 * The capacity of a key derivation is the maximum number of bytes that it can
2897 * return. When you get *N* bytes of output from a key derivation operation,
2898 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002899 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002900 * \param[in] operation The operation to query.
2901 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002902 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002903 * \retval #PSA_SUCCESS
2904 * \retval #PSA_ERROR_BAD_STATE
2905 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002906 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002907psa_status_t psa_key_derivation_get_capacity(
2908 const psa_key_derivation_operation_t *operation,
2909 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002910
Gilles Peskine35675b62019-05-16 17:26:11 +02002911/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002912 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002913 * The capacity of a key derivation operation is the maximum number of bytes
2914 * that the key derivation operation can return from this point onwards.
2915 *
2916 * \param[in,out] operation The key derivation operation object to modify.
2917 * \param capacity The new capacity of the operation.
2918 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002919 * current capacity.
2920 *
2921 * \retval #PSA_SUCCESS
2922 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02002923 * \p capacity is larger than the operation's current capacity.
2924 * In this case, the operation object remains valid and its capacity
2925 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002926 * \retval #PSA_ERROR_BAD_STATE
2927 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2928 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002929psa_status_t psa_key_derivation_set_capacity(
2930 psa_key_derivation_operation_t *operation,
2931 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002932
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002933/** Use the maximum possible capacity for a key derivation operation.
2934 *
2935 * Use this value as the capacity argument when setting up a key derivation
2936 * to indicate that the operation should have the maximum possible capacity.
2937 * The value of the maximum possible capacity depends on the key derivation
2938 * algorithm.
2939 */
2940#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
2941
2942/** Provide an input for key derivation or key agreement.
2943 *
2944 * Which inputs are required and in what order depends on the algorithm.
2945 * Refer to the documentation of each key derivation or key agreement
2946 * algorithm for information.
2947 *
2948 * This function passes direct inputs. Some inputs must be passed as keys
2949 * using psa_key_derivation_input_key() instead of this function. Refer to
2950 * the documentation of individual step types for information.
2951 *
2952 * \param[in,out] operation The key derivation operation object to use.
2953 * It must have been set up with
2954 * psa_key_derivation_setup() and must not
2955 * have produced any output yet.
2956 * \param step Which step the input data is for.
2957 * \param[in] data Input data to use.
2958 * \param data_length Size of the \p data buffer in bytes.
2959 *
2960 * \retval #PSA_SUCCESS
2961 * Success.
2962 * \retval #PSA_ERROR_INVALID_ARGUMENT
2963 * \c step is not compatible with the operation's algorithm.
2964 * \retval #PSA_ERROR_INVALID_ARGUMENT
2965 * \c step does not allow direct inputs.
2966 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2967 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2968 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002969 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002970 * \retval #PSA_ERROR_BAD_STATE
2971 * The value of \p step is not valid given the state of \p operation.
2972 * \retval #PSA_ERROR_BAD_STATE
2973 * The library has not been previously initialized by psa_crypto_init().
2974 * It is implementation-dependent whether a failure to initialize
2975 * results in this error code.
2976 */
2977psa_status_t psa_key_derivation_input_bytes(
2978 psa_key_derivation_operation_t *operation,
2979 psa_key_derivation_step_t step,
2980 const uint8_t *data,
2981 size_t data_length);
2982
2983/** Provide an input for key derivation in the form of a key.
2984 *
2985 * Which inputs are required and in what order depends on the algorithm.
2986 * Refer to the documentation of each key derivation or key agreement
2987 * algorithm for information.
2988 *
2989 * This function passes key inputs. Some inputs must be passed as keys
2990 * of the appropriate type using this function, while others must be
2991 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
2992 * the documentation of individual step types for information.
2993 *
2994 * \param[in,out] operation The key derivation operation object to use.
2995 * It must have been set up with
2996 * psa_key_derivation_setup() and must not
2997 * have produced any output yet.
2998 * \param step Which step the input data is for.
2999 * \param handle Handle to the key. It must have an
3000 * appropriate type for \p step and must
3001 * allow the usage #PSA_KEY_USAGE_DERIVE.
3002 *
3003 * \retval #PSA_SUCCESS
3004 * Success.
3005 * \retval #PSA_ERROR_INVALID_HANDLE
3006 * \retval #PSA_ERROR_DOES_NOT_EXIST
3007 * \retval #PSA_ERROR_NOT_PERMITTED
3008 * \retval #PSA_ERROR_INVALID_ARGUMENT
3009 * \c step is not compatible with the operation's algorithm.
3010 * \retval #PSA_ERROR_INVALID_ARGUMENT
3011 * \c step does not allow key inputs.
3012 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3013 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3014 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003015 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003016 * \retval #PSA_ERROR_BAD_STATE
3017 * The value of \p step is not valid given the state of \p operation.
3018 * \retval #PSA_ERROR_BAD_STATE
3019 * The library has not been previously initialized by psa_crypto_init().
3020 * It is implementation-dependent whether a failure to initialize
3021 * results in this error code.
3022 */
3023psa_status_t psa_key_derivation_input_key(
3024 psa_key_derivation_operation_t *operation,
3025 psa_key_derivation_step_t step,
3026 psa_key_handle_t handle);
3027
3028/** Perform a key agreement and use the shared secret as input to a key
3029 * derivation.
3030 *
3031 * A key agreement algorithm takes two inputs: a private key \p private_key
3032 * a public key \p peer_key.
3033 * The result of this function is passed as input to a key derivation.
3034 * The output of this key derivation can be extracted by reading from the
3035 * resulting operation to produce keys and other cryptographic material.
3036 *
3037 * \param[in,out] operation The key derivation operation object to use.
3038 * It must have been set up with
3039 * psa_key_derivation_setup() with a
3040 * key agreement and derivation algorithm
3041 * \c alg (\c PSA_ALG_XXX value such that
3042 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3043 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3044 * is false).
3045 * The operation must be ready for an
3046 * input of the type given by \p step.
3047 * \param step Which step the input data is for.
3048 * \param private_key Handle to the private key to use.
3049 * \param[in] peer_key Public key of the peer. The peer key must be in the
3050 * same format that psa_import_key() accepts for the
3051 * public key type corresponding to the type of
3052 * private_key. That is, this function performs the
3053 * equivalent of
3054 * #psa_import_key(...,
3055 * `peer_key`, `peer_key_length`) where
3056 * with key attributes indicating the public key
3057 * type corresponding to the type of `private_key`.
3058 * For example, for EC keys, this means that peer_key
3059 * is interpreted as a point on the curve that the
3060 * private key is on. The standard formats for public
3061 * keys are documented in the documentation of
3062 * psa_export_public_key().
3063 * \param peer_key_length Size of \p peer_key in bytes.
3064 *
3065 * \retval #PSA_SUCCESS
3066 * Success.
3067 * \retval #PSA_ERROR_INVALID_HANDLE
3068 * \retval #PSA_ERROR_DOES_NOT_EXIST
3069 * \retval #PSA_ERROR_NOT_PERMITTED
3070 * \retval #PSA_ERROR_INVALID_ARGUMENT
3071 * \c private_key is not compatible with \c alg,
3072 * or \p peer_key is not valid for \c alg or not compatible with
3073 * \c private_key.
3074 * \retval #PSA_ERROR_NOT_SUPPORTED
3075 * \c alg is not supported or is not a key derivation algorithm.
3076 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3077 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3078 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003079 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003080 */
3081psa_status_t psa_key_derivation_key_agreement(
3082 psa_key_derivation_operation_t *operation,
3083 psa_key_derivation_step_t step,
3084 psa_key_handle_t private_key,
3085 const uint8_t *peer_key,
3086 size_t peer_key_length);
3087
Gilles Peskine35675b62019-05-16 17:26:11 +02003088/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003089 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003090 * This function calculates output bytes from a key derivation algorithm and
3091 * return those bytes.
3092 * If you view the key derivation's output as a stream of bytes, this
3093 * function destructively reads the requested number of bytes from the
3094 * stream.
3095 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003096 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003097 * \param[in,out] operation The key derivation operation object to read from.
3098 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003099 * \param output_length Number of bytes to output.
3100 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003101 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003102 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003103 * The operation's capacity was less than
3104 * \p output_length bytes. Note that in this case,
3105 * no output is written to the output buffer.
3106 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003107 * subsequent calls to this function will not
3108 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003109 * \retval #PSA_ERROR_BAD_STATE
3110 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3111 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3112 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003113 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003114 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003115psa_status_t psa_key_derivation_output_bytes(
3116 psa_key_derivation_operation_t *operation,
3117 uint8_t *output,
3118 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003119
Gilles Peskine35675b62019-05-16 17:26:11 +02003120/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003121 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003122 * This function calculates output bytes from a key derivation algorithm
3123 * and uses those bytes to generate a key deterministically.
3124 * If you view the key derivation's output as a stream of bytes, this
3125 * function destructively reads as many bytes as required from the
3126 * stream.
3127 * The operation's capacity decreases by the number of bytes read.
3128 *
3129 * How much output is produced and consumed from the operation, and how
3130 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003131 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003132 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003133 * of a given size, this function is functionally equivalent to
3134 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003135 * and passing the resulting output to #psa_import_key.
3136 * However, this function has a security benefit:
3137 * if the implementation provides an isolation boundary then
3138 * the key material is not exposed outside the isolation boundary.
3139 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003140 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003141 * The following key types defined in this specification follow this scheme:
3142 *
3143 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003144 * - #PSA_KEY_TYPE_ARC4;
3145 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003146 * - #PSA_KEY_TYPE_DERIVE;
3147 * - #PSA_KEY_TYPE_HMAC.
3148 *
3149 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003150 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003151 * Montgomery curve), this function always draws a byte string whose
3152 * length is determined by the curve, and sets the mandatory bits
3153 * accordingly. That is:
3154 *
3155 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3156 * and process it as specified in RFC 7748 &sect;5.
3157 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3158 * and process it as specified in RFC 7748 &sect;5.
3159 *
3160 * - For key types for which the key is represented by a single sequence of
3161 * \p bits bits with constraints as to which bit sequences are acceptable,
3162 * this function draws a byte string of length (\p bits / 8) bytes rounded
3163 * up to the nearest whole number of bytes. If the resulting byte string
3164 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3165 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003166 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003167 * for the output produced by psa_export_key().
3168 * The following key types defined in this specification follow this scheme:
3169 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003170 * - #PSA_KEY_TYPE_DES.
3171 * Force-set the parity bits, but discard forbidden weak keys.
3172 * For 2-key and 3-key triple-DES, the three keys are generated
3173 * successively (for example, for 3-key triple-DES,
3174 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3175 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003176 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003177 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003178 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003179 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003180 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003181 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003182 * Weierstrass curve).
3183 * For these key types, interpret the byte string as integer
3184 * in big-endian order. Discard it if it is not in the range
3185 * [0, *N* - 2] where *N* is the boundary of the private key domain
3186 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003187 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003188 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003189 * This method allows compliance to NIST standards, specifically
3190 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003191 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3192 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3193 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3194 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003195 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003196 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003197 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003198 * implementation-defined.
3199 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003200 * In all cases, the data that is read is discarded from the operation.
3201 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003202 *
Gilles Peskine20628592019-04-19 19:29:50 +02003203 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003204 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003205 * \param[out] handle On success, a handle to the newly created key.
3206 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003207 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003208 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003209 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003210 * If the key is persistent, the key material and the key's metadata
3211 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003212 * \retval #PSA_ERROR_ALREADY_EXISTS
3213 * This is an attempt to create a persistent key, and there is
3214 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003215 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003216 * There was not enough data to create the desired key.
3217 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003218 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003219 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003220 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003221 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003222 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003223 * \retval #PSA_ERROR_INVALID_ARGUMENT
3224 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003225 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003226 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3227 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3228 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3229 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003230 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003231 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003232 * The library has not been previously initialized by psa_crypto_init().
3233 * It is implementation-dependent whether a failure to initialize
3234 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003235 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003236psa_status_t psa_key_derivation_output_key(
3237 const psa_key_attributes_t *attributes,
3238 psa_key_derivation_operation_t *operation,
3239 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003240
Gilles Peskine35675b62019-05-16 17:26:11 +02003241/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003242 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003243 * Once a key derivation operation has been aborted, its capacity is zero.
3244 * Aborting an operation frees all associated resources except for the
3245 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003246 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003247 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003248 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003249 * psa_key_derivation_operation_init() or a zero value. In particular,
3250 * it is valid to call psa_key_derivation_abort() twice, or to call
3251 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003252 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003253 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003254 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003255 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003256 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003257 * \retval #PSA_SUCCESS
3258 * \retval #PSA_ERROR_BAD_STATE
3259 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3260 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003261 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003262 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003263psa_status_t psa_key_derivation_abort(
3264 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003265
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003266/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003267 *
3268 * \warning The raw result of a key agreement algorithm such as finite-field
3269 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3270 * not be used directly as key material. It should instead be passed as
3271 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003272 * a key derivation, use psa_key_derivation_key_agreement() and other
3273 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003274 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003275 * \param alg The key agreement algorithm to compute
3276 * (\c PSA_ALG_XXX value such that
3277 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3278 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003279 * \param private_key Handle to the private key to use.
3280 * \param[in] peer_key Public key of the peer. It must be
3281 * in the same format that psa_import_key()
3282 * accepts. The standard formats for public
3283 * keys are documented in the documentation
3284 * of psa_export_public_key().
3285 * \param peer_key_length Size of \p peer_key in bytes.
3286 * \param[out] output Buffer where the decrypted message is to
3287 * be written.
3288 * \param output_size Size of the \c output buffer in bytes.
3289 * \param[out] output_length On success, the number of bytes
3290 * that make up the returned output.
3291 *
3292 * \retval #PSA_SUCCESS
3293 * Success.
3294 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003295 * \retval #PSA_ERROR_NOT_PERMITTED
3296 * \retval #PSA_ERROR_INVALID_ARGUMENT
3297 * \p alg is not a key agreement algorithm
3298 * \retval #PSA_ERROR_INVALID_ARGUMENT
3299 * \p private_key is not compatible with \p alg,
3300 * or \p peer_key is not valid for \p alg or not compatible with
3301 * \p private_key.
3302 * \retval #PSA_ERROR_NOT_SUPPORTED
3303 * \p alg is not a supported key agreement algorithm.
3304 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3305 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3306 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003307 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine769c7a62019-01-18 16:42:29 +01003308 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003309psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3310 psa_key_handle_t private_key,
3311 const uint8_t *peer_key,
3312 size_t peer_key_length,
3313 uint8_t *output,
3314 size_t output_size,
3315 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003316
Gilles Peskineea0fb492018-07-12 17:17:20 +02003317/**@}*/
3318
Gilles Peskineedd76872018-07-20 17:42:05 +02003319/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003320 * @{
3321 */
3322
3323/**
3324 * \brief Generate random bytes.
3325 *
3326 * \warning This function **can** fail! Callers MUST check the return status
3327 * and MUST NOT use the content of the output buffer if the return
3328 * status is not #PSA_SUCCESS.
3329 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003330 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003331 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003332 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003333 * \param output_size Number of bytes to generate and output.
3334 *
Gilles Peskine28538492018-07-11 17:34:00 +02003335 * \retval #PSA_SUCCESS
3336 * \retval #PSA_ERROR_NOT_SUPPORTED
3337 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3338 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3339 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003340 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003341 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003342 * The library has not been previously initialized by psa_crypto_init().
3343 * It is implementation-dependent whether a failure to initialize
3344 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003345 */
3346psa_status_t psa_generate_random(uint8_t *output,
3347 size_t output_size);
3348
3349/**
3350 * \brief Generate a key or key pair.
3351 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003352 * The key is generated randomly.
3353 * Its location, policy, type and size are taken from \p attributes.
3354 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003355 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003356 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003357 * the public exponent is 65537.
3358 * The modulus is a product of two probabilistic primes
3359 * between 2^{n-1} and 2^n where n is the bit size specified in the
3360 * attributes.
3361 *
Gilles Peskine20628592019-04-19 19:29:50 +02003362 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003363 * \param[out] handle On success, a handle to the newly created key.
3364 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003365 *
Gilles Peskine28538492018-07-11 17:34:00 +02003366 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003367 * Success.
3368 * If the key is persistent, the key material and the key's metadata
3369 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003370 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003371 * This is an attempt to create a persistent key, and there is
3372 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003373 * \retval #PSA_ERROR_NOT_SUPPORTED
3374 * \retval #PSA_ERROR_INVALID_ARGUMENT
3375 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3376 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3377 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3378 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003379 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003380 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003381 * The library has not been previously initialized by psa_crypto_init().
3382 * It is implementation-dependent whether a failure to initialize
3383 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003384 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003385psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003386 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003387
3388/**@}*/
3389
Gilles Peskinee59236f2018-01-27 23:32:46 +01003390#ifdef __cplusplus
3391}
3392#endif
3393
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003394/* The file "crypto_sizes.h" contains definitions for size calculation
3395 * macros whose definitions are implementation-specific. */
3396#include "crypto_sizes.h"
3397
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003398/* The file "crypto_struct.h" contains definitions for
3399 * implementation-specific structs that are declared above. */
3400#include "crypto_struct.h"
3401
3402/* The file "crypto_extra.h" contains vendor-specific definitions. This
3403 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003404#include "crypto_extra.h"
3405
3406#endif /* PSA_CRYPTO_H */