blob: e8d37a7b91f8dee9a06ac448413c897c4868a96f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +020089 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020090 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine105f67f2019-07-23 18:16:05 +020096/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +020097 * @{
98 */
99
Gilles Peskinea0c06552019-05-21 15:54:54 +0200100/** \def PSA_KEY_ATTRIBUTES_INIT
101 *
102 * This macro returns a suitable initializer for a key attribute structure
103 * of type #psa_key_attributes_t.
104 */
105#ifdef __DOXYGEN_ONLY__
106/* This is an example definition for documentation purposes.
107 * Implementations should define a suitable value in `crypto_struct.h`.
108 */
109#define PSA_KEY_ATTRIBUTES_INIT {0}
110#endif
111
112/** Return an initial value for a key attributes structure.
113 */
114static psa_key_attributes_t psa_key_attributes_init(void);
115
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200116/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200117 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200118 * If the attribute structure currently declares the key as volatile (which
119 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200120 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200121 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200122 * This function does not access storage, it merely stores the given
123 * value in the structure.
124 * The persistent key will be written to storage when the attribute
125 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200126 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200127 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200128 *
Gilles Peskine20628592019-04-19 19:29:50 +0200129 * This function may be declared as `static` (i.e. without external
130 * linkage). This function may be provided as a function-like macro,
131 * but in this case it must evaluate each of its arguments exactly once.
132 *
133 * \param[out] attributes The attribute structure to write to.
134 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200135 */
136static void psa_set_key_id(psa_key_attributes_t *attributes,
137 psa_key_id_t id);
138
139/** Set the location of a persistent key.
140 *
141 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200142 * with psa_set_key_id(). By default, a key that has a persistent identifier
143 * is stored in the default storage area identifier by
144 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
145 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200146 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200147 * This function does not access storage, it merely stores the given
148 * value in the structure.
149 * The persistent key will be written to storage when the attribute
150 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200151 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200152 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200153 *
154 * This function may be declared as `static` (i.e. without external
155 * linkage). This function may be provided as a function-like macro,
156 * but in this case it must evaluate each of its arguments exactly once.
157 *
158 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200159 * \param lifetime The lifetime for the key.
160 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200161 * key will be volatile, and the key identifier
162 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200163 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200164static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
165 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200166
Gilles Peskine20628592019-04-19 19:29:50 +0200167/** Retrieve the key identifier from key attributes.
168 *
169 * This function may be declared as `static` (i.e. without external
170 * linkage). This function may be provided as a function-like macro,
171 * but in this case it must evaluate its argument exactly once.
172 *
173 * \param[in] attributes The key attribute structure to query.
174 *
175 * \return The persistent identifier stored in the attribute structure.
176 * This value is unspecified if the attribute structure declares
177 * the key as volatile.
178 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200179static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
180
Gilles Peskine20628592019-04-19 19:29:50 +0200181/** Retrieve the lifetime from key attributes.
182 *
183 * This function may be declared as `static` (i.e. without external
184 * linkage). This function may be provided as a function-like macro,
185 * but in this case it must evaluate its argument exactly once.
186 *
187 * \param[in] attributes The key attribute structure to query.
188 *
189 * \return The lifetime value stored in the attribute structure.
190 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200191static psa_key_lifetime_t psa_get_key_lifetime(
192 const psa_key_attributes_t *attributes);
193
Gilles Peskine20628592019-04-19 19:29:50 +0200194/** Declare usage flags for a key.
195 *
196 * Usage flags are part of a key's usage policy. They encode what
197 * kind of operations are permitted on the key. For more details,
198 * refer to the documentation of the type #psa_key_usage_t.
199 *
200 * This function overwrites any usage flags
201 * previously set in \p attributes.
202 *
203 * This function may be declared as `static` (i.e. without external
204 * linkage). This function may be provided as a function-like macro,
205 * but in this case it must evaluate each of its arguments exactly once.
206 *
207 * \param[out] attributes The attribute structure to write to.
208 * \param usage_flags The usage flags to write.
209 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200210static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
211 psa_key_usage_t usage_flags);
212
Gilles Peskine20628592019-04-19 19:29:50 +0200213/** Retrieve the usage flags from key attributes.
214 *
215 * This function may be declared as `static` (i.e. without external
216 * linkage). This function may be provided as a function-like macro,
217 * but in this case it must evaluate its argument exactly once.
218 *
219 * \param[in] attributes The key attribute structure to query.
220 *
221 * \return The usage flags stored in the attribute structure.
222 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200223static psa_key_usage_t psa_get_key_usage_flags(
224 const psa_key_attributes_t *attributes);
225
Gilles Peskine20628592019-04-19 19:29:50 +0200226/** Declare the permitted algorithm policy for a key.
227 *
228 * The permitted algorithm policy of a key encodes which algorithm or
229 * algorithms are permitted to be used with this key.
230 *
231 * This function overwrites any algorithm policy
232 * previously set in \p attributes.
233 *
234 * This function may be declared as `static` (i.e. without external
235 * linkage). This function may be provided as a function-like macro,
236 * but in this case it must evaluate each of its arguments exactly once.
237 *
238 * \param[out] attributes The attribute structure to write to.
239 * \param alg The permitted algorithm policy to write.
240 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200241static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
242 psa_algorithm_t alg);
243
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100244
Gilles Peskine20628592019-04-19 19:29:50 +0200245/** Retrieve the algorithm policy from key attributes.
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate its argument exactly once.
250 *
251 * \param[in] attributes The key attribute structure to query.
252 *
253 * \return The algorithm stored in the attribute structure.
254 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200255static psa_algorithm_t psa_get_key_algorithm(
256 const psa_key_attributes_t *attributes);
257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Declare the type of a key.
259 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200260 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200261 * previously set in \p attributes.
262 *
263 * This function may be declared as `static` (i.e. without external
264 * linkage). This function may be provided as a function-like macro,
265 * but in this case it must evaluate each of its arguments exactly once.
266 *
267 * \param[out] attributes The attribute structure to write to.
268 * \param type The key type to write.
269 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200270static void psa_set_key_type(psa_key_attributes_t *attributes,
271 psa_key_type_t type);
272
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100273
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200274/** Declare the size of a key.
275 *
276 * This function overwrites any key size previously set in \p attributes.
277 *
278 * This function may be declared as `static` (i.e. without external
279 * linkage). This function may be provided as a function-like macro,
280 * but in this case it must evaluate each of its arguments exactly once.
281 *
282 * \param[out] attributes The attribute structure to write to.
283 * \param bits The key size in bits.
284 */
285static void psa_set_key_bits(psa_key_attributes_t *attributes,
286 size_t bits);
287
Gilles Peskine20628592019-04-19 19:29:50 +0200288/** Retrieve the key type from key attributes.
289 *
290 * This function may be declared as `static` (i.e. without external
291 * linkage). This function may be provided as a function-like macro,
292 * but in this case it must evaluate its argument exactly once.
293 *
294 * \param[in] attributes The key attribute structure to query.
295 *
296 * \return The key type stored in the attribute structure.
297 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200298static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
299
Gilles Peskine20628592019-04-19 19:29:50 +0200300/** Retrieve the key size from key attributes.
301 *
302 * This function may be declared as `static` (i.e. without external
303 * linkage). This function may be provided as a function-like macro,
304 * but in this case it must evaluate its argument exactly once.
305 *
306 * \param[in] attributes The key attribute structure to query.
307 *
308 * \return The key size stored in the attribute structure, in bits.
309 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200310static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
311
Gilles Peskine20628592019-04-19 19:29:50 +0200312/** Retrieve the attributes of a key.
313 *
314 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200315 * psa_reset_key_attributes(). It then copies the attributes of
316 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200317 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200318 * \note This function may allocate memory or other resources.
319 * Once you have called this function on an attribute structure,
320 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200321 *
Gilles Peskine20628592019-04-19 19:29:50 +0200322 * \param[in] handle Handle to the key to query.
323 * \param[in,out] attributes On success, the attributes of the key.
324 * On failure, equivalent to a
325 * freshly-initialized structure.
326 *
327 * \retval #PSA_SUCCESS
328 * \retval #PSA_ERROR_INVALID_HANDLE
329 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
330 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100331 * \retval #PSA_ERROR_CORRUPTION_DETECTED
332 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100333 * \retval #PSA_ERROR_BAD_STATE
334 * The library has not been previously initialized by psa_crypto_init().
335 * It is implementation-dependent whether a failure to initialize
336 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200337 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200338psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
339 psa_key_attributes_t *attributes);
340
Gilles Peskine20628592019-04-19 19:29:50 +0200341/** Reset a key attribute structure to a freshly initialized state.
342 *
343 * You must initialize the attribute structure as described in the
344 * documentation of the type #psa_key_attributes_t before calling this
345 * function. Once the structure has been initialized, you may call this
346 * function at any time.
347 *
348 * This function frees any auxiliary resources that the structure
349 * may contain.
350 *
351 * \param[in,out] attributes The attribute structure to reset.
352 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200353void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200354
Gilles Peskine87a5e562019-04-17 12:28:25 +0200355/**@}*/
356
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357/** \defgroup key_management Key management
358 * @{
359 */
360
Gilles Peskinef535eb22018-11-30 14:08:36 +0100361/** Open a handle to an existing persistent key.
362 *
Gilles Peskine4754cde2019-05-21 15:56:29 +0200363 * Open a handle to a persistent key. A key is persistent if it was created
364 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
365 * always has a nonzero key identifier, set with psa_set_key_id() when
366 * creating the key. Implementations may provide additional pre-provisioned
Andrew Thoelke203491c2019-08-21 17:55:30 +0100367 * keys that can be opened with psa_open_key(). Such keys have a key identifier
368 * in the vendor range, as documented in the description of #psa_key_id_t.
Gilles Peskine4754cde2019-05-21 15:56:29 +0200369 *
370 * The application must eventually close the handle with psa_close_key()
371 * to release associated resources. If the application dies without calling
372 * psa_close_key(), the implementation should perform the equivalent of a
373 * call to psa_close_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100374 *
Andrew Thoelke9741b112019-08-21 18:20:41 +0100375 * Some implementations permit an application to open the same key multiple
376 * times. Applications that rely on this behavior will not be portable to
377 * implementations that only permit a single key handle to be opened. See
378 * also :ref:\`key-handles\`.
379 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100380 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100381 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100382 *
383 * \retval #PSA_SUCCESS
384 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100385 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100386 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100387 * The implementation does not have sufficient resources to open the
388 * key. This can be due to reaching an implementation limit on the
389 * number of open keys, the number of open key handles, or available
390 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200391 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100392 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100393 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100394 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100395 * \retval #PSA_ERROR_NOT_PERMITTED
396 * The specified key exists, but the application does not have the
397 * permission to access it. Note that this specification does not
398 * define any way to create such a key, but it may be possible
399 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200400 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawd789dc12019-08-12 15:06:48 +0100401 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine225010f2019-05-06 18:44:55 +0200402 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100403 * \retval #PSA_ERROR_BAD_STATE
404 * The library has not been previously initialized by psa_crypto_init().
405 * It is implementation-dependent whether a failure to initialize
406 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100407 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200408psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100409 psa_key_handle_t *handle);
410
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100411
Gilles Peskinef535eb22018-11-30 14:08:36 +0100412/** Close a key handle.
413 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100414 * If the handle designates a volatile key, this will destroy the key material
415 * and free all associated resources, just like psa_destroy_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100416 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100417 * If this is the last open handle to a persistent key, then closing the handle
418 * will free all resources associated with the key in volatile memory. The key
419 * data in persistent storage is not affected and can be opened again later
420 * with a call to psa_open_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100421 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100422 * Closing the key handle makes the handle invalid, and the key handle
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100423 * must not be used again by the application.
Andrew Thoelke3daba812019-08-21 22:46:56 +0100424 *
425 * If the key is currently in use in a multipart operation, then closing the
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100426 * last remaining handle to the key will abort the multipart operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100427 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100428 * \param handle The key handle to close.
429 *
430 * \retval #PSA_SUCCESS
431 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100432 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawf97c8522019-08-15 13:27:12 +0100433 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100434 * \retval #PSA_ERROR_BAD_STATE
435 * The library has not been previously initialized by psa_crypto_init().
436 * It is implementation-dependent whether a failure to initialize
437 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100438 */
439psa_status_t psa_close_key(psa_key_handle_t handle);
440
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100441/**@}*/
442
443/** \defgroup import_export Key import and export
444 * @{
445 */
446
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100447/**
448 * \brief Import a key in binary format.
449 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100450 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100451 * documentation of psa_export_public_key() for the format of public keys
452 * and to the documentation of psa_export_key() for the format for
453 * other key types.
454 *
455 * This specification supports a single format for each key type.
456 * Implementations may support other formats as long as the standard
457 * format is supported. Implementations that support other formats
458 * should ensure that the formats are clearly unambiguous so as to
459 * minimize the risk that an invalid input is accidentally interpreted
460 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100461 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100462
Gilles Peskine20628592019-04-19 19:29:50 +0200463 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200464 * The key size is always determined from the
465 * \p data buffer.
466 * If the key size in \p attributes is nonzero,
467 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200468 * \param[out] handle On success, a handle to the newly created key.
469 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100470 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200471 * buffer is interpreted according to the type declared
472 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200473 * All implementations must support at least the format
474 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100475 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200476 * the chosen type. Implementations may allow other
477 * formats, but should be conservative: implementations
478 * should err on the side of rejecting content if it
479 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200480 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100481 *
Gilles Peskine28538492018-07-11 17:34:00 +0200482 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100483 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100484 * If the key is persistent, the key material and the key's metadata
485 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200486 * \retval #PSA_ERROR_ALREADY_EXISTS
487 * This is an attempt to create a persistent key, and there is
488 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200489 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200490 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200491 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200492 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200493 * The key attributes, as a whole, are invalid.
494 * \retval #PSA_ERROR_INVALID_ARGUMENT
495 * The key data is not correctly formatted.
496 * \retval #PSA_ERROR_INVALID_ARGUMENT
497 * The size in \p attributes is nonzero and does not match the size
498 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200499 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
500 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
501 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100502 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200503 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200504 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300505 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw97d3bc32019-08-08 14:40:04 +0100506 * \p operation is either not initialized or is in use
507 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300508 * The library has not been previously initialized by psa_crypto_init().
509 * It is implementation-dependent whether a failure to initialize
510 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100511 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200512psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100513 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200514 size_t data_length,
515 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100516
517/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100518 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200519 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100520 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200521 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100522 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200523 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100524 * This function also erases any metadata such as policies and frees all
525 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200526 *
Andrew Thoelke07f16b72019-08-21 22:48:47 +0100527 * Destroying a key will invalidate all existing handles to the key.
528 *
529 * If the key is currently in use in a multipart operation, then destroying the
530 * key will abort the multipart operation.
531 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100532 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100533 *
Gilles Peskine28538492018-07-11 17:34:00 +0200534 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100535 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200536 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100537 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200538 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100539 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200540 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200541 * There was an failure in communication with the cryptoprocessor.
542 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200543 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200544 * The storage is corrupted. Implementations shall make a best effort
545 * to erase key material even in this stage, however applications
546 * should be aware that it may be impossible to guarantee that the
547 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200548 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200549 * An unexpected condition which is not a storage corruption or
550 * a communication failure occurred. The cryptoprocessor may have
551 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300552 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300553 * The library has not been previously initialized by psa_crypto_init().
554 * It is implementation-dependent whether a failure to initialize
555 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100557psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100558
559/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100560 * \brief Export a key in binary format.
561 *
562 * The output of this function can be passed to psa_import_key() to
563 * create an equivalent object.
564 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100565 * If the implementation of psa_import_key() supports other formats
566 * beyond the format specified here, the output from psa_export_key()
567 * must use the representation specified here, not the original
568 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100569 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100570 * For standard key types, the output format is as follows:
571 *
572 * - For symmetric keys (including MAC keys), the format is the
573 * raw bytes of the key.
574 * - For DES, the key data consists of 8 bytes. The parity bits must be
575 * correct.
576 * - For Triple-DES, the format is the concatenation of the
577 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200578 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200579 * is the non-encrypted DER encoding of the representation defined by
580 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
581 * ```
582 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200583 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200584 * modulus INTEGER, -- n
585 * publicExponent INTEGER, -- e
586 * privateExponent INTEGER, -- d
587 * prime1 INTEGER, -- p
588 * prime2 INTEGER, -- q
589 * exponent1 INTEGER, -- d mod (p-1)
590 * exponent2 INTEGER, -- d mod (q-1)
591 * coefficient INTEGER, -- (inverse of q) mod p
592 * }
593 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200594 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200595 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100596 * a representation of the private value as a `ceiling(m/8)`-byte string
597 * where `m` is the bit size associated with the curve, i.e. the bit size
598 * of the order of the curve's coordinate field. This byte string is
599 * in little-endian order for Montgomery curves (curve types
600 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
601 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
602 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100603 * This is the content of the `privateKey` field of the `ECPrivateKey`
604 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200605 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200606 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000607 * format is the representation of the private key `x` as a big-endian byte
608 * string. The length of the byte string is the private key size in bytes
609 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200610 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
611 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100612 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200613 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
614 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100615 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200616 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200617 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200618 * \param[out] data_length On success, the number of bytes
619 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100620 *
Gilles Peskine28538492018-07-11 17:34:00 +0200621 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100622 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200623 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200624 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100625 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200626 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
627 * The size of the \p data buffer is too small. You can determine a
628 * sufficient buffer size by calling
629 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
630 * where \c type is the key type
631 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200632 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
633 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200634 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100635 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100636 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300637 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300638 * The library has not been previously initialized by psa_crypto_init().
639 * It is implementation-dependent whether a failure to initialize
640 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100641 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100642psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100643 uint8_t *data,
644 size_t data_size,
645 size_t *data_length);
646
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100647/**
648 * \brief Export a public key or the public part of a key pair in binary format.
649 *
650 * The output of this function can be passed to psa_import_key() to
651 * create an object that is equivalent to the public key.
652 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000653 * This specification supports a single format for each key type.
654 * Implementations may support other formats as long as the standard
655 * format is supported. Implementations that support other formats
656 * should ensure that the formats are clearly unambiguous so as to
657 * minimize the risk that an invalid input is accidentally interpreted
658 * according to a different format.
659 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000660 * For standard key types, the output format is as follows:
661 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
662 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
663 * ```
664 * RSAPublicKey ::= SEQUENCE {
665 * modulus INTEGER, -- n
666 * publicExponent INTEGER } -- e
667 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000668 * - For elliptic curve public keys (key types for which
669 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
670 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
671 * Let `m` be the bit size associated with the curve, i.e. the bit size of
672 * `q` for a curve over `F_q`. The representation consists of:
673 * - The byte 0x04;
674 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
675 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200676 * - For Diffie-Hellman key exchange public keys (key types for which
677 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000678 * the format is the representation of the public key `y = g^x mod p` as a
679 * big-endian byte string. The length of the byte string is the length of the
680 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100681 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200682 * Exporting a public key object or the public part of a key pair is
683 * always permitted, regardless of the key's usage flags.
684 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100685 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200686 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200687 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200688 * \param[out] data_length On success, the number of bytes
689 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100690 *
Gilles Peskine28538492018-07-11 17:34:00 +0200691 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100692 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200693 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200694 * The key is neither a public key nor a key pair.
695 * \retval #PSA_ERROR_NOT_SUPPORTED
696 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
697 * The size of the \p data buffer is too small. You can determine a
698 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200699 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200700 * where \c type is the key type
701 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200702 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
703 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200704 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100705 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100706 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300707 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300708 * The library has not been previously initialized by psa_crypto_init().
709 * It is implementation-dependent whether a failure to initialize
710 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100711 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100712psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100713 uint8_t *data,
714 size_t data_size,
715 size_t *data_length);
716
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100717/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100718 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100719 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000720 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100721 * This function is primarily useful to copy a key from one location
722 * to another, since it populates a key using the material from
723 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200724 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100725 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100726 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100727 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200728 * The policy on the source key must have the usage flag
729 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200730 * This flag is sufficient to permit the copy if the key has the lifetime
731 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
732 * Some secure elements do not provide a way to copy a key without
733 * making it extractable from the secure element. If a key is located
734 * in such a secure element, then the key must have both usage flags
735 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
736 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200737 *
Gilles Peskine20628592019-04-19 19:29:50 +0200738 * The resulting key may only be used in a way that conforms to
739 * both the policy of the original key and the policy specified in
740 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100741 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200742 * usage flags on the source policy and the usage flags in \p attributes.
743 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100744 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200745 * - If either of the policies allows an algorithm and the other policy
746 * allows a wildcard-based algorithm policy that includes this algorithm,
747 * the resulting key allows the same algorithm.
748 * - If the policies do not allow any algorithm in common, this function
749 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200750 *
Gilles Peskine20628592019-04-19 19:29:50 +0200751 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100752 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200753 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100754 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200755 * \param[in] attributes The attributes for the new key.
756 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200757 * - The key type and size may be 0. If either is
758 * nonzero, it must match the corresponding
759 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200760 * - The key location (the lifetime and, for
761 * persistent keys, the key identifier) is
762 * used directly.
763 * - The policy constraints (usage flags and
764 * algorithm policy) are combined from
765 * the source key and \p attributes so that
766 * both sets of restrictions apply, as
767 * described in the documentation of this function.
768 * \param[out] target_handle On success, a handle to the newly created key.
769 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200770 *
771 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100772 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200773 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200774 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200775 * This is an attempt to create a persistent key, and there is
776 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200777 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200778 * The lifetime or identifier in \p attributes are invalid.
779 * \retval #PSA_ERROR_INVALID_ARGUMENT
780 * The policy constraints on the source and specified in
781 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200782 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200783 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200784 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100785 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200786 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
787 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100788 * The source key is not exportable and its lifetime does not
789 * allow copying it to the target's lifetime.
790 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
791 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200792 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
793 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shaw60b03202019-08-06 17:26:16 +0100794 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200795 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100796 * \retval #PSA_ERROR_BAD_STATE
797 * The library has not been previously initialized by psa_crypto_init().
798 * It is implementation-dependent whether a failure to initialize
799 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100800 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100801psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200802 const psa_key_attributes_t *attributes,
803 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100804
805/**@}*/
806
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100807/** \defgroup hash Message digests
808 * @{
809 */
810
Gilles Peskine69647a42019-01-14 20:18:12 +0100811/** Calculate the hash (digest) of a message.
812 *
813 * \note To verify the hash of a message against an
814 * expected value, use psa_hash_compare() instead.
815 *
816 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
817 * such that #PSA_ALG_IS_HASH(\p alg) is true).
818 * \param[in] input Buffer containing the message to hash.
819 * \param input_length Size of the \p input buffer in bytes.
820 * \param[out] hash Buffer where the hash is to be written.
821 * \param hash_size Size of the \p hash buffer in bytes.
822 * \param[out] hash_length On success, the number of bytes
823 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100824 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100825 *
826 * \retval #PSA_SUCCESS
827 * Success.
828 * \retval #PSA_ERROR_NOT_SUPPORTED
829 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100830 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100831 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
832 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100833 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
834 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
835 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200836 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100837 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100838 * \retval #PSA_ERROR_BAD_STATE
839 * The library has not been previously initialized by psa_crypto_init().
840 * It is implementation-dependent whether a failure to initialize
841 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100842 */
843psa_status_t psa_hash_compute(psa_algorithm_t alg,
844 const uint8_t *input,
845 size_t input_length,
846 uint8_t *hash,
847 size_t hash_size,
848 size_t *hash_length);
849
850/** Calculate the hash (digest) of a message and compare it with a
851 * reference value.
852 *
853 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
854 * such that #PSA_ALG_IS_HASH(\p alg) is true).
855 * \param[in] input Buffer containing the message to hash.
856 * \param input_length Size of the \p input buffer in bytes.
857 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100858 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100859 *
860 * \retval #PSA_SUCCESS
861 * The expected hash is identical to the actual hash of the input.
862 * \retval #PSA_ERROR_INVALID_SIGNATURE
863 * The hash of the message was calculated successfully, but it
864 * differs from the expected hash.
865 * \retval #PSA_ERROR_NOT_SUPPORTED
866 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100867 * \retval #PSA_ERROR_INVALID_ARGUMENT
868 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100869 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
870 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
871 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200872 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100873 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100874 * \retval #PSA_ERROR_BAD_STATE
875 * The library has not been previously initialized by psa_crypto_init().
876 * It is implementation-dependent whether a failure to initialize
877 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100878 */
879psa_status_t psa_hash_compare(psa_algorithm_t alg,
880 const uint8_t *input,
881 size_t input_length,
882 const uint8_t *hash,
883 const size_t hash_length);
884
Gilles Peskine308b91d2018-02-08 09:47:44 +0100885/** The type of the state data structure for multipart hash operations.
886 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000887 * Before calling any function on a hash operation object, the application must
888 * initialize it by any of the following means:
889 * - Set the structure to all-bits-zero, for example:
890 * \code
891 * psa_hash_operation_t operation;
892 * memset(&operation, 0, sizeof(operation));
893 * \endcode
894 * - Initialize the structure to logical zero values, for example:
895 * \code
896 * psa_hash_operation_t operation = {0};
897 * \endcode
898 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
899 * for example:
900 * \code
901 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
902 * \endcode
903 * - Assign the result of the function psa_hash_operation_init()
904 * to the structure, for example:
905 * \code
906 * psa_hash_operation_t operation;
907 * operation = psa_hash_operation_init();
908 * \endcode
909 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100910 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100911 * make any assumptions about the content of this structure except
912 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100913typedef struct psa_hash_operation_s psa_hash_operation_t;
914
Jaeden Amero6a25b412019-01-04 11:47:44 +0000915/** \def PSA_HASH_OPERATION_INIT
916 *
917 * This macro returns a suitable initializer for a hash operation object
918 * of type #psa_hash_operation_t.
919 */
920#ifdef __DOXYGEN_ONLY__
921/* This is an example definition for documentation purposes.
922 * Implementations should define a suitable value in `crypto_struct.h`.
923 */
924#define PSA_HASH_OPERATION_INIT {0}
925#endif
926
927/** Return an initial value for a hash operation object.
928 */
929static psa_hash_operation_t psa_hash_operation_init(void);
930
Gilles Peskinef45adda2019-01-14 18:29:18 +0100931/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100932 *
933 * The sequence of operations to calculate a hash (message digest)
934 * is as follows:
935 * -# Allocate an operation object which will be passed to all the functions
936 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000937 * -# Initialize the operation object with one of the methods described in the
938 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200939 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100940 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100941 * of the message each time. The hash that is calculated is the hash
942 * of the concatenation of these messages in order.
943 * -# To calculate the hash, call psa_hash_finish().
944 * To compare the hash with an expected value, call psa_hash_verify().
945 *
946 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000947 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100948 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200949 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100950 * eventually terminate the operation. The following events terminate an
951 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100952 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100953 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100954 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000955 * \param[in,out] operation The operation object to set up. It must have
956 * been initialized as per the documentation for
957 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200958 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
959 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100960 *
Gilles Peskine28538492018-07-11 17:34:00 +0200961 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100962 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200963 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100964 * \p alg is not a supported hash algorithm.
965 * \retval #PSA_ERROR_INVALID_ARGUMENT
966 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100967 * \retval #PSA_ERROR_BAD_STATE
968 * The operation state is not valid (already set up and not
969 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200970 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
971 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
972 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200973 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100974 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawe970d652019-08-08 14:40:04 +0100975 * \p operation is either not initialized or is in use
976 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100977 * The library has not been previously initialized by psa_crypto_init().
978 * It is implementation-dependent whether a failure to initialize
979 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100980 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200981psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100982 psa_algorithm_t alg);
983
Gilles Peskine308b91d2018-02-08 09:47:44 +0100984/** Add a message fragment to a multipart hash operation.
985 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200986 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100987 *
988 * If this function returns an error status, the operation becomes inactive.
989 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200990 * \param[in,out] operation Active hash operation.
991 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200992 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100993 *
Gilles Peskine28538492018-07-11 17:34:00 +0200994 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100995 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200996 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100997 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200998 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
999 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1000 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001001 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001002 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001003 * The operation state is not valid.
1004 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001005 * The library has not been previously initialized by psa_crypto_init().
1006 * It is implementation-dependent whether a failure to initialize
1007 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001008 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001009psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1010 const uint8_t *input,
1011 size_t input_length);
1012
Gilles Peskine308b91d2018-02-08 09:47:44 +01001013/** Finish the calculation of the hash of a message.
1014 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001015 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001016 * This function calculates the hash of the message formed by concatenating
1017 * the inputs passed to preceding calls to psa_hash_update().
1018 *
1019 * When this function returns, the operation becomes inactive.
1020 *
1021 * \warning Applications should not call this function if they expect
1022 * a specific value for the hash. Call psa_hash_verify() instead.
1023 * Beware that comparing integrity or authenticity data such as
1024 * hash values with a function such as \c memcmp is risky
1025 * because the time taken by the comparison may leak information
1026 * about the hashed data which could allow an attacker to guess
1027 * a valid hash and thereby bypass security controls.
1028 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001029 * \param[in,out] operation Active hash operation.
1030 * \param[out] hash Buffer where the hash is to be written.
1031 * \param hash_size Size of the \p hash buffer in bytes.
1032 * \param[out] hash_length On success, the number of bytes
1033 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001034 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001035 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001036 *
Gilles Peskine28538492018-07-11 17:34:00 +02001037 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001038 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001039 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001040 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001041 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001042 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001043 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001044 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001045 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1046 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1047 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001048 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001049 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001050 * The operation state is not valid.
1051 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001052 * The library has not been previously initialized by psa_crypto_init().
1053 * It is implementation-dependent whether a failure to initialize
1054 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001055 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001056psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1057 uint8_t *hash,
1058 size_t hash_size,
1059 size_t *hash_length);
1060
Gilles Peskine308b91d2018-02-08 09:47:44 +01001061/** Finish the calculation of the hash of a message and compare it with
1062 * an expected value.
1063 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001064 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001065 * This function calculates the hash of the message formed by concatenating
1066 * the inputs passed to preceding calls to psa_hash_update(). It then
1067 * compares the calculated hash with the expected hash passed as a
1068 * parameter to this function.
1069 *
1070 * When this function returns, the operation becomes inactive.
1071 *
Gilles Peskine19067982018-03-20 17:54:53 +01001072 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001073 * comparison between the actual hash and the expected hash is performed
1074 * in constant time.
1075 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001076 * \param[in,out] operation Active hash operation.
1077 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001078 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001079 *
Gilles Peskine28538492018-07-11 17:34:00 +02001080 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001081 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001082 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001083 * The hash of the message was calculated successfully, but it
1084 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001085 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001086 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001087 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1088 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1089 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001090 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001091 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001092 * The operation state is not valid.
1093 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001094 * The library has not been previously initialized by psa_crypto_init().
1095 * It is implementation-dependent whether a failure to initialize
1096 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001097 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001098psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1099 const uint8_t *hash,
1100 size_t hash_length);
1101
Gilles Peskine308b91d2018-02-08 09:47:44 +01001102/** Abort a hash operation.
1103 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001104 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001105 * \p operation structure itself. Once aborted, the operation object
1106 * can be reused for another operation by calling
1107 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001108 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001109 * You may call this function any time after the operation object has
1110 * been initialized by any of the following methods:
1111 * - A call to psa_hash_setup(), whether it succeeds or not.
1112 * - Initializing the \c struct to all-bits-zero.
1113 * - Initializing the \c struct to logical zeros, e.g.
1114 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001115 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001116 * In particular, calling psa_hash_abort() after the operation has been
1117 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1118 * psa_hash_verify() is safe and has no effect.
1119 *
1120 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001121 *
Gilles Peskine28538492018-07-11 17:34:00 +02001122 * \retval #PSA_SUCCESS
1123 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001124 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001125 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1126 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001127 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001128 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001129 * The operation state is not valid.
1130 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001131 * 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.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001134 */
1135psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001136
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001137/** Clone a hash operation.
1138 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001139 * This function copies the state of an ongoing hash operation to
1140 * a new operation object. In other words, this function is equivalent
1141 * to calling psa_hash_setup() on \p target_operation with the same
1142 * algorithm that \p source_operation was set up for, then
1143 * psa_hash_update() on \p target_operation with the same input that
1144 * that was passed to \p source_operation. After this function returns, the
1145 * two objects are independent, i.e. subsequent calls involving one of
1146 * the objects do not affect the other object.
1147 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001148 * \param[in] source_operation The active hash operation to clone.
1149 * \param[in,out] target_operation The operation object to set up.
1150 * It must be initialized but not active.
1151 *
1152 * \retval #PSA_SUCCESS
1153 * \retval #PSA_ERROR_BAD_STATE
1154 * \p source_operation is not an active hash operation.
1155 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001156 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001157 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1158 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001159 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001160 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001161 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001162 * The operation state is either not initialized or has already been setup.
1163 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw8f7cd1e2019-08-08 14:49:01 +01001164 * The operation state is either not initialized or has already been setup.
1165 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001166 * The library has not been previously initialized by psa_crypto_init().
1167 * It is implementation-dependent whether a failure to initialize
1168 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001169 */
1170psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1171 psa_hash_operation_t *target_operation);
1172
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001173/**@}*/
1174
Gilles Peskine8c9def32018-02-08 10:02:12 +01001175/** \defgroup MAC Message authentication codes
1176 * @{
1177 */
1178
Gilles Peskine69647a42019-01-14 20:18:12 +01001179/** Calculate the MAC (message authentication code) of a message.
1180 *
1181 * \note To verify the MAC of a message against an
1182 * expected value, use psa_mac_verify() instead.
1183 * Beware that comparing integrity or authenticity data such as
1184 * MAC values with a function such as \c memcmp is risky
1185 * because the time taken by the comparison may leak information
1186 * about the MAC value which could allow an attacker to guess
1187 * a valid MAC and thereby bypass security controls.
1188 *
1189 * \param handle Handle to the key to use for the operation.
1190 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001191 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001192 * \param[in] input Buffer containing the input message.
1193 * \param input_length Size of the \p input buffer in bytes.
1194 * \param[out] mac Buffer where the MAC value is to be written.
1195 * \param mac_size Size of the \p mac buffer in bytes.
1196 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001197 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001198 *
1199 * \retval #PSA_SUCCESS
1200 * Success.
1201 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001202 * \retval #PSA_ERROR_NOT_PERMITTED
1203 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001204 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001205 * \retval #PSA_ERROR_NOT_SUPPORTED
1206 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001207 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1208 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001209 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1210 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1211 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001212 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001213 * \retval #PSA_ERROR_STORAGE_FAILURE
1214 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001215 * \retval #PSA_ERROR_BAD_STATE
1216 * The library has not been previously initialized by psa_crypto_init().
1217 * It is implementation-dependent whether a failure to initialize
1218 * results in this error code.
1219 */
1220psa_status_t psa_mac_compute(psa_key_handle_t handle,
1221 psa_algorithm_t alg,
1222 const uint8_t *input,
1223 size_t input_length,
1224 uint8_t *mac,
1225 size_t mac_size,
1226 size_t *mac_length);
1227
1228/** Calculate the MAC of a message and compare it with a reference value.
1229 *
1230 * \param handle Handle to the key to use for the operation.
1231 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001232 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001233 * \param[in] input Buffer containing the input message.
1234 * \param input_length Size of the \p input buffer in bytes.
1235 * \param[out] mac Buffer containing the expected MAC value.
1236 * \param mac_length Size of the \p mac buffer in bytes.
1237 *
1238 * \retval #PSA_SUCCESS
1239 * The expected MAC is identical to the actual MAC of the input.
1240 * \retval #PSA_ERROR_INVALID_SIGNATURE
1241 * The MAC of the message was calculated successfully, but it
1242 * differs from the expected value.
1243 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001244 * \retval #PSA_ERROR_NOT_PERMITTED
1245 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001246 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001247 * \retval #PSA_ERROR_NOT_SUPPORTED
1248 * \p alg is not supported or is not a MAC algorithm.
1249 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1250 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1251 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001252 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001253 * \retval #PSA_ERROR_STORAGE_FAILURE
1254 * The key could not be retrieved from storage.
1255 * \retval #PSA_ERROR_BAD_STATE
1256 * The library has not been previously initialized by psa_crypto_init().
1257 * It is implementation-dependent whether a failure to initialize
1258 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001259 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001260psa_status_t psa_mac_verify(psa_key_handle_t handle,
1261 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001262 const uint8_t *input,
1263 size_t input_length,
1264 const uint8_t *mac,
1265 const size_t mac_length);
1266
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001267/** The type of the state data structure for multipart MAC operations.
1268 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001269 * Before calling any function on a MAC operation object, the application must
1270 * initialize it by any of the following means:
1271 * - Set the structure to all-bits-zero, for example:
1272 * \code
1273 * psa_mac_operation_t operation;
1274 * memset(&operation, 0, sizeof(operation));
1275 * \endcode
1276 * - Initialize the structure to logical zero values, for example:
1277 * \code
1278 * psa_mac_operation_t operation = {0};
1279 * \endcode
1280 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1281 * for example:
1282 * \code
1283 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1284 * \endcode
1285 * - Assign the result of the function psa_mac_operation_init()
1286 * to the structure, for example:
1287 * \code
1288 * psa_mac_operation_t operation;
1289 * operation = psa_mac_operation_init();
1290 * \endcode
1291 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001292 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001293 * make any assumptions about the content of this structure except
1294 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001295typedef struct psa_mac_operation_s psa_mac_operation_t;
1296
Jaeden Amero769ce272019-01-04 11:48:03 +00001297/** \def PSA_MAC_OPERATION_INIT
1298 *
1299 * This macro returns a suitable initializer for a MAC operation object of type
1300 * #psa_mac_operation_t.
1301 */
1302#ifdef __DOXYGEN_ONLY__
1303/* This is an example definition for documentation purposes.
1304 * Implementations should define a suitable value in `crypto_struct.h`.
1305 */
1306#define PSA_MAC_OPERATION_INIT {0}
1307#endif
1308
1309/** Return an initial value for a MAC operation object.
1310 */
1311static psa_mac_operation_t psa_mac_operation_init(void);
1312
Gilles Peskinef45adda2019-01-14 18:29:18 +01001313/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001314 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001315 * This function sets up the calculation of the MAC
1316 * (message authentication code) of a byte string.
1317 * To verify the MAC of a message against an
1318 * expected value, use psa_mac_verify_setup() instead.
1319 *
1320 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001321 * -# Allocate an operation object which will be passed to all the functions
1322 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001323 * -# Initialize the operation object with one of the methods described in the
1324 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001325 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001326 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1327 * of the message each time. The MAC that is calculated is the MAC
1328 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001329 * -# At the end of the message, call psa_mac_sign_finish() to finish
1330 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001331 *
1332 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001333 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001334 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001335 * After a successful call to psa_mac_sign_setup(), the application must
1336 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001337 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001338 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001339 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001340 * \param[in,out] operation The operation object to set up. It must have
1341 * been initialized as per the documentation for
1342 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001343 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001344 * It must remain valid until the operation
1345 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001346 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001347 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001348 *
Gilles Peskine28538492018-07-11 17:34:00 +02001349 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001350 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001351 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001352 * \retval #PSA_ERROR_NOT_PERMITTED
1353 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001354 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001355 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001356 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001357 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1358 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1359 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001360 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001361 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001362 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001363 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001364 * The operation state is not valid (already set up and not
1365 * subsequently completed).
1366 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001367 * The library has not been previously initialized by psa_crypto_init().
1368 * It is implementation-dependent whether a failure to initialize
1369 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001370 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001371psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001372 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001373 psa_algorithm_t alg);
1374
Gilles Peskinef45adda2019-01-14 18:29:18 +01001375/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001376 *
1377 * This function sets up the verification of the MAC
1378 * (message authentication code) of a byte string against an expected value.
1379 *
1380 * The sequence of operations to verify a MAC is as follows:
1381 * -# Allocate an operation object which will be passed to all the functions
1382 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001383 * -# Initialize the operation object with one of the methods described in the
1384 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001385 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001386 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1387 * of the message each time. The MAC that is calculated is the MAC
1388 * of the concatenation of these messages in order.
1389 * -# At the end of the message, call psa_mac_verify_finish() to finish
1390 * calculating the actual MAC of the message and verify it against
1391 * the expected value.
1392 *
1393 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001394 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001395 *
1396 * After a successful call to psa_mac_verify_setup(), the application must
1397 * eventually terminate the operation through one of the following methods:
1398 * - A failed call to psa_mac_update().
1399 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1400 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001401 * \param[in,out] operation The operation object to set up. It must have
1402 * been initialized as per the documentation for
1403 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001404 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001405 * It must remain valid until the operation
1406 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001407 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1408 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001409 *
Gilles Peskine28538492018-07-11 17:34:00 +02001410 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001411 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001412 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001413 * \retval #PSA_ERROR_NOT_PERMITTED
1414 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001415 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001416 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001417 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001418 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1419 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1420 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001421 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001422 * \retval #PSA_ERROR_STORAGE_FAILURE
1423 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001424 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001425 * The operation state is not valid (already set up and not
1426 * subsequently completed).
1427 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001428 * The library has not been previously initialized by psa_crypto_init().
1429 * It is implementation-dependent whether a failure to initialize
1430 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001431 */
1432psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001433 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001434 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001435
Gilles Peskinedcd14942018-07-12 00:30:52 +02001436/** Add a message fragment to a multipart MAC operation.
1437 *
1438 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1439 * before calling this function.
1440 *
1441 * If this function returns an error status, the operation becomes inactive.
1442 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001443 * \param[in,out] operation Active MAC operation.
1444 * \param[in] input Buffer containing the message fragment to add to
1445 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001446 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001447 *
1448 * \retval #PSA_SUCCESS
1449 * Success.
1450 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001451 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001452 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1453 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1454 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001455 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001456 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001457 * \retval #PSA_ERROR_BAD_STATE
1458 * The library has not been previously initialized by psa_crypto_init().
1459 * It is implementation-dependent whether a failure to initialize
1460 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001461 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001462psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1463 const uint8_t *input,
1464 size_t input_length);
1465
Gilles Peskinedcd14942018-07-12 00:30:52 +02001466/** Finish the calculation of the MAC of a message.
1467 *
1468 * The application must call psa_mac_sign_setup() before calling this function.
1469 * This function calculates the MAC of the message formed by concatenating
1470 * the inputs passed to preceding calls to psa_mac_update().
1471 *
1472 * When this function returns, the operation becomes inactive.
1473 *
1474 * \warning Applications should not call this function if they expect
1475 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1476 * Beware that comparing integrity or authenticity data such as
1477 * MAC values with a function such as \c memcmp is risky
1478 * because the time taken by the comparison may leak information
1479 * about the MAC value which could allow an attacker to guess
1480 * a valid MAC and thereby bypass security controls.
1481 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001482 * \param[in,out] operation Active MAC operation.
1483 * \param[out] mac Buffer where the MAC value is to be written.
1484 * \param mac_size Size of the \p mac buffer in bytes.
1485 * \param[out] mac_length On success, the number of bytes
1486 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001487 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001488 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001489 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001490 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001491 *
1492 * \retval #PSA_SUCCESS
1493 * Success.
1494 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001495 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001496 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001497 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001498 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1499 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1500 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1501 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001502 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001503 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001504 * \retval #PSA_ERROR_BAD_STATE
1505 * The library has not been previously initialized by psa_crypto_init().
1506 * It is implementation-dependent whether a failure to initialize
1507 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001508 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001509psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1510 uint8_t *mac,
1511 size_t mac_size,
1512 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001513
Gilles Peskinedcd14942018-07-12 00:30:52 +02001514/** Finish the calculation of the MAC of a message and compare it with
1515 * an expected value.
1516 *
1517 * The application must call psa_mac_verify_setup() before calling this function.
1518 * This function calculates the MAC of the message formed by concatenating
1519 * the inputs passed to preceding calls to psa_mac_update(). It then
1520 * compares the calculated MAC with the expected MAC passed as a
1521 * parameter to this function.
1522 *
1523 * When this function returns, the operation becomes inactive.
1524 *
1525 * \note Implementations shall make the best effort to ensure that the
1526 * comparison between the actual MAC and the expected MAC is performed
1527 * in constant time.
1528 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001529 * \param[in,out] operation Active MAC operation.
1530 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001531 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001532 *
1533 * \retval #PSA_SUCCESS
1534 * The expected MAC is identical to the actual MAC of the message.
1535 * \retval #PSA_ERROR_INVALID_SIGNATURE
1536 * The MAC of the message was calculated successfully, but it
1537 * differs from the expected MAC.
1538 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001539 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001540 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1541 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1542 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001543 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001544 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001545 * \retval #PSA_ERROR_BAD_STATE
1546 * The library has not been previously initialized by psa_crypto_init().
1547 * It is implementation-dependent whether a failure to initialize
1548 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001549 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001550psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1551 const uint8_t *mac,
1552 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001553
Gilles Peskinedcd14942018-07-12 00:30:52 +02001554/** Abort a MAC operation.
1555 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001556 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001557 * \p operation structure itself. Once aborted, the operation object
1558 * can be reused for another operation by calling
1559 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001560 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001561 * You may call this function any time after the operation object has
1562 * been initialized by any of the following methods:
1563 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1564 * it succeeds or not.
1565 * - Initializing the \c struct to all-bits-zero.
1566 * - Initializing the \c struct to logical zeros, e.g.
1567 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001568 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001569 * In particular, calling psa_mac_abort() after the operation has been
1570 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1571 * psa_mac_verify_finish() is safe and has no effect.
1572 *
1573 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001574 *
1575 * \retval #PSA_SUCCESS
1576 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001577 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001578 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1579 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001580 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001581 * \retval #PSA_ERROR_BAD_STATE
1582 * The library has not been previously initialized by psa_crypto_init().
1583 * It is implementation-dependent whether a failure to initialize
1584 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001585 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001586psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1587
1588/**@}*/
1589
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001590/** \defgroup cipher Symmetric ciphers
1591 * @{
1592 */
1593
Gilles Peskine69647a42019-01-14 20:18:12 +01001594/** Encrypt a message using a symmetric cipher.
1595 *
1596 * This function encrypts a message with a random IV (initialization
1597 * vector).
1598 *
1599 * \param handle Handle to the key to use for the operation.
1600 * It must remain valid until the operation
1601 * terminates.
1602 * \param alg The cipher algorithm to compute
1603 * (\c PSA_ALG_XXX value such that
1604 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1605 * \param[in] input Buffer containing the message to encrypt.
1606 * \param input_length Size of the \p input buffer in bytes.
1607 * \param[out] output Buffer where the output is to be written.
1608 * The output contains the IV followed by
1609 * the ciphertext proper.
1610 * \param output_size Size of the \p output buffer in bytes.
1611 * \param[out] output_length On success, the number of bytes
1612 * that make up the output.
1613 *
1614 * \retval #PSA_SUCCESS
1615 * Success.
1616 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001617 * \retval #PSA_ERROR_NOT_PERMITTED
1618 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001619 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001620 * \retval #PSA_ERROR_NOT_SUPPORTED
1621 * \p alg is not supported or is not a cipher algorithm.
1622 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1623 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1624 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1625 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001626 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001627 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001628 * \retval #PSA_ERROR_BAD_STATE
1629 * The library has not been previously initialized by psa_crypto_init().
1630 * It is implementation-dependent whether a failure to initialize
1631 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001632 */
1633psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1634 psa_algorithm_t alg,
1635 const uint8_t *input,
1636 size_t input_length,
1637 uint8_t *output,
1638 size_t output_size,
1639 size_t *output_length);
1640
1641/** Decrypt a message using a symmetric cipher.
1642 *
1643 * This function decrypts a message encrypted with a symmetric cipher.
1644 *
1645 * \param handle Handle to the key to use for the operation.
1646 * It must remain valid until the operation
1647 * terminates.
1648 * \param alg The cipher algorithm to compute
1649 * (\c PSA_ALG_XXX value such that
1650 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1651 * \param[in] input Buffer containing the message to decrypt.
1652 * This consists of the IV followed by the
1653 * ciphertext proper.
1654 * \param input_length Size of the \p input buffer in bytes.
1655 * \param[out] output Buffer where the plaintext is to be written.
1656 * \param output_size Size of the \p output buffer in bytes.
1657 * \param[out] output_length On success, the number of bytes
1658 * that make up the output.
1659 *
1660 * \retval #PSA_SUCCESS
1661 * Success.
1662 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001663 * \retval #PSA_ERROR_NOT_PERMITTED
1664 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001665 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001666 * \retval #PSA_ERROR_NOT_SUPPORTED
1667 * \p alg is not supported or is not a cipher algorithm.
1668 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1669 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1670 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1671 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001672 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001673 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001674 * \retval #PSA_ERROR_BAD_STATE
1675 * The library has not been previously initialized by psa_crypto_init().
1676 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001677 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001678 */
1679psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1680 psa_algorithm_t alg,
1681 const uint8_t *input,
1682 size_t input_length,
1683 uint8_t *output,
1684 size_t output_size,
1685 size_t *output_length);
1686
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001687/** The type of the state data structure for multipart cipher operations.
1688 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001689 * Before calling any function on a cipher operation object, the application
1690 * must initialize it by any of the following means:
1691 * - Set the structure to all-bits-zero, for example:
1692 * \code
1693 * psa_cipher_operation_t operation;
1694 * memset(&operation, 0, sizeof(operation));
1695 * \endcode
1696 * - Initialize the structure to logical zero values, for example:
1697 * \code
1698 * psa_cipher_operation_t operation = {0};
1699 * \endcode
1700 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1701 * for example:
1702 * \code
1703 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1704 * \endcode
1705 * - Assign the result of the function psa_cipher_operation_init()
1706 * to the structure, for example:
1707 * \code
1708 * psa_cipher_operation_t operation;
1709 * operation = psa_cipher_operation_init();
1710 * \endcode
1711 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001712 * This is an implementation-defined \c struct. Applications should not
1713 * make any assumptions about the content of this structure except
1714 * as directed by the documentation of a specific implementation. */
1715typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1716
Jaeden Amero5bae2272019-01-04 11:48:27 +00001717/** \def PSA_CIPHER_OPERATION_INIT
1718 *
1719 * This macro returns a suitable initializer for a cipher operation object of
1720 * type #psa_cipher_operation_t.
1721 */
1722#ifdef __DOXYGEN_ONLY__
1723/* This is an example definition for documentation purposes.
1724 * Implementations should define a suitable value in `crypto_struct.h`.
1725 */
1726#define PSA_CIPHER_OPERATION_INIT {0}
1727#endif
1728
1729/** Return an initial value for a cipher operation object.
1730 */
1731static psa_cipher_operation_t psa_cipher_operation_init(void);
1732
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001733/** Set the key for a multipart symmetric encryption operation.
1734 *
1735 * The sequence of operations to encrypt a message with a symmetric cipher
1736 * is as follows:
1737 * -# Allocate an operation object which will be passed to all the functions
1738 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001739 * -# Initialize the operation object with one of the methods described in the
1740 * documentation for #psa_cipher_operation_t, e.g.
1741 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001742 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001743 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001744 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001745 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001746 * requires a specific IV value.
1747 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1748 * of the message each time.
1749 * -# Call psa_cipher_finish().
1750 *
1751 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001752 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001753 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001754 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001755 * eventually terminate the operation. The following events terminate an
1756 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001757 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001758 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001759 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001760 * \param[in,out] operation The operation object to set up. It must have
1761 * been initialized as per the documentation for
1762 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001763 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001764 * It must remain valid until the operation
1765 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001766 * \param alg The cipher algorithm to compute
1767 * (\c PSA_ALG_XXX value such that
1768 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001769 *
Gilles Peskine28538492018-07-11 17:34:00 +02001770 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001771 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001772 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001773 * \retval #PSA_ERROR_NOT_PERMITTED
1774 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001775 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001776 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001777 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001778 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1779 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1780 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001781 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001782 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001783 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001784 * The operation state is not valid (already set up and not
1785 * subsequently completed).
1786 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001787 * The library has not been previously initialized by psa_crypto_init().
1788 * It is implementation-dependent whether a failure to initialize
1789 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001790 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001791psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001792 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001793 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001794
1795/** Set the key for a multipart symmetric decryption operation.
1796 *
1797 * The sequence of operations to decrypt a message with a symmetric cipher
1798 * is as follows:
1799 * -# Allocate an operation object which will be passed to all the functions
1800 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001801 * -# Initialize the operation object with one of the methods described in the
1802 * documentation for #psa_cipher_operation_t, e.g.
1803 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001804 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001805 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001806 * decryption. If the IV is prepended to the ciphertext, you can call
1807 * psa_cipher_update() on a buffer containing the IV followed by the
1808 * beginning of the message.
1809 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1810 * of the message each time.
1811 * -# Call psa_cipher_finish().
1812 *
1813 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001814 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001815 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001816 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001817 * eventually terminate the operation. The following events terminate an
1818 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001819 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001820 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001821 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001822 * \param[in,out] operation The operation object to set up. It must have
1823 * been initialized as per the documentation for
1824 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001825 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001826 * It must remain valid until the operation
1827 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001828 * \param alg The cipher algorithm to compute
1829 * (\c PSA_ALG_XXX value such that
1830 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001831 *
Gilles Peskine28538492018-07-11 17:34:00 +02001832 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001833 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001834 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001835 * \retval #PSA_ERROR_NOT_PERMITTED
1836 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001837 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001838 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001839 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001840 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1841 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1842 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001843 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001844 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001845 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001846 * The operation state is not valid (already set up and not
1847 * subsequently completed).
1848 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001849 * The library has not been previously initialized by psa_crypto_init().
1850 * It is implementation-dependent whether a failure to initialize
1851 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001852 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001853psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001854 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001855 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001856
Gilles Peskinedcd14942018-07-12 00:30:52 +02001857/** Generate an IV for a symmetric encryption operation.
1858 *
1859 * This function generates a random IV (initialization vector), nonce
1860 * or initial counter value for the encryption operation as appropriate
1861 * for the chosen algorithm, key type and key size.
1862 *
1863 * The application must call psa_cipher_encrypt_setup() before
1864 * calling this function.
1865 *
1866 * If this function returns an error status, the operation becomes inactive.
1867 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001868 * \param[in,out] operation Active cipher operation.
1869 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001870 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001871 * \param[out] iv_length On success, the number of bytes of the
1872 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001873 *
1874 * \retval #PSA_SUCCESS
1875 * Success.
1876 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001877 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001878 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001879 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001880 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1881 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1882 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001883 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001884 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001885 * \retval #PSA_ERROR_BAD_STATE
1886 * The library has not been previously initialized by psa_crypto_init().
1887 * It is implementation-dependent whether a failure to initialize
1888 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001889 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001890psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001891 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001892 size_t iv_size,
1893 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001894
Gilles Peskinedcd14942018-07-12 00:30:52 +02001895/** Set the IV for a symmetric encryption or decryption operation.
1896 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001897 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001898 * or initial counter value for the encryption or decryption operation.
1899 *
1900 * The application must call psa_cipher_encrypt_setup() before
1901 * calling this function.
1902 *
1903 * If this function returns an error status, the operation becomes inactive.
1904 *
1905 * \note When encrypting, applications should use psa_cipher_generate_iv()
1906 * instead of this function, unless implementing a protocol that requires
1907 * a non-random IV.
1908 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001909 * \param[in,out] operation Active cipher operation.
1910 * \param[in] iv Buffer containing the IV to use.
1911 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001912 *
1913 * \retval #PSA_SUCCESS
1914 * Success.
1915 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001916 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001917 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001918 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001919 * or the chosen algorithm does not use an IV.
1920 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1921 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1922 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001923 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001924 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001925 * \retval #PSA_ERROR_BAD_STATE
1926 * The library has not been previously initialized by psa_crypto_init().
1927 * It is implementation-dependent whether a failure to initialize
1928 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001929 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001930psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001931 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001932 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001933
Gilles Peskinedcd14942018-07-12 00:30:52 +02001934/** Encrypt or decrypt a message fragment in an active cipher operation.
1935 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001936 * Before calling this function, you must:
1937 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1938 * The choice of setup function determines whether this function
1939 * encrypts or decrypts its input.
1940 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1941 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001942 *
1943 * If this function returns an error status, the operation becomes inactive.
1944 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001945 * \param[in,out] operation Active cipher operation.
1946 * \param[in] input Buffer containing the message fragment to
1947 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001948 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001949 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001950 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001951 * \param[out] output_length On success, the number of bytes
1952 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001953 *
1954 * \retval #PSA_SUCCESS
1955 * Success.
1956 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001957 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001958 * not set, or already completed).
1959 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1960 * The size of the \p output buffer is too small.
1961 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1962 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1963 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001964 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001965 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001966 * \retval #PSA_ERROR_BAD_STATE
1967 * The library has not been previously initialized by psa_crypto_init().
1968 * It is implementation-dependent whether a failure to initialize
1969 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001970 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001971psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1972 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001973 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001974 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001975 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001976 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001977
Gilles Peskinedcd14942018-07-12 00:30:52 +02001978/** Finish encrypting or decrypting a message in a cipher operation.
1979 *
1980 * The application must call psa_cipher_encrypt_setup() or
1981 * psa_cipher_decrypt_setup() before calling this function. The choice
1982 * of setup function determines whether this function encrypts or
1983 * decrypts its input.
1984 *
1985 * This function finishes the encryption or decryption of the message
1986 * formed by concatenating the inputs passed to preceding calls to
1987 * psa_cipher_update().
1988 *
1989 * When this function returns, the operation becomes inactive.
1990 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001991 * \param[in,out] operation Active cipher operation.
1992 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001993 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001994 * \param[out] output_length On success, the number of bytes
1995 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001996 *
1997 * \retval #PSA_SUCCESS
1998 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02001999 * \retval #PSA_ERROR_INVALID_ARGUMENT
2000 * The total input size passed to this operation is not valid for
2001 * this particular algorithm. For example, the algorithm is a based
2002 * on block cipher and requires a whole number of blocks, but the
2003 * total input size is not a multiple of the block size.
2004 * \retval #PSA_ERROR_INVALID_PADDING
2005 * This is a decryption operation for an algorithm that includes
2006 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002007 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002008 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002009 * not set, or already completed).
2010 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2011 * The size of the \p output buffer is too small.
2012 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2013 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2014 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002015 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002016 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002017 * \retval #PSA_ERROR_BAD_STATE
2018 * The library has not been previously initialized by psa_crypto_init().
2019 * It is implementation-dependent whether a failure to initialize
2020 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002021 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002022psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002023 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002024 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002025 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002026
Gilles Peskinedcd14942018-07-12 00:30:52 +02002027/** Abort a cipher operation.
2028 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002029 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002030 * \p operation structure itself. Once aborted, the operation object
2031 * can be reused for another operation by calling
2032 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002033 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002034 * You may call this function any time after the operation object has
2035 * been initialized by any of the following methods:
2036 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2037 * whether it succeeds or not.
2038 * - Initializing the \c struct to all-bits-zero.
2039 * - Initializing the \c struct to logical zeros, e.g.
2040 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002041 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002042 * In particular, calling psa_cipher_abort() after the operation has been
2043 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2044 * is safe and has no effect.
2045 *
2046 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002047 *
2048 * \retval #PSA_SUCCESS
2049 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002050 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002051 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2052 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002053 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002054 * \retval #PSA_ERROR_BAD_STATE
2055 * The library has not been previously initialized by psa_crypto_init().
2056 * It is implementation-dependent whether a failure to initialize
2057 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002058 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002059psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2060
2061/**@}*/
2062
Gilles Peskine3b555712018-03-03 21:27:57 +01002063/** \defgroup aead Authenticated encryption with associated data (AEAD)
2064 * @{
2065 */
2066
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002067/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002068 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002069 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002070 * \param alg The AEAD algorithm to compute
2071 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002072 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002073 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002074 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002075 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002076 * but not encrypted.
2077 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002078 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002079 * encrypted.
2080 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002081 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002082 * encrypted data. The additional data is not
2083 * part of this output. For algorithms where the
2084 * encrypted data and the authentication tag
2085 * are defined as separate outputs, the
2086 * authentication tag is appended to the
2087 * encrypted data.
2088 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2089 * This must be at least
2090 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2091 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002092 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002093 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002094 *
Gilles Peskine28538492018-07-11 17:34:00 +02002095 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002096 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002097 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002098 * \retval #PSA_ERROR_NOT_PERMITTED
2099 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002100 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002101 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002102 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002103 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002104 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2105 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002106 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2107 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002108 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002109 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002110 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002111 * The library has not been previously initialized by psa_crypto_init().
2112 * It is implementation-dependent whether a failure to initialize
2113 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002114 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002115psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002116 psa_algorithm_t alg,
2117 const uint8_t *nonce,
2118 size_t nonce_length,
2119 const uint8_t *additional_data,
2120 size_t additional_data_length,
2121 const uint8_t *plaintext,
2122 size_t plaintext_length,
2123 uint8_t *ciphertext,
2124 size_t ciphertext_size,
2125 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002126
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002127/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002128 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002129 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002130 * \param alg The AEAD algorithm to compute
2131 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002132 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002133 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002134 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002135 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002136 * but not encrypted.
2137 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002138 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002139 * encrypted. For algorithms where the
2140 * encrypted data and the authentication tag
2141 * are defined as separate inputs, the buffer
2142 * must contain the encrypted data followed
2143 * by the authentication tag.
2144 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002145 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002146 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2147 * This must be at least
2148 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2149 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002150 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002151 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002152 *
Gilles Peskine28538492018-07-11 17:34:00 +02002153 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002154 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002155 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002156 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002157 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002158 * \retval #PSA_ERROR_NOT_PERMITTED
2159 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002160 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002161 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002162 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002163 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002164 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2165 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002166 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2167 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002168 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002169 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002170 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002171 * The library has not been previously initialized by psa_crypto_init().
2172 * It is implementation-dependent whether a failure to initialize
2173 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002174 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002175psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002176 psa_algorithm_t alg,
2177 const uint8_t *nonce,
2178 size_t nonce_length,
2179 const uint8_t *additional_data,
2180 size_t additional_data_length,
2181 const uint8_t *ciphertext,
2182 size_t ciphertext_length,
2183 uint8_t *plaintext,
2184 size_t plaintext_size,
2185 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002186
Gilles Peskine30a9e412019-01-14 18:36:12 +01002187/** The type of the state data structure for multipart AEAD operations.
2188 *
2189 * Before calling any function on an AEAD operation object, the application
2190 * must initialize it by any of the following means:
2191 * - Set the structure to all-bits-zero, for example:
2192 * \code
2193 * psa_aead_operation_t operation;
2194 * memset(&operation, 0, sizeof(operation));
2195 * \endcode
2196 * - Initialize the structure to logical zero values, for example:
2197 * \code
2198 * psa_aead_operation_t operation = {0};
2199 * \endcode
2200 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2201 * for example:
2202 * \code
2203 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2204 * \endcode
2205 * - Assign the result of the function psa_aead_operation_init()
2206 * to the structure, for example:
2207 * \code
2208 * psa_aead_operation_t operation;
2209 * operation = psa_aead_operation_init();
2210 * \endcode
2211 *
2212 * This is an implementation-defined \c struct. Applications should not
2213 * make any assumptions about the content of this structure except
2214 * as directed by the documentation of a specific implementation. */
2215typedef struct psa_aead_operation_s psa_aead_operation_t;
2216
2217/** \def PSA_AEAD_OPERATION_INIT
2218 *
2219 * This macro returns a suitable initializer for an AEAD operation object of
2220 * type #psa_aead_operation_t.
2221 */
2222#ifdef __DOXYGEN_ONLY__
2223/* This is an example definition for documentation purposes.
2224 * Implementations should define a suitable value in `crypto_struct.h`.
2225 */
2226#define PSA_AEAD_OPERATION_INIT {0}
2227#endif
2228
2229/** Return an initial value for an AEAD operation object.
2230 */
2231static psa_aead_operation_t psa_aead_operation_init(void);
2232
2233/** Set the key for a multipart authenticated encryption operation.
2234 *
2235 * The sequence of operations to encrypt a message with authentication
2236 * is as follows:
2237 * -# Allocate an operation object which will be passed to all the functions
2238 * listed here.
2239 * -# Initialize the operation object with one of the methods described in the
2240 * documentation for #psa_aead_operation_t, e.g.
2241 * PSA_AEAD_OPERATION_INIT.
2242 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002243 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2244 * inputs to the subsequent calls to psa_aead_update_ad() and
2245 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2246 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002247 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2248 * generate or set the nonce. You should use
2249 * psa_aead_generate_nonce() unless the protocol you are implementing
2250 * requires a specific nonce value.
2251 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2252 * of the non-encrypted additional authenticated data each time.
2253 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002254 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002255 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002256 *
2257 * The application may call psa_aead_abort() at any time after the operation
2258 * has been initialized.
2259 *
2260 * After a successful call to psa_aead_encrypt_setup(), the application must
2261 * eventually terminate the operation. The following events terminate an
2262 * operation:
2263 * - A failed call to any of the \c psa_aead_xxx functions.
2264 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2265 *
2266 * \param[in,out] operation The operation object to set up. It must have
2267 * been initialized as per the documentation for
2268 * #psa_aead_operation_t and not yet in use.
2269 * \param handle Handle to the key to use for the operation.
2270 * It must remain valid until the operation
2271 * terminates.
2272 * \param alg The AEAD algorithm to compute
2273 * (\c PSA_ALG_XXX value such that
2274 * #PSA_ALG_IS_AEAD(\p alg) is true).
2275 *
2276 * \retval #PSA_SUCCESS
2277 * Success.
2278 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002279 * \retval #PSA_ERROR_NOT_PERMITTED
2280 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002281 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002282 * \retval #PSA_ERROR_NOT_SUPPORTED
2283 * \p alg is not supported or is not an AEAD algorithm.
2284 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2285 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2286 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002287 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002288 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002289 * \retval #PSA_ERROR_BAD_STATE
2290 * The library has not been previously initialized by psa_crypto_init().
2291 * It is implementation-dependent whether a failure to initialize
2292 * results in this error code.
2293 */
2294psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2295 psa_key_handle_t handle,
2296 psa_algorithm_t alg);
2297
2298/** Set the key for a multipart authenticated decryption operation.
2299 *
2300 * The sequence of operations to decrypt a message with authentication
2301 * is as follows:
2302 * -# Allocate an operation object which will be passed to all the functions
2303 * listed here.
2304 * -# Initialize the operation object with one of the methods described in the
2305 * documentation for #psa_aead_operation_t, e.g.
2306 * PSA_AEAD_OPERATION_INIT.
2307 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002308 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2309 * inputs to the subsequent calls to psa_aead_update_ad() and
2310 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2311 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002312 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2313 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2314 * of the non-encrypted additional authenticated data each time.
2315 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002316 * of the ciphertext to decrypt each time.
2317 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002318 *
2319 * The application may call psa_aead_abort() at any time after the operation
2320 * has been initialized.
2321 *
2322 * After a successful call to psa_aead_decrypt_setup(), the application must
2323 * eventually terminate the operation. The following events terminate an
2324 * operation:
2325 * - A failed call to any of the \c psa_aead_xxx functions.
2326 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2327 *
2328 * \param[in,out] operation The operation object to set up. It must have
2329 * been initialized as per the documentation for
2330 * #psa_aead_operation_t and not yet in use.
2331 * \param handle Handle to the key to use for the operation.
2332 * It must remain valid until the operation
2333 * terminates.
2334 * \param alg The AEAD algorithm to compute
2335 * (\c PSA_ALG_XXX value such that
2336 * #PSA_ALG_IS_AEAD(\p alg) is true).
2337 *
2338 * \retval #PSA_SUCCESS
2339 * Success.
2340 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002341 * \retval #PSA_ERROR_NOT_PERMITTED
2342 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002343 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002344 * \retval #PSA_ERROR_NOT_SUPPORTED
2345 * \p alg is not supported or is not an AEAD algorithm.
2346 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2347 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2348 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002349 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002350 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002351 * \retval #PSA_ERROR_BAD_STATE
2352 * The library has not been previously initialized by psa_crypto_init().
2353 * It is implementation-dependent whether a failure to initialize
2354 * results in this error code.
2355 */
2356psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2357 psa_key_handle_t handle,
2358 psa_algorithm_t alg);
2359
2360/** Generate a random nonce for an authenticated encryption operation.
2361 *
2362 * This function generates a random nonce for the authenticated encryption
2363 * operation with an appropriate size for the chosen algorithm, key type
2364 * and key size.
2365 *
2366 * The application must call psa_aead_encrypt_setup() before
2367 * calling this function.
2368 *
2369 * If this function returns an error status, the operation becomes inactive.
2370 *
2371 * \param[in,out] operation Active AEAD operation.
2372 * \param[out] nonce Buffer where the generated nonce is to be
2373 * written.
2374 * \param nonce_size Size of the \p nonce buffer in bytes.
2375 * \param[out] nonce_length On success, the number of bytes of the
2376 * generated nonce.
2377 *
2378 * \retval #PSA_SUCCESS
2379 * Success.
2380 * \retval #PSA_ERROR_BAD_STATE
2381 * The operation state is not valid (not set up, or nonce already set).
2382 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2383 * The size of the \p nonce buffer is too small.
2384 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2385 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2386 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002387 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002388 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002389 * \retval #PSA_ERROR_BAD_STATE
2390 * The library has not been previously initialized by psa_crypto_init().
2391 * It is implementation-dependent whether a failure to initialize
2392 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002393 */
2394psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002395 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002396 size_t nonce_size,
2397 size_t *nonce_length);
2398
2399/** Set the nonce for an authenticated encryption or decryption operation.
2400 *
2401 * This function sets the nonce for the authenticated
2402 * encryption or decryption operation.
2403 *
2404 * The application must call psa_aead_encrypt_setup() before
2405 * calling this function.
2406 *
2407 * If this function returns an error status, the operation becomes inactive.
2408 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002409 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002410 * instead of this function, unless implementing a protocol that requires
2411 * a non-random IV.
2412 *
2413 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002414 * \param[in] nonce Buffer containing the nonce to use.
2415 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002416 *
2417 * \retval #PSA_SUCCESS
2418 * Success.
2419 * \retval #PSA_ERROR_BAD_STATE
2420 * The operation state is not valid (not set up, or nonce already set).
2421 * \retval #PSA_ERROR_INVALID_ARGUMENT
2422 * The size of \p nonce is not acceptable for the chosen algorithm.
2423 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2424 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2425 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002426 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002427 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002428 * \retval #PSA_ERROR_BAD_STATE
2429 * The library has not been previously initialized by psa_crypto_init().
2430 * It is implementation-dependent whether a failure to initialize
2431 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002432 */
2433psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002434 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002435 size_t nonce_length);
2436
Gilles Peskinebc59c852019-01-17 15:26:08 +01002437/** Declare the lengths of the message and additional data for AEAD.
2438 *
2439 * The application must call this function before calling
2440 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2441 * the operation requires it. If the algorithm does not require it,
2442 * calling this function is optional, but if this function is called
2443 * then the implementation must enforce the lengths.
2444 *
2445 * You may call this function before or after setting the nonce with
2446 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2447 *
2448 * - For #PSA_ALG_CCM, calling this function is required.
2449 * - For the other AEAD algorithms defined in this specification, calling
2450 * this function is not required.
2451 * - For vendor-defined algorithm, refer to the vendor documentation.
2452 *
2453 * \param[in,out] operation Active AEAD operation.
2454 * \param ad_length Size of the non-encrypted additional
2455 * authenticated data in bytes.
2456 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2457 *
2458 * \retval #PSA_SUCCESS
2459 * Success.
2460 * \retval #PSA_ERROR_BAD_STATE
2461 * The operation state is not valid (not set up, already completed,
2462 * or psa_aead_update_ad() or psa_aead_update() already called).
2463 * \retval #PSA_ERROR_INVALID_ARGUMENT
2464 * At least one of the lengths is not acceptable for the chosen
2465 * algorithm.
2466 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2467 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2468 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002469 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002470 * \retval #PSA_ERROR_BAD_STATE
2471 * The library has not been previously initialized by psa_crypto_init().
2472 * It is implementation-dependent whether a failure to initialize
2473 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002474 */
2475psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2476 size_t ad_length,
2477 size_t plaintext_length);
2478
Gilles Peskine30a9e412019-01-14 18:36:12 +01002479/** Pass additional data to an active AEAD operation.
2480 *
2481 * Additional data is authenticated, but not encrypted.
2482 *
2483 * You may call this function multiple times to pass successive fragments
2484 * of the additional data. You may not call this function after passing
2485 * data to encrypt or decrypt with psa_aead_update().
2486 *
2487 * Before calling this function, you must:
2488 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2489 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2490 *
2491 * If this function returns an error status, the operation becomes inactive.
2492 *
2493 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2494 * there is no guarantee that the input is valid. Therefore, until
2495 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2496 * treat the input as untrusted and prepare to undo any action that
2497 * depends on the input if psa_aead_verify() returns an error status.
2498 *
2499 * \param[in,out] operation Active AEAD operation.
2500 * \param[in] input Buffer containing the fragment of
2501 * additional data.
2502 * \param input_length Size of the \p input buffer in bytes.
2503 *
2504 * \retval #PSA_SUCCESS
2505 * Success.
2506 * \retval #PSA_ERROR_BAD_STATE
2507 * The operation state is not valid (not set up, nonce not set,
2508 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002509 * \retval #PSA_ERROR_INVALID_ARGUMENT
2510 * The total input length overflows the additional data length that
2511 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002512 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2513 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2514 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002515 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002516 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002517 * \retval #PSA_ERROR_BAD_STATE
2518 * The library has not been previously initialized by psa_crypto_init().
2519 * It is implementation-dependent whether a failure to initialize
2520 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002521 */
2522psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2523 const uint8_t *input,
2524 size_t input_length);
2525
2526/** Encrypt or decrypt a message fragment in an active AEAD operation.
2527 *
2528 * Before calling this function, you must:
2529 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2530 * The choice of setup function determines whether this function
2531 * encrypts or decrypts its input.
2532 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2533 * 3. Call psa_aead_update_ad() to pass all the additional data.
2534 *
2535 * If this function returns an error status, the operation becomes inactive.
2536 *
2537 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2538 * there is no guarantee that the input is valid. Therefore, until
2539 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2540 * - Do not use the output in any way other than storing it in a
2541 * confidential location. If you take any action that depends
2542 * on the tentative decrypted data, this action will need to be
2543 * undone if the input turns out not to be valid. Furthermore,
2544 * if an adversary can observe that this action took place
2545 * (for example through timing), they may be able to use this
2546 * fact as an oracle to decrypt any message encrypted with the
2547 * same key.
2548 * - In particular, do not copy the output anywhere but to a
2549 * memory or storage space that you have exclusive access to.
2550 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002551 * This function does not require the input to be aligned to any
2552 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002553 * a whole block at a time, it must consume all the input provided, but
2554 * it may delay the end of the corresponding output until a subsequent
2555 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2556 * provides sufficient input. The amount of data that can be delayed
2557 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002558 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002559 * \param[in,out] operation Active AEAD operation.
2560 * \param[in] input Buffer containing the message fragment to
2561 * encrypt or decrypt.
2562 * \param input_length Size of the \p input buffer in bytes.
2563 * \param[out] output Buffer where the output is to be written.
2564 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002565 * This must be at least
2566 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2567 * \p input_length) where \c alg is the
2568 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002569 * \param[out] output_length On success, the number of bytes
2570 * that make up the returned output.
2571 *
2572 * \retval #PSA_SUCCESS
2573 * Success.
2574 * \retval #PSA_ERROR_BAD_STATE
2575 * The operation state is not valid (not set up, nonce not set
2576 * or already completed).
2577 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2578 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002579 * You can determine a sufficient buffer size by calling
2580 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2581 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002582 * \retval #PSA_ERROR_INVALID_ARGUMENT
2583 * The total length of input to psa_aead_update_ad() so far is
2584 * less than the additional data length that was previously
2585 * specified with psa_aead_set_lengths().
2586 * \retval #PSA_ERROR_INVALID_ARGUMENT
2587 * The total input length overflows the plaintext length that
2588 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002589 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2590 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2591 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002592 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002593 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002594 * \retval #PSA_ERROR_BAD_STATE
2595 * The library has not been previously initialized by psa_crypto_init().
2596 * It is implementation-dependent whether a failure to initialize
2597 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002598 */
2599psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2600 const uint8_t *input,
2601 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002602 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002603 size_t output_size,
2604 size_t *output_length);
2605
2606/** Finish encrypting a message in an AEAD operation.
2607 *
2608 * The operation must have been set up with psa_aead_encrypt_setup().
2609 *
2610 * This function finishes the authentication of the additional data
2611 * formed by concatenating the inputs passed to preceding calls to
2612 * psa_aead_update_ad() with the plaintext formed by concatenating the
2613 * inputs passed to preceding calls to psa_aead_update().
2614 *
2615 * This function has two output buffers:
2616 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002617 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002618 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002619 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002620 * that the operation performs.
2621 *
2622 * When this function returns, the operation becomes inactive.
2623 *
2624 * \param[in,out] operation Active AEAD operation.
2625 * \param[out] ciphertext Buffer where the last part of the ciphertext
2626 * is to be written.
2627 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002628 * This must be at least
2629 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2630 * \c alg is the algorithm that is being
2631 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002632 * \param[out] ciphertext_length On success, the number of bytes of
2633 * returned ciphertext.
2634 * \param[out] tag Buffer where the authentication tag is
2635 * to be written.
2636 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002637 * This must be at least
2638 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2639 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002640 * \param[out] tag_length On success, the number of bytes
2641 * that make up the returned tag.
2642 *
2643 * \retval #PSA_SUCCESS
2644 * Success.
2645 * \retval #PSA_ERROR_BAD_STATE
2646 * The operation state is not valid (not set up, nonce not set,
2647 * decryption, or already completed).
2648 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002649 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002650 * You can determine a sufficient buffer size for \p ciphertext by
2651 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2652 * where \c alg is the algorithm that is being calculated.
2653 * You can determine a sufficient buffer size for \p tag by
2654 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002655 * \retval #PSA_ERROR_INVALID_ARGUMENT
2656 * The total length of input to psa_aead_update_ad() so far is
2657 * less than the additional data length that was previously
2658 * specified with psa_aead_set_lengths().
2659 * \retval #PSA_ERROR_INVALID_ARGUMENT
2660 * The total length of input to psa_aead_update() so far is
2661 * less than the plaintext length that was previously
2662 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002663 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2664 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2665 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002666 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002667 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002668 * \retval #PSA_ERROR_BAD_STATE
2669 * The library has not been previously initialized by psa_crypto_init().
2670 * It is implementation-dependent whether a failure to initialize
2671 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002672 */
2673psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002674 uint8_t *ciphertext,
2675 size_t ciphertext_size,
2676 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002677 uint8_t *tag,
2678 size_t tag_size,
2679 size_t *tag_length);
2680
2681/** Finish authenticating and decrypting a message in an AEAD operation.
2682 *
2683 * The operation must have been set up with psa_aead_decrypt_setup().
2684 *
2685 * This function finishes the authentication of the additional data
2686 * formed by concatenating the inputs passed to preceding calls to
2687 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2688 * inputs passed to preceding calls to psa_aead_update().
2689 *
2690 * When this function returns, the operation becomes inactive.
2691 *
2692 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002693 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002694 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002695 * from previous calls to psa_aead_update()
2696 * that could not be processed until the end
2697 * of the input.
2698 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002699 * This must be at least
2700 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2701 * \c alg is the algorithm that is being
2702 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002703 * \param[out] plaintext_length On success, the number of bytes of
2704 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002705 * \param[in] tag Buffer containing the authentication tag.
2706 * \param tag_length Size of the \p tag buffer in bytes.
2707 *
2708 * \retval #PSA_SUCCESS
2709 * Success.
2710 * \retval #PSA_ERROR_BAD_STATE
2711 * The operation state is not valid (not set up, nonce not set,
2712 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002713 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2714 * The size of the \p plaintext buffer is too small.
2715 * You can determine a sufficient buffer size for \p plaintext by
2716 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2717 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002718 * \retval #PSA_ERROR_INVALID_ARGUMENT
2719 * The total length of input to psa_aead_update_ad() so far is
2720 * less than the additional data length that was previously
2721 * specified with psa_aead_set_lengths().
2722 * \retval #PSA_ERROR_INVALID_ARGUMENT
2723 * The total length of input to psa_aead_update() so far is
2724 * less than the plaintext length that was previously
2725 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002726 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2727 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2728 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002729 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002730 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002731 * \retval #PSA_ERROR_BAD_STATE
2732 * The library has not been previously initialized by psa_crypto_init().
2733 * It is implementation-dependent whether a failure to initialize
2734 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002735 */
2736psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002737 uint8_t *plaintext,
2738 size_t plaintext_size,
2739 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002740 const uint8_t *tag,
2741 size_t tag_length);
2742
2743/** Abort an AEAD operation.
2744 *
2745 * Aborting an operation frees all associated resources except for the
2746 * \p operation structure itself. Once aborted, the operation object
2747 * can be reused for another operation by calling
2748 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2749 *
2750 * You may call this function any time after the operation object has
2751 * been initialized by any of the following methods:
2752 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2753 * whether it succeeds or not.
2754 * - Initializing the \c struct to all-bits-zero.
2755 * - Initializing the \c struct to logical zeros, e.g.
2756 * `psa_aead_operation_t operation = {0}`.
2757 *
2758 * In particular, calling psa_aead_abort() after the operation has been
2759 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2760 * is safe and has no effect.
2761 *
2762 * \param[in,out] operation Initialized AEAD operation.
2763 *
2764 * \retval #PSA_SUCCESS
2765 * \retval #PSA_ERROR_BAD_STATE
2766 * \p operation is not an active AEAD operation.
2767 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2768 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002769 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002770 * \retval #PSA_ERROR_BAD_STATE
2771 * The library has not been previously initialized by psa_crypto_init().
2772 * It is implementation-dependent whether a failure to initialize
2773 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002774 */
2775psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2776
Gilles Peskine3b555712018-03-03 21:27:57 +01002777/**@}*/
2778
Gilles Peskine20035e32018-02-03 22:44:14 +01002779/** \defgroup asymmetric Asymmetric cryptography
2780 * @{
2781 */
2782
2783/**
2784 * \brief Sign a hash or short message with a private key.
2785 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002786 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002787 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002788 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2789 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2790 * to determine the hash algorithm to use.
2791 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002792 * \param handle Handle to the key to use for the operation.
2793 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002794 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002795 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002796 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002797 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002798 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002799 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002800 * \param[out] signature_length On success, the number of bytes
2801 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002802 *
Gilles Peskine28538492018-07-11 17:34:00 +02002803 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002804 * \retval #PSA_ERROR_INVALID_HANDLE
2805 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002806 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002807 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002808 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002809 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002810 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002811 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002812 * \retval #PSA_ERROR_NOT_SUPPORTED
2813 * \retval #PSA_ERROR_INVALID_ARGUMENT
2814 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2815 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2816 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002817 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002818 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002819 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002820 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002821 * The library has not been previously initialized by psa_crypto_init().
2822 * It is implementation-dependent whether a failure to initialize
2823 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002824 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002825psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002826 psa_algorithm_t alg,
2827 const uint8_t *hash,
2828 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002829 uint8_t *signature,
2830 size_t signature_size,
2831 size_t *signature_length);
2832
2833/**
2834 * \brief Verify the signature a hash or short message using a public key.
2835 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002836 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002837 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002838 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2839 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2840 * to determine the hash algorithm to use.
2841 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002842 * \param handle Handle to the key to use for the operation.
2843 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002844 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002845 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002846 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002847 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002848 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002849 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002850 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002851 *
Gilles Peskine28538492018-07-11 17:34:00 +02002852 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002853 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002854 * \retval #PSA_ERROR_INVALID_HANDLE
2855 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002856 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002857 * The calculation was perfomed successfully, but the passed
2858 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002859 * \retval #PSA_ERROR_NOT_SUPPORTED
2860 * \retval #PSA_ERROR_INVALID_ARGUMENT
2861 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2862 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2863 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002864 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002865 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002866 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002867 * The library has not been previously initialized by psa_crypto_init().
2868 * It is implementation-dependent whether a failure to initialize
2869 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002870 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002871psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002872 psa_algorithm_t alg,
2873 const uint8_t *hash,
2874 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002875 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002876 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002877
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002878/**
2879 * \brief Encrypt a short message with a public key.
2880 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002881 * \param handle Handle to the key to use for the operation.
2882 * It must be a public key or an asymmetric
2883 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002884 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002885 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002886 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002887 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002888 * \param[in] salt A salt or label, if supported by the
2889 * encryption algorithm.
2890 * If the algorithm does not support a
2891 * salt, pass \c NULL.
2892 * If the algorithm supports an optional
2893 * salt and you do not want to pass a salt,
2894 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002895 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002896 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2897 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002898 * \param salt_length Size of the \p salt buffer in bytes.
2899 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002900 * \param[out] output Buffer where the encrypted message is to
2901 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002902 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002903 * \param[out] output_length On success, the number of bytes
2904 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002905 *
Gilles Peskine28538492018-07-11 17:34:00 +02002906 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002907 * \retval #PSA_ERROR_INVALID_HANDLE
2908 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002909 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002910 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002911 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002912 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002913 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002914 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002915 * \retval #PSA_ERROR_NOT_SUPPORTED
2916 * \retval #PSA_ERROR_INVALID_ARGUMENT
2917 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2918 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2919 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002920 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002921 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002922 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002923 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002924 * The library has not been previously initialized by psa_crypto_init().
2925 * It is implementation-dependent whether a failure to initialize
2926 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002927 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002928psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002929 psa_algorithm_t alg,
2930 const uint8_t *input,
2931 size_t input_length,
2932 const uint8_t *salt,
2933 size_t salt_length,
2934 uint8_t *output,
2935 size_t output_size,
2936 size_t *output_length);
2937
2938/**
2939 * \brief Decrypt a short message with a private key.
2940 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002941 * \param handle Handle to the key to use for the operation.
2942 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002943 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002944 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002945 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002946 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002947 * \param[in] salt A salt or label, if supported by the
2948 * encryption algorithm.
2949 * If the algorithm does not support a
2950 * salt, pass \c NULL.
2951 * If the algorithm supports an optional
2952 * salt and you do not want to pass a salt,
2953 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002954 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002955 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2956 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002957 * \param salt_length Size of the \p salt buffer in bytes.
2958 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002959 * \param[out] output Buffer where the decrypted message is to
2960 * be written.
2961 * \param output_size Size of the \c output buffer in bytes.
2962 * \param[out] output_length On success, the number of bytes
2963 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002964 *
Gilles Peskine28538492018-07-11 17:34:00 +02002965 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002966 * \retval #PSA_ERROR_INVALID_HANDLE
2967 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002968 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002969 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002970 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002971 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002972 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002973 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002974 * \retval #PSA_ERROR_NOT_SUPPORTED
2975 * \retval #PSA_ERROR_INVALID_ARGUMENT
2976 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2977 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2978 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002979 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002980 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002981 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2982 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002983 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002984 * The library has not been previously initialized by psa_crypto_init().
2985 * It is implementation-dependent whether a failure to initialize
2986 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002987 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002988psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002989 psa_algorithm_t alg,
2990 const uint8_t *input,
2991 size_t input_length,
2992 const uint8_t *salt,
2993 size_t salt_length,
2994 uint8_t *output,
2995 size_t output_size,
2996 size_t *output_length);
2997
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002998/**@}*/
2999
Gilles Peskine35675b62019-05-16 17:26:11 +02003000/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003001 * @{
3002 */
3003
Gilles Peskine35675b62019-05-16 17:26:11 +02003004/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003005 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003006 * Before calling any function on a key derivation operation object, the
3007 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003008 * - Set the structure to all-bits-zero, for example:
3009 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003010 * psa_key_derivation_operation_t operation;
3011 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003012 * \endcode
3013 * - Initialize the structure to logical zero values, for example:
3014 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003015 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003016 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003017 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003018 * for example:
3019 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003020 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003021 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003022 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003023 * to the structure, for example:
3024 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003025 * psa_key_derivation_operation_t operation;
3026 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003027 * \endcode
3028 *
3029 * This is an implementation-defined \c struct. Applications should not
3030 * make any assumptions about the content of this structure except
3031 * as directed by the documentation of a specific implementation.
3032 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003033typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003034
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003035/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003036 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003037 * This macro returns a suitable initializer for a key derivation operation
3038 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003039 */
3040#ifdef __DOXYGEN_ONLY__
3041/* This is an example definition for documentation purposes.
3042 * Implementations should define a suitable value in `crypto_struct.h`.
3043 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003044#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003045#endif
3046
Gilles Peskine35675b62019-05-16 17:26:11 +02003047/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003048 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003049static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003050
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003051/** Set up a key derivation operation.
3052 *
3053 * A key derivation algorithm takes some inputs and uses them to generate
3054 * a byte stream in a deterministic way.
3055 * This byte stream can be used to produce keys and other
3056 * cryptographic material.
3057 *
3058 * To derive a key:
3059 * - Start with an initialized object of type #psa_key_derivation_operation_t.
3060 * - Call psa_key_derivation_setup() to select the algorithm.
3061 * - Provide the inputs for the key derivation by calling
3062 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3063 * as appropriate. Which inputs are needed, in what order, and whether
3064 * they may be keys and if so of what type depends on the algorithm.
3065 * - Optionally set the operation's maximum capacity with
3066 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3067 * of or after providing inputs. For some algorithms, this step is mandatory
3068 * because the output depends on the maximum capacity.
3069 * - To derive a key, call psa_key_derivation_output_key().
3070 * To derive a byte string for a different purpose, call
3071 * - psa_key_derivation_output_bytes().
3072 * Successive calls to these functions use successive output bytes
3073 * calculated by the key derivation algorithm.
3074 * - Clean up the key derivation operation object with
3075 * psa_key_derivation_abort().
3076 *
3077 * \param[in,out] operation The key derivation operation object
3078 * to set up. It must
3079 * have been initialized but not set up yet.
3080 * \param alg The key derivation algorithm to compute
3081 * (\c PSA_ALG_XXX value such that
3082 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3083 *
3084 * \retval #PSA_SUCCESS
3085 * Success.
3086 * \retval #PSA_ERROR_INVALID_ARGUMENT
3087 * \c alg is not a key derivation algorithm.
3088 * \retval #PSA_ERROR_NOT_SUPPORTED
3089 * \c alg is not supported or is not a key derivation algorithm.
3090 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3091 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3092 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003093 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003094 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003095 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01003096 * The operation state is either not initialized or has already been setup.
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003097 * \retval #PSA_ERROR_BAD_STATE
3098 * The library has not been previously initialized by psa_crypto_init().
3099 * It is implementation-dependent whether a failure to initialize
3100 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003101 */
3102psa_status_t psa_key_derivation_setup(
3103 psa_key_derivation_operation_t *operation,
3104 psa_algorithm_t alg);
3105
Gilles Peskine35675b62019-05-16 17:26:11 +02003106/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003107 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003108 * The capacity of a key derivation is the maximum number of bytes that it can
3109 * return. When you get *N* bytes of output from a key derivation operation,
3110 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003111 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003112 * \param[in] operation The operation to query.
3113 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003114 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003115 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003116 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003117 * \retval #PSA_ERROR_BAD_STATE
3118 * The operation state is not valid.
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003119 * \retval #PSA_ERROR_HARDWARE_FAILURE
3120 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003121 * \retval #PSA_ERROR_BAD_STATE
3122 * The library has not been previously initialized by psa_crypto_init().
3123 * It is implementation-dependent whether a failure to initialize
3124 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003125 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003126psa_status_t psa_key_derivation_get_capacity(
3127 const psa_key_derivation_operation_t *operation,
3128 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003129
Gilles Peskine35675b62019-05-16 17:26:11 +02003130/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003131 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003132 * The capacity of a key derivation operation is the maximum number of bytes
3133 * that the key derivation operation can return from this point onwards.
3134 *
3135 * \param[in,out] operation The key derivation operation object to modify.
3136 * \param capacity The new capacity of the operation.
3137 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003138 * current capacity.
3139 *
3140 * \retval #PSA_SUCCESS
3141 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003142 * \p capacity is larger than the operation's current capacity.
3143 * In this case, the operation object remains valid and its capacity
3144 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003145 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003146 * The operation state is not valid.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003147 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003148 * \retval #PSA_ERROR_HARDWARE_FAILURE
3149 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003150 * \retval #PSA_ERROR_BAD_STATE
3151 * The library has not been previously initialized by psa_crypto_init().
3152 * It is implementation-dependent whether a failure to initialize
3153 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003154 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003155psa_status_t psa_key_derivation_set_capacity(
3156 psa_key_derivation_operation_t *operation,
3157 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003158
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003159/** Use the maximum possible capacity for a key derivation operation.
3160 *
3161 * Use this value as the capacity argument when setting up a key derivation
3162 * to indicate that the operation should have the maximum possible capacity.
3163 * The value of the maximum possible capacity depends on the key derivation
3164 * algorithm.
3165 */
3166#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3167
3168/** Provide an input for key derivation or key agreement.
3169 *
3170 * Which inputs are required and in what order depends on the algorithm.
3171 * Refer to the documentation of each key derivation or key agreement
3172 * algorithm for information.
3173 *
3174 * This function passes direct inputs. Some inputs must be passed as keys
3175 * using psa_key_derivation_input_key() instead of this function. Refer to
3176 * the documentation of individual step types for information.
3177 *
3178 * \param[in,out] operation The key derivation operation object to use.
3179 * It must have been set up with
3180 * psa_key_derivation_setup() and must not
3181 * have produced any output yet.
3182 * \param step Which step the input data is for.
3183 * \param[in] data Input data to use.
3184 * \param data_length Size of the \p data buffer in bytes.
3185 *
3186 * \retval #PSA_SUCCESS
3187 * Success.
3188 * \retval #PSA_ERROR_INVALID_ARGUMENT
3189 * \c step is not compatible with the operation's algorithm.
3190 * \retval #PSA_ERROR_INVALID_ARGUMENT
3191 * \c step does not allow direct inputs.
3192 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3193 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3194 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003195 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003196 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003197 * \retval #PSA_ERROR_BAD_STATE
3198 * The value of \p step is not valid given the state of \p operation.
3199 * \retval #PSA_ERROR_BAD_STATE
3200 * The library has not been previously initialized by psa_crypto_init().
3201 * It is implementation-dependent whether a failure to initialize
3202 * results in this error code.
3203 */
3204psa_status_t psa_key_derivation_input_bytes(
3205 psa_key_derivation_operation_t *operation,
3206 psa_key_derivation_step_t step,
3207 const uint8_t *data,
3208 size_t data_length);
3209
3210/** Provide an input for key derivation in the form of a key.
3211 *
3212 * Which inputs are required and in what order depends on the algorithm.
3213 * Refer to the documentation of each key derivation or key agreement
3214 * algorithm for information.
3215 *
3216 * This function passes key inputs. Some inputs must be passed as keys
3217 * of the appropriate type using this function, while others must be
3218 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3219 * the documentation of individual step types for information.
3220 *
3221 * \param[in,out] operation The key derivation operation object to use.
3222 * It must have been set up with
3223 * psa_key_derivation_setup() and must not
3224 * have produced any output yet.
3225 * \param step Which step the input data is for.
3226 * \param handle Handle to the key. It must have an
3227 * appropriate type for \p step and must
3228 * allow the usage #PSA_KEY_USAGE_DERIVE.
3229 *
3230 * \retval #PSA_SUCCESS
3231 * Success.
3232 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003233 * \retval #PSA_ERROR_NOT_PERMITTED
3234 * \retval #PSA_ERROR_INVALID_ARGUMENT
3235 * \c step is not compatible with the operation's algorithm.
3236 * \retval #PSA_ERROR_INVALID_ARGUMENT
3237 * \c step does not allow key inputs.
3238 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3239 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3240 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003241 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003242 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003243 * \retval #PSA_ERROR_BAD_STATE
3244 * The value of \p step is not valid given the state of \p operation.
3245 * \retval #PSA_ERROR_BAD_STATE
3246 * The library has not been previously initialized by psa_crypto_init().
3247 * It is implementation-dependent whether a failure to initialize
3248 * results in this error code.
3249 */
3250psa_status_t psa_key_derivation_input_key(
3251 psa_key_derivation_operation_t *operation,
3252 psa_key_derivation_step_t step,
3253 psa_key_handle_t handle);
3254
3255/** Perform a key agreement and use the shared secret as input to a key
3256 * derivation.
3257 *
3258 * A key agreement algorithm takes two inputs: a private key \p private_key
3259 * a public key \p peer_key.
3260 * The result of this function is passed as input to a key derivation.
3261 * The output of this key derivation can be extracted by reading from the
3262 * resulting operation to produce keys and other cryptographic material.
3263 *
3264 * \param[in,out] operation The key derivation operation object to use.
3265 * It must have been set up with
3266 * psa_key_derivation_setup() with a
3267 * key agreement and derivation algorithm
3268 * \c alg (\c PSA_ALG_XXX value such that
3269 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3270 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3271 * is false).
3272 * The operation must be ready for an
3273 * input of the type given by \p step.
3274 * \param step Which step the input data is for.
3275 * \param private_key Handle to the private key to use.
3276 * \param[in] peer_key Public key of the peer. The peer key must be in the
3277 * same format that psa_import_key() accepts for the
3278 * public key type corresponding to the type of
3279 * private_key. That is, this function performs the
3280 * equivalent of
3281 * #psa_import_key(...,
3282 * `peer_key`, `peer_key_length`) where
3283 * with key attributes indicating the public key
3284 * type corresponding to the type of `private_key`.
3285 * For example, for EC keys, this means that peer_key
3286 * is interpreted as a point on the curve that the
3287 * private key is on. The standard formats for public
3288 * keys are documented in the documentation of
3289 * psa_export_public_key().
3290 * \param peer_key_length Size of \p peer_key in bytes.
3291 *
3292 * \retval #PSA_SUCCESS
3293 * Success.
3294 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003295 * \retval #PSA_ERROR_NOT_PERMITTED
3296 * \retval #PSA_ERROR_INVALID_ARGUMENT
3297 * \c private_key is not compatible with \c alg,
3298 * or \p peer_key is not valid for \c alg or not compatible with
3299 * \c private_key.
3300 * \retval #PSA_ERROR_NOT_SUPPORTED
3301 * \c alg is not supported or is not a key derivation algorithm.
3302 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3303 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3304 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003305 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003306 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003307 * \retval #PSA_ERROR_BAD_STATE
3308 * The library has not been previously initialized by psa_crypto_init().
3309 * It is implementation-dependent whether a failure to initialize
3310 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003311 */
3312psa_status_t psa_key_derivation_key_agreement(
3313 psa_key_derivation_operation_t *operation,
3314 psa_key_derivation_step_t step,
3315 psa_key_handle_t private_key,
3316 const uint8_t *peer_key,
3317 size_t peer_key_length);
3318
Gilles Peskine35675b62019-05-16 17:26:11 +02003319/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003320 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003321 * This function calculates output bytes from a key derivation algorithm and
3322 * return those bytes.
3323 * If you view the key derivation's output as a stream of bytes, this
3324 * function destructively reads the requested number of bytes from the
3325 * stream.
3326 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003327 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003328 * \param[in,out] operation The key derivation operation object to read from.
3329 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003330 * \param output_length Number of bytes to output.
3331 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003332 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003333 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003334 * The operation's capacity was less than
3335 * \p output_length bytes. Note that in this case,
3336 * no output is written to the output buffer.
3337 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003338 * subsequent calls to this function will not
3339 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003340 * \retval #PSA_ERROR_BAD_STATE
3341 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3342 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3343 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003344 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003345 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003346 * \retval #PSA_ERROR_BAD_STATE
3347 * The library has not been previously initialized by psa_crypto_init().
3348 * It is implementation-dependent whether a failure to initialize
3349 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003350 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003351psa_status_t psa_key_derivation_output_bytes(
3352 psa_key_derivation_operation_t *operation,
3353 uint8_t *output,
3354 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003355
Gilles Peskine35675b62019-05-16 17:26:11 +02003356/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003357 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003358 * This function calculates output bytes from a key derivation algorithm
3359 * and uses those bytes to generate a key deterministically.
3360 * If you view the key derivation's output as a stream of bytes, this
3361 * function destructively reads as many bytes as required from the
3362 * stream.
3363 * The operation's capacity decreases by the number of bytes read.
3364 *
3365 * How much output is produced and consumed from the operation, and how
3366 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003367 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003368 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003369 * of a given size, this function is functionally equivalent to
3370 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003371 * and passing the resulting output to #psa_import_key.
3372 * However, this function has a security benefit:
3373 * if the implementation provides an isolation boundary then
3374 * the key material is not exposed outside the isolation boundary.
3375 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003376 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003377 * The following key types defined in this specification follow this scheme:
3378 *
3379 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003380 * - #PSA_KEY_TYPE_ARC4;
3381 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003382 * - #PSA_KEY_TYPE_DERIVE;
3383 * - #PSA_KEY_TYPE_HMAC.
3384 *
3385 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003386 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003387 * Montgomery curve), this function always draws a byte string whose
3388 * length is determined by the curve, and sets the mandatory bits
3389 * accordingly. That is:
3390 *
3391 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3392 * and process it as specified in RFC 7748 &sect;5.
3393 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3394 * and process it as specified in RFC 7748 &sect;5.
3395 *
3396 * - For key types for which the key is represented by a single sequence of
3397 * \p bits bits with constraints as to which bit sequences are acceptable,
3398 * this function draws a byte string of length (\p bits / 8) bytes rounded
3399 * up to the nearest whole number of bytes. If the resulting byte string
3400 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3401 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003402 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003403 * for the output produced by psa_export_key().
3404 * The following key types defined in this specification follow this scheme:
3405 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003406 * - #PSA_KEY_TYPE_DES.
3407 * Force-set the parity bits, but discard forbidden weak keys.
3408 * For 2-key and 3-key triple-DES, the three keys are generated
3409 * successively (for example, for 3-key triple-DES,
3410 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3411 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003412 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003413 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003414 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003415 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003416 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003417 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003418 * Weierstrass curve).
3419 * For these key types, interpret the byte string as integer
3420 * in big-endian order. Discard it if it is not in the range
3421 * [0, *N* - 2] where *N* is the boundary of the private key domain
3422 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003423 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003424 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003425 * This method allows compliance to NIST standards, specifically
3426 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003427 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3428 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3429 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3430 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003431 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003432 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003433 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003434 * implementation-defined.
3435 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003436 * In all cases, the data that is read is discarded from the operation.
3437 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003438 *
Gilles Peskine20628592019-04-19 19:29:50 +02003439 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003440 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003441 * \param[out] handle On success, a handle to the newly created key.
3442 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003443 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003444 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003445 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003446 * If the key is persistent, the key material and the key's metadata
3447 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003448 * \retval #PSA_ERROR_ALREADY_EXISTS
3449 * This is an attempt to create a persistent key, and there is
3450 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003451 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003452 * There was not enough data to create the desired key.
3453 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003454 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003455 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003456 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003457 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003458 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003459 * \retval #PSA_ERROR_INVALID_ARGUMENT
3460 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003461 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003462 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3463 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3464 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3465 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003466 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003467 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003468 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003469 * The library has not been previously initialized by psa_crypto_init().
3470 * It is implementation-dependent whether a failure to initialize
3471 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003472 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003473psa_status_t psa_key_derivation_output_key(
3474 const psa_key_attributes_t *attributes,
3475 psa_key_derivation_operation_t *operation,
3476 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003477
Gilles Peskine35675b62019-05-16 17:26:11 +02003478/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003479 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003480 * Once a key derivation operation has been aborted, its capacity is zero.
3481 * Aborting an operation frees all associated resources except for the
3482 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003483 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003484 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003485 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003486 * psa_key_derivation_operation_init() or a zero value. In particular,
3487 * it is valid to call psa_key_derivation_abort() twice, or to call
3488 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003489 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003490 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003491 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003492 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003493 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003494 * \retval #PSA_SUCCESS
3495 * \retval #PSA_ERROR_BAD_STATE
3496 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3497 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003498 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003499 * \retval #PSA_ERROR_BAD_STATE
3500 * The library has not been previously initialized by psa_crypto_init().
3501 * It is implementation-dependent whether a failure to initialize
3502 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003503 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003504psa_status_t psa_key_derivation_abort(
3505 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003506
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003507/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003508 *
3509 * \warning The raw result of a key agreement algorithm such as finite-field
3510 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3511 * not be used directly as key material. It should instead be passed as
3512 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003513 * a key derivation, use psa_key_derivation_key_agreement() and other
3514 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003515 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003516 * \param alg The key agreement algorithm to compute
3517 * (\c PSA_ALG_XXX value such that
3518 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3519 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003520 * \param private_key Handle to the private key to use.
3521 * \param[in] peer_key Public key of the peer. It must be
3522 * in the same format that psa_import_key()
3523 * accepts. The standard formats for public
3524 * keys are documented in the documentation
3525 * of psa_export_public_key().
3526 * \param peer_key_length Size of \p peer_key in bytes.
3527 * \param[out] output Buffer where the decrypted message is to
3528 * be written.
3529 * \param output_size Size of the \c output buffer in bytes.
3530 * \param[out] output_length On success, the number of bytes
3531 * that make up the returned output.
3532 *
3533 * \retval #PSA_SUCCESS
3534 * Success.
3535 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003536 * \retval #PSA_ERROR_NOT_PERMITTED
3537 * \retval #PSA_ERROR_INVALID_ARGUMENT
3538 * \p alg is not a key agreement algorithm
3539 * \retval #PSA_ERROR_INVALID_ARGUMENT
3540 * \p private_key is not compatible with \p alg,
3541 * or \p peer_key is not valid for \p alg or not compatible with
3542 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003543 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3544 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003545 * \retval #PSA_ERROR_NOT_SUPPORTED
3546 * \p alg is not a supported key agreement algorithm.
3547 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3548 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3549 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003550 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003551 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003552 * \retval #PSA_ERROR_BAD_STATE
3553 * The library has not been previously initialized by psa_crypto_init().
3554 * It is implementation-dependent whether a failure to initialize
3555 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003556 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003557psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3558 psa_key_handle_t private_key,
3559 const uint8_t *peer_key,
3560 size_t peer_key_length,
3561 uint8_t *output,
3562 size_t output_size,
3563 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003564
Gilles Peskineea0fb492018-07-12 17:17:20 +02003565/**@}*/
3566
Gilles Peskineedd76872018-07-20 17:42:05 +02003567/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003568 * @{
3569 */
3570
3571/**
3572 * \brief Generate random bytes.
3573 *
3574 * \warning This function **can** fail! Callers MUST check the return status
3575 * and MUST NOT use the content of the output buffer if the return
3576 * status is not #PSA_SUCCESS.
3577 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003578 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003579 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003580 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003581 * \param output_size Number of bytes to generate and output.
3582 *
Gilles Peskine28538492018-07-11 17:34:00 +02003583 * \retval #PSA_SUCCESS
3584 * \retval #PSA_ERROR_NOT_SUPPORTED
3585 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003586 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003587 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3588 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003589 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003590 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003591 * The library has not been previously initialized by psa_crypto_init().
3592 * It is implementation-dependent whether a failure to initialize
3593 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003594 */
3595psa_status_t psa_generate_random(uint8_t *output,
3596 size_t output_size);
3597
3598/**
3599 * \brief Generate a key or key pair.
3600 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003601 * The key is generated randomly.
3602 * Its location, policy, type and size are taken from \p attributes.
3603 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003604 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003605 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003606 * the public exponent is 65537.
3607 * The modulus is a product of two probabilistic primes
3608 * between 2^{n-1} and 2^n where n is the bit size specified in the
3609 * attributes.
3610 *
Gilles Peskine20628592019-04-19 19:29:50 +02003611 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003612 * \param[out] handle On success, a handle to the newly created key.
3613 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003614 *
Gilles Peskine28538492018-07-11 17:34:00 +02003615 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003616 * Success.
3617 * If the key is persistent, the key material and the key's metadata
3618 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003619 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003620 * This is an attempt to create a persistent key, and there is
3621 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003622 * \retval #PSA_ERROR_NOT_SUPPORTED
3623 * \retval #PSA_ERROR_INVALID_ARGUMENT
3624 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3625 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3626 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3627 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003628 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003629 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3630 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003631 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003632 * The library has not been previously initialized by psa_crypto_init().
3633 * It is implementation-dependent whether a failure to initialize
3634 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003635 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003636psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003637 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003638
3639/**@}*/
3640
Gilles Peskinee59236f2018-01-27 23:32:46 +01003641#ifdef __cplusplus
3642}
3643#endif
3644
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003645/* The file "crypto_sizes.h" contains definitions for size calculation
3646 * macros whose definitions are implementation-specific. */
3647#include "crypto_sizes.h"
3648
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003649/* The file "crypto_struct.h" contains definitions for
3650 * implementation-specific structs that are declared above. */
3651#include "crypto_struct.h"
3652
3653/* The file "crypto_extra.h" contains vendor-specific definitions. This
3654 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003655#include "crypto_extra.h"
3656
3657#endif /* PSA_CRYPTO_H */