blob: eac0ff2bfcada3b9158a944d336a7010d22b92b1 [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 *
364 * The application must eventually close the handle with psa_close_key()
365 * to release associated resources. If the application dies without calling
366 * psa_close_key(), the implementation should perform the equivalent of a
367 * 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
370 * times. Applications that rely on this behavior will not be portable to
371 * implementations that only permit a single key handle to be opened. See
372 * also :ref:\`key-handles\`.
373 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100374 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100375 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100376 *
377 * \retval #PSA_SUCCESS
378 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100379 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100380 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100381 * The implementation does not have sufficient resources to open the
382 * key. This can be due to reaching an implementation limit on the
383 * number of open keys, the number of open key handles, or available
384 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200385 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100386 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100387 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100388 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100389 * \retval #PSA_ERROR_NOT_PERMITTED
390 * The specified key exists, but the application does not have the
391 * permission to access it. Note that this specification does not
392 * define any way to create such a key, but it may be possible
393 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200394 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
395 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100396 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200397psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100398 psa_key_handle_t *handle);
399
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100400
Gilles Peskinef535eb22018-11-30 14:08:36 +0100401/** Close a key handle.
402 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100403 * If the handle designates a volatile key, this will destroy the key material
404 * and free all associated resources, just like psa_destroy_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100405 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100406 * If this is the last open handle to a persistent key, then closing the handle
407 * will free all resources associated with the key in volatile memory. The key
408 * data in persistent storage is not affected and can be opened again later
409 * with a call to psa_open_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100410 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100411 * Closing the key handle makes the handle invalid, and the key handle
412 * must not be used again by the application..
413 *
414 * If the key is currently in use in a multipart operation, then closing the
415 * last handle to the key will abort the multipart operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100416 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100417 * \param handle The key handle to close.
418 *
419 * \retval #PSA_SUCCESS
420 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100421 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100422 */
423psa_status_t psa_close_key(psa_key_handle_t handle);
424
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100425/**@}*/
426
427/** \defgroup import_export Key import and export
428 * @{
429 */
430
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100431/**
432 * \brief Import a key in binary format.
433 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100434 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100435 * documentation of psa_export_public_key() for the format of public keys
436 * and to the documentation of psa_export_key() for the format for
437 * other key types.
438 *
439 * This specification supports a single format for each key type.
440 * Implementations may support other formats as long as the standard
441 * format is supported. Implementations that support other formats
442 * should ensure that the formats are clearly unambiguous so as to
443 * minimize the risk that an invalid input is accidentally interpreted
444 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100445 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100446
Gilles Peskine20628592019-04-19 19:29:50 +0200447 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200448 * The key size is always determined from the
449 * \p data buffer.
450 * If the key size in \p attributes is nonzero,
451 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200452 * \param[out] handle On success, a handle to the newly created key.
453 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100454 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200455 * buffer is interpreted according to the type declared
456 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200457 * All implementations must support at least the format
458 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100459 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200460 * the chosen type. Implementations may allow other
461 * formats, but should be conservative: implementations
462 * should err on the side of rejecting content if it
463 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200464 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100465 *
Gilles Peskine28538492018-07-11 17:34:00 +0200466 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100467 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100468 * If the key is persistent, the key material and the key's metadata
469 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200470 * \retval #PSA_ERROR_ALREADY_EXISTS
471 * This is an attempt to create a persistent key, and there is
472 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200473 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200474 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200475 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200476 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200477 * The key attributes, as a whole, are invalid.
478 * \retval #PSA_ERROR_INVALID_ARGUMENT
479 * The key data is not correctly formatted.
480 * \retval #PSA_ERROR_INVALID_ARGUMENT
481 * The size in \p attributes is nonzero and does not match the size
482 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200483 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
484 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
485 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100486 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200487 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200488 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300489 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300490 * The library has not been previously initialized by psa_crypto_init().
491 * It is implementation-dependent whether a failure to initialize
492 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100493 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200494psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100495 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200496 size_t data_length,
497 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100498
499/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100500 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200501 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100502 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200503 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100504 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200505 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100506 * This function also erases any metadata such as policies and frees all
507 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200508 *
Andrew Thoelke07f16b72019-08-21 22:48:47 +0100509 * Destroying a key will invalidate all existing handles to the key.
510 *
511 * If the key is currently in use in a multipart operation, then destroying the
512 * key will abort the multipart operation.
513 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100514 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515 *
Gilles Peskine28538492018-07-11 17:34:00 +0200516 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100517 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200518 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100519 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200520 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100521 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200522 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200523 * There was an failure in communication with the cryptoprocessor.
524 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200525 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200526 * The storage is corrupted. Implementations shall make a best effort
527 * to erase key material even in this stage, however applications
528 * should be aware that it may be impossible to guarantee that the
529 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200530 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200531 * An unexpected condition which is not a storage corruption or
532 * a communication failure occurred. The cryptoprocessor may have
533 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300534 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300535 * The library has not been previously initialized by psa_crypto_init().
536 * It is implementation-dependent whether a failure to initialize
537 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100538 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100539psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100540
541/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 * \brief Export a key in binary format.
543 *
544 * The output of this function can be passed to psa_import_key() to
545 * create an equivalent object.
546 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100547 * If the implementation of psa_import_key() supports other formats
548 * beyond the format specified here, the output from psa_export_key()
549 * must use the representation specified here, not the original
550 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100552 * For standard key types, the output format is as follows:
553 *
554 * - For symmetric keys (including MAC keys), the format is the
555 * raw bytes of the key.
556 * - For DES, the key data consists of 8 bytes. The parity bits must be
557 * correct.
558 * - For Triple-DES, the format is the concatenation of the
559 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200560 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200561 * is the non-encrypted DER encoding of the representation defined by
562 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
563 * ```
564 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200565 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200566 * modulus INTEGER, -- n
567 * publicExponent INTEGER, -- e
568 * privateExponent INTEGER, -- d
569 * prime1 INTEGER, -- p
570 * prime2 INTEGER, -- q
571 * exponent1 INTEGER, -- d mod (p-1)
572 * exponent2 INTEGER, -- d mod (q-1)
573 * coefficient INTEGER, -- (inverse of q) mod p
574 * }
575 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200576 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200577 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100578 * a representation of the private value as a `ceiling(m/8)`-byte string
579 * where `m` is the bit size associated with the curve, i.e. the bit size
580 * of the order of the curve's coordinate field. This byte string is
581 * in little-endian order for Montgomery curves (curve types
582 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
583 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
584 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100585 * This is the content of the `privateKey` field of the `ECPrivateKey`
586 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200587 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200588 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000589 * format is the representation of the private key `x` as a big-endian byte
590 * string. The length of the byte string is the private key size in bytes
591 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200592 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
593 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100594 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200595 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
596 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100597 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200598 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200599 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200600 * \param[out] data_length On success, the number of bytes
601 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100602 *
Gilles Peskine28538492018-07-11 17:34:00 +0200603 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100604 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200605 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200606 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200607 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100608 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200609 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
610 * The size of the \p data buffer is too small. You can determine a
611 * sufficient buffer size by calling
612 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
613 * where \c type is the key type
614 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200615 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
616 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200617 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300618 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300619 * The library has not been previously initialized by psa_crypto_init().
620 * It is implementation-dependent whether a failure to initialize
621 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100622 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100623psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624 uint8_t *data,
625 size_t data_size,
626 size_t *data_length);
627
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100628/**
629 * \brief Export a public key or the public part of a key pair in binary format.
630 *
631 * The output of this function can be passed to psa_import_key() to
632 * create an object that is equivalent to the public key.
633 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000634 * This specification supports a single format for each key type.
635 * Implementations may support other formats as long as the standard
636 * format is supported. Implementations that support other formats
637 * should ensure that the formats are clearly unambiguous so as to
638 * minimize the risk that an invalid input is accidentally interpreted
639 * according to a different format.
640 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000641 * For standard key types, the output format is as follows:
642 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
643 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
644 * ```
645 * RSAPublicKey ::= SEQUENCE {
646 * modulus INTEGER, -- n
647 * publicExponent INTEGER } -- e
648 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000649 * - For elliptic curve public keys (key types for which
650 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
651 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
652 * Let `m` be the bit size associated with the curve, i.e. the bit size of
653 * `q` for a curve over `F_q`. The representation consists of:
654 * - The byte 0x04;
655 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
656 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200657 * - For Diffie-Hellman key exchange public keys (key types for which
658 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000659 * the format is the representation of the public key `y = g^x mod p` as a
660 * big-endian byte string. The length of the byte string is the length of the
661 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100662 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200663 * Exporting a public key object or the public part of a key pair is
664 * always permitted, regardless of the key's usage flags.
665 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100666 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200667 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200668 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200669 * \param[out] data_length On success, the number of bytes
670 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100671 *
Gilles Peskine28538492018-07-11 17:34:00 +0200672 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100673 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200674 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200675 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200676 * The key is neither a public key nor a key pair.
677 * \retval #PSA_ERROR_NOT_SUPPORTED
678 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
679 * The size of the \p data buffer is too small. You can determine a
680 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200681 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200682 * where \c type is the key type
683 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200684 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
685 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200686 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300687 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300688 * The library has not been previously initialized by psa_crypto_init().
689 * It is implementation-dependent whether a failure to initialize
690 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100691 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100692psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100693 uint8_t *data,
694 size_t data_size,
695 size_t *data_length);
696
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100697/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100698 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100699 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000700 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100701 * This function is primarily useful to copy a key from one location
702 * to another, since it populates a key using the material from
703 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200704 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100705 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100706 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100707 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200708 * The policy on the source key must have the usage flag
709 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200710 * This flag is sufficient to permit the copy if the key has the lifetime
711 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
712 * Some secure elements do not provide a way to copy a key without
713 * making it extractable from the secure element. If a key is located
714 * in such a secure element, then the key must have both usage flags
715 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
716 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200717 *
Gilles Peskine20628592019-04-19 19:29:50 +0200718 * The resulting key may only be used in a way that conforms to
719 * both the policy of the original key and the policy specified in
720 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100721 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200722 * usage flags on the source policy and the usage flags in \p attributes.
723 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100724 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200725 * - If either of the policies allows an algorithm and the other policy
726 * allows a wildcard-based algorithm policy that includes this algorithm,
727 * the resulting key allows the same algorithm.
728 * - If the policies do not allow any algorithm in common, this function
729 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200730 *
Gilles Peskine20628592019-04-19 19:29:50 +0200731 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100732 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200733 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100734 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200735 * \param[in] attributes The attributes for the new key.
736 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200737 * - The key type and size may be 0. If either is
738 * nonzero, it must match the corresponding
739 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200740 * - The key location (the lifetime and, for
741 * persistent keys, the key identifier) is
742 * used directly.
743 * - The policy constraints (usage flags and
744 * algorithm policy) are combined from
745 * the source key and \p attributes so that
746 * both sets of restrictions apply, as
747 * described in the documentation of this function.
748 * \param[out] target_handle On success, a handle to the newly created key.
749 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200750 *
751 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100752 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200753 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200754 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200755 * This is an attempt to create a persistent key, and there is
756 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200757 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200758 * The lifetime or identifier in \p attributes are invalid.
759 * \retval #PSA_ERROR_INVALID_ARGUMENT
760 * The policy constraints on the source and specified in
761 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200762 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200763 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200764 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100765 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200766 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
767 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100768 * The source key is not exportable and its lifetime does not
769 * allow copying it to the target's lifetime.
770 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
771 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200772 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
773 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200774 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100775 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100776psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200777 const psa_key_attributes_t *attributes,
778 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100779
780/**@}*/
781
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100782/** \defgroup hash Message digests
783 * @{
784 */
785
Gilles Peskine69647a42019-01-14 20:18:12 +0100786/** Calculate the hash (digest) of a message.
787 *
788 * \note To verify the hash of a message against an
789 * expected value, use psa_hash_compare() instead.
790 *
791 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
792 * such that #PSA_ALG_IS_HASH(\p alg) is true).
793 * \param[in] input Buffer containing the message to hash.
794 * \param input_length Size of the \p input buffer in bytes.
795 * \param[out] hash Buffer where the hash is to be written.
796 * \param hash_size Size of the \p hash buffer in bytes.
797 * \param[out] hash_length On success, the number of bytes
798 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100799 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100800 *
801 * \retval #PSA_SUCCESS
802 * Success.
803 * \retval #PSA_ERROR_NOT_SUPPORTED
804 * \p alg is not supported or is not a hash algorithm.
805 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
806 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
807 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200808 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100809 */
810psa_status_t psa_hash_compute(psa_algorithm_t alg,
811 const uint8_t *input,
812 size_t input_length,
813 uint8_t *hash,
814 size_t hash_size,
815 size_t *hash_length);
816
817/** Calculate the hash (digest) of a message and compare it with a
818 * reference value.
819 *
820 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
821 * such that #PSA_ALG_IS_HASH(\p alg) is true).
822 * \param[in] input Buffer containing the message to hash.
823 * \param input_length Size of the \p input buffer in bytes.
824 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100825 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100826 *
827 * \retval #PSA_SUCCESS
828 * The expected hash is identical to the actual hash of the input.
829 * \retval #PSA_ERROR_INVALID_SIGNATURE
830 * The hash of the message was calculated successfully, but it
831 * differs from the expected hash.
832 * \retval #PSA_ERROR_NOT_SUPPORTED
833 * \p alg is not supported or is not a hash algorithm.
834 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
835 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
836 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200837 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100838 */
839psa_status_t psa_hash_compare(psa_algorithm_t alg,
840 const uint8_t *input,
841 size_t input_length,
842 const uint8_t *hash,
843 const size_t hash_length);
844
Gilles Peskine308b91d2018-02-08 09:47:44 +0100845/** The type of the state data structure for multipart hash operations.
846 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000847 * Before calling any function on a hash operation object, the application must
848 * initialize it by any of the following means:
849 * - Set the structure to all-bits-zero, for example:
850 * \code
851 * psa_hash_operation_t operation;
852 * memset(&operation, 0, sizeof(operation));
853 * \endcode
854 * - Initialize the structure to logical zero values, for example:
855 * \code
856 * psa_hash_operation_t operation = {0};
857 * \endcode
858 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
859 * for example:
860 * \code
861 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
862 * \endcode
863 * - Assign the result of the function psa_hash_operation_init()
864 * to the structure, for example:
865 * \code
866 * psa_hash_operation_t operation;
867 * operation = psa_hash_operation_init();
868 * \endcode
869 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100870 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100871 * make any assumptions about the content of this structure except
872 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100873typedef struct psa_hash_operation_s psa_hash_operation_t;
874
Jaeden Amero6a25b412019-01-04 11:47:44 +0000875/** \def PSA_HASH_OPERATION_INIT
876 *
877 * This macro returns a suitable initializer for a hash operation object
878 * of type #psa_hash_operation_t.
879 */
880#ifdef __DOXYGEN_ONLY__
881/* This is an example definition for documentation purposes.
882 * Implementations should define a suitable value in `crypto_struct.h`.
883 */
884#define PSA_HASH_OPERATION_INIT {0}
885#endif
886
887/** Return an initial value for a hash operation object.
888 */
889static psa_hash_operation_t psa_hash_operation_init(void);
890
Gilles Peskinef45adda2019-01-14 18:29:18 +0100891/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100892 *
893 * The sequence of operations to calculate a hash (message digest)
894 * is as follows:
895 * -# Allocate an operation object which will be passed to all the functions
896 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000897 * -# Initialize the operation object with one of the methods described in the
898 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200899 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100900 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100901 * of the message each time. The hash that is calculated is the hash
902 * of the concatenation of these messages in order.
903 * -# To calculate the hash, call psa_hash_finish().
904 * To compare the hash with an expected value, call psa_hash_verify().
905 *
906 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000907 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100908 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200909 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100910 * eventually terminate the operation. The following events terminate an
911 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100912 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100913 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100914 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000915 * \param[in,out] operation The operation object to set up. It must have
916 * been initialized as per the documentation for
917 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200918 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
919 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100920 *
Gilles Peskine28538492018-07-11 17:34:00 +0200921 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100922 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200923 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200924 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100925 * \retval #PSA_ERROR_BAD_STATE
926 * The operation state is not valid (already set up and not
927 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200928 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
929 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
930 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200931 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100932 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200933psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100934 psa_algorithm_t alg);
935
Gilles Peskine308b91d2018-02-08 09:47:44 +0100936/** Add a message fragment to a multipart hash operation.
937 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200938 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100939 *
940 * If this function returns an error status, the operation becomes inactive.
941 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200942 * \param[in,out] operation Active hash operation.
943 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200944 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100945 *
Gilles Peskine28538492018-07-11 17:34:00 +0200946 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100947 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200948 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100949 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200950 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
951 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
952 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200953 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100954 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100955psa_status_t psa_hash_update(psa_hash_operation_t *operation,
956 const uint8_t *input,
957 size_t input_length);
958
Gilles Peskine308b91d2018-02-08 09:47:44 +0100959/** Finish the calculation of the hash of a message.
960 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200961 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100962 * This function calculates the hash of the message formed by concatenating
963 * the inputs passed to preceding calls to psa_hash_update().
964 *
965 * When this function returns, the operation becomes inactive.
966 *
967 * \warning Applications should not call this function if they expect
968 * a specific value for the hash. Call psa_hash_verify() instead.
969 * Beware that comparing integrity or authenticity data such as
970 * hash values with a function such as \c memcmp is risky
971 * because the time taken by the comparison may leak information
972 * about the hashed data which could allow an attacker to guess
973 * a valid hash and thereby bypass security controls.
974 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200975 * \param[in,out] operation Active hash operation.
976 * \param[out] hash Buffer where the hash is to be written.
977 * \param hash_size Size of the \p hash buffer in bytes.
978 * \param[out] hash_length On success, the number of bytes
979 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200980 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200981 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100982 *
Gilles Peskine28538492018-07-11 17:34:00 +0200983 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100984 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200985 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100986 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200987 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200988 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200989 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200991 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
992 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
993 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200994 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100995 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100996psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
997 uint8_t *hash,
998 size_t hash_size,
999 size_t *hash_length);
1000
Gilles Peskine308b91d2018-02-08 09:47:44 +01001001/** Finish the calculation of the hash of a message and compare it with
1002 * an expected value.
1003 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001004 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001005 * This function calculates the hash of the message formed by concatenating
1006 * the inputs passed to preceding calls to psa_hash_update(). It then
1007 * compares the calculated hash with the expected hash passed as a
1008 * parameter to this function.
1009 *
1010 * When this function returns, the operation becomes inactive.
1011 *
Gilles Peskine19067982018-03-20 17:54:53 +01001012 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001013 * comparison between the actual hash and the expected hash is performed
1014 * in constant time.
1015 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001016 * \param[in,out] operation Active hash operation.
1017 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001018 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001019 *
Gilles Peskine28538492018-07-11 17:34:00 +02001020 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001021 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001022 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001023 * The hash of the message was calculated successfully, but it
1024 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001025 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001026 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001027 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1028 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1029 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001030 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001031 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001032psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1033 const uint8_t *hash,
1034 size_t hash_length);
1035
Gilles Peskine308b91d2018-02-08 09:47:44 +01001036/** Abort a hash operation.
1037 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001038 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001039 * \p operation structure itself. Once aborted, the operation object
1040 * can be reused for another operation by calling
1041 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001042 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001043 * You may call this function any time after the operation object has
1044 * been initialized by any of the following methods:
1045 * - A call to psa_hash_setup(), whether it succeeds or not.
1046 * - Initializing the \c struct to all-bits-zero.
1047 * - Initializing the \c struct to logical zeros, e.g.
1048 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001049 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001050 * In particular, calling psa_hash_abort() after the operation has been
1051 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1052 * psa_hash_verify() is safe and has no effect.
1053 *
1054 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001055 *
Gilles Peskine28538492018-07-11 17:34:00 +02001056 * \retval #PSA_SUCCESS
1057 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001058 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001059 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1060 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001061 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001062 */
1063psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001064
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001065/** Clone a hash operation.
1066 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001067 * This function copies the state of an ongoing hash operation to
1068 * a new operation object. In other words, this function is equivalent
1069 * to calling psa_hash_setup() on \p target_operation with the same
1070 * algorithm that \p source_operation was set up for, then
1071 * psa_hash_update() on \p target_operation with the same input that
1072 * that was passed to \p source_operation. After this function returns, the
1073 * two objects are independent, i.e. subsequent calls involving one of
1074 * the objects do not affect the other object.
1075 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001076 * \param[in] source_operation The active hash operation to clone.
1077 * \param[in,out] target_operation The operation object to set up.
1078 * It must be initialized but not active.
1079 *
1080 * \retval #PSA_SUCCESS
1081 * \retval #PSA_ERROR_BAD_STATE
1082 * \p source_operation is not an active hash operation.
1083 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001084 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001085 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1086 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001087 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001088 */
1089psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1090 psa_hash_operation_t *target_operation);
1091
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001092/**@}*/
1093
Gilles Peskine8c9def32018-02-08 10:02:12 +01001094/** \defgroup MAC Message authentication codes
1095 * @{
1096 */
1097
Gilles Peskine69647a42019-01-14 20:18:12 +01001098/** Calculate the MAC (message authentication code) of a message.
1099 *
1100 * \note To verify the MAC of a message against an
1101 * expected value, use psa_mac_verify() instead.
1102 * Beware that comparing integrity or authenticity data such as
1103 * MAC values with a function such as \c memcmp is risky
1104 * because the time taken by the comparison may leak information
1105 * about the MAC value which could allow an attacker to guess
1106 * a valid MAC and thereby bypass security controls.
1107 *
1108 * \param handle Handle to the key to use for the operation.
1109 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001110 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001111 * \param[in] input Buffer containing the input message.
1112 * \param input_length Size of the \p input buffer in bytes.
1113 * \param[out] mac Buffer where the MAC value is to be written.
1114 * \param mac_size Size of the \p mac buffer in bytes.
1115 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001116 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001117 *
1118 * \retval #PSA_SUCCESS
1119 * Success.
1120 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001121 * \retval #PSA_ERROR_NOT_PERMITTED
1122 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001123 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001124 * \retval #PSA_ERROR_NOT_SUPPORTED
1125 * \p alg is not supported or is not a MAC algorithm.
1126 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1127 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1128 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001129 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001130 * \retval #PSA_ERROR_BAD_STATE
1131 * The library has not been previously initialized by psa_crypto_init().
1132 * It is implementation-dependent whether a failure to initialize
1133 * results in this error code.
1134 */
1135psa_status_t psa_mac_compute(psa_key_handle_t handle,
1136 psa_algorithm_t alg,
1137 const uint8_t *input,
1138 size_t input_length,
1139 uint8_t *mac,
1140 size_t mac_size,
1141 size_t *mac_length);
1142
1143/** Calculate the MAC of a message and compare it with a reference value.
1144 *
1145 * \param handle Handle to the key to use for the operation.
1146 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001147 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001148 * \param[in] input Buffer containing the input message.
1149 * \param input_length Size of the \p input buffer in bytes.
1150 * \param[out] mac Buffer containing the expected MAC value.
1151 * \param mac_length Size of the \p mac buffer in bytes.
1152 *
1153 * \retval #PSA_SUCCESS
1154 * The expected MAC is identical to the actual MAC of the input.
1155 * \retval #PSA_ERROR_INVALID_SIGNATURE
1156 * The MAC of the message was calculated successfully, but it
1157 * differs from the expected value.
1158 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001159 * \retval #PSA_ERROR_NOT_PERMITTED
1160 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001161 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001162 * \retval #PSA_ERROR_NOT_SUPPORTED
1163 * \p alg is not supported or is not a MAC algorithm.
1164 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1165 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1166 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001167 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001168 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001169psa_status_t psa_mac_verify(psa_key_handle_t handle,
1170 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001171 const uint8_t *input,
1172 size_t input_length,
1173 const uint8_t *mac,
1174 const size_t mac_length);
1175
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001176/** The type of the state data structure for multipart MAC operations.
1177 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001178 * Before calling any function on a MAC operation object, the application must
1179 * initialize it by any of the following means:
1180 * - Set the structure to all-bits-zero, for example:
1181 * \code
1182 * psa_mac_operation_t operation;
1183 * memset(&operation, 0, sizeof(operation));
1184 * \endcode
1185 * - Initialize the structure to logical zero values, for example:
1186 * \code
1187 * psa_mac_operation_t operation = {0};
1188 * \endcode
1189 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1190 * for example:
1191 * \code
1192 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1193 * \endcode
1194 * - Assign the result of the function psa_mac_operation_init()
1195 * to the structure, for example:
1196 * \code
1197 * psa_mac_operation_t operation;
1198 * operation = psa_mac_operation_init();
1199 * \endcode
1200 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001201 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001202 * make any assumptions about the content of this structure except
1203 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001204typedef struct psa_mac_operation_s psa_mac_operation_t;
1205
Jaeden Amero769ce272019-01-04 11:48:03 +00001206/** \def PSA_MAC_OPERATION_INIT
1207 *
1208 * This macro returns a suitable initializer for a MAC operation object of type
1209 * #psa_mac_operation_t.
1210 */
1211#ifdef __DOXYGEN_ONLY__
1212/* This is an example definition for documentation purposes.
1213 * Implementations should define a suitable value in `crypto_struct.h`.
1214 */
1215#define PSA_MAC_OPERATION_INIT {0}
1216#endif
1217
1218/** Return an initial value for a MAC operation object.
1219 */
1220static psa_mac_operation_t psa_mac_operation_init(void);
1221
Gilles Peskinef45adda2019-01-14 18:29:18 +01001222/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001223 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001224 * This function sets up the calculation of the MAC
1225 * (message authentication code) of a byte string.
1226 * To verify the MAC of a message against an
1227 * expected value, use psa_mac_verify_setup() instead.
1228 *
1229 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001230 * -# Allocate an operation object which will be passed to all the functions
1231 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001232 * -# Initialize the operation object with one of the methods described in the
1233 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001234 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001235 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1236 * of the message each time. The MAC that is calculated is the MAC
1237 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001238 * -# At the end of the message, call psa_mac_sign_finish() to finish
1239 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001240 *
1241 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001242 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001243 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001244 * After a successful call to psa_mac_sign_setup(), the application must
1245 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001246 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001247 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001248 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001249 * \param[in,out] operation The operation object to set up. It must have
1250 * been initialized as per the documentation for
1251 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001252 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001253 * It must remain valid until the operation
1254 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001255 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001256 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001257 *
Gilles Peskine28538492018-07-11 17:34:00 +02001258 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001259 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001260 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001261 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001262 * \retval #PSA_ERROR_NOT_PERMITTED
1263 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001264 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001265 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001266 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001267 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1268 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1269 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001270 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001271 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001272 * The operation state is not valid (already set up and not
1273 * subsequently completed).
1274 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001275 * The library has not been previously initialized by psa_crypto_init().
1276 * It is implementation-dependent whether a failure to initialize
1277 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001278 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001279psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001280 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001281 psa_algorithm_t alg);
1282
Gilles Peskinef45adda2019-01-14 18:29:18 +01001283/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001284 *
1285 * This function sets up the verification of the MAC
1286 * (message authentication code) of a byte string against an expected value.
1287 *
1288 * The sequence of operations to verify a MAC is as follows:
1289 * -# Allocate an operation object which will be passed to all the functions
1290 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001291 * -# Initialize the operation object with one of the methods described in the
1292 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001293 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001294 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1295 * of the message each time. The MAC that is calculated is the MAC
1296 * of the concatenation of these messages in order.
1297 * -# At the end of the message, call psa_mac_verify_finish() to finish
1298 * calculating the actual MAC of the message and verify it against
1299 * the expected value.
1300 *
1301 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001302 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001303 *
1304 * After a successful call to psa_mac_verify_setup(), the application must
1305 * eventually terminate the operation through one of the following methods:
1306 * - A failed call to psa_mac_update().
1307 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1308 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001309 * \param[in,out] operation The operation object to set up. It must have
1310 * been initialized as per the documentation for
1311 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001312 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001313 * It must remain valid until the operation
1314 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001315 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1316 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001317 *
Gilles Peskine28538492018-07-11 17:34:00 +02001318 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001319 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001320 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001321 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001322 * \retval #PSA_ERROR_NOT_PERMITTED
1323 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001324 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001325 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001326 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001327 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1328 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1329 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001330 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001331 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001332 * The operation state is not valid (already set up and not
1333 * subsequently completed).
1334 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001335 * The library has not been previously initialized by psa_crypto_init().
1336 * It is implementation-dependent whether a failure to initialize
1337 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001338 */
1339psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001340 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001341 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001342
Gilles Peskinedcd14942018-07-12 00:30:52 +02001343/** Add a message fragment to a multipart MAC operation.
1344 *
1345 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1346 * before calling this function.
1347 *
1348 * If this function returns an error status, the operation becomes inactive.
1349 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001350 * \param[in,out] operation Active MAC operation.
1351 * \param[in] input Buffer containing the message fragment to add to
1352 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001353 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001354 *
1355 * \retval #PSA_SUCCESS
1356 * Success.
1357 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001358 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001359 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1360 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1361 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001362 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001363 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001364psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1365 const uint8_t *input,
1366 size_t input_length);
1367
Gilles Peskinedcd14942018-07-12 00:30:52 +02001368/** Finish the calculation of the MAC of a message.
1369 *
1370 * The application must call psa_mac_sign_setup() before calling this function.
1371 * This function calculates the MAC of the message formed by concatenating
1372 * the inputs passed to preceding calls to psa_mac_update().
1373 *
1374 * When this function returns, the operation becomes inactive.
1375 *
1376 * \warning Applications should not call this function if they expect
1377 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1378 * Beware that comparing integrity or authenticity data such as
1379 * MAC values with a function such as \c memcmp is risky
1380 * because the time taken by the comparison may leak information
1381 * about the MAC value which could allow an attacker to guess
1382 * a valid MAC and thereby bypass security controls.
1383 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001384 * \param[in,out] operation Active MAC operation.
1385 * \param[out] mac Buffer where the MAC value is to be written.
1386 * \param mac_size Size of the \p mac buffer in bytes.
1387 * \param[out] mac_length On success, the number of bytes
1388 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001389 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001390 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001391 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001392 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001393 *
1394 * \retval #PSA_SUCCESS
1395 * Success.
1396 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001397 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001398 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001399 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001400 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1401 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1402 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1403 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001404 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001405 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001406psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1407 uint8_t *mac,
1408 size_t mac_size,
1409 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001410
Gilles Peskinedcd14942018-07-12 00:30:52 +02001411/** Finish the calculation of the MAC of a message and compare it with
1412 * an expected value.
1413 *
1414 * The application must call psa_mac_verify_setup() before calling this function.
1415 * This function calculates the MAC of the message formed by concatenating
1416 * the inputs passed to preceding calls to psa_mac_update(). It then
1417 * compares the calculated MAC with the expected MAC passed as a
1418 * parameter to this function.
1419 *
1420 * When this function returns, the operation becomes inactive.
1421 *
1422 * \note Implementations shall make the best effort to ensure that the
1423 * comparison between the actual MAC and the expected MAC is performed
1424 * in constant time.
1425 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001426 * \param[in,out] operation Active MAC operation.
1427 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001428 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001429 *
1430 * \retval #PSA_SUCCESS
1431 * The expected MAC is identical to the actual MAC of the message.
1432 * \retval #PSA_ERROR_INVALID_SIGNATURE
1433 * The MAC of the message was calculated successfully, but it
1434 * differs from the expected MAC.
1435 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001436 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001437 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1438 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1439 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001440 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001441 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001442psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1443 const uint8_t *mac,
1444 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001445
Gilles Peskinedcd14942018-07-12 00:30:52 +02001446/** Abort a MAC operation.
1447 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001448 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001449 * \p operation structure itself. Once aborted, the operation object
1450 * can be reused for another operation by calling
1451 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001452 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001453 * You may call this function any time after the operation object has
1454 * been initialized by any of the following methods:
1455 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1456 * it succeeds or not.
1457 * - Initializing the \c struct to all-bits-zero.
1458 * - Initializing the \c struct to logical zeros, e.g.
1459 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001460 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001461 * In particular, calling psa_mac_abort() after the operation has been
1462 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1463 * psa_mac_verify_finish() is safe and has no effect.
1464 *
1465 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001466 *
1467 * \retval #PSA_SUCCESS
1468 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001469 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001470 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1471 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001472 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001473 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001474psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1475
1476/**@}*/
1477
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001478/** \defgroup cipher Symmetric ciphers
1479 * @{
1480 */
1481
Gilles Peskine69647a42019-01-14 20:18:12 +01001482/** Encrypt a message using a symmetric cipher.
1483 *
1484 * This function encrypts a message with a random IV (initialization
1485 * vector).
1486 *
1487 * \param handle Handle to the key to use for the operation.
1488 * It must remain valid until the operation
1489 * terminates.
1490 * \param alg The cipher algorithm to compute
1491 * (\c PSA_ALG_XXX value such that
1492 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1493 * \param[in] input Buffer containing the message to encrypt.
1494 * \param input_length Size of the \p input buffer in bytes.
1495 * \param[out] output Buffer where the output is to be written.
1496 * The output contains the IV followed by
1497 * the ciphertext proper.
1498 * \param output_size Size of the \p output buffer in bytes.
1499 * \param[out] output_length On success, the number of bytes
1500 * that make up the output.
1501 *
1502 * \retval #PSA_SUCCESS
1503 * Success.
1504 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001505 * \retval #PSA_ERROR_NOT_PERMITTED
1506 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001507 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001508 * \retval #PSA_ERROR_NOT_SUPPORTED
1509 * \p alg is not supported or is not a cipher algorithm.
1510 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1511 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1512 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1513 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001514 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001515 */
1516psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1517 psa_algorithm_t alg,
1518 const uint8_t *input,
1519 size_t input_length,
1520 uint8_t *output,
1521 size_t output_size,
1522 size_t *output_length);
1523
1524/** Decrypt a message using a symmetric cipher.
1525 *
1526 * This function decrypts a message encrypted with a symmetric cipher.
1527 *
1528 * \param handle Handle to the key to use for the operation.
1529 * It must remain valid until the operation
1530 * terminates.
1531 * \param alg The cipher algorithm to compute
1532 * (\c PSA_ALG_XXX value such that
1533 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1534 * \param[in] input Buffer containing the message to decrypt.
1535 * This consists of the IV followed by the
1536 * ciphertext proper.
1537 * \param input_length Size of the \p input buffer in bytes.
1538 * \param[out] output Buffer where the plaintext is to be written.
1539 * \param output_size Size of the \p output buffer in bytes.
1540 * \param[out] output_length On success, the number of bytes
1541 * that make up the output.
1542 *
1543 * \retval #PSA_SUCCESS
1544 * Success.
1545 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001546 * \retval #PSA_ERROR_NOT_PERMITTED
1547 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001548 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001549 * \retval #PSA_ERROR_NOT_SUPPORTED
1550 * \p alg is not supported or is not a cipher algorithm.
1551 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1552 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1553 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1554 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001555 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001556 */
1557psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1558 psa_algorithm_t alg,
1559 const uint8_t *input,
1560 size_t input_length,
1561 uint8_t *output,
1562 size_t output_size,
1563 size_t *output_length);
1564
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001565/** The type of the state data structure for multipart cipher operations.
1566 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001567 * Before calling any function on a cipher operation object, the application
1568 * must initialize it by any of the following means:
1569 * - Set the structure to all-bits-zero, for example:
1570 * \code
1571 * psa_cipher_operation_t operation;
1572 * memset(&operation, 0, sizeof(operation));
1573 * \endcode
1574 * - Initialize the structure to logical zero values, for example:
1575 * \code
1576 * psa_cipher_operation_t operation = {0};
1577 * \endcode
1578 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1579 * for example:
1580 * \code
1581 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1582 * \endcode
1583 * - Assign the result of the function psa_cipher_operation_init()
1584 * to the structure, for example:
1585 * \code
1586 * psa_cipher_operation_t operation;
1587 * operation = psa_cipher_operation_init();
1588 * \endcode
1589 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001590 * This is an implementation-defined \c struct. Applications should not
1591 * make any assumptions about the content of this structure except
1592 * as directed by the documentation of a specific implementation. */
1593typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1594
Jaeden Amero5bae2272019-01-04 11:48:27 +00001595/** \def PSA_CIPHER_OPERATION_INIT
1596 *
1597 * This macro returns a suitable initializer for a cipher operation object of
1598 * type #psa_cipher_operation_t.
1599 */
1600#ifdef __DOXYGEN_ONLY__
1601/* This is an example definition for documentation purposes.
1602 * Implementations should define a suitable value in `crypto_struct.h`.
1603 */
1604#define PSA_CIPHER_OPERATION_INIT {0}
1605#endif
1606
1607/** Return an initial value for a cipher operation object.
1608 */
1609static psa_cipher_operation_t psa_cipher_operation_init(void);
1610
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001611/** Set the key for a multipart symmetric encryption operation.
1612 *
1613 * The sequence of operations to encrypt a message with a symmetric cipher
1614 * is as follows:
1615 * -# Allocate an operation object which will be passed to all the functions
1616 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001617 * -# Initialize the operation object with one of the methods described in the
1618 * documentation for #psa_cipher_operation_t, e.g.
1619 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001620 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001621 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001622 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001623 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001624 * requires a specific IV value.
1625 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1626 * of the message each time.
1627 * -# Call psa_cipher_finish().
1628 *
1629 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001630 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001631 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001632 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001633 * eventually terminate the operation. The following events terminate an
1634 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001635 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001636 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001637 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001638 * \param[in,out] operation The operation object to set up. It must have
1639 * been initialized as per the documentation for
1640 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001641 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001642 * It must remain valid until the operation
1643 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001644 * \param alg The cipher algorithm to compute
1645 * (\c PSA_ALG_XXX value such that
1646 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001647 *
Gilles Peskine28538492018-07-11 17:34:00 +02001648 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001649 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001650 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001651 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001652 * \retval #PSA_ERROR_NOT_PERMITTED
1653 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001654 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001655 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001656 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001657 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1658 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1659 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001660 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001661 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001662 * The operation state is not valid (already set up and not
1663 * subsequently completed).
1664 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001665 * The library has not been previously initialized by psa_crypto_init().
1666 * It is implementation-dependent whether a failure to initialize
1667 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001668 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001669psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001670 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001671 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001672
1673/** Set the key for a multipart symmetric decryption operation.
1674 *
1675 * The sequence of operations to decrypt a message with a symmetric cipher
1676 * is as follows:
1677 * -# Allocate an operation object which will be passed to all the functions
1678 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001679 * -# Initialize the operation object with one of the methods described in the
1680 * documentation for #psa_cipher_operation_t, e.g.
1681 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001682 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001683 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001684 * decryption. If the IV is prepended to the ciphertext, you can call
1685 * psa_cipher_update() on a buffer containing the IV followed by the
1686 * beginning of the message.
1687 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1688 * of the message each time.
1689 * -# Call psa_cipher_finish().
1690 *
1691 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001692 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001693 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001694 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001695 * eventually terminate the operation. The following events terminate an
1696 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001697 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001698 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001699 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001700 * \param[in,out] operation The operation object to set up. It must have
1701 * been initialized as per the documentation for
1702 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001703 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001704 * It must remain valid until the operation
1705 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001706 * \param alg The cipher algorithm to compute
1707 * (\c PSA_ALG_XXX value such that
1708 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001709 *
Gilles Peskine28538492018-07-11 17:34:00 +02001710 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001711 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001712 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001713 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001714 * \retval #PSA_ERROR_NOT_PERMITTED
1715 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001716 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001717 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001718 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001719 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1720 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1721 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001722 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001723 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001724 * The operation state is not valid (already set up and not
1725 * subsequently completed).
1726 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001727 * The library has not been previously initialized by psa_crypto_init().
1728 * It is implementation-dependent whether a failure to initialize
1729 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001730 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001731psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001732 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001733 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001734
Gilles Peskinedcd14942018-07-12 00:30:52 +02001735/** Generate an IV for a symmetric encryption operation.
1736 *
1737 * This function generates a random IV (initialization vector), nonce
1738 * or initial counter value for the encryption operation as appropriate
1739 * for the chosen algorithm, key type and key size.
1740 *
1741 * The application must call psa_cipher_encrypt_setup() before
1742 * calling this function.
1743 *
1744 * If this function returns an error status, the operation becomes inactive.
1745 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001746 * \param[in,out] operation Active cipher operation.
1747 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001748 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001749 * \param[out] iv_length On success, the number of bytes of the
1750 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001751 *
1752 * \retval #PSA_SUCCESS
1753 * Success.
1754 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001755 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001756 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001757 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001758 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1759 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1760 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001761 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001762 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001763psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001764 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001765 size_t iv_size,
1766 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001767
Gilles Peskinedcd14942018-07-12 00:30:52 +02001768/** Set the IV for a symmetric encryption or decryption operation.
1769 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001770 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001771 * or initial counter value for the encryption or decryption operation.
1772 *
1773 * The application must call psa_cipher_encrypt_setup() before
1774 * calling this function.
1775 *
1776 * If this function returns an error status, the operation becomes inactive.
1777 *
1778 * \note When encrypting, applications should use psa_cipher_generate_iv()
1779 * instead of this function, unless implementing a protocol that requires
1780 * a non-random IV.
1781 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001782 * \param[in,out] operation Active cipher operation.
1783 * \param[in] iv Buffer containing the IV to use.
1784 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001785 *
1786 * \retval #PSA_SUCCESS
1787 * Success.
1788 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001789 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001790 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001791 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001792 * or the chosen algorithm does not use an IV.
1793 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1794 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1795 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001796 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001797 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001798psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001799 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001800 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001801
Gilles Peskinedcd14942018-07-12 00:30:52 +02001802/** Encrypt or decrypt a message fragment in an active cipher operation.
1803 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001804 * Before calling this function, you must:
1805 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1806 * The choice of setup function determines whether this function
1807 * encrypts or decrypts its input.
1808 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1809 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001810 *
1811 * If this function returns an error status, the operation becomes inactive.
1812 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001813 * \param[in,out] operation Active cipher operation.
1814 * \param[in] input Buffer containing the message fragment to
1815 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001816 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001817 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001818 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001819 * \param[out] output_length On success, the number of bytes
1820 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001821 *
1822 * \retval #PSA_SUCCESS
1823 * Success.
1824 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001825 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001826 * not set, or already completed).
1827 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1828 * The size of the \p output buffer is too small.
1829 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1830 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1831 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001832 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001833 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001834psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1835 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001836 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001837 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001838 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001839 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001840
Gilles Peskinedcd14942018-07-12 00:30:52 +02001841/** Finish encrypting or decrypting a message in a cipher operation.
1842 *
1843 * The application must call psa_cipher_encrypt_setup() or
1844 * psa_cipher_decrypt_setup() before calling this function. The choice
1845 * of setup function determines whether this function encrypts or
1846 * decrypts its input.
1847 *
1848 * This function finishes the encryption or decryption of the message
1849 * formed by concatenating the inputs passed to preceding calls to
1850 * psa_cipher_update().
1851 *
1852 * When this function returns, the operation becomes inactive.
1853 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001854 * \param[in,out] operation Active cipher operation.
1855 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001856 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001857 * \param[out] output_length On success, the number of bytes
1858 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001859 *
1860 * \retval #PSA_SUCCESS
1861 * Success.
1862 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001863 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001864 * not set, or already completed).
1865 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1866 * The size of the \p output buffer is too small.
1867 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1868 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1869 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001870 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001871 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001872psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001873 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001874 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001875 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001876
Gilles Peskinedcd14942018-07-12 00:30:52 +02001877/** Abort a cipher operation.
1878 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001879 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001880 * \p operation structure itself. Once aborted, the operation object
1881 * can be reused for another operation by calling
1882 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001883 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001884 * You may call this function any time after the operation object has
1885 * been initialized by any of the following methods:
1886 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1887 * whether it succeeds or not.
1888 * - Initializing the \c struct to all-bits-zero.
1889 * - Initializing the \c struct to logical zeros, e.g.
1890 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001891 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001892 * In particular, calling psa_cipher_abort() after the operation has been
1893 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1894 * is safe and has no effect.
1895 *
1896 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001897 *
1898 * \retval #PSA_SUCCESS
1899 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001900 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001901 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1902 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001903 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001904 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001905psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1906
1907/**@}*/
1908
Gilles Peskine3b555712018-03-03 21:27:57 +01001909/** \defgroup aead Authenticated encryption with associated data (AEAD)
1910 * @{
1911 */
1912
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001913/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001914 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001915 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001916 * \param alg The AEAD algorithm to compute
1917 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001918 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001919 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001920 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001921 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001922 * but not encrypted.
1923 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001924 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001925 * encrypted.
1926 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001927 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001928 * encrypted data. The additional data is not
1929 * part of this output. For algorithms where the
1930 * encrypted data and the authentication tag
1931 * are defined as separate outputs, the
1932 * authentication tag is appended to the
1933 * encrypted data.
1934 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1935 * This must be at least
1936 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1937 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001938 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001939 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001940 *
Gilles Peskine28538492018-07-11 17:34:00 +02001941 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001942 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001943 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001944 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001945 * \retval #PSA_ERROR_NOT_PERMITTED
1946 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001947 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001948 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001949 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001950 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1951 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1952 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001953 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001954 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001955 * The library has not been previously initialized by psa_crypto_init().
1956 * It is implementation-dependent whether a failure to initialize
1957 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001958 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001959psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001960 psa_algorithm_t alg,
1961 const uint8_t *nonce,
1962 size_t nonce_length,
1963 const uint8_t *additional_data,
1964 size_t additional_data_length,
1965 const uint8_t *plaintext,
1966 size_t plaintext_length,
1967 uint8_t *ciphertext,
1968 size_t ciphertext_size,
1969 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001970
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001971/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001972 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001973 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001974 * \param alg The AEAD algorithm to compute
1975 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001976 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001977 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001978 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001979 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001980 * but not encrypted.
1981 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001982 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001983 * encrypted. For algorithms where the
1984 * encrypted data and the authentication tag
1985 * are defined as separate inputs, the buffer
1986 * must contain the encrypted data followed
1987 * by the authentication tag.
1988 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001989 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001990 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1991 * This must be at least
1992 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1993 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001994 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001995 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001996 *
Gilles Peskine28538492018-07-11 17:34:00 +02001997 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001998 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001999 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002000 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002001 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002002 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002003 * \retval #PSA_ERROR_NOT_PERMITTED
2004 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002005 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002006 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002007 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002008 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2009 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2010 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002011 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002012 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002013 * The library has not been previously initialized by psa_crypto_init().
2014 * It is implementation-dependent whether a failure to initialize
2015 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002016 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002017psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002018 psa_algorithm_t alg,
2019 const uint8_t *nonce,
2020 size_t nonce_length,
2021 const uint8_t *additional_data,
2022 size_t additional_data_length,
2023 const uint8_t *ciphertext,
2024 size_t ciphertext_length,
2025 uint8_t *plaintext,
2026 size_t plaintext_size,
2027 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002028
Gilles Peskine30a9e412019-01-14 18:36:12 +01002029/** The type of the state data structure for multipart AEAD operations.
2030 *
2031 * Before calling any function on an AEAD operation object, the application
2032 * must initialize it by any of the following means:
2033 * - Set the structure to all-bits-zero, for example:
2034 * \code
2035 * psa_aead_operation_t operation;
2036 * memset(&operation, 0, sizeof(operation));
2037 * \endcode
2038 * - Initialize the structure to logical zero values, for example:
2039 * \code
2040 * psa_aead_operation_t operation = {0};
2041 * \endcode
2042 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2043 * for example:
2044 * \code
2045 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2046 * \endcode
2047 * - Assign the result of the function psa_aead_operation_init()
2048 * to the structure, for example:
2049 * \code
2050 * psa_aead_operation_t operation;
2051 * operation = psa_aead_operation_init();
2052 * \endcode
2053 *
2054 * This is an implementation-defined \c struct. Applications should not
2055 * make any assumptions about the content of this structure except
2056 * as directed by the documentation of a specific implementation. */
2057typedef struct psa_aead_operation_s psa_aead_operation_t;
2058
2059/** \def PSA_AEAD_OPERATION_INIT
2060 *
2061 * This macro returns a suitable initializer for an AEAD operation object of
2062 * type #psa_aead_operation_t.
2063 */
2064#ifdef __DOXYGEN_ONLY__
2065/* This is an example definition for documentation purposes.
2066 * Implementations should define a suitable value in `crypto_struct.h`.
2067 */
2068#define PSA_AEAD_OPERATION_INIT {0}
2069#endif
2070
2071/** Return an initial value for an AEAD operation object.
2072 */
2073static psa_aead_operation_t psa_aead_operation_init(void);
2074
2075/** Set the key for a multipart authenticated encryption operation.
2076 *
2077 * The sequence of operations to encrypt a message with authentication
2078 * is as follows:
2079 * -# Allocate an operation object which will be passed to all the functions
2080 * listed here.
2081 * -# Initialize the operation object with one of the methods described in the
2082 * documentation for #psa_aead_operation_t, e.g.
2083 * PSA_AEAD_OPERATION_INIT.
2084 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002085 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2086 * inputs to the subsequent calls to psa_aead_update_ad() and
2087 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2088 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002089 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2090 * generate or set the nonce. You should use
2091 * psa_aead_generate_nonce() unless the protocol you are implementing
2092 * requires a specific nonce value.
2093 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2094 * of the non-encrypted additional authenticated data each time.
2095 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002096 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002097 * -# Call psa_aead_finish().
2098 *
2099 * The application may call psa_aead_abort() at any time after the operation
2100 * has been initialized.
2101 *
2102 * After a successful call to psa_aead_encrypt_setup(), the application must
2103 * eventually terminate the operation. The following events terminate an
2104 * operation:
2105 * - A failed call to any of the \c psa_aead_xxx functions.
2106 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2107 *
2108 * \param[in,out] operation The operation object to set up. It must have
2109 * been initialized as per the documentation for
2110 * #psa_aead_operation_t and not yet in use.
2111 * \param handle Handle to the key to use for the operation.
2112 * It must remain valid until the operation
2113 * terminates.
2114 * \param alg The AEAD algorithm to compute
2115 * (\c PSA_ALG_XXX value such that
2116 * #PSA_ALG_IS_AEAD(\p alg) is true).
2117 *
2118 * \retval #PSA_SUCCESS
2119 * Success.
2120 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002121 * \retval #PSA_ERROR_NOT_PERMITTED
2122 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002123 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002124 * \retval #PSA_ERROR_NOT_SUPPORTED
2125 * \p alg is not supported or is not an AEAD algorithm.
2126 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2127 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2128 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002129 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002130 * \retval #PSA_ERROR_BAD_STATE
2131 * The library has not been previously initialized by psa_crypto_init().
2132 * It is implementation-dependent whether a failure to initialize
2133 * results in this error code.
2134 */
2135psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2136 psa_key_handle_t handle,
2137 psa_algorithm_t alg);
2138
2139/** Set the key for a multipart authenticated decryption operation.
2140 *
2141 * The sequence of operations to decrypt a message with authentication
2142 * is as follows:
2143 * -# Allocate an operation object which will be passed to all the functions
2144 * listed here.
2145 * -# Initialize the operation object with one of the methods described in the
2146 * documentation for #psa_aead_operation_t, e.g.
2147 * PSA_AEAD_OPERATION_INIT.
2148 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002149 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2150 * inputs to the subsequent calls to psa_aead_update_ad() and
2151 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2152 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002153 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2154 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2155 * of the non-encrypted additional authenticated data each time.
2156 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002157 * of the ciphertext to decrypt each time.
2158 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002159 *
2160 * The application may call psa_aead_abort() at any time after the operation
2161 * has been initialized.
2162 *
2163 * After a successful call to psa_aead_decrypt_setup(), the application must
2164 * eventually terminate the operation. The following events terminate an
2165 * operation:
2166 * - A failed call to any of the \c psa_aead_xxx functions.
2167 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2168 *
2169 * \param[in,out] operation The operation object to set up. It must have
2170 * been initialized as per the documentation for
2171 * #psa_aead_operation_t and not yet in use.
2172 * \param handle Handle to the key to use for the operation.
2173 * It must remain valid until the operation
2174 * terminates.
2175 * \param alg The AEAD algorithm to compute
2176 * (\c PSA_ALG_XXX value such that
2177 * #PSA_ALG_IS_AEAD(\p alg) is true).
2178 *
2179 * \retval #PSA_SUCCESS
2180 * Success.
2181 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002182 * \retval #PSA_ERROR_NOT_PERMITTED
2183 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002184 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002185 * \retval #PSA_ERROR_NOT_SUPPORTED
2186 * \p alg is not supported or is not an AEAD algorithm.
2187 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2188 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2189 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002190 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002191 * \retval #PSA_ERROR_BAD_STATE
2192 * The library has not been previously initialized by psa_crypto_init().
2193 * It is implementation-dependent whether a failure to initialize
2194 * results in this error code.
2195 */
2196psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2197 psa_key_handle_t handle,
2198 psa_algorithm_t alg);
2199
2200/** Generate a random nonce for an authenticated encryption operation.
2201 *
2202 * This function generates a random nonce for the authenticated encryption
2203 * operation with an appropriate size for the chosen algorithm, key type
2204 * and key size.
2205 *
2206 * The application must call psa_aead_encrypt_setup() before
2207 * calling this function.
2208 *
2209 * If this function returns an error status, the operation becomes inactive.
2210 *
2211 * \param[in,out] operation Active AEAD operation.
2212 * \param[out] nonce Buffer where the generated nonce is to be
2213 * written.
2214 * \param nonce_size Size of the \p nonce buffer in bytes.
2215 * \param[out] nonce_length On success, the number of bytes of the
2216 * generated nonce.
2217 *
2218 * \retval #PSA_SUCCESS
2219 * Success.
2220 * \retval #PSA_ERROR_BAD_STATE
2221 * The operation state is not valid (not set up, or nonce already set).
2222 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2223 * The size of the \p nonce buffer is too small.
2224 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2225 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2226 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002227 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002228 */
2229psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002230 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002231 size_t nonce_size,
2232 size_t *nonce_length);
2233
2234/** Set the nonce for an authenticated encryption or decryption operation.
2235 *
2236 * This function sets the nonce for the authenticated
2237 * encryption or decryption operation.
2238 *
2239 * The application must call psa_aead_encrypt_setup() before
2240 * calling this function.
2241 *
2242 * If this function returns an error status, the operation becomes inactive.
2243 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002244 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002245 * instead of this function, unless implementing a protocol that requires
2246 * a non-random IV.
2247 *
2248 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002249 * \param[in] nonce Buffer containing the nonce to use.
2250 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002251 *
2252 * \retval #PSA_SUCCESS
2253 * Success.
2254 * \retval #PSA_ERROR_BAD_STATE
2255 * The operation state is not valid (not set up, or nonce already set).
2256 * \retval #PSA_ERROR_INVALID_ARGUMENT
2257 * The size of \p nonce is not acceptable for the chosen algorithm.
2258 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2259 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2260 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002261 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002262 */
2263psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002264 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002265 size_t nonce_length);
2266
Gilles Peskinebc59c852019-01-17 15:26:08 +01002267/** Declare the lengths of the message and additional data for AEAD.
2268 *
2269 * The application must call this function before calling
2270 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2271 * the operation requires it. If the algorithm does not require it,
2272 * calling this function is optional, but if this function is called
2273 * then the implementation must enforce the lengths.
2274 *
2275 * You may call this function before or after setting the nonce with
2276 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2277 *
2278 * - For #PSA_ALG_CCM, calling this function is required.
2279 * - For the other AEAD algorithms defined in this specification, calling
2280 * this function is not required.
2281 * - For vendor-defined algorithm, refer to the vendor documentation.
2282 *
2283 * \param[in,out] operation Active AEAD operation.
2284 * \param ad_length Size of the non-encrypted additional
2285 * authenticated data in bytes.
2286 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2287 *
2288 * \retval #PSA_SUCCESS
2289 * Success.
2290 * \retval #PSA_ERROR_BAD_STATE
2291 * The operation state is not valid (not set up, already completed,
2292 * or psa_aead_update_ad() or psa_aead_update() already called).
2293 * \retval #PSA_ERROR_INVALID_ARGUMENT
2294 * At least one of the lengths is not acceptable for the chosen
2295 * algorithm.
2296 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2297 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2298 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002299 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinebc59c852019-01-17 15:26:08 +01002300 */
2301psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2302 size_t ad_length,
2303 size_t plaintext_length);
2304
Gilles Peskine30a9e412019-01-14 18:36:12 +01002305/** Pass additional data to an active AEAD operation.
2306 *
2307 * Additional data is authenticated, but not encrypted.
2308 *
2309 * You may call this function multiple times to pass successive fragments
2310 * of the additional data. You may not call this function after passing
2311 * data to encrypt or decrypt with psa_aead_update().
2312 *
2313 * Before calling this function, you must:
2314 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2315 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2316 *
2317 * If this function returns an error status, the operation becomes inactive.
2318 *
2319 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2320 * there is no guarantee that the input is valid. Therefore, until
2321 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2322 * treat the input as untrusted and prepare to undo any action that
2323 * depends on the input if psa_aead_verify() returns an error status.
2324 *
2325 * \param[in,out] operation Active AEAD operation.
2326 * \param[in] input Buffer containing the fragment of
2327 * additional data.
2328 * \param input_length Size of the \p input buffer in bytes.
2329 *
2330 * \retval #PSA_SUCCESS
2331 * Success.
2332 * \retval #PSA_ERROR_BAD_STATE
2333 * The operation state is not valid (not set up, nonce not set,
2334 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002335 * \retval #PSA_ERROR_INVALID_ARGUMENT
2336 * The total input length overflows the additional data length that
2337 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002338 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2339 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2340 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002341 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002342 */
2343psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2344 const uint8_t *input,
2345 size_t input_length);
2346
2347/** Encrypt or decrypt a message fragment in an active AEAD operation.
2348 *
2349 * Before calling this function, you must:
2350 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2351 * The choice of setup function determines whether this function
2352 * encrypts or decrypts its input.
2353 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2354 * 3. Call psa_aead_update_ad() to pass all the additional data.
2355 *
2356 * If this function returns an error status, the operation becomes inactive.
2357 *
2358 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2359 * there is no guarantee that the input is valid. Therefore, until
2360 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2361 * - Do not use the output in any way other than storing it in a
2362 * confidential location. If you take any action that depends
2363 * on the tentative decrypted data, this action will need to be
2364 * undone if the input turns out not to be valid. Furthermore,
2365 * if an adversary can observe that this action took place
2366 * (for example through timing), they may be able to use this
2367 * fact as an oracle to decrypt any message encrypted with the
2368 * same key.
2369 * - In particular, do not copy the output anywhere but to a
2370 * memory or storage space that you have exclusive access to.
2371 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002372 * This function does not require the input to be aligned to any
2373 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002374 * a whole block at a time, it must consume all the input provided, but
2375 * it may delay the end of the corresponding output until a subsequent
2376 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2377 * provides sufficient input. The amount of data that can be delayed
2378 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002379 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002380 * \param[in,out] operation Active AEAD operation.
2381 * \param[in] input Buffer containing the message fragment to
2382 * encrypt or decrypt.
2383 * \param input_length Size of the \p input buffer in bytes.
2384 * \param[out] output Buffer where the output is to be written.
2385 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002386 * This must be at least
2387 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2388 * \p input_length) where \c alg is the
2389 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002390 * \param[out] output_length On success, the number of bytes
2391 * that make up the returned output.
2392 *
2393 * \retval #PSA_SUCCESS
2394 * Success.
2395 * \retval #PSA_ERROR_BAD_STATE
2396 * The operation state is not valid (not set up, nonce not set
2397 * or already completed).
2398 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2399 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002400 * You can determine a sufficient buffer size by calling
2401 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2402 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002403 * \retval #PSA_ERROR_INVALID_ARGUMENT
2404 * The total length of input to psa_aead_update_ad() so far is
2405 * less than the additional data length that was previously
2406 * specified with psa_aead_set_lengths().
2407 * \retval #PSA_ERROR_INVALID_ARGUMENT
2408 * The total input length overflows the plaintext length that
2409 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002410 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2411 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2412 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002413 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002414 */
2415psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2416 const uint8_t *input,
2417 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002418 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002419 size_t output_size,
2420 size_t *output_length);
2421
2422/** Finish encrypting a message in an AEAD operation.
2423 *
2424 * The operation must have been set up with psa_aead_encrypt_setup().
2425 *
2426 * This function finishes the authentication of the additional data
2427 * formed by concatenating the inputs passed to preceding calls to
2428 * psa_aead_update_ad() with the plaintext formed by concatenating the
2429 * inputs passed to preceding calls to psa_aead_update().
2430 *
2431 * This function has two output buffers:
2432 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002433 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002434 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002435 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002436 * that the operation performs.
2437 *
2438 * When this function returns, the operation becomes inactive.
2439 *
2440 * \param[in,out] operation Active AEAD operation.
2441 * \param[out] ciphertext Buffer where the last part of the ciphertext
2442 * is to be written.
2443 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002444 * This must be at least
2445 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2446 * \c alg is the algorithm that is being
2447 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002448 * \param[out] ciphertext_length On success, the number of bytes of
2449 * returned ciphertext.
2450 * \param[out] tag Buffer where the authentication tag is
2451 * to be written.
2452 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002453 * This must be at least
2454 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2455 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002456 * \param[out] tag_length On success, the number of bytes
2457 * that make up the returned tag.
2458 *
2459 * \retval #PSA_SUCCESS
2460 * Success.
2461 * \retval #PSA_ERROR_BAD_STATE
2462 * The operation state is not valid (not set up, nonce not set,
2463 * decryption, or already completed).
2464 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002465 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002466 * You can determine a sufficient buffer size for \p ciphertext by
2467 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2468 * where \c alg is the algorithm that is being calculated.
2469 * You can determine a sufficient buffer size for \p tag by
2470 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002471 * \retval #PSA_ERROR_INVALID_ARGUMENT
2472 * The total length of input to psa_aead_update_ad() so far is
2473 * less than the additional data length that was previously
2474 * specified with psa_aead_set_lengths().
2475 * \retval #PSA_ERROR_INVALID_ARGUMENT
2476 * The total length of input to psa_aead_update() so far is
2477 * less than the plaintext length that was previously
2478 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002479 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2480 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2481 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002482 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002483 */
2484psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002485 uint8_t *ciphertext,
2486 size_t ciphertext_size,
2487 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002488 uint8_t *tag,
2489 size_t tag_size,
2490 size_t *tag_length);
2491
2492/** Finish authenticating and decrypting a message in an AEAD operation.
2493 *
2494 * The operation must have been set up with psa_aead_decrypt_setup().
2495 *
2496 * This function finishes the authentication of the additional data
2497 * formed by concatenating the inputs passed to preceding calls to
2498 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2499 * inputs passed to preceding calls to psa_aead_update().
2500 *
2501 * When this function returns, the operation becomes inactive.
2502 *
2503 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002504 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002505 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002506 * from previous calls to psa_aead_update()
2507 * that could not be processed until the end
2508 * of the input.
2509 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002510 * This must be at least
2511 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2512 * \c alg is the algorithm that is being
2513 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002514 * \param[out] plaintext_length On success, the number of bytes of
2515 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002516 * \param[in] tag Buffer containing the authentication tag.
2517 * \param tag_length Size of the \p tag buffer in bytes.
2518 *
2519 * \retval #PSA_SUCCESS
2520 * Success.
2521 * \retval #PSA_ERROR_BAD_STATE
2522 * The operation state is not valid (not set up, nonce not set,
2523 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002524 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2525 * The size of the \p plaintext buffer is too small.
2526 * You can determine a sufficient buffer size for \p plaintext by
2527 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2528 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002529 * \retval #PSA_ERROR_INVALID_ARGUMENT
2530 * The total length of input to psa_aead_update_ad() so far is
2531 * less than the additional data length that was previously
2532 * specified with psa_aead_set_lengths().
2533 * \retval #PSA_ERROR_INVALID_ARGUMENT
2534 * The total length of input to psa_aead_update() so far is
2535 * less than the plaintext length that was previously
2536 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002537 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2538 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2539 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002540 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002541 */
2542psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002543 uint8_t *plaintext,
2544 size_t plaintext_size,
2545 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002546 const uint8_t *tag,
2547 size_t tag_length);
2548
2549/** Abort an AEAD operation.
2550 *
2551 * Aborting an operation frees all associated resources except for the
2552 * \p operation structure itself. Once aborted, the operation object
2553 * can be reused for another operation by calling
2554 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2555 *
2556 * You may call this function any time after the operation object has
2557 * been initialized by any of the following methods:
2558 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2559 * whether it succeeds or not.
2560 * - Initializing the \c struct to all-bits-zero.
2561 * - Initializing the \c struct to logical zeros, e.g.
2562 * `psa_aead_operation_t operation = {0}`.
2563 *
2564 * In particular, calling psa_aead_abort() after the operation has been
2565 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2566 * is safe and has no effect.
2567 *
2568 * \param[in,out] operation Initialized AEAD operation.
2569 *
2570 * \retval #PSA_SUCCESS
2571 * \retval #PSA_ERROR_BAD_STATE
2572 * \p operation is not an active AEAD operation.
2573 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2574 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002575 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002576 */
2577psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2578
Gilles Peskine3b555712018-03-03 21:27:57 +01002579/**@}*/
2580
Gilles Peskine20035e32018-02-03 22:44:14 +01002581/** \defgroup asymmetric Asymmetric cryptography
2582 * @{
2583 */
2584
2585/**
2586 * \brief Sign a hash or short message with a private key.
2587 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002588 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002589 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002590 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2591 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2592 * to determine the hash algorithm to use.
2593 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002594 * \param handle Handle to the key to use for the operation.
2595 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002596 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002597 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002598 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002599 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002600 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002601 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002602 * \param[out] signature_length On success, the number of bytes
2603 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002604 *
Gilles Peskine28538492018-07-11 17:34:00 +02002605 * \retval #PSA_SUCCESS
2606 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002607 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002608 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002609 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002610 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002611 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002612 * \retval #PSA_ERROR_NOT_SUPPORTED
2613 * \retval #PSA_ERROR_INVALID_ARGUMENT
2614 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2615 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2616 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002617 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002618 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002619 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002620 * The library has not been previously initialized by psa_crypto_init().
2621 * It is implementation-dependent whether a failure to initialize
2622 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002623 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002624psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002625 psa_algorithm_t alg,
2626 const uint8_t *hash,
2627 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002628 uint8_t *signature,
2629 size_t signature_size,
2630 size_t *signature_length);
2631
2632/**
2633 * \brief Verify the signature a hash or short message using a public key.
2634 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002635 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002636 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002637 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2638 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2639 * to determine the hash algorithm to use.
2640 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002641 * \param handle Handle to the key to use for the operation.
2642 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002643 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002644 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002645 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002646 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002647 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002648 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002649 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002650 *
Gilles Peskine28538492018-07-11 17:34:00 +02002651 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002652 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002653 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002654 * The calculation was perfomed successfully, but the passed
2655 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002656 * \retval #PSA_ERROR_NOT_SUPPORTED
2657 * \retval #PSA_ERROR_INVALID_ARGUMENT
2658 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2659 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2660 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002661 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002662 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002663 * The library has not been previously initialized by psa_crypto_init().
2664 * It is implementation-dependent whether a failure to initialize
2665 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002666 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002667psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002668 psa_algorithm_t alg,
2669 const uint8_t *hash,
2670 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002671 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002672 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002673
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002674/**
2675 * \brief Encrypt a short message with a public key.
2676 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002677 * \param handle Handle to the key to use for the operation.
2678 * It must be a public key or an asymmetric
2679 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002680 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002681 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002682 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002683 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002684 * \param[in] salt A salt or label, if supported by the
2685 * encryption algorithm.
2686 * If the algorithm does not support a
2687 * salt, pass \c NULL.
2688 * If the algorithm supports an optional
2689 * salt and you do not want to pass a salt,
2690 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002691 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002692 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2693 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002694 * \param salt_length Size of the \p salt buffer in bytes.
2695 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002696 * \param[out] output Buffer where the encrypted message is to
2697 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002698 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002699 * \param[out] output_length On success, the number of bytes
2700 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002701 *
Gilles Peskine28538492018-07-11 17:34:00 +02002702 * \retval #PSA_SUCCESS
2703 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002704 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002705 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002706 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002707 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002708 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002709 * \retval #PSA_ERROR_NOT_SUPPORTED
2710 * \retval #PSA_ERROR_INVALID_ARGUMENT
2711 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2712 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2713 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002714 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002715 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002716 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002717 * The library has not been previously initialized by psa_crypto_init().
2718 * It is implementation-dependent whether a failure to initialize
2719 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002720 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002721psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002722 psa_algorithm_t alg,
2723 const uint8_t *input,
2724 size_t input_length,
2725 const uint8_t *salt,
2726 size_t salt_length,
2727 uint8_t *output,
2728 size_t output_size,
2729 size_t *output_length);
2730
2731/**
2732 * \brief Decrypt a short message with a private key.
2733 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002734 * \param handle Handle to the key to use for the operation.
2735 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002736 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002737 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002738 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002739 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002740 * \param[in] salt A salt or label, if supported by the
2741 * encryption algorithm.
2742 * If the algorithm does not support a
2743 * salt, pass \c NULL.
2744 * If the algorithm supports an optional
2745 * salt and you do not want to pass a salt,
2746 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002747 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002748 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2749 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002750 * \param salt_length Size of the \p salt buffer in bytes.
2751 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002752 * \param[out] output Buffer where the decrypted message is to
2753 * be written.
2754 * \param output_size Size of the \c output buffer in bytes.
2755 * \param[out] output_length On success, the number of bytes
2756 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002757 *
Gilles Peskine28538492018-07-11 17:34:00 +02002758 * \retval #PSA_SUCCESS
2759 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002760 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002761 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002762 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002763 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002764 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002765 * \retval #PSA_ERROR_NOT_SUPPORTED
2766 * \retval #PSA_ERROR_INVALID_ARGUMENT
2767 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2768 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2769 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002770 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002771 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2772 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002773 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002774 * The library has not been previously initialized by psa_crypto_init().
2775 * It is implementation-dependent whether a failure to initialize
2776 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002777 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002778psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002779 psa_algorithm_t alg,
2780 const uint8_t *input,
2781 size_t input_length,
2782 const uint8_t *salt,
2783 size_t salt_length,
2784 uint8_t *output,
2785 size_t output_size,
2786 size_t *output_length);
2787
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002788/**@}*/
2789
Gilles Peskine35675b62019-05-16 17:26:11 +02002790/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002791 * @{
2792 */
2793
Gilles Peskine35675b62019-05-16 17:26:11 +02002794/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002795 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002796 * Before calling any function on a key derivation operation object, the
2797 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002798 * - Set the structure to all-bits-zero, for example:
2799 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002800 * psa_key_derivation_operation_t operation;
2801 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002802 * \endcode
2803 * - Initialize the structure to logical zero values, for example:
2804 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002805 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002806 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002807 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002808 * for example:
2809 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002810 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002811 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002812 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002813 * to the structure, for example:
2814 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002815 * psa_key_derivation_operation_t operation;
2816 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002817 * \endcode
2818 *
2819 * This is an implementation-defined \c struct. Applications should not
2820 * make any assumptions about the content of this structure except
2821 * as directed by the documentation of a specific implementation.
2822 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002823typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002824
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002825/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002826 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002827 * This macro returns a suitable initializer for a key derivation operation
2828 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002829 */
2830#ifdef __DOXYGEN_ONLY__
2831/* This is an example definition for documentation purposes.
2832 * Implementations should define a suitable value in `crypto_struct.h`.
2833 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002834#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02002835#endif
2836
Gilles Peskine35675b62019-05-16 17:26:11 +02002837/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002838 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002839static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002840
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002841/** Set up a key derivation operation.
2842 *
2843 * A key derivation algorithm takes some inputs and uses them to generate
2844 * a byte stream in a deterministic way.
2845 * This byte stream can be used to produce keys and other
2846 * cryptographic material.
2847 *
2848 * To derive a key:
2849 * - Start with an initialized object of type #psa_key_derivation_operation_t.
2850 * - Call psa_key_derivation_setup() to select the algorithm.
2851 * - Provide the inputs for the key derivation by calling
2852 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2853 * as appropriate. Which inputs are needed, in what order, and whether
2854 * they may be keys and if so of what type depends on the algorithm.
2855 * - Optionally set the operation's maximum capacity with
2856 * psa_key_derivation_set_capacity(). You may do this before, in the middle
2857 * of or after providing inputs. For some algorithms, this step is mandatory
2858 * because the output depends on the maximum capacity.
2859 * - To derive a key, call psa_key_derivation_output_key().
2860 * To derive a byte string for a different purpose, call
2861 * - psa_key_derivation_output_bytes().
2862 * Successive calls to these functions use successive output bytes
2863 * calculated by the key derivation algorithm.
2864 * - Clean up the key derivation operation object with
2865 * psa_key_derivation_abort().
2866 *
2867 * \param[in,out] operation The key derivation operation object
2868 * to set up. It must
2869 * have been initialized but not set up yet.
2870 * \param alg The key derivation algorithm to compute
2871 * (\c PSA_ALG_XXX value such that
2872 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2873 *
2874 * \retval #PSA_SUCCESS
2875 * Success.
2876 * \retval #PSA_ERROR_INVALID_ARGUMENT
2877 * \c alg is not a key derivation algorithm.
2878 * \retval #PSA_ERROR_NOT_SUPPORTED
2879 * \c alg is not supported or is not a key derivation algorithm.
2880 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2881 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2882 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002883 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002884 * \retval #PSA_ERROR_BAD_STATE
2885 */
2886psa_status_t psa_key_derivation_setup(
2887 psa_key_derivation_operation_t *operation,
2888 psa_algorithm_t alg);
2889
Gilles Peskine35675b62019-05-16 17:26:11 +02002890/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002891 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002892 * The capacity of a key derivation is the maximum number of bytes that it can
2893 * return. When you get *N* bytes of output from a key derivation operation,
2894 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002895 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002896 * \param[in] operation The operation to query.
2897 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002898 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002899 * \retval #PSA_SUCCESS
2900 * \retval #PSA_ERROR_BAD_STATE
2901 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002902 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002903psa_status_t psa_key_derivation_get_capacity(
2904 const psa_key_derivation_operation_t *operation,
2905 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002906
Gilles Peskine35675b62019-05-16 17:26:11 +02002907/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002908 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002909 * The capacity of a key derivation operation is the maximum number of bytes
2910 * that the key derivation operation can return from this point onwards.
2911 *
2912 * \param[in,out] operation The key derivation operation object to modify.
2913 * \param capacity The new capacity of the operation.
2914 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002915 * current capacity.
2916 *
2917 * \retval #PSA_SUCCESS
2918 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02002919 * \p capacity is larger than the operation's current capacity.
2920 * In this case, the operation object remains valid and its capacity
2921 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002922 * \retval #PSA_ERROR_BAD_STATE
2923 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2924 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002925psa_status_t psa_key_derivation_set_capacity(
2926 psa_key_derivation_operation_t *operation,
2927 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002928
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002929/** Use the maximum possible capacity for a key derivation operation.
2930 *
2931 * Use this value as the capacity argument when setting up a key derivation
2932 * to indicate that the operation should have the maximum possible capacity.
2933 * The value of the maximum possible capacity depends on the key derivation
2934 * algorithm.
2935 */
2936#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
2937
2938/** Provide an input for key derivation or key agreement.
2939 *
2940 * Which inputs are required and in what order depends on the algorithm.
2941 * Refer to the documentation of each key derivation or key agreement
2942 * algorithm for information.
2943 *
2944 * This function passes direct inputs. Some inputs must be passed as keys
2945 * using psa_key_derivation_input_key() instead of this function. Refer to
2946 * the documentation of individual step types for information.
2947 *
2948 * \param[in,out] operation The key derivation operation object to use.
2949 * It must have been set up with
2950 * psa_key_derivation_setup() and must not
2951 * have produced any output yet.
2952 * \param step Which step the input data is for.
2953 * \param[in] data Input data to use.
2954 * \param data_length Size of the \p data buffer in bytes.
2955 *
2956 * \retval #PSA_SUCCESS
2957 * Success.
2958 * \retval #PSA_ERROR_INVALID_ARGUMENT
2959 * \c step is not compatible with the operation's algorithm.
2960 * \retval #PSA_ERROR_INVALID_ARGUMENT
2961 * \c step does not allow direct inputs.
2962 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2963 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2964 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002965 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002966 * \retval #PSA_ERROR_BAD_STATE
2967 * The value of \p step is not valid given the state of \p operation.
2968 * \retval #PSA_ERROR_BAD_STATE
2969 * The library has not been previously initialized by psa_crypto_init().
2970 * It is implementation-dependent whether a failure to initialize
2971 * results in this error code.
2972 */
2973psa_status_t psa_key_derivation_input_bytes(
2974 psa_key_derivation_operation_t *operation,
2975 psa_key_derivation_step_t step,
2976 const uint8_t *data,
2977 size_t data_length);
2978
2979/** Provide an input for key derivation in the form of a key.
2980 *
2981 * Which inputs are required and in what order depends on the algorithm.
2982 * Refer to the documentation of each key derivation or key agreement
2983 * algorithm for information.
2984 *
2985 * This function passes key inputs. Some inputs must be passed as keys
2986 * of the appropriate type using this function, while others must be
2987 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
2988 * the documentation of individual step types for information.
2989 *
2990 * \param[in,out] operation The key derivation operation object to use.
2991 * It must have been set up with
2992 * psa_key_derivation_setup() and must not
2993 * have produced any output yet.
2994 * \param step Which step the input data is for.
2995 * \param handle Handle to the key. It must have an
2996 * appropriate type for \p step and must
2997 * allow the usage #PSA_KEY_USAGE_DERIVE.
2998 *
2999 * \retval #PSA_SUCCESS
3000 * Success.
3001 * \retval #PSA_ERROR_INVALID_HANDLE
3002 * \retval #PSA_ERROR_DOES_NOT_EXIST
3003 * \retval #PSA_ERROR_NOT_PERMITTED
3004 * \retval #PSA_ERROR_INVALID_ARGUMENT
3005 * \c step is not compatible with the operation's algorithm.
3006 * \retval #PSA_ERROR_INVALID_ARGUMENT
3007 * \c step does not allow key inputs.
3008 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3009 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3010 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003011 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003012 * \retval #PSA_ERROR_BAD_STATE
3013 * The value of \p step is not valid given the state of \p operation.
3014 * \retval #PSA_ERROR_BAD_STATE
3015 * The library has not been previously initialized by psa_crypto_init().
3016 * It is implementation-dependent whether a failure to initialize
3017 * results in this error code.
3018 */
3019psa_status_t psa_key_derivation_input_key(
3020 psa_key_derivation_operation_t *operation,
3021 psa_key_derivation_step_t step,
3022 psa_key_handle_t handle);
3023
3024/** Perform a key agreement and use the shared secret as input to a key
3025 * derivation.
3026 *
3027 * A key agreement algorithm takes two inputs: a private key \p private_key
3028 * a public key \p peer_key.
3029 * The result of this function is passed as input to a key derivation.
3030 * The output of this key derivation can be extracted by reading from the
3031 * resulting operation to produce keys and other cryptographic material.
3032 *
3033 * \param[in,out] operation The key derivation operation object to use.
3034 * It must have been set up with
3035 * psa_key_derivation_setup() with a
3036 * key agreement and derivation algorithm
3037 * \c alg (\c PSA_ALG_XXX value such that
3038 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3039 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3040 * is false).
3041 * The operation must be ready for an
3042 * input of the type given by \p step.
3043 * \param step Which step the input data is for.
3044 * \param private_key Handle to the private key to use.
3045 * \param[in] peer_key Public key of the peer. The peer key must be in the
3046 * same format that psa_import_key() accepts for the
3047 * public key type corresponding to the type of
3048 * private_key. That is, this function performs the
3049 * equivalent of
3050 * #psa_import_key(...,
3051 * `peer_key`, `peer_key_length`) where
3052 * with key attributes indicating the public key
3053 * type corresponding to the type of `private_key`.
3054 * For example, for EC keys, this means that peer_key
3055 * is interpreted as a point on the curve that the
3056 * private key is on. The standard formats for public
3057 * keys are documented in the documentation of
3058 * psa_export_public_key().
3059 * \param peer_key_length Size of \p peer_key in bytes.
3060 *
3061 * \retval #PSA_SUCCESS
3062 * Success.
3063 * \retval #PSA_ERROR_INVALID_HANDLE
3064 * \retval #PSA_ERROR_DOES_NOT_EXIST
3065 * \retval #PSA_ERROR_NOT_PERMITTED
3066 * \retval #PSA_ERROR_INVALID_ARGUMENT
3067 * \c private_key is not compatible with \c alg,
3068 * or \p peer_key is not valid for \c alg or not compatible with
3069 * \c private_key.
3070 * \retval #PSA_ERROR_NOT_SUPPORTED
3071 * \c alg is not supported or is not a key derivation algorithm.
3072 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3073 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3074 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003075 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003076 */
3077psa_status_t psa_key_derivation_key_agreement(
3078 psa_key_derivation_operation_t *operation,
3079 psa_key_derivation_step_t step,
3080 psa_key_handle_t private_key,
3081 const uint8_t *peer_key,
3082 size_t peer_key_length);
3083
Gilles Peskine35675b62019-05-16 17:26:11 +02003084/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003085 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003086 * This function calculates output bytes from a key derivation algorithm and
3087 * return those bytes.
3088 * If you view the key derivation's output as a stream of bytes, this
3089 * function destructively reads the requested number of bytes from the
3090 * stream.
3091 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003092 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003093 * \param[in,out] operation The key derivation operation object to read from.
3094 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003095 * \param output_length Number of bytes to output.
3096 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003097 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003098 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003099 * The operation's capacity was less than
3100 * \p output_length bytes. Note that in this case,
3101 * no output is written to the output buffer.
3102 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003103 * subsequent calls to this function will not
3104 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003105 * \retval #PSA_ERROR_BAD_STATE
3106 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3107 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3108 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003109 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003110 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003111psa_status_t psa_key_derivation_output_bytes(
3112 psa_key_derivation_operation_t *operation,
3113 uint8_t *output,
3114 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003115
Gilles Peskine35675b62019-05-16 17:26:11 +02003116/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003117 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003118 * This function calculates output bytes from a key derivation algorithm
3119 * and uses those bytes to generate a key deterministically.
3120 * If you view the key derivation's output as a stream of bytes, this
3121 * function destructively reads as many bytes as required from the
3122 * stream.
3123 * The operation's capacity decreases by the number of bytes read.
3124 *
3125 * How much output is produced and consumed from the operation, and how
3126 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003127 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003128 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003129 * of a given size, this function is functionally equivalent to
3130 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003131 * and passing the resulting output to #psa_import_key.
3132 * However, this function has a security benefit:
3133 * if the implementation provides an isolation boundary then
3134 * the key material is not exposed outside the isolation boundary.
3135 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003136 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003137 * The following key types defined in this specification follow this scheme:
3138 *
3139 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003140 * - #PSA_KEY_TYPE_ARC4;
3141 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003142 * - #PSA_KEY_TYPE_DERIVE;
3143 * - #PSA_KEY_TYPE_HMAC.
3144 *
3145 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003146 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003147 * Montgomery curve), this function always draws a byte string whose
3148 * length is determined by the curve, and sets the mandatory bits
3149 * accordingly. That is:
3150 *
3151 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3152 * and process it as specified in RFC 7748 &sect;5.
3153 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3154 * and process it as specified in RFC 7748 &sect;5.
3155 *
3156 * - For key types for which the key is represented by a single sequence of
3157 * \p bits bits with constraints as to which bit sequences are acceptable,
3158 * this function draws a byte string of length (\p bits / 8) bytes rounded
3159 * up to the nearest whole number of bytes. If the resulting byte string
3160 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3161 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003162 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003163 * for the output produced by psa_export_key().
3164 * The following key types defined in this specification follow this scheme:
3165 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003166 * - #PSA_KEY_TYPE_DES.
3167 * Force-set the parity bits, but discard forbidden weak keys.
3168 * For 2-key and 3-key triple-DES, the three keys are generated
3169 * successively (for example, for 3-key triple-DES,
3170 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3171 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003172 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003173 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003174 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003175 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003176 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003177 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003178 * Weierstrass curve).
3179 * For these key types, interpret the byte string as integer
3180 * in big-endian order. Discard it if it is not in the range
3181 * [0, *N* - 2] where *N* is the boundary of the private key domain
3182 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003183 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003184 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003185 * This method allows compliance to NIST standards, specifically
3186 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003187 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3188 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3189 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3190 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003191 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003192 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003193 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003194 * implementation-defined.
3195 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003196 * In all cases, the data that is read is discarded from the operation.
3197 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003198 *
Gilles Peskine20628592019-04-19 19:29:50 +02003199 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003200 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003201 * \param[out] handle On success, a handle to the newly created key.
3202 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003203 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003204 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003205 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003206 * If the key is persistent, the key material and the key's metadata
3207 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003208 * \retval #PSA_ERROR_ALREADY_EXISTS
3209 * This is an attempt to create a persistent key, and there is
3210 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003211 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003212 * There was not enough data to create the desired key.
3213 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003214 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003215 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003216 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003217 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003218 * implementation in general or in this particular location.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003219 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003220 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3221 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3222 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3223 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003224 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003225 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003226 * The library has not been previously initialized by psa_crypto_init().
3227 * It is implementation-dependent whether a failure to initialize
3228 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003229 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003230psa_status_t psa_key_derivation_output_key(
3231 const psa_key_attributes_t *attributes,
3232 psa_key_derivation_operation_t *operation,
3233 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003234
Gilles Peskine35675b62019-05-16 17:26:11 +02003235/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003236 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003237 * Once a key derivation operation has been aborted, its capacity is zero.
3238 * Aborting an operation frees all associated resources except for the
3239 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003240 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003241 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003242 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003243 * psa_key_derivation_operation_init() or a zero value. In particular,
3244 * it is valid to call psa_key_derivation_abort() twice, or to call
3245 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003246 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003247 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003248 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003249 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003250 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003251 * \retval #PSA_SUCCESS
3252 * \retval #PSA_ERROR_BAD_STATE
3253 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3254 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003255 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003256 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003257psa_status_t psa_key_derivation_abort(
3258 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003259
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003260/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003261 *
3262 * \warning The raw result of a key agreement algorithm such as finite-field
3263 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3264 * not be used directly as key material. It should instead be passed as
3265 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003266 * a key derivation, use psa_key_derivation_key_agreement() and other
3267 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003268 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003269 * \param alg The key agreement algorithm to compute
3270 * (\c PSA_ALG_XXX value such that
3271 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3272 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003273 * \param private_key Handle to the private key to use.
3274 * \param[in] peer_key Public key of the peer. It must be
3275 * in the same format that psa_import_key()
3276 * accepts. The standard formats for public
3277 * keys are documented in the documentation
3278 * of psa_export_public_key().
3279 * \param peer_key_length Size of \p peer_key in bytes.
3280 * \param[out] output Buffer where the decrypted message is to
3281 * be written.
3282 * \param output_size Size of the \c output buffer in bytes.
3283 * \param[out] output_length On success, the number of bytes
3284 * that make up the returned output.
3285 *
3286 * \retval #PSA_SUCCESS
3287 * Success.
3288 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003289 * \retval #PSA_ERROR_NOT_PERMITTED
3290 * \retval #PSA_ERROR_INVALID_ARGUMENT
3291 * \p alg is not a key agreement algorithm
3292 * \retval #PSA_ERROR_INVALID_ARGUMENT
3293 * \p private_key is not compatible with \p alg,
3294 * or \p peer_key is not valid for \p alg or not compatible with
3295 * \p private_key.
3296 * \retval #PSA_ERROR_NOT_SUPPORTED
3297 * \p alg is not a supported key agreement algorithm.
3298 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3299 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3300 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003301 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine769c7a62019-01-18 16:42:29 +01003302 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003303psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3304 psa_key_handle_t private_key,
3305 const uint8_t *peer_key,
3306 size_t peer_key_length,
3307 uint8_t *output,
3308 size_t output_size,
3309 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003310
Gilles Peskineea0fb492018-07-12 17:17:20 +02003311/**@}*/
3312
Gilles Peskineedd76872018-07-20 17:42:05 +02003313/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003314 * @{
3315 */
3316
3317/**
3318 * \brief Generate random bytes.
3319 *
3320 * \warning This function **can** fail! Callers MUST check the return status
3321 * and MUST NOT use the content of the output buffer if the return
3322 * status is not #PSA_SUCCESS.
3323 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003324 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003325 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003326 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003327 * \param output_size Number of bytes to generate and output.
3328 *
Gilles Peskine28538492018-07-11 17:34:00 +02003329 * \retval #PSA_SUCCESS
3330 * \retval #PSA_ERROR_NOT_SUPPORTED
3331 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3332 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3333 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003334 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003335 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003336 * The library has not been previously initialized by psa_crypto_init().
3337 * It is implementation-dependent whether a failure to initialize
3338 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003339 */
3340psa_status_t psa_generate_random(uint8_t *output,
3341 size_t output_size);
3342
3343/**
3344 * \brief Generate a key or key pair.
3345 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003346 * The key is generated randomly.
3347 * Its location, policy, type and size are taken from \p attributes.
3348 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003349 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003350 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003351 * the public exponent is 65537.
3352 * The modulus is a product of two probabilistic primes
3353 * between 2^{n-1} and 2^n where n is the bit size specified in the
3354 * attributes.
3355 *
Gilles Peskine20628592019-04-19 19:29:50 +02003356 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003357 * \param[out] handle On success, a handle to the newly created key.
3358 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003359 *
Gilles Peskine28538492018-07-11 17:34:00 +02003360 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003361 * Success.
3362 * If the key is persistent, the key material and the key's metadata
3363 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003364 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003365 * This is an attempt to create a persistent key, and there is
3366 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003367 * \retval #PSA_ERROR_NOT_SUPPORTED
3368 * \retval #PSA_ERROR_INVALID_ARGUMENT
3369 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3370 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3371 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3372 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003373 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003374 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003375 * The library has not been previously initialized by psa_crypto_init().
3376 * It is implementation-dependent whether a failure to initialize
3377 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003378 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003379psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003380 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003381
3382/**@}*/
3383
Gilles Peskinee59236f2018-01-27 23:32:46 +01003384#ifdef __cplusplus
3385}
3386#endif
3387
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003388/* The file "crypto_sizes.h" contains definitions for size calculation
3389 * macros whose definitions are implementation-specific. */
3390#include "crypto_sizes.h"
3391
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003392/* The file "crypto_struct.h" contains definitions for
3393 * implementation-specific structs that are declared above. */
3394#include "crypto_struct.h"
3395
3396/* The file "crypto_extra.h" contains vendor-specific definitions. This
3397 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003398#include "crypto_extra.h"
3399
3400#endif /* PSA_CRYPTO_H */