blob: f6211a80079200d6a6c7c3ac4aea7c4ed2dae98a [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
331 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200332psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
333 psa_key_attributes_t *attributes);
334
Gilles Peskine20628592019-04-19 19:29:50 +0200335/** Reset a key attribute structure to a freshly initialized state.
336 *
337 * You must initialize the attribute structure as described in the
338 * documentation of the type #psa_key_attributes_t before calling this
339 * function. Once the structure has been initialized, you may call this
340 * function at any time.
341 *
342 * This function frees any auxiliary resources that the structure
343 * may contain.
344 *
345 * \param[in,out] attributes The attribute structure to reset.
346 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200347void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200348
Gilles Peskine87a5e562019-04-17 12:28:25 +0200349/**@}*/
350
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100351/** \defgroup key_management Key management
352 * @{
353 */
354
Gilles Peskinef535eb22018-11-30 14:08:36 +0100355/** Open a handle to an existing persistent key.
356 *
Gilles Peskine4754cde2019-05-21 15:56:29 +0200357 * Open a handle to a persistent key. A key is persistent if it was created
358 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
359 * always has a nonzero key identifier, set with psa_set_key_id() when
360 * creating the key. Implementations may provide additional pre-provisioned
Andrew Thoelke203491c2019-08-21 17:55:30 +0100361 * keys that can be opened with psa_open_key(). Such keys have a key identifier
362 * in the vendor range, as documented in the description of #psa_key_id_t.
Gilles Peskine4754cde2019-05-21 15:56:29 +0200363 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100364 * The application must eventually close the handle with psa_close_key() or
365 * psa_destroy_key() to release associated resources. If the application dies
366 * without calling one of these functions, the implementation should perform
367 * the equivalent of a call to psa_close_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100368 *
Andrew Thoelke9741b112019-08-21 18:20:41 +0100369 * Some implementations permit an application to open the same key multiple
Andrew Thoelkede183412019-09-05 09:38:06 +0100370 * times. If this is successful, each call to psa_open_key() will return a
371 * different key handle.
372 *
373 * \note Applications that rely on opening a key multiple times will not be
374 * portable to implementations that only permit a single key handle to be
375 * opened. See also :ref:\`key-handles\`.
Andrew Thoelke9741b112019-08-21 18:20:41 +0100376 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100377 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100378 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100379 *
380 * \retval #PSA_SUCCESS
381 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100382 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100383 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100384 * The implementation does not have sufficient resources to open the
385 * key. This can be due to reaching an implementation limit on the
386 * number of open keys, the number of open key handles, or available
387 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200388 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100389 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100390 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100391 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100392 * \retval #PSA_ERROR_NOT_PERMITTED
393 * The specified key exists, but the application does not have the
394 * permission to access it. Note that this specification does not
395 * define any way to create such a key, but it may be possible
396 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200397 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
398 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100399 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200400psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100401 psa_key_handle_t *handle);
402
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100403
Gilles Peskinef535eb22018-11-30 14:08:36 +0100404/** Close a key handle.
405 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100406 * If the handle designates a volatile key, this will destroy the key material
407 * and free all associated resources, just like psa_destroy_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100408 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100409 * If this is the last open handle to a persistent key, then closing the handle
410 * will free all resources associated with the key in volatile memory. The key
411 * data in persistent storage is not affected and can be opened again later
412 * with a call to psa_open_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100413 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100414 * Closing the key handle makes the handle invalid, and the key handle
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100415 * must not be used again by the application.
Andrew Thoelke3daba812019-08-21 22:46:56 +0100416 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100417 * \note If the key handle was used to setup an active
418 * :ref:\`multipart operation <multipart-operations>\`, then closing the
419 * key handle can cause the multipart operation to fail. Applications should
420 * maintain the key handle until after the multipart operation has finished.
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100421 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100422 * \param handle The key handle to close.
423 *
424 * \retval #PSA_SUCCESS
425 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100426 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100427 */
428psa_status_t psa_close_key(psa_key_handle_t handle);
429
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100430/**@}*/
431
432/** \defgroup import_export Key import and export
433 * @{
434 */
435
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100436/**
437 * \brief Import a key in binary format.
438 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100439 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100440 * documentation of psa_export_public_key() for the format of public keys
441 * and to the documentation of psa_export_key() for the format for
442 * other key types.
443 *
444 * This specification supports a single format for each key type.
445 * Implementations may support other formats as long as the standard
446 * format is supported. Implementations that support other formats
447 * should ensure that the formats are clearly unambiguous so as to
448 * minimize the risk that an invalid input is accidentally interpreted
449 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100450 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100451
Gilles Peskine20628592019-04-19 19:29:50 +0200452 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200453 * The key size is always determined from the
454 * \p data buffer.
455 * If the key size in \p attributes is nonzero,
456 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200457 * \param[out] handle On success, a handle to the newly created key.
458 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100459 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200460 * buffer is interpreted according to the type declared
461 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200462 * All implementations must support at least the format
463 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100464 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200465 * the chosen type. Implementations may allow other
466 * formats, but should be conservative: implementations
467 * should err on the side of rejecting content if it
468 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200469 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100470 *
Gilles Peskine28538492018-07-11 17:34:00 +0200471 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100472 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100473 * If the key is persistent, the key material and the key's metadata
474 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200475 * \retval #PSA_ERROR_ALREADY_EXISTS
476 * This is an attempt to create a persistent key, and there is
477 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200478 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200479 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200480 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200481 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200482 * The key attributes, as a whole, are invalid.
483 * \retval #PSA_ERROR_INVALID_ARGUMENT
484 * The key data is not correctly formatted.
485 * \retval #PSA_ERROR_INVALID_ARGUMENT
486 * The size in \p attributes is nonzero and does not match the size
487 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200488 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
489 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
490 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100491 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200492 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200493 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300494 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300495 * The library has not been previously initialized by psa_crypto_init().
496 * It is implementation-dependent whether a failure to initialize
497 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100498 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200499psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100500 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200501 size_t data_length,
502 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100503
504/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100505 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200506 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100507 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200508 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100509 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200510 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100511 * This function also erases any metadata such as policies and frees
512 * resources associated with the key. To free all resources associated with
513 * the key, all handles to the key must be closed or destroyed.
Gilles Peskine154bd952018-04-19 08:38:16 +0200514 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100515 * Destroying the key makes the handle invalid, and the key handle
516 * must not be used again by the application. Using other open handles to the
517 * destroyed key in a cryptographic operation will result in an error.
Andrew Thoelke07f16b72019-08-21 22:48:47 +0100518 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100519 * If a key is currently in use in a multipart operation, then destroying the
520 * key will cause the multipart operation to fail.
Andrew Thoelke07f16b72019-08-21 22:48:47 +0100521 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100522 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100523 *
Gilles Peskine28538492018-07-11 17:34:00 +0200524 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100525 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200526 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100527 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200528 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100529 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200530 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200531 * There was an failure in communication with the cryptoprocessor.
532 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200533 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200534 * The storage is corrupted. Implementations shall make a best effort
535 * to erase key material even in this stage, however applications
536 * should be aware that it may be impossible to guarantee that the
537 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200538 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200539 * An unexpected condition which is not a storage corruption or
540 * a communication failure occurred. The cryptoprocessor may have
541 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300542 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300543 * The library has not been previously initialized by psa_crypto_init().
544 * It is implementation-dependent whether a failure to initialize
545 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100546 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100547psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548
549/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100550 * \brief Export a key in binary format.
551 *
552 * The output of this function can be passed to psa_import_key() to
553 * create an equivalent object.
554 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100555 * If the implementation of psa_import_key() supports other formats
556 * beyond the format specified here, the output from psa_export_key()
557 * must use the representation specified here, not the original
558 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100560 * For standard key types, the output format is as follows:
561 *
562 * - For symmetric keys (including MAC keys), the format is the
563 * raw bytes of the key.
564 * - For DES, the key data consists of 8 bytes. The parity bits must be
565 * correct.
566 * - For Triple-DES, the format is the concatenation of the
567 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200568 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200569 * is the non-encrypted DER encoding of the representation defined by
570 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
571 * ```
572 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200573 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200574 * modulus INTEGER, -- n
575 * publicExponent INTEGER, -- e
576 * privateExponent INTEGER, -- d
577 * prime1 INTEGER, -- p
578 * prime2 INTEGER, -- q
579 * exponent1 INTEGER, -- d mod (p-1)
580 * exponent2 INTEGER, -- d mod (q-1)
581 * coefficient INTEGER, -- (inverse of q) mod p
582 * }
583 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200584 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200585 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100586 * a representation of the private value as a `ceiling(m/8)`-byte string
587 * where `m` is the bit size associated with the curve, i.e. the bit size
588 * of the order of the curve's coordinate field. This byte string is
589 * in little-endian order for Montgomery curves (curve types
590 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
591 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
592 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100593 * This is the content of the `privateKey` field of the `ECPrivateKey`
594 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200595 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200596 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000597 * format is the representation of the private key `x` as a big-endian byte
598 * string. The length of the byte string is the private key size in bytes
599 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200600 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
601 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100602 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200603 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
604 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100605 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200606 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200607 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200608 * \param[out] data_length On success, the number of bytes
609 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100610 *
Gilles Peskine28538492018-07-11 17:34:00 +0200611 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100612 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200613 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200614 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200615 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100616 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200617 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
618 * The size of the \p data buffer is too small. You can determine a
619 * sufficient buffer size by calling
620 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
621 * where \c type is the key type
622 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200623 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
624 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200625 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300626 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300627 * The library has not been previously initialized by psa_crypto_init().
628 * It is implementation-dependent whether a failure to initialize
629 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100630 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100631psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100632 uint8_t *data,
633 size_t data_size,
634 size_t *data_length);
635
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100636/**
637 * \brief Export a public key or the public part of a key pair in binary format.
638 *
639 * The output of this function can be passed to psa_import_key() to
640 * create an object that is equivalent to the public key.
641 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000642 * This specification supports a single format for each key type.
643 * Implementations may support other formats as long as the standard
644 * format is supported. Implementations that support other formats
645 * should ensure that the formats are clearly unambiguous so as to
646 * minimize the risk that an invalid input is accidentally interpreted
647 * according to a different format.
648 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000649 * For standard key types, the output format is as follows:
650 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
651 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
652 * ```
653 * RSAPublicKey ::= SEQUENCE {
654 * modulus INTEGER, -- n
655 * publicExponent INTEGER } -- e
656 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000657 * - For elliptic curve public keys (key types for which
658 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
659 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
660 * Let `m` be the bit size associated with the curve, i.e. the bit size of
661 * `q` for a curve over `F_q`. The representation consists of:
662 * - The byte 0x04;
663 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
664 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200665 * - For Diffie-Hellman key exchange public keys (key types for which
666 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000667 * the format is the representation of the public key `y = g^x mod p` as a
668 * big-endian byte string. The length of the byte string is the length of the
669 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100670 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200671 * Exporting a public key object or the public part of a key pair is
672 * always permitted, regardless of the key's usage flags.
673 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100674 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200675 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200676 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200677 * \param[out] data_length On success, the number of bytes
678 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100679 *
Gilles Peskine28538492018-07-11 17:34:00 +0200680 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100681 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200682 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200683 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200684 * The key is neither a public key nor a key pair.
685 * \retval #PSA_ERROR_NOT_SUPPORTED
686 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
687 * The size of the \p data buffer is too small. You can determine a
688 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200689 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200690 * where \c type is the key type
691 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200692 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
693 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200694 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300695 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300696 * The library has not been previously initialized by psa_crypto_init().
697 * It is implementation-dependent whether a failure to initialize
698 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100699 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100700psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100701 uint8_t *data,
702 size_t data_size,
703 size_t *data_length);
704
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100705/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100706 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100707 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000708 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100709 * This function is primarily useful to copy a key from one location
710 * to another, since it populates a key using the material from
711 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200712 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100713 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100714 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100715 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200716 * The policy on the source key must have the usage flag
717 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200718 * This flag is sufficient to permit the copy if the key has the lifetime
719 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
720 * Some secure elements do not provide a way to copy a key without
721 * making it extractable from the secure element. If a key is located
722 * in such a secure element, then the key must have both usage flags
723 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
724 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200725 *
Gilles Peskine20628592019-04-19 19:29:50 +0200726 * The resulting key may only be used in a way that conforms to
727 * both the policy of the original key and the policy specified in
728 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100729 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200730 * usage flags on the source policy and the usage flags in \p attributes.
731 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100732 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200733 * - If either of the policies allows an algorithm and the other policy
734 * allows a wildcard-based algorithm policy that includes this algorithm,
735 * the resulting key allows the same algorithm.
736 * - If the policies do not allow any algorithm in common, this function
737 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200738 *
Gilles Peskine20628592019-04-19 19:29:50 +0200739 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100740 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200741 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100742 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200743 * \param[in] attributes The attributes for the new key.
744 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200745 * - The key type and size may be 0. If either is
746 * nonzero, it must match the corresponding
747 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200748 * - The key location (the lifetime and, for
749 * persistent keys, the key identifier) is
750 * used directly.
751 * - The policy constraints (usage flags and
752 * algorithm policy) are combined from
753 * the source key and \p attributes so that
754 * both sets of restrictions apply, as
755 * described in the documentation of this function.
756 * \param[out] target_handle On success, a handle to the newly created key.
757 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200758 *
759 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100760 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200761 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200762 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200763 * This is an attempt to create a persistent key, and there is
764 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200765 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200766 * The lifetime or identifier in \p attributes are invalid.
767 * \retval #PSA_ERROR_INVALID_ARGUMENT
768 * The policy constraints on the source and specified in
769 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200770 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200771 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200772 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100773 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200774 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
775 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100776 * The source key is not exportable and its lifetime does not
777 * allow copying it to the target's lifetime.
778 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
779 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200780 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
781 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200782 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100783 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100784psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200785 const psa_key_attributes_t *attributes,
786 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100787
788/**@}*/
789
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100790/** \defgroup hash Message digests
791 * @{
792 */
793
Gilles Peskine69647a42019-01-14 20:18:12 +0100794/** Calculate the hash (digest) of a message.
795 *
796 * \note To verify the hash of a message against an
797 * expected value, use psa_hash_compare() instead.
798 *
799 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
800 * such that #PSA_ALG_IS_HASH(\p alg) is true).
801 * \param[in] input Buffer containing the message to hash.
802 * \param input_length Size of the \p input buffer in bytes.
803 * \param[out] hash Buffer where the hash is to be written.
804 * \param hash_size Size of the \p hash buffer in bytes.
805 * \param[out] hash_length On success, the number of bytes
806 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100807 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100808 *
809 * \retval #PSA_SUCCESS
810 * Success.
811 * \retval #PSA_ERROR_NOT_SUPPORTED
812 * \p alg is not supported or is not a hash algorithm.
813 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
814 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
815 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200816 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100817 */
818psa_status_t psa_hash_compute(psa_algorithm_t alg,
819 const uint8_t *input,
820 size_t input_length,
821 uint8_t *hash,
822 size_t hash_size,
823 size_t *hash_length);
824
825/** Calculate the hash (digest) of a message and compare it with a
826 * reference value.
827 *
828 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
829 * such that #PSA_ALG_IS_HASH(\p alg) is true).
830 * \param[in] input Buffer containing the message to hash.
831 * \param input_length Size of the \p input buffer in bytes.
832 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100833 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100834 *
835 * \retval #PSA_SUCCESS
836 * The expected hash is identical to the actual hash of the input.
837 * \retval #PSA_ERROR_INVALID_SIGNATURE
838 * The hash of the message was calculated successfully, but it
839 * differs from the expected hash.
840 * \retval #PSA_ERROR_NOT_SUPPORTED
841 * \p alg is not supported or is not a hash algorithm.
842 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
843 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
844 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200845 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100846 */
847psa_status_t psa_hash_compare(psa_algorithm_t alg,
848 const uint8_t *input,
849 size_t input_length,
850 const uint8_t *hash,
851 const size_t hash_length);
852
Gilles Peskine308b91d2018-02-08 09:47:44 +0100853/** The type of the state data structure for multipart hash operations.
854 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000855 * Before calling any function on a hash operation object, the application must
856 * initialize it by any of the following means:
857 * - Set the structure to all-bits-zero, for example:
858 * \code
859 * psa_hash_operation_t operation;
860 * memset(&operation, 0, sizeof(operation));
861 * \endcode
862 * - Initialize the structure to logical zero values, for example:
863 * \code
864 * psa_hash_operation_t operation = {0};
865 * \endcode
866 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
867 * for example:
868 * \code
869 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
870 * \endcode
871 * - Assign the result of the function psa_hash_operation_init()
872 * to the structure, for example:
873 * \code
874 * psa_hash_operation_t operation;
875 * operation = psa_hash_operation_init();
876 * \endcode
877 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100878 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100879 * make any assumptions about the content of this structure except
880 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100881typedef struct psa_hash_operation_s psa_hash_operation_t;
882
Jaeden Amero6a25b412019-01-04 11:47:44 +0000883/** \def PSA_HASH_OPERATION_INIT
884 *
885 * This macro returns a suitable initializer for a hash operation object
886 * of type #psa_hash_operation_t.
887 */
888#ifdef __DOXYGEN_ONLY__
889/* This is an example definition for documentation purposes.
890 * Implementations should define a suitable value in `crypto_struct.h`.
891 */
892#define PSA_HASH_OPERATION_INIT {0}
893#endif
894
895/** Return an initial value for a hash operation object.
896 */
897static psa_hash_operation_t psa_hash_operation_init(void);
898
Gilles Peskinef45adda2019-01-14 18:29:18 +0100899/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100900 *
901 * The sequence of operations to calculate a hash (message digest)
902 * is as follows:
903 * -# Allocate an operation object which will be passed to all the functions
904 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000905 * -# Initialize the operation object with one of the methods described in the
906 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200907 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100908 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100909 * of the message each time. The hash that is calculated is the hash
910 * of the concatenation of these messages in order.
911 * -# To calculate the hash, call psa_hash_finish().
912 * To compare the hash with an expected value, call psa_hash_verify().
913 *
914 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000915 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100916 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200917 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100918 * eventually terminate the operation. The following events terminate an
919 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100920 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100921 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100922 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000923 * \param[in,out] operation The operation object to set up. It must have
924 * been initialized as per the documentation for
925 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200926 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
927 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100928 *
Gilles Peskine28538492018-07-11 17:34:00 +0200929 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100930 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200931 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200932 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100933 * \retval #PSA_ERROR_BAD_STATE
934 * The operation state is not valid (already set up and not
935 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200936 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
937 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
938 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200939 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100940 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200941psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100942 psa_algorithm_t alg);
943
Gilles Peskine308b91d2018-02-08 09:47:44 +0100944/** Add a message fragment to a multipart hash operation.
945 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200946 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100947 *
948 * If this function returns an error status, the operation becomes inactive.
949 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200950 * \param[in,out] operation Active hash operation.
951 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200952 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100953 *
Gilles Peskine28538492018-07-11 17:34:00 +0200954 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100955 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200956 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100957 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200958 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
959 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
960 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200961 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100962 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100963psa_status_t psa_hash_update(psa_hash_operation_t *operation,
964 const uint8_t *input,
965 size_t input_length);
966
Gilles Peskine308b91d2018-02-08 09:47:44 +0100967/** Finish the calculation of the hash of a message.
968 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200969 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100970 * This function calculates the hash of the message formed by concatenating
971 * the inputs passed to preceding calls to psa_hash_update().
972 *
973 * When this function returns, the operation becomes inactive.
974 *
975 * \warning Applications should not call this function if they expect
976 * a specific value for the hash. Call psa_hash_verify() instead.
977 * Beware that comparing integrity or authenticity data such as
978 * hash values with a function such as \c memcmp is risky
979 * because the time taken by the comparison may leak information
980 * about the hashed data which could allow an attacker to guess
981 * a valid hash and thereby bypass security controls.
982 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200983 * \param[in,out] operation Active hash operation.
984 * \param[out] hash Buffer where the hash is to be written.
985 * \param hash_size Size of the \p hash buffer in bytes.
986 * \param[out] hash_length On success, the number of bytes
987 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200988 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200989 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990 *
Gilles Peskine28538492018-07-11 17:34:00 +0200991 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100992 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200993 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100994 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200995 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200996 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200997 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100998 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200999 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1000 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1001 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001002 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001003 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001004psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1005 uint8_t *hash,
1006 size_t hash_size,
1007 size_t *hash_length);
1008
Gilles Peskine308b91d2018-02-08 09:47:44 +01001009/** Finish the calculation of the hash of a message and compare it with
1010 * an expected value.
1011 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001012 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001013 * This function calculates the hash of the message formed by concatenating
1014 * the inputs passed to preceding calls to psa_hash_update(). It then
1015 * compares the calculated hash with the expected hash passed as a
1016 * parameter to this function.
1017 *
1018 * When this function returns, the operation becomes inactive.
1019 *
Gilles Peskine19067982018-03-20 17:54:53 +01001020 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001021 * comparison between the actual hash and the expected hash is performed
1022 * in constant time.
1023 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001024 * \param[in,out] operation Active hash operation.
1025 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001026 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001027 *
Gilles Peskine28538492018-07-11 17:34:00 +02001028 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001029 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001030 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001031 * The hash of the message was calculated successfully, but it
1032 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001033 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001034 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001035 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1036 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1037 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001038 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001039 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001040psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1041 const uint8_t *hash,
1042 size_t hash_length);
1043
Gilles Peskine308b91d2018-02-08 09:47:44 +01001044/** Abort a hash operation.
1045 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001046 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001047 * \p operation structure itself. Once aborted, the operation object
1048 * can be reused for another operation by calling
1049 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001050 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001051 * You may call this function any time after the operation object has
1052 * been initialized by any of the following methods:
1053 * - A call to psa_hash_setup(), whether it succeeds or not.
1054 * - Initializing the \c struct to all-bits-zero.
1055 * - Initializing the \c struct to logical zeros, e.g.
1056 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001057 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001058 * In particular, calling psa_hash_abort() after the operation has been
1059 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1060 * psa_hash_verify() is safe and has no effect.
1061 *
1062 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001063 *
Gilles Peskine28538492018-07-11 17:34:00 +02001064 * \retval #PSA_SUCCESS
1065 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001066 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001067 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1068 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001069 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001070 */
1071psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001072
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001073/** Clone a hash operation.
1074 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001075 * This function copies the state of an ongoing hash operation to
1076 * a new operation object. In other words, this function is equivalent
1077 * to calling psa_hash_setup() on \p target_operation with the same
1078 * algorithm that \p source_operation was set up for, then
1079 * psa_hash_update() on \p target_operation with the same input that
1080 * that was passed to \p source_operation. After this function returns, the
1081 * two objects are independent, i.e. subsequent calls involving one of
1082 * the objects do not affect the other object.
1083 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001084 * \param[in] source_operation The active hash operation to clone.
1085 * \param[in,out] target_operation The operation object to set up.
1086 * It must be initialized but not active.
1087 *
1088 * \retval #PSA_SUCCESS
1089 * \retval #PSA_ERROR_BAD_STATE
1090 * \p source_operation is not an active hash operation.
1091 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001092 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001093 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1094 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001095 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001096 */
1097psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1098 psa_hash_operation_t *target_operation);
1099
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001100/**@}*/
1101
Gilles Peskine8c9def32018-02-08 10:02:12 +01001102/** \defgroup MAC Message authentication codes
1103 * @{
1104 */
1105
Gilles Peskine69647a42019-01-14 20:18:12 +01001106/** Calculate the MAC (message authentication code) of a message.
1107 *
1108 * \note To verify the MAC of a message against an
1109 * expected value, use psa_mac_verify() instead.
1110 * Beware that comparing integrity or authenticity data such as
1111 * MAC values with a function such as \c memcmp is risky
1112 * because the time taken by the comparison may leak information
1113 * about the MAC value which could allow an attacker to guess
1114 * a valid MAC and thereby bypass security controls.
1115 *
1116 * \param handle Handle to the key to use for the operation.
1117 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001118 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001119 * \param[in] input Buffer containing the input message.
1120 * \param input_length Size of the \p input buffer in bytes.
1121 * \param[out] mac Buffer where the MAC value is to be written.
1122 * \param mac_size Size of the \p mac buffer in bytes.
1123 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001124 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001125 *
1126 * \retval #PSA_SUCCESS
1127 * Success.
1128 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001129 * \retval #PSA_ERROR_NOT_PERMITTED
1130 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001131 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001132 * \retval #PSA_ERROR_NOT_SUPPORTED
1133 * \p alg is not supported or is not a MAC algorithm.
1134 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1135 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1136 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001137 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001138 * \retval #PSA_ERROR_BAD_STATE
1139 * The library has not been previously initialized by psa_crypto_init().
1140 * It is implementation-dependent whether a failure to initialize
1141 * results in this error code.
1142 */
1143psa_status_t psa_mac_compute(psa_key_handle_t handle,
1144 psa_algorithm_t alg,
1145 const uint8_t *input,
1146 size_t input_length,
1147 uint8_t *mac,
1148 size_t mac_size,
1149 size_t *mac_length);
1150
1151/** Calculate the MAC of a message and compare it with a reference value.
1152 *
1153 * \param handle Handle to the key to use for the operation.
1154 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001155 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001156 * \param[in] input Buffer containing the input message.
1157 * \param input_length Size of the \p input buffer in bytes.
1158 * \param[out] mac Buffer containing the expected MAC value.
1159 * \param mac_length Size of the \p mac buffer in bytes.
1160 *
1161 * \retval #PSA_SUCCESS
1162 * The expected MAC is identical to the actual MAC of the input.
1163 * \retval #PSA_ERROR_INVALID_SIGNATURE
1164 * The MAC of the message was calculated successfully, but it
1165 * differs from the expected value.
1166 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001167 * \retval #PSA_ERROR_NOT_PERMITTED
1168 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001169 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001170 * \retval #PSA_ERROR_NOT_SUPPORTED
1171 * \p alg is not supported or is not a MAC algorithm.
1172 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1173 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1174 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001175 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001176 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001177psa_status_t psa_mac_verify(psa_key_handle_t handle,
1178 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001179 const uint8_t *input,
1180 size_t input_length,
1181 const uint8_t *mac,
1182 const size_t mac_length);
1183
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001184/** The type of the state data structure for multipart MAC operations.
1185 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001186 * Before calling any function on a MAC operation object, the application must
1187 * initialize it by any of the following means:
1188 * - Set the structure to all-bits-zero, for example:
1189 * \code
1190 * psa_mac_operation_t operation;
1191 * memset(&operation, 0, sizeof(operation));
1192 * \endcode
1193 * - Initialize the structure to logical zero values, for example:
1194 * \code
1195 * psa_mac_operation_t operation = {0};
1196 * \endcode
1197 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1198 * for example:
1199 * \code
1200 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1201 * \endcode
1202 * - Assign the result of the function psa_mac_operation_init()
1203 * to the structure, for example:
1204 * \code
1205 * psa_mac_operation_t operation;
1206 * operation = psa_mac_operation_init();
1207 * \endcode
1208 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001209 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001210 * make any assumptions about the content of this structure except
1211 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001212typedef struct psa_mac_operation_s psa_mac_operation_t;
1213
Jaeden Amero769ce272019-01-04 11:48:03 +00001214/** \def PSA_MAC_OPERATION_INIT
1215 *
1216 * This macro returns a suitable initializer for a MAC operation object of type
1217 * #psa_mac_operation_t.
1218 */
1219#ifdef __DOXYGEN_ONLY__
1220/* This is an example definition for documentation purposes.
1221 * Implementations should define a suitable value in `crypto_struct.h`.
1222 */
1223#define PSA_MAC_OPERATION_INIT {0}
1224#endif
1225
1226/** Return an initial value for a MAC operation object.
1227 */
1228static psa_mac_operation_t psa_mac_operation_init(void);
1229
Gilles Peskinef45adda2019-01-14 18:29:18 +01001230/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001231 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001232 * This function sets up the calculation of the MAC
1233 * (message authentication code) of a byte string.
1234 * To verify the MAC of a message against an
1235 * expected value, use psa_mac_verify_setup() instead.
1236 *
1237 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001238 * -# Allocate an operation object which will be passed to all the functions
1239 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001240 * -# Initialize the operation object with one of the methods described in the
1241 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001242 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001243 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1244 * of the message each time. The MAC that is calculated is the MAC
1245 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001246 * -# At the end of the message, call psa_mac_sign_finish() to finish
1247 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001248 *
1249 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001250 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001251 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001252 * After a successful call to psa_mac_sign_setup(), the application must
1253 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001254 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001255 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001256 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001257 * \param[in,out] operation The operation object to set up. It must have
1258 * been initialized as per the documentation for
1259 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001260 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001261 * It must remain valid until the operation
1262 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001263 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001264 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001265 *
Gilles Peskine28538492018-07-11 17:34:00 +02001266 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001267 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001268 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001269 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001270 * \retval #PSA_ERROR_NOT_PERMITTED
1271 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001272 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001273 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001274 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001275 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1276 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1277 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001278 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001279 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001280 * The operation state is not valid (already set up and not
1281 * subsequently completed).
1282 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001283 * The library has not been previously initialized by psa_crypto_init().
1284 * It is implementation-dependent whether a failure to initialize
1285 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001286 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001287psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001288 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001289 psa_algorithm_t alg);
1290
Gilles Peskinef45adda2019-01-14 18:29:18 +01001291/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001292 *
1293 * This function sets up the verification of the MAC
1294 * (message authentication code) of a byte string against an expected value.
1295 *
1296 * The sequence of operations to verify a MAC is as follows:
1297 * -# Allocate an operation object which will be passed to all the functions
1298 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001299 * -# Initialize the operation object with one of the methods described in the
1300 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001301 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001302 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1303 * of the message each time. The MAC that is calculated is the MAC
1304 * of the concatenation of these messages in order.
1305 * -# At the end of the message, call psa_mac_verify_finish() to finish
1306 * calculating the actual MAC of the message and verify it against
1307 * the expected value.
1308 *
1309 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001310 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001311 *
1312 * After a successful call to psa_mac_verify_setup(), the application must
1313 * eventually terminate the operation through one of the following methods:
1314 * - A failed call to psa_mac_update().
1315 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1316 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001317 * \param[in,out] operation The operation object to set up. It must have
1318 * been initialized as per the documentation for
1319 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001320 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001321 * It must remain valid until the operation
1322 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001323 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1324 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001325 *
Gilles Peskine28538492018-07-11 17:34:00 +02001326 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001327 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001328 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001329 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001330 * \retval #PSA_ERROR_NOT_PERMITTED
1331 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001332 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001333 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001334 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001335 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1336 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1337 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001338 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001339 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001340 * The operation state is not valid (already set up and not
1341 * subsequently completed).
1342 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001343 * The library has not been previously initialized by psa_crypto_init().
1344 * It is implementation-dependent whether a failure to initialize
1345 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001346 */
1347psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001348 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001349 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001350
Gilles Peskinedcd14942018-07-12 00:30:52 +02001351/** Add a message fragment to a multipart MAC operation.
1352 *
1353 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1354 * before calling this function.
1355 *
1356 * If this function returns an error status, the operation becomes inactive.
1357 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001358 * \param[in,out] operation Active MAC operation.
1359 * \param[in] input Buffer containing the message fragment to add to
1360 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001361 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001362 *
1363 * \retval #PSA_SUCCESS
1364 * Success.
1365 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001366 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001367 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1368 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1369 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001370 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001371 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001372psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1373 const uint8_t *input,
1374 size_t input_length);
1375
Gilles Peskinedcd14942018-07-12 00:30:52 +02001376/** Finish the calculation of the MAC of a message.
1377 *
1378 * The application must call psa_mac_sign_setup() before calling this function.
1379 * This function calculates the MAC of the message formed by concatenating
1380 * the inputs passed to preceding calls to psa_mac_update().
1381 *
1382 * When this function returns, the operation becomes inactive.
1383 *
1384 * \warning Applications should not call this function if they expect
1385 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1386 * Beware that comparing integrity or authenticity data such as
1387 * MAC values with a function such as \c memcmp is risky
1388 * because the time taken by the comparison may leak information
1389 * about the MAC value which could allow an attacker to guess
1390 * a valid MAC and thereby bypass security controls.
1391 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001392 * \param[in,out] operation Active MAC operation.
1393 * \param[out] mac Buffer where the MAC value is to be written.
1394 * \param mac_size Size of the \p mac buffer in bytes.
1395 * \param[out] mac_length On success, the number of bytes
1396 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001397 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001398 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001399 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001400 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001401 *
1402 * \retval #PSA_SUCCESS
1403 * Success.
1404 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001405 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001406 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001407 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001408 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1409 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1410 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1411 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001412 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001413 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001414psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1415 uint8_t *mac,
1416 size_t mac_size,
1417 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001418
Gilles Peskinedcd14942018-07-12 00:30:52 +02001419/** Finish the calculation of the MAC of a message and compare it with
1420 * an expected value.
1421 *
1422 * The application must call psa_mac_verify_setup() before calling this function.
1423 * This function calculates the MAC of the message formed by concatenating
1424 * the inputs passed to preceding calls to psa_mac_update(). It then
1425 * compares the calculated MAC with the expected MAC passed as a
1426 * parameter to this function.
1427 *
1428 * When this function returns, the operation becomes inactive.
1429 *
1430 * \note Implementations shall make the best effort to ensure that the
1431 * comparison between the actual MAC and the expected MAC is performed
1432 * in constant time.
1433 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001434 * \param[in,out] operation Active MAC operation.
1435 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001436 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001437 *
1438 * \retval #PSA_SUCCESS
1439 * The expected MAC is identical to the actual MAC of the message.
1440 * \retval #PSA_ERROR_INVALID_SIGNATURE
1441 * The MAC of the message was calculated successfully, but it
1442 * differs from the expected MAC.
1443 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001444 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001445 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1446 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1447 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001448 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001449 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001450psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1451 const uint8_t *mac,
1452 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001453
Gilles Peskinedcd14942018-07-12 00:30:52 +02001454/** Abort a MAC operation.
1455 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001456 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001457 * \p operation structure itself. Once aborted, the operation object
1458 * can be reused for another operation by calling
1459 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001460 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001461 * You may call this function any time after the operation object has
1462 * been initialized by any of the following methods:
1463 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1464 * it succeeds or not.
1465 * - Initializing the \c struct to all-bits-zero.
1466 * - Initializing the \c struct to logical zeros, e.g.
1467 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001468 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001469 * In particular, calling psa_mac_abort() after the operation has been
1470 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1471 * psa_mac_verify_finish() is safe and has no effect.
1472 *
1473 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001474 *
1475 * \retval #PSA_SUCCESS
1476 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001477 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001478 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1479 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001480 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001481 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001482psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1483
1484/**@}*/
1485
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001486/** \defgroup cipher Symmetric ciphers
1487 * @{
1488 */
1489
Gilles Peskine69647a42019-01-14 20:18:12 +01001490/** Encrypt a message using a symmetric cipher.
1491 *
1492 * This function encrypts a message with a random IV (initialization
1493 * vector).
1494 *
1495 * \param handle Handle to the key to use for the operation.
1496 * It must remain valid until the operation
1497 * terminates.
1498 * \param alg The cipher algorithm to compute
1499 * (\c PSA_ALG_XXX value such that
1500 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1501 * \param[in] input Buffer containing the message to encrypt.
1502 * \param input_length Size of the \p input buffer in bytes.
1503 * \param[out] output Buffer where the output is to be written.
1504 * The output contains the IV followed by
1505 * the ciphertext proper.
1506 * \param output_size Size of the \p output buffer in bytes.
1507 * \param[out] output_length On success, the number of bytes
1508 * that make up the output.
1509 *
1510 * \retval #PSA_SUCCESS
1511 * Success.
1512 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001513 * \retval #PSA_ERROR_NOT_PERMITTED
1514 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001515 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001516 * \retval #PSA_ERROR_NOT_SUPPORTED
1517 * \p alg is not supported or is not a cipher algorithm.
1518 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1519 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1520 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1521 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001522 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001523 */
1524psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1525 psa_algorithm_t alg,
1526 const uint8_t *input,
1527 size_t input_length,
1528 uint8_t *output,
1529 size_t output_size,
1530 size_t *output_length);
1531
1532/** Decrypt a message using a symmetric cipher.
1533 *
1534 * This function decrypts a message encrypted with a symmetric cipher.
1535 *
1536 * \param handle Handle to the key to use for the operation.
1537 * It must remain valid until the operation
1538 * terminates.
1539 * \param alg The cipher algorithm to compute
1540 * (\c PSA_ALG_XXX value such that
1541 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1542 * \param[in] input Buffer containing the message to decrypt.
1543 * This consists of the IV followed by the
1544 * ciphertext proper.
1545 * \param input_length Size of the \p input buffer in bytes.
1546 * \param[out] output Buffer where the plaintext is to be written.
1547 * \param output_size Size of the \p output buffer in bytes.
1548 * \param[out] output_length On success, the number of bytes
1549 * that make up the output.
1550 *
1551 * \retval #PSA_SUCCESS
1552 * Success.
1553 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001554 * \retval #PSA_ERROR_NOT_PERMITTED
1555 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001556 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001557 * \retval #PSA_ERROR_NOT_SUPPORTED
1558 * \p alg is not supported or is not a cipher algorithm.
1559 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1560 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1561 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1562 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001563 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001564 */
1565psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1566 psa_algorithm_t alg,
1567 const uint8_t *input,
1568 size_t input_length,
1569 uint8_t *output,
1570 size_t output_size,
1571 size_t *output_length);
1572
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001573/** The type of the state data structure for multipart cipher operations.
1574 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001575 * Before calling any function on a cipher operation object, the application
1576 * must initialize it by any of the following means:
1577 * - Set the structure to all-bits-zero, for example:
1578 * \code
1579 * psa_cipher_operation_t operation;
1580 * memset(&operation, 0, sizeof(operation));
1581 * \endcode
1582 * - Initialize the structure to logical zero values, for example:
1583 * \code
1584 * psa_cipher_operation_t operation = {0};
1585 * \endcode
1586 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1587 * for example:
1588 * \code
1589 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1590 * \endcode
1591 * - Assign the result of the function psa_cipher_operation_init()
1592 * to the structure, for example:
1593 * \code
1594 * psa_cipher_operation_t operation;
1595 * operation = psa_cipher_operation_init();
1596 * \endcode
1597 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001598 * This is an implementation-defined \c struct. Applications should not
1599 * make any assumptions about the content of this structure except
1600 * as directed by the documentation of a specific implementation. */
1601typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1602
Jaeden Amero5bae2272019-01-04 11:48:27 +00001603/** \def PSA_CIPHER_OPERATION_INIT
1604 *
1605 * This macro returns a suitable initializer for a cipher operation object of
1606 * type #psa_cipher_operation_t.
1607 */
1608#ifdef __DOXYGEN_ONLY__
1609/* This is an example definition for documentation purposes.
1610 * Implementations should define a suitable value in `crypto_struct.h`.
1611 */
1612#define PSA_CIPHER_OPERATION_INIT {0}
1613#endif
1614
1615/** Return an initial value for a cipher operation object.
1616 */
1617static psa_cipher_operation_t psa_cipher_operation_init(void);
1618
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001619/** Set the key for a multipart symmetric encryption operation.
1620 *
1621 * The sequence of operations to encrypt a message with a symmetric cipher
1622 * is as follows:
1623 * -# Allocate an operation object which will be passed to all the functions
1624 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001625 * -# Initialize the operation object with one of the methods described in the
1626 * documentation for #psa_cipher_operation_t, e.g.
1627 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001628 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001629 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001630 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001631 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001632 * requires a specific IV value.
1633 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1634 * of the message each time.
1635 * -# Call psa_cipher_finish().
1636 *
1637 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001638 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001639 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001640 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001641 * eventually terminate the operation. The following events terminate an
1642 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001643 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001644 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001645 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001646 * \param[in,out] operation The operation object to set up. It must have
1647 * been initialized as per the documentation for
1648 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001649 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001650 * It must remain valid until the operation
1651 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001652 * \param alg The cipher algorithm to compute
1653 * (\c PSA_ALG_XXX value such that
1654 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001655 *
Gilles Peskine28538492018-07-11 17:34:00 +02001656 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001657 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001658 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001659 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001660 * \retval #PSA_ERROR_NOT_PERMITTED
1661 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001662 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001663 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001664 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001665 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1666 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1667 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001668 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001669 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001670 * The operation state is not valid (already set up and not
1671 * subsequently completed).
1672 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001673 * The library has not been previously initialized by psa_crypto_init().
1674 * It is implementation-dependent whether a failure to initialize
1675 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001676 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001677psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001678 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001679 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001680
1681/** Set the key for a multipart symmetric decryption operation.
1682 *
1683 * The sequence of operations to decrypt a message with a symmetric cipher
1684 * is as follows:
1685 * -# Allocate an operation object which will be passed to all the functions
1686 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001687 * -# Initialize the operation object with one of the methods described in the
1688 * documentation for #psa_cipher_operation_t, e.g.
1689 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001690 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001691 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001692 * decryption. If the IV is prepended to the ciphertext, you can call
1693 * psa_cipher_update() on a buffer containing the IV followed by the
1694 * beginning of the message.
1695 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1696 * of the message each time.
1697 * -# Call psa_cipher_finish().
1698 *
1699 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001700 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001701 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001702 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001703 * eventually terminate the operation. The following events terminate an
1704 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001705 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001706 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001707 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001708 * \param[in,out] operation The operation object to set up. It must have
1709 * been initialized as per the documentation for
1710 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001711 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001712 * It must remain valid until the operation
1713 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001714 * \param alg The cipher algorithm to compute
1715 * (\c PSA_ALG_XXX value such that
1716 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001717 *
Gilles Peskine28538492018-07-11 17:34:00 +02001718 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001719 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001720 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001721 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001722 * \retval #PSA_ERROR_NOT_PERMITTED
1723 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001724 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001725 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001726 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001727 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1728 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1729 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001730 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001731 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001732 * The operation state is not valid (already set up and not
1733 * subsequently completed).
1734 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001735 * The library has not been previously initialized by psa_crypto_init().
1736 * It is implementation-dependent whether a failure to initialize
1737 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001738 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001739psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001740 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001741 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001742
Gilles Peskinedcd14942018-07-12 00:30:52 +02001743/** Generate an IV for a symmetric encryption operation.
1744 *
1745 * This function generates a random IV (initialization vector), nonce
1746 * or initial counter value for the encryption operation as appropriate
1747 * for the chosen algorithm, key type and key size.
1748 *
1749 * The application must call psa_cipher_encrypt_setup() before
1750 * calling this function.
1751 *
1752 * If this function returns an error status, the operation becomes inactive.
1753 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001754 * \param[in,out] operation Active cipher operation.
1755 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001756 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001757 * \param[out] iv_length On success, the number of bytes of the
1758 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001759 *
1760 * \retval #PSA_SUCCESS
1761 * Success.
1762 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001763 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001764 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001765 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001766 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1767 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1768 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001769 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001770 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001771psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001772 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001773 size_t iv_size,
1774 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001775
Gilles Peskinedcd14942018-07-12 00:30:52 +02001776/** Set the IV for a symmetric encryption or decryption operation.
1777 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001778 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001779 * or initial counter value for the encryption or decryption operation.
1780 *
1781 * The application must call psa_cipher_encrypt_setup() before
1782 * calling this function.
1783 *
1784 * If this function returns an error status, the operation becomes inactive.
1785 *
1786 * \note When encrypting, applications should use psa_cipher_generate_iv()
1787 * instead of this function, unless implementing a protocol that requires
1788 * a non-random IV.
1789 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001790 * \param[in,out] operation Active cipher operation.
1791 * \param[in] iv Buffer containing the IV to use.
1792 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001793 *
1794 * \retval #PSA_SUCCESS
1795 * Success.
1796 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001797 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001798 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001799 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001800 * or the chosen algorithm does not use an IV.
1801 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1802 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1803 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001804 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001805 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001806psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001807 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001808 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001809
Gilles Peskinedcd14942018-07-12 00:30:52 +02001810/** Encrypt or decrypt a message fragment in an active cipher operation.
1811 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001812 * Before calling this function, you must:
1813 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1814 * The choice of setup function determines whether this function
1815 * encrypts or decrypts its input.
1816 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1817 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001818 *
1819 * If this function returns an error status, the operation becomes inactive.
1820 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001821 * \param[in,out] operation Active cipher operation.
1822 * \param[in] input Buffer containing the message fragment to
1823 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001824 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001825 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001826 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001827 * \param[out] output_length On success, the number of bytes
1828 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001829 *
1830 * \retval #PSA_SUCCESS
1831 * Success.
1832 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001833 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001834 * not set, or already completed).
1835 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1836 * The size of the \p output buffer is too small.
1837 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1838 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1839 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001840 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001841 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001842psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1843 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001844 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001845 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001846 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001847 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001848
Gilles Peskinedcd14942018-07-12 00:30:52 +02001849/** Finish encrypting or decrypting a message in a cipher operation.
1850 *
1851 * The application must call psa_cipher_encrypt_setup() or
1852 * psa_cipher_decrypt_setup() before calling this function. The choice
1853 * of setup function determines whether this function encrypts or
1854 * decrypts its input.
1855 *
1856 * This function finishes the encryption or decryption of the message
1857 * formed by concatenating the inputs passed to preceding calls to
1858 * psa_cipher_update().
1859 *
1860 * When this function returns, the operation becomes inactive.
1861 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001862 * \param[in,out] operation Active cipher operation.
1863 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001864 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001865 * \param[out] output_length On success, the number of bytes
1866 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001867 *
1868 * \retval #PSA_SUCCESS
1869 * Success.
1870 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001871 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001872 * not set, or already completed).
1873 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1874 * The size of the \p output buffer is too small.
1875 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1876 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1877 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001878 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001879 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001880psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001881 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001882 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001883 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001884
Gilles Peskinedcd14942018-07-12 00:30:52 +02001885/** Abort a cipher operation.
1886 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001887 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001888 * \p operation structure itself. Once aborted, the operation object
1889 * can be reused for another operation by calling
1890 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001891 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001892 * You may call this function any time after the operation object has
1893 * been initialized by any of the following methods:
1894 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1895 * whether it succeeds or not.
1896 * - Initializing the \c struct to all-bits-zero.
1897 * - Initializing the \c struct to logical zeros, e.g.
1898 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001899 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001900 * In particular, calling psa_cipher_abort() after the operation has been
1901 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1902 * is safe and has no effect.
1903 *
1904 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001905 *
1906 * \retval #PSA_SUCCESS
1907 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001908 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001909 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1910 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001911 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001912 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001913psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1914
1915/**@}*/
1916
Gilles Peskine3b555712018-03-03 21:27:57 +01001917/** \defgroup aead Authenticated encryption with associated data (AEAD)
1918 * @{
1919 */
1920
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001921/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001922 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001923 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001924 * \param alg The AEAD algorithm to compute
1925 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001926 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001927 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001928 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001929 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001930 * but not encrypted.
1931 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001932 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001933 * encrypted.
1934 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001935 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001936 * encrypted data. The additional data is not
1937 * part of this output. For algorithms where the
1938 * encrypted data and the authentication tag
1939 * are defined as separate outputs, the
1940 * authentication tag is appended to the
1941 * encrypted data.
1942 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1943 * This must be at least
1944 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1945 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001946 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001947 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001948 *
Gilles Peskine28538492018-07-11 17:34:00 +02001949 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001950 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001951 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001952 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001953 * \retval #PSA_ERROR_NOT_PERMITTED
1954 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001955 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001956 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001957 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001958 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1959 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1960 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001961 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001962 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001963 * The library has not been previously initialized by psa_crypto_init().
1964 * It is implementation-dependent whether a failure to initialize
1965 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001966 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001967psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001968 psa_algorithm_t alg,
1969 const uint8_t *nonce,
1970 size_t nonce_length,
1971 const uint8_t *additional_data,
1972 size_t additional_data_length,
1973 const uint8_t *plaintext,
1974 size_t plaintext_length,
1975 uint8_t *ciphertext,
1976 size_t ciphertext_size,
1977 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001978
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001979/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001980 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001981 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001982 * \param alg The AEAD algorithm to compute
1983 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001984 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001985 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001986 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001987 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001988 * but not encrypted.
1989 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001990 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001991 * encrypted. For algorithms where the
1992 * encrypted data and the authentication tag
1993 * are defined as separate inputs, the buffer
1994 * must contain the encrypted data followed
1995 * by the authentication tag.
1996 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001997 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001998 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1999 * This must be at least
2000 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2001 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002002 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002003 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002004 *
Gilles Peskine28538492018-07-11 17:34:00 +02002005 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002006 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002007 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002008 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002009 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002010 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002011 * \retval #PSA_ERROR_NOT_PERMITTED
2012 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002013 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002014 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002015 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002016 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2017 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2018 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002019 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002020 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002021 * The library has not been previously initialized by psa_crypto_init().
2022 * It is implementation-dependent whether a failure to initialize
2023 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002024 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002025psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002026 psa_algorithm_t alg,
2027 const uint8_t *nonce,
2028 size_t nonce_length,
2029 const uint8_t *additional_data,
2030 size_t additional_data_length,
2031 const uint8_t *ciphertext,
2032 size_t ciphertext_length,
2033 uint8_t *plaintext,
2034 size_t plaintext_size,
2035 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002036
Gilles Peskine30a9e412019-01-14 18:36:12 +01002037/** The type of the state data structure for multipart AEAD operations.
2038 *
2039 * Before calling any function on an AEAD operation object, the application
2040 * must initialize it by any of the following means:
2041 * - Set the structure to all-bits-zero, for example:
2042 * \code
2043 * psa_aead_operation_t operation;
2044 * memset(&operation, 0, sizeof(operation));
2045 * \endcode
2046 * - Initialize the structure to logical zero values, for example:
2047 * \code
2048 * psa_aead_operation_t operation = {0};
2049 * \endcode
2050 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2051 * for example:
2052 * \code
2053 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2054 * \endcode
2055 * - Assign the result of the function psa_aead_operation_init()
2056 * to the structure, for example:
2057 * \code
2058 * psa_aead_operation_t operation;
2059 * operation = psa_aead_operation_init();
2060 * \endcode
2061 *
2062 * This is an implementation-defined \c struct. Applications should not
2063 * make any assumptions about the content of this structure except
2064 * as directed by the documentation of a specific implementation. */
2065typedef struct psa_aead_operation_s psa_aead_operation_t;
2066
2067/** \def PSA_AEAD_OPERATION_INIT
2068 *
2069 * This macro returns a suitable initializer for an AEAD operation object of
2070 * type #psa_aead_operation_t.
2071 */
2072#ifdef __DOXYGEN_ONLY__
2073/* This is an example definition for documentation purposes.
2074 * Implementations should define a suitable value in `crypto_struct.h`.
2075 */
2076#define PSA_AEAD_OPERATION_INIT {0}
2077#endif
2078
2079/** Return an initial value for an AEAD operation object.
2080 */
2081static psa_aead_operation_t psa_aead_operation_init(void);
2082
2083/** Set the key for a multipart authenticated encryption operation.
2084 *
2085 * The sequence of operations to encrypt a message with authentication
2086 * is as follows:
2087 * -# Allocate an operation object which will be passed to all the functions
2088 * listed here.
2089 * -# Initialize the operation object with one of the methods described in the
2090 * documentation for #psa_aead_operation_t, e.g.
2091 * PSA_AEAD_OPERATION_INIT.
2092 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002093 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2094 * inputs to the subsequent calls to psa_aead_update_ad() and
2095 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2096 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002097 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2098 * generate or set the nonce. You should use
2099 * psa_aead_generate_nonce() unless the protocol you are implementing
2100 * requires a specific nonce value.
2101 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2102 * of the non-encrypted additional authenticated data each time.
2103 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002104 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002105 * -# Call psa_aead_finish().
2106 *
2107 * The application may call psa_aead_abort() at any time after the operation
2108 * has been initialized.
2109 *
2110 * After a successful call to psa_aead_encrypt_setup(), the application must
2111 * eventually terminate the operation. The following events terminate an
2112 * operation:
2113 * - A failed call to any of the \c psa_aead_xxx functions.
2114 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2115 *
2116 * \param[in,out] operation The operation object to set up. It must have
2117 * been initialized as per the documentation for
2118 * #psa_aead_operation_t and not yet in use.
2119 * \param handle Handle to the key to use for the operation.
2120 * It must remain valid until the operation
2121 * terminates.
2122 * \param alg The AEAD algorithm to compute
2123 * (\c PSA_ALG_XXX value such that
2124 * #PSA_ALG_IS_AEAD(\p alg) is true).
2125 *
2126 * \retval #PSA_SUCCESS
2127 * Success.
2128 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002129 * \retval #PSA_ERROR_NOT_PERMITTED
2130 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002131 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002132 * \retval #PSA_ERROR_NOT_SUPPORTED
2133 * \p alg is not supported or is not an AEAD algorithm.
2134 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2135 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2136 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002137 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002138 * \retval #PSA_ERROR_BAD_STATE
2139 * The library has not been previously initialized by psa_crypto_init().
2140 * It is implementation-dependent whether a failure to initialize
2141 * results in this error code.
2142 */
2143psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2144 psa_key_handle_t handle,
2145 psa_algorithm_t alg);
2146
2147/** Set the key for a multipart authenticated decryption operation.
2148 *
2149 * The sequence of operations to decrypt a message with authentication
2150 * is as follows:
2151 * -# Allocate an operation object which will be passed to all the functions
2152 * listed here.
2153 * -# Initialize the operation object with one of the methods described in the
2154 * documentation for #psa_aead_operation_t, e.g.
2155 * PSA_AEAD_OPERATION_INIT.
2156 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002157 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2158 * inputs to the subsequent calls to psa_aead_update_ad() and
2159 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2160 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002161 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2162 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2163 * of the non-encrypted additional authenticated data each time.
2164 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002165 * of the ciphertext to decrypt each time.
2166 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002167 *
2168 * The application may call psa_aead_abort() at any time after the operation
2169 * has been initialized.
2170 *
2171 * After a successful call to psa_aead_decrypt_setup(), the application must
2172 * eventually terminate the operation. The following events terminate an
2173 * operation:
2174 * - A failed call to any of the \c psa_aead_xxx functions.
2175 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2176 *
2177 * \param[in,out] operation The operation object to set up. It must have
2178 * been initialized as per the documentation for
2179 * #psa_aead_operation_t and not yet in use.
2180 * \param handle Handle to the key to use for the operation.
2181 * It must remain valid until the operation
2182 * terminates.
2183 * \param alg The AEAD algorithm to compute
2184 * (\c PSA_ALG_XXX value such that
2185 * #PSA_ALG_IS_AEAD(\p alg) is true).
2186 *
2187 * \retval #PSA_SUCCESS
2188 * Success.
2189 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002190 * \retval #PSA_ERROR_NOT_PERMITTED
2191 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002192 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002193 * \retval #PSA_ERROR_NOT_SUPPORTED
2194 * \p alg is not supported or is not an AEAD algorithm.
2195 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2196 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2197 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002198 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002199 * \retval #PSA_ERROR_BAD_STATE
2200 * The library has not been previously initialized by psa_crypto_init().
2201 * It is implementation-dependent whether a failure to initialize
2202 * results in this error code.
2203 */
2204psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2205 psa_key_handle_t handle,
2206 psa_algorithm_t alg);
2207
2208/** Generate a random nonce for an authenticated encryption operation.
2209 *
2210 * This function generates a random nonce for the authenticated encryption
2211 * operation with an appropriate size for the chosen algorithm, key type
2212 * and key size.
2213 *
2214 * The application must call psa_aead_encrypt_setup() before
2215 * calling this function.
2216 *
2217 * If this function returns an error status, the operation becomes inactive.
2218 *
2219 * \param[in,out] operation Active AEAD operation.
2220 * \param[out] nonce Buffer where the generated nonce is to be
2221 * written.
2222 * \param nonce_size Size of the \p nonce buffer in bytes.
2223 * \param[out] nonce_length On success, the number of bytes of the
2224 * generated nonce.
2225 *
2226 * \retval #PSA_SUCCESS
2227 * Success.
2228 * \retval #PSA_ERROR_BAD_STATE
2229 * The operation state is not valid (not set up, or nonce already set).
2230 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2231 * The size of the \p nonce buffer is too small.
2232 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2233 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2234 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002235 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002236 */
2237psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002238 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002239 size_t nonce_size,
2240 size_t *nonce_length);
2241
2242/** Set the nonce for an authenticated encryption or decryption operation.
2243 *
2244 * This function sets the nonce for the authenticated
2245 * encryption or decryption operation.
2246 *
2247 * The application must call psa_aead_encrypt_setup() before
2248 * calling this function.
2249 *
2250 * If this function returns an error status, the operation becomes inactive.
2251 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002252 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002253 * instead of this function, unless implementing a protocol that requires
2254 * a non-random IV.
2255 *
2256 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002257 * \param[in] nonce Buffer containing the nonce to use.
2258 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002259 *
2260 * \retval #PSA_SUCCESS
2261 * Success.
2262 * \retval #PSA_ERROR_BAD_STATE
2263 * The operation state is not valid (not set up, or nonce already set).
2264 * \retval #PSA_ERROR_INVALID_ARGUMENT
2265 * The size of \p nonce is not acceptable for the chosen algorithm.
2266 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2267 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2268 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002269 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002270 */
2271psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002272 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002273 size_t nonce_length);
2274
Gilles Peskinebc59c852019-01-17 15:26:08 +01002275/** Declare the lengths of the message and additional data for AEAD.
2276 *
2277 * The application must call this function before calling
2278 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2279 * the operation requires it. If the algorithm does not require it,
2280 * calling this function is optional, but if this function is called
2281 * then the implementation must enforce the lengths.
2282 *
2283 * You may call this function before or after setting the nonce with
2284 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2285 *
2286 * - For #PSA_ALG_CCM, calling this function is required.
2287 * - For the other AEAD algorithms defined in this specification, calling
2288 * this function is not required.
2289 * - For vendor-defined algorithm, refer to the vendor documentation.
2290 *
2291 * \param[in,out] operation Active AEAD operation.
2292 * \param ad_length Size of the non-encrypted additional
2293 * authenticated data in bytes.
2294 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2295 *
2296 * \retval #PSA_SUCCESS
2297 * Success.
2298 * \retval #PSA_ERROR_BAD_STATE
2299 * The operation state is not valid (not set up, already completed,
2300 * or psa_aead_update_ad() or psa_aead_update() already called).
2301 * \retval #PSA_ERROR_INVALID_ARGUMENT
2302 * At least one of the lengths is not acceptable for the chosen
2303 * algorithm.
2304 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2305 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2306 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002307 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinebc59c852019-01-17 15:26:08 +01002308 */
2309psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2310 size_t ad_length,
2311 size_t plaintext_length);
2312
Gilles Peskine30a9e412019-01-14 18:36:12 +01002313/** Pass additional data to an active AEAD operation.
2314 *
2315 * Additional data is authenticated, but not encrypted.
2316 *
2317 * You may call this function multiple times to pass successive fragments
2318 * of the additional data. You may not call this function after passing
2319 * data to encrypt or decrypt with psa_aead_update().
2320 *
2321 * Before calling this function, you must:
2322 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2323 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2324 *
2325 * If this function returns an error status, the operation becomes inactive.
2326 *
2327 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2328 * there is no guarantee that the input is valid. Therefore, until
2329 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2330 * treat the input as untrusted and prepare to undo any action that
2331 * depends on the input if psa_aead_verify() returns an error status.
2332 *
2333 * \param[in,out] operation Active AEAD operation.
2334 * \param[in] input Buffer containing the fragment of
2335 * additional data.
2336 * \param input_length Size of the \p input buffer in bytes.
2337 *
2338 * \retval #PSA_SUCCESS
2339 * Success.
2340 * \retval #PSA_ERROR_BAD_STATE
2341 * The operation state is not valid (not set up, nonce not set,
2342 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002343 * \retval #PSA_ERROR_INVALID_ARGUMENT
2344 * The total input length overflows the additional data length that
2345 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002346 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2347 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2348 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002349 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002350 */
2351psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2352 const uint8_t *input,
2353 size_t input_length);
2354
2355/** Encrypt or decrypt a message fragment in an active AEAD operation.
2356 *
2357 * Before calling this function, you must:
2358 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2359 * The choice of setup function determines whether this function
2360 * encrypts or decrypts its input.
2361 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2362 * 3. Call psa_aead_update_ad() to pass all the additional data.
2363 *
2364 * If this function returns an error status, the operation becomes inactive.
2365 *
2366 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2367 * there is no guarantee that the input is valid. Therefore, until
2368 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2369 * - Do not use the output in any way other than storing it in a
2370 * confidential location. If you take any action that depends
2371 * on the tentative decrypted data, this action will need to be
2372 * undone if the input turns out not to be valid. Furthermore,
2373 * if an adversary can observe that this action took place
2374 * (for example through timing), they may be able to use this
2375 * fact as an oracle to decrypt any message encrypted with the
2376 * same key.
2377 * - In particular, do not copy the output anywhere but to a
2378 * memory or storage space that you have exclusive access to.
2379 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002380 * This function does not require the input to be aligned to any
2381 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002382 * a whole block at a time, it must consume all the input provided, but
2383 * it may delay the end of the corresponding output until a subsequent
2384 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2385 * provides sufficient input. The amount of data that can be delayed
2386 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002387 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002388 * \param[in,out] operation Active AEAD operation.
2389 * \param[in] input Buffer containing the message fragment to
2390 * encrypt or decrypt.
2391 * \param input_length Size of the \p input buffer in bytes.
2392 * \param[out] output Buffer where the output is to be written.
2393 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002394 * This must be at least
2395 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2396 * \p input_length) where \c alg is the
2397 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002398 * \param[out] output_length On success, the number of bytes
2399 * that make up the returned output.
2400 *
2401 * \retval #PSA_SUCCESS
2402 * Success.
2403 * \retval #PSA_ERROR_BAD_STATE
2404 * The operation state is not valid (not set up, nonce not set
2405 * or already completed).
2406 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2407 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002408 * You can determine a sufficient buffer size by calling
2409 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2410 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002411 * \retval #PSA_ERROR_INVALID_ARGUMENT
2412 * The total length of input to psa_aead_update_ad() so far is
2413 * less than the additional data length that was previously
2414 * specified with psa_aead_set_lengths().
2415 * \retval #PSA_ERROR_INVALID_ARGUMENT
2416 * The total input length overflows the plaintext length that
2417 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002418 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2419 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2420 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002421 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002422 */
2423psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2424 const uint8_t *input,
2425 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002426 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002427 size_t output_size,
2428 size_t *output_length);
2429
2430/** Finish encrypting a message in an AEAD operation.
2431 *
2432 * The operation must have been set up with psa_aead_encrypt_setup().
2433 *
2434 * This function finishes the authentication of the additional data
2435 * formed by concatenating the inputs passed to preceding calls to
2436 * psa_aead_update_ad() with the plaintext formed by concatenating the
2437 * inputs passed to preceding calls to psa_aead_update().
2438 *
2439 * This function has two output buffers:
2440 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002441 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002442 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002443 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002444 * that the operation performs.
2445 *
2446 * When this function returns, the operation becomes inactive.
2447 *
2448 * \param[in,out] operation Active AEAD operation.
2449 * \param[out] ciphertext Buffer where the last part of the ciphertext
2450 * is to be written.
2451 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002452 * This must be at least
2453 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2454 * \c alg is the algorithm that is being
2455 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002456 * \param[out] ciphertext_length On success, the number of bytes of
2457 * returned ciphertext.
2458 * \param[out] tag Buffer where the authentication tag is
2459 * to be written.
2460 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002461 * This must be at least
2462 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2463 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002464 * \param[out] tag_length On success, the number of bytes
2465 * that make up the returned tag.
2466 *
2467 * \retval #PSA_SUCCESS
2468 * Success.
2469 * \retval #PSA_ERROR_BAD_STATE
2470 * The operation state is not valid (not set up, nonce not set,
2471 * decryption, or already completed).
2472 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002473 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002474 * You can determine a sufficient buffer size for \p ciphertext by
2475 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2476 * where \c alg is the algorithm that is being calculated.
2477 * You can determine a sufficient buffer size for \p tag by
2478 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002479 * \retval #PSA_ERROR_INVALID_ARGUMENT
2480 * The total length of input to psa_aead_update_ad() so far is
2481 * less than the additional data length that was previously
2482 * specified with psa_aead_set_lengths().
2483 * \retval #PSA_ERROR_INVALID_ARGUMENT
2484 * The total length of input to psa_aead_update() so far is
2485 * less than the plaintext length that was previously
2486 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002487 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2488 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2489 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002490 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002491 */
2492psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002493 uint8_t *ciphertext,
2494 size_t ciphertext_size,
2495 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002496 uint8_t *tag,
2497 size_t tag_size,
2498 size_t *tag_length);
2499
2500/** Finish authenticating and decrypting a message in an AEAD operation.
2501 *
2502 * The operation must have been set up with psa_aead_decrypt_setup().
2503 *
2504 * This function finishes the authentication of the additional data
2505 * formed by concatenating the inputs passed to preceding calls to
2506 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2507 * inputs passed to preceding calls to psa_aead_update().
2508 *
2509 * When this function returns, the operation becomes inactive.
2510 *
2511 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002512 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002513 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002514 * from previous calls to psa_aead_update()
2515 * that could not be processed until the end
2516 * of the input.
2517 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002518 * This must be at least
2519 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2520 * \c alg is the algorithm that is being
2521 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002522 * \param[out] plaintext_length On success, the number of bytes of
2523 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002524 * \param[in] tag Buffer containing the authentication tag.
2525 * \param tag_length Size of the \p tag buffer in bytes.
2526 *
2527 * \retval #PSA_SUCCESS
2528 * Success.
2529 * \retval #PSA_ERROR_BAD_STATE
2530 * The operation state is not valid (not set up, nonce not set,
2531 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002532 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2533 * The size of the \p plaintext buffer is too small.
2534 * You can determine a sufficient buffer size for \p plaintext by
2535 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2536 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002537 * \retval #PSA_ERROR_INVALID_ARGUMENT
2538 * The total length of input to psa_aead_update_ad() so far is
2539 * less than the additional data length that was previously
2540 * specified with psa_aead_set_lengths().
2541 * \retval #PSA_ERROR_INVALID_ARGUMENT
2542 * The total length of input to psa_aead_update() so far is
2543 * less than the plaintext length that was previously
2544 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002545 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2546 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2547 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002548 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002549 */
2550psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002551 uint8_t *plaintext,
2552 size_t plaintext_size,
2553 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002554 const uint8_t *tag,
2555 size_t tag_length);
2556
2557/** Abort an AEAD operation.
2558 *
2559 * Aborting an operation frees all associated resources except for the
2560 * \p operation structure itself. Once aborted, the operation object
2561 * can be reused for another operation by calling
2562 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2563 *
2564 * You may call this function any time after the operation object has
2565 * been initialized by any of the following methods:
2566 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2567 * whether it succeeds or not.
2568 * - Initializing the \c struct to all-bits-zero.
2569 * - Initializing the \c struct to logical zeros, e.g.
2570 * `psa_aead_operation_t operation = {0}`.
2571 *
2572 * In particular, calling psa_aead_abort() after the operation has been
2573 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2574 * is safe and has no effect.
2575 *
2576 * \param[in,out] operation Initialized AEAD operation.
2577 *
2578 * \retval #PSA_SUCCESS
2579 * \retval #PSA_ERROR_BAD_STATE
2580 * \p operation is not an active AEAD operation.
2581 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2582 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002583 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002584 */
2585psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2586
Gilles Peskine3b555712018-03-03 21:27:57 +01002587/**@}*/
2588
Gilles Peskine20035e32018-02-03 22:44:14 +01002589/** \defgroup asymmetric Asymmetric cryptography
2590 * @{
2591 */
2592
2593/**
2594 * \brief Sign a hash or short message with a private key.
2595 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002596 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002597 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002598 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2599 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2600 * to determine the hash algorithm to use.
2601 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002602 * \param handle Handle to the key to use for the operation.
2603 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002604 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002605 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002606 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002607 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002608 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002609 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002610 * \param[out] signature_length On success, the number of bytes
2611 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002612 *
Gilles Peskine28538492018-07-11 17:34:00 +02002613 * \retval #PSA_SUCCESS
2614 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002615 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002616 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002617 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002618 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002619 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002620 * \retval #PSA_ERROR_NOT_SUPPORTED
2621 * \retval #PSA_ERROR_INVALID_ARGUMENT
2622 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2623 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2624 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002625 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002626 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002627 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002628 * The library has not been previously initialized by psa_crypto_init().
2629 * It is implementation-dependent whether a failure to initialize
2630 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002631 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002632psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002633 psa_algorithm_t alg,
2634 const uint8_t *hash,
2635 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002636 uint8_t *signature,
2637 size_t signature_size,
2638 size_t *signature_length);
2639
2640/**
2641 * \brief Verify the signature a hash or short message using a public key.
2642 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002643 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002644 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002645 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2646 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2647 * to determine the hash algorithm to use.
2648 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002649 * \param handle Handle to the key to use for the operation.
2650 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002651 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002652 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002653 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002654 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002655 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002656 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002657 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002658 *
Gilles Peskine28538492018-07-11 17:34:00 +02002659 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002660 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002661 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002662 * The calculation was perfomed successfully, but the passed
2663 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002664 * \retval #PSA_ERROR_NOT_SUPPORTED
2665 * \retval #PSA_ERROR_INVALID_ARGUMENT
2666 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2667 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2668 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002669 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002670 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002671 * The library has not been previously initialized by psa_crypto_init().
2672 * It is implementation-dependent whether a failure to initialize
2673 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002674 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002675psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002676 psa_algorithm_t alg,
2677 const uint8_t *hash,
2678 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002679 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002680 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002681
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002682/**
2683 * \brief Encrypt a short message with a public key.
2684 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002685 * \param handle Handle to the key to use for the operation.
2686 * It must be a public key or an asymmetric
2687 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002688 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002689 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002690 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002691 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002692 * \param[in] salt A salt or label, if supported by the
2693 * encryption algorithm.
2694 * If the algorithm does not support a
2695 * salt, pass \c NULL.
2696 * If the algorithm supports an optional
2697 * salt and you do not want to pass a salt,
2698 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002699 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002700 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2701 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002702 * \param salt_length Size of the \p salt buffer in bytes.
2703 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002704 * \param[out] output Buffer where the encrypted message is to
2705 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002706 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002707 * \param[out] output_length On success, the number of bytes
2708 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002709 *
Gilles Peskine28538492018-07-11 17:34:00 +02002710 * \retval #PSA_SUCCESS
2711 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002712 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002713 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002714 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002715 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002716 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002717 * \retval #PSA_ERROR_NOT_SUPPORTED
2718 * \retval #PSA_ERROR_INVALID_ARGUMENT
2719 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2720 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2721 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002722 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002723 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002724 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002725 * The library has not been previously initialized by psa_crypto_init().
2726 * It is implementation-dependent whether a failure to initialize
2727 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002728 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002729psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002730 psa_algorithm_t alg,
2731 const uint8_t *input,
2732 size_t input_length,
2733 const uint8_t *salt,
2734 size_t salt_length,
2735 uint8_t *output,
2736 size_t output_size,
2737 size_t *output_length);
2738
2739/**
2740 * \brief Decrypt a short message with a private key.
2741 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002742 * \param handle Handle to the key to use for the operation.
2743 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002744 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002745 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002746 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002747 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002748 * \param[in] salt A salt or label, if supported by the
2749 * encryption algorithm.
2750 * If the algorithm does not support a
2751 * salt, pass \c NULL.
2752 * If the algorithm supports an optional
2753 * salt and you do not want to pass a salt,
2754 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002755 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002756 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2757 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002758 * \param salt_length Size of the \p salt buffer in bytes.
2759 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002760 * \param[out] output Buffer where the decrypted message is to
2761 * be written.
2762 * \param output_size Size of the \c output buffer in bytes.
2763 * \param[out] output_length On success, the number of bytes
2764 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002765 *
Gilles Peskine28538492018-07-11 17:34:00 +02002766 * \retval #PSA_SUCCESS
2767 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002768 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002769 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002770 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002771 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002772 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002773 * \retval #PSA_ERROR_NOT_SUPPORTED
2774 * \retval #PSA_ERROR_INVALID_ARGUMENT
2775 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2776 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2777 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002778 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002779 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2780 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002781 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002782 * The library has not been previously initialized by psa_crypto_init().
2783 * It is implementation-dependent whether a failure to initialize
2784 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002785 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002786psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002787 psa_algorithm_t alg,
2788 const uint8_t *input,
2789 size_t input_length,
2790 const uint8_t *salt,
2791 size_t salt_length,
2792 uint8_t *output,
2793 size_t output_size,
2794 size_t *output_length);
2795
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002796/**@}*/
2797
Gilles Peskine35675b62019-05-16 17:26:11 +02002798/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002799 * @{
2800 */
2801
Gilles Peskine35675b62019-05-16 17:26:11 +02002802/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002803 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002804 * Before calling any function on a key derivation operation object, the
2805 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002806 * - Set the structure to all-bits-zero, for example:
2807 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002808 * psa_key_derivation_operation_t operation;
2809 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002810 * \endcode
2811 * - Initialize the structure to logical zero values, for example:
2812 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002813 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002814 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002815 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002816 * for example:
2817 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002818 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002819 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002820 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002821 * to the structure, for example:
2822 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002823 * psa_key_derivation_operation_t operation;
2824 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002825 * \endcode
2826 *
2827 * This is an implementation-defined \c struct. Applications should not
2828 * make any assumptions about the content of this structure except
2829 * as directed by the documentation of a specific implementation.
2830 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002831typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002832
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002833/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002834 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002835 * This macro returns a suitable initializer for a key derivation operation
2836 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002837 */
2838#ifdef __DOXYGEN_ONLY__
2839/* This is an example definition for documentation purposes.
2840 * Implementations should define a suitable value in `crypto_struct.h`.
2841 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002842#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02002843#endif
2844
Gilles Peskine35675b62019-05-16 17:26:11 +02002845/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002846 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002847static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002848
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002849/** Set up a key derivation operation.
2850 *
2851 * A key derivation algorithm takes some inputs and uses them to generate
2852 * a byte stream in a deterministic way.
2853 * This byte stream can be used to produce keys and other
2854 * cryptographic material.
2855 *
2856 * To derive a key:
2857 * - Start with an initialized object of type #psa_key_derivation_operation_t.
2858 * - Call psa_key_derivation_setup() to select the algorithm.
2859 * - Provide the inputs for the key derivation by calling
2860 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2861 * as appropriate. Which inputs are needed, in what order, and whether
2862 * they may be keys and if so of what type depends on the algorithm.
2863 * - Optionally set the operation's maximum capacity with
2864 * psa_key_derivation_set_capacity(). You may do this before, in the middle
2865 * of or after providing inputs. For some algorithms, this step is mandatory
2866 * because the output depends on the maximum capacity.
2867 * - To derive a key, call psa_key_derivation_output_key().
2868 * To derive a byte string for a different purpose, call
2869 * - psa_key_derivation_output_bytes().
2870 * Successive calls to these functions use successive output bytes
2871 * calculated by the key derivation algorithm.
2872 * - Clean up the key derivation operation object with
2873 * psa_key_derivation_abort().
2874 *
2875 * \param[in,out] operation The key derivation operation object
2876 * to set up. It must
2877 * have been initialized but not set up yet.
2878 * \param alg The key derivation algorithm to compute
2879 * (\c PSA_ALG_XXX value such that
2880 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2881 *
2882 * \retval #PSA_SUCCESS
2883 * Success.
2884 * \retval #PSA_ERROR_INVALID_ARGUMENT
2885 * \c alg is not a key derivation algorithm.
2886 * \retval #PSA_ERROR_NOT_SUPPORTED
2887 * \c alg is not supported or is not a key derivation algorithm.
2888 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2889 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2890 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002891 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002892 * \retval #PSA_ERROR_BAD_STATE
2893 */
2894psa_status_t psa_key_derivation_setup(
2895 psa_key_derivation_operation_t *operation,
2896 psa_algorithm_t alg);
2897
Gilles Peskine35675b62019-05-16 17:26:11 +02002898/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002899 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002900 * The capacity of a key derivation is the maximum number of bytes that it can
2901 * return. When you get *N* bytes of output from a key derivation operation,
2902 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002903 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002904 * \param[in] operation The operation to query.
2905 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002906 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002907 * \retval #PSA_SUCCESS
2908 * \retval #PSA_ERROR_BAD_STATE
2909 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002910 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002911psa_status_t psa_key_derivation_get_capacity(
2912 const psa_key_derivation_operation_t *operation,
2913 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002914
Gilles Peskine35675b62019-05-16 17:26:11 +02002915/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002916 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002917 * The capacity of a key derivation operation is the maximum number of bytes
2918 * that the key derivation operation can return from this point onwards.
2919 *
2920 * \param[in,out] operation The key derivation operation object to modify.
2921 * \param capacity The new capacity of the operation.
2922 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002923 * current capacity.
2924 *
2925 * \retval #PSA_SUCCESS
2926 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02002927 * \p capacity is larger than the operation's current capacity.
2928 * In this case, the operation object remains valid and its capacity
2929 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002930 * \retval #PSA_ERROR_BAD_STATE
2931 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2932 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002933psa_status_t psa_key_derivation_set_capacity(
2934 psa_key_derivation_operation_t *operation,
2935 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002936
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002937/** Use the maximum possible capacity for a key derivation operation.
2938 *
2939 * Use this value as the capacity argument when setting up a key derivation
2940 * to indicate that the operation should have the maximum possible capacity.
2941 * The value of the maximum possible capacity depends on the key derivation
2942 * algorithm.
2943 */
2944#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
2945
2946/** Provide an input for key derivation or key agreement.
2947 *
2948 * Which inputs are required and in what order depends on the algorithm.
2949 * Refer to the documentation of each key derivation or key agreement
2950 * algorithm for information.
2951 *
2952 * This function passes direct inputs. Some inputs must be passed as keys
2953 * using psa_key_derivation_input_key() instead of this function. Refer to
2954 * the documentation of individual step types for information.
2955 *
2956 * \param[in,out] operation The key derivation operation object to use.
2957 * It must have been set up with
2958 * psa_key_derivation_setup() and must not
2959 * have produced any output yet.
2960 * \param step Which step the input data is for.
2961 * \param[in] data Input data to use.
2962 * \param data_length Size of the \p data buffer in bytes.
2963 *
2964 * \retval #PSA_SUCCESS
2965 * Success.
2966 * \retval #PSA_ERROR_INVALID_ARGUMENT
2967 * \c step is not compatible with the operation's algorithm.
2968 * \retval #PSA_ERROR_INVALID_ARGUMENT
2969 * \c step does not allow direct inputs.
2970 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2971 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2972 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002973 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002974 * \retval #PSA_ERROR_BAD_STATE
2975 * The value of \p step is not valid given the state of \p operation.
2976 * \retval #PSA_ERROR_BAD_STATE
2977 * The library has not been previously initialized by psa_crypto_init().
2978 * It is implementation-dependent whether a failure to initialize
2979 * results in this error code.
2980 */
2981psa_status_t psa_key_derivation_input_bytes(
2982 psa_key_derivation_operation_t *operation,
2983 psa_key_derivation_step_t step,
2984 const uint8_t *data,
2985 size_t data_length);
2986
2987/** Provide an input for key derivation in the form of a key.
2988 *
2989 * Which inputs are required and in what order depends on the algorithm.
2990 * Refer to the documentation of each key derivation or key agreement
2991 * algorithm for information.
2992 *
2993 * This function passes key inputs. Some inputs must be passed as keys
2994 * of the appropriate type using this function, while others must be
2995 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
2996 * the documentation of individual step types for information.
2997 *
2998 * \param[in,out] operation The key derivation operation object to use.
2999 * It must have been set up with
3000 * psa_key_derivation_setup() and must not
3001 * have produced any output yet.
3002 * \param step Which step the input data is for.
3003 * \param handle Handle to the key. It must have an
3004 * appropriate type for \p step and must
3005 * allow the usage #PSA_KEY_USAGE_DERIVE.
3006 *
3007 * \retval #PSA_SUCCESS
3008 * Success.
3009 * \retval #PSA_ERROR_INVALID_HANDLE
3010 * \retval #PSA_ERROR_DOES_NOT_EXIST
3011 * \retval #PSA_ERROR_NOT_PERMITTED
3012 * \retval #PSA_ERROR_INVALID_ARGUMENT
3013 * \c step is not compatible with the operation's algorithm.
3014 * \retval #PSA_ERROR_INVALID_ARGUMENT
3015 * \c step does not allow key inputs.
3016 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3017 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3018 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003019 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003020 * \retval #PSA_ERROR_BAD_STATE
3021 * The value of \p step is not valid given the state of \p operation.
3022 * \retval #PSA_ERROR_BAD_STATE
3023 * The library has not been previously initialized by psa_crypto_init().
3024 * It is implementation-dependent whether a failure to initialize
3025 * results in this error code.
3026 */
3027psa_status_t psa_key_derivation_input_key(
3028 psa_key_derivation_operation_t *operation,
3029 psa_key_derivation_step_t step,
3030 psa_key_handle_t handle);
3031
3032/** Perform a key agreement and use the shared secret as input to a key
3033 * derivation.
3034 *
3035 * A key agreement algorithm takes two inputs: a private key \p private_key
3036 * a public key \p peer_key.
3037 * The result of this function is passed as input to a key derivation.
3038 * The output of this key derivation can be extracted by reading from the
3039 * resulting operation to produce keys and other cryptographic material.
3040 *
3041 * \param[in,out] operation The key derivation operation object to use.
3042 * It must have been set up with
3043 * psa_key_derivation_setup() with a
3044 * key agreement and derivation algorithm
3045 * \c alg (\c PSA_ALG_XXX value such that
3046 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3047 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3048 * is false).
3049 * The operation must be ready for an
3050 * input of the type given by \p step.
3051 * \param step Which step the input data is for.
3052 * \param private_key Handle to the private key to use.
3053 * \param[in] peer_key Public key of the peer. The peer key must be in the
3054 * same format that psa_import_key() accepts for the
3055 * public key type corresponding to the type of
3056 * private_key. That is, this function performs the
3057 * equivalent of
3058 * #psa_import_key(...,
3059 * `peer_key`, `peer_key_length`) where
3060 * with key attributes indicating the public key
3061 * type corresponding to the type of `private_key`.
3062 * For example, for EC keys, this means that peer_key
3063 * is interpreted as a point on the curve that the
3064 * private key is on. The standard formats for public
3065 * keys are documented in the documentation of
3066 * psa_export_public_key().
3067 * \param peer_key_length Size of \p peer_key in bytes.
3068 *
3069 * \retval #PSA_SUCCESS
3070 * Success.
3071 * \retval #PSA_ERROR_INVALID_HANDLE
3072 * \retval #PSA_ERROR_DOES_NOT_EXIST
3073 * \retval #PSA_ERROR_NOT_PERMITTED
3074 * \retval #PSA_ERROR_INVALID_ARGUMENT
3075 * \c private_key is not compatible with \c alg,
3076 * or \p peer_key is not valid for \c alg or not compatible with
3077 * \c private_key.
3078 * \retval #PSA_ERROR_NOT_SUPPORTED
3079 * \c alg is not supported or is not a key derivation algorithm.
3080 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3081 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3082 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003083 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003084 */
3085psa_status_t psa_key_derivation_key_agreement(
3086 psa_key_derivation_operation_t *operation,
3087 psa_key_derivation_step_t step,
3088 psa_key_handle_t private_key,
3089 const uint8_t *peer_key,
3090 size_t peer_key_length);
3091
Gilles Peskine35675b62019-05-16 17:26:11 +02003092/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003093 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003094 * This function calculates output bytes from a key derivation algorithm and
3095 * return those bytes.
3096 * If you view the key derivation's output as a stream of bytes, this
3097 * function destructively reads the requested number of bytes from the
3098 * stream.
3099 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003100 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003101 * \param[in,out] operation The key derivation operation object to read from.
3102 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003103 * \param output_length Number of bytes to output.
3104 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003105 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003106 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003107 * The operation's capacity was less than
3108 * \p output_length bytes. Note that in this case,
3109 * no output is written to the output buffer.
3110 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003111 * subsequent calls to this function will not
3112 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003113 * \retval #PSA_ERROR_BAD_STATE
3114 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3115 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3116 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003117 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003118 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003119psa_status_t psa_key_derivation_output_bytes(
3120 psa_key_derivation_operation_t *operation,
3121 uint8_t *output,
3122 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003123
Gilles Peskine35675b62019-05-16 17:26:11 +02003124/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003125 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003126 * This function calculates output bytes from a key derivation algorithm
3127 * and uses those bytes to generate a key deterministically.
3128 * If you view the key derivation's output as a stream of bytes, this
3129 * function destructively reads as many bytes as required from the
3130 * stream.
3131 * The operation's capacity decreases by the number of bytes read.
3132 *
3133 * How much output is produced and consumed from the operation, and how
3134 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003135 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003136 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003137 * of a given size, this function is functionally equivalent to
3138 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003139 * and passing the resulting output to #psa_import_key.
3140 * However, this function has a security benefit:
3141 * if the implementation provides an isolation boundary then
3142 * the key material is not exposed outside the isolation boundary.
3143 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003144 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003145 * The following key types defined in this specification follow this scheme:
3146 *
3147 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003148 * - #PSA_KEY_TYPE_ARC4;
3149 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003150 * - #PSA_KEY_TYPE_DERIVE;
3151 * - #PSA_KEY_TYPE_HMAC.
3152 *
3153 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003154 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003155 * Montgomery curve), this function always draws a byte string whose
3156 * length is determined by the curve, and sets the mandatory bits
3157 * accordingly. That is:
3158 *
3159 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3160 * and process it as specified in RFC 7748 &sect;5.
3161 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3162 * and process it as specified in RFC 7748 &sect;5.
3163 *
3164 * - For key types for which the key is represented by a single sequence of
3165 * \p bits bits with constraints as to which bit sequences are acceptable,
3166 * this function draws a byte string of length (\p bits / 8) bytes rounded
3167 * up to the nearest whole number of bytes. If the resulting byte string
3168 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3169 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003170 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003171 * for the output produced by psa_export_key().
3172 * The following key types defined in this specification follow this scheme:
3173 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003174 * - #PSA_KEY_TYPE_DES.
3175 * Force-set the parity bits, but discard forbidden weak keys.
3176 * For 2-key and 3-key triple-DES, the three keys are generated
3177 * successively (for example, for 3-key triple-DES,
3178 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3179 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003180 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003181 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003182 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003183 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003184 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003185 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003186 * Weierstrass curve).
3187 * For these key types, interpret the byte string as integer
3188 * in big-endian order. Discard it if it is not in the range
3189 * [0, *N* - 2] where *N* is the boundary of the private key domain
3190 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003191 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003192 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003193 * This method allows compliance to NIST standards, specifically
3194 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003195 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3196 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3197 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3198 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003199 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003200 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003201 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003202 * implementation-defined.
3203 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003204 * In all cases, the data that is read is discarded from the operation.
3205 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003206 *
Gilles Peskine20628592019-04-19 19:29:50 +02003207 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003208 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003209 * \param[out] handle On success, a handle to the newly created key.
3210 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003211 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003212 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003213 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003214 * If the key is persistent, the key material and the key's metadata
3215 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003216 * \retval #PSA_ERROR_ALREADY_EXISTS
3217 * This is an attempt to create a persistent key, and there is
3218 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003219 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003220 * There was not enough data to create the desired key.
3221 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003222 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003223 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003224 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003225 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003226 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003227 * \retval #PSA_ERROR_INVALID_ARGUMENT
3228 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003229 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003230 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3231 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3232 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3233 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003234 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003235 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003236 * The library has not been previously initialized by psa_crypto_init().
3237 * It is implementation-dependent whether a failure to initialize
3238 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003239 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003240psa_status_t psa_key_derivation_output_key(
3241 const psa_key_attributes_t *attributes,
3242 psa_key_derivation_operation_t *operation,
3243 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003244
Gilles Peskine35675b62019-05-16 17:26:11 +02003245/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003246 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003247 * Once a key derivation operation has been aborted, its capacity is zero.
3248 * Aborting an operation frees all associated resources except for the
3249 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003250 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003251 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003252 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003253 * psa_key_derivation_operation_init() or a zero value. In particular,
3254 * it is valid to call psa_key_derivation_abort() twice, or to call
3255 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003256 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003257 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003258 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003259 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003260 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003261 * \retval #PSA_SUCCESS
3262 * \retval #PSA_ERROR_BAD_STATE
3263 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3264 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003265 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003266 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003267psa_status_t psa_key_derivation_abort(
3268 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003269
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003270/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003271 *
3272 * \warning The raw result of a key agreement algorithm such as finite-field
3273 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3274 * not be used directly as key material. It should instead be passed as
3275 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003276 * a key derivation, use psa_key_derivation_key_agreement() and other
3277 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003278 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003279 * \param alg The key agreement algorithm to compute
3280 * (\c PSA_ALG_XXX value such that
3281 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3282 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003283 * \param private_key Handle to the private key to use.
3284 * \param[in] peer_key Public key of the peer. It must be
3285 * in the same format that psa_import_key()
3286 * accepts. The standard formats for public
3287 * keys are documented in the documentation
3288 * of psa_export_public_key().
3289 * \param peer_key_length Size of \p peer_key in bytes.
3290 * \param[out] output Buffer where the decrypted message is to
3291 * be written.
3292 * \param output_size Size of the \c output buffer in bytes.
3293 * \param[out] output_length On success, the number of bytes
3294 * that make up the returned output.
3295 *
3296 * \retval #PSA_SUCCESS
3297 * Success.
3298 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003299 * \retval #PSA_ERROR_NOT_PERMITTED
3300 * \retval #PSA_ERROR_INVALID_ARGUMENT
3301 * \p alg is not a key agreement algorithm
3302 * \retval #PSA_ERROR_INVALID_ARGUMENT
3303 * \p private_key is not compatible with \p alg,
3304 * or \p peer_key is not valid for \p alg or not compatible with
3305 * \p private_key.
3306 * \retval #PSA_ERROR_NOT_SUPPORTED
3307 * \p alg is not a supported key agreement algorithm.
3308 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3309 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3310 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003311 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine769c7a62019-01-18 16:42:29 +01003312 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003313psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3314 psa_key_handle_t private_key,
3315 const uint8_t *peer_key,
3316 size_t peer_key_length,
3317 uint8_t *output,
3318 size_t output_size,
3319 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003320
Gilles Peskineea0fb492018-07-12 17:17:20 +02003321/**@}*/
3322
Gilles Peskineedd76872018-07-20 17:42:05 +02003323/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003324 * @{
3325 */
3326
3327/**
3328 * \brief Generate random bytes.
3329 *
3330 * \warning This function **can** fail! Callers MUST check the return status
3331 * and MUST NOT use the content of the output buffer if the return
3332 * status is not #PSA_SUCCESS.
3333 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003334 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003335 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003336 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003337 * \param output_size Number of bytes to generate and output.
3338 *
Gilles Peskine28538492018-07-11 17:34:00 +02003339 * \retval #PSA_SUCCESS
3340 * \retval #PSA_ERROR_NOT_SUPPORTED
3341 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3342 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3343 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003344 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003345 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003346 * The library has not been previously initialized by psa_crypto_init().
3347 * It is implementation-dependent whether a failure to initialize
3348 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003349 */
3350psa_status_t psa_generate_random(uint8_t *output,
3351 size_t output_size);
3352
3353/**
3354 * \brief Generate a key or key pair.
3355 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003356 * The key is generated randomly.
3357 * Its location, policy, type and size are taken from \p attributes.
3358 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003359 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003360 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003361 * the public exponent is 65537.
3362 * The modulus is a product of two probabilistic primes
3363 * between 2^{n-1} and 2^n where n is the bit size specified in the
3364 * attributes.
3365 *
Gilles Peskine20628592019-04-19 19:29:50 +02003366 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003367 * \param[out] handle On success, a handle to the newly created key.
3368 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003369 *
Gilles Peskine28538492018-07-11 17:34:00 +02003370 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003371 * Success.
3372 * If the key is persistent, the key material and the key's metadata
3373 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003374 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003375 * This is an attempt to create a persistent key, and there is
3376 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003377 * \retval #PSA_ERROR_NOT_SUPPORTED
3378 * \retval #PSA_ERROR_INVALID_ARGUMENT
3379 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3380 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3381 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3382 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003383 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003384 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003385 * The library has not been previously initialized by psa_crypto_init().
3386 * It is implementation-dependent whether a failure to initialize
3387 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003388 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003389psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003390 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003391
3392/**@}*/
3393
Gilles Peskinee59236f2018-01-27 23:32:46 +01003394#ifdef __cplusplus
3395}
3396#endif
3397
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003398/* The file "crypto_sizes.h" contains definitions for size calculation
3399 * macros whose definitions are implementation-specific. */
3400#include "crypto_sizes.h"
3401
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003402/* The file "crypto_struct.h" contains definitions for
3403 * implementation-specific structs that are declared above. */
3404#include "crypto_struct.h"
3405
3406/* The file "crypto_extra.h" contains vendor-specific definitions. This
3407 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003408#include "crypto_extra.h"
3409
3410#endif /* PSA_CRYPTO_H */