blob: 2f5ec018cabbbdad14eaf20886eeff37eff0dac9 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +020089 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020090 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine105f67f2019-07-23 18:16:05 +020096/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +020097 * @{
98 */
99
Gilles Peskinea0c06552019-05-21 15:54:54 +0200100/** \def PSA_KEY_ATTRIBUTES_INIT
101 *
102 * This macro returns a suitable initializer for a key attribute structure
103 * of type #psa_key_attributes_t.
104 */
105#ifdef __DOXYGEN_ONLY__
106/* This is an example definition for documentation purposes.
107 * Implementations should define a suitable value in `crypto_struct.h`.
108 */
109#define PSA_KEY_ATTRIBUTES_INIT {0}
110#endif
111
112/** Return an initial value for a key attributes structure.
113 */
114static psa_key_attributes_t psa_key_attributes_init(void);
115
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200116/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200117 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200118 * If the attribute structure currently declares the key as volatile (which
119 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200120 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200121 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200122 * This function does not access storage, it merely stores the given
123 * value in the structure.
124 * The persistent key will be written to storage when the attribute
125 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200126 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200127 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200128 *
Gilles Peskine20628592019-04-19 19:29:50 +0200129 * This function may be declared as `static` (i.e. without external
130 * linkage). This function may be provided as a function-like macro,
131 * but in this case it must evaluate each of its arguments exactly once.
132 *
133 * \param[out] attributes The attribute structure to write to.
134 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200135 */
136static void psa_set_key_id(psa_key_attributes_t *attributes,
137 psa_key_id_t id);
138
139/** Set the location of a persistent key.
140 *
141 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200142 * with psa_set_key_id(). By default, a key that has a persistent identifier
143 * is stored in the default storage area identifier by
144 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
145 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200146 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200147 * This function does not access storage, it merely stores the given
148 * value in the structure.
149 * The persistent key will be written to storage when the attribute
150 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200151 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200152 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200153 *
154 * This function may be declared as `static` (i.e. without external
155 * linkage). This function may be provided as a function-like macro,
156 * but in this case it must evaluate each of its arguments exactly once.
157 *
158 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200159 * \param lifetime The lifetime for the key.
160 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200161 * key will be volatile, and the key identifier
162 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200163 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200164static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
165 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200166
Gilles Peskine20628592019-04-19 19:29:50 +0200167/** Retrieve the key identifier from key attributes.
168 *
169 * This function may be declared as `static` (i.e. without external
170 * linkage). This function may be provided as a function-like macro,
171 * but in this case it must evaluate its argument exactly once.
172 *
173 * \param[in] attributes The key attribute structure to query.
174 *
175 * \return The persistent identifier stored in the attribute structure.
176 * This value is unspecified if the attribute structure declares
177 * the key as volatile.
178 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200179static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
180
Gilles Peskine20628592019-04-19 19:29:50 +0200181/** Retrieve the lifetime from key attributes.
182 *
183 * This function may be declared as `static` (i.e. without external
184 * linkage). This function may be provided as a function-like macro,
185 * but in this case it must evaluate its argument exactly once.
186 *
187 * \param[in] attributes The key attribute structure to query.
188 *
189 * \return The lifetime value stored in the attribute structure.
190 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200191static psa_key_lifetime_t psa_get_key_lifetime(
192 const psa_key_attributes_t *attributes);
193
Gilles Peskine20628592019-04-19 19:29:50 +0200194/** Declare usage flags for a key.
195 *
196 * Usage flags are part of a key's usage policy. They encode what
197 * kind of operations are permitted on the key. For more details,
198 * refer to the documentation of the type #psa_key_usage_t.
199 *
200 * This function overwrites any usage flags
201 * previously set in \p attributes.
202 *
203 * This function may be declared as `static` (i.e. without external
204 * linkage). This function may be provided as a function-like macro,
205 * but in this case it must evaluate each of its arguments exactly once.
206 *
207 * \param[out] attributes The attribute structure to write to.
208 * \param usage_flags The usage flags to write.
209 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200210static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
211 psa_key_usage_t usage_flags);
212
Gilles Peskine20628592019-04-19 19:29:50 +0200213/** Retrieve the usage flags from key attributes.
214 *
215 * This function may be declared as `static` (i.e. without external
216 * linkage). This function may be provided as a function-like macro,
217 * but in this case it must evaluate its argument exactly once.
218 *
219 * \param[in] attributes The key attribute structure to query.
220 *
221 * \return The usage flags stored in the attribute structure.
222 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200223static psa_key_usage_t psa_get_key_usage_flags(
224 const psa_key_attributes_t *attributes);
225
Gilles Peskine20628592019-04-19 19:29:50 +0200226/** Declare the permitted algorithm policy for a key.
227 *
228 * The permitted algorithm policy of a key encodes which algorithm or
229 * algorithms are permitted to be used with this key.
230 *
231 * This function overwrites any algorithm policy
232 * previously set in \p attributes.
233 *
234 * This function may be declared as `static` (i.e. without external
235 * linkage). This function may be provided as a function-like macro,
236 * but in this case it must evaluate each of its arguments exactly once.
237 *
238 * \param[out] attributes The attribute structure to write to.
239 * \param alg The permitted algorithm policy to write.
240 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200241static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
242 psa_algorithm_t alg);
243
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100244
Gilles Peskine20628592019-04-19 19:29:50 +0200245/** Retrieve the algorithm policy from key attributes.
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate its argument exactly once.
250 *
251 * \param[in] attributes The key attribute structure to query.
252 *
253 * \return The algorithm stored in the attribute structure.
254 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200255static psa_algorithm_t psa_get_key_algorithm(
256 const psa_key_attributes_t *attributes);
257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Declare the type of a key.
259 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200260 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200261 * previously set in \p attributes.
262 *
263 * This function may be declared as `static` (i.e. without external
264 * linkage). This function may be provided as a function-like macro,
265 * but in this case it must evaluate each of its arguments exactly once.
266 *
267 * \param[out] attributes The attribute structure to write to.
268 * \param type The key type to write.
269 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200270static void psa_set_key_type(psa_key_attributes_t *attributes,
271 psa_key_type_t type);
272
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100273
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200274/** Declare the size of a key.
275 *
276 * This function overwrites any key size previously set in \p attributes.
277 *
278 * This function may be declared as `static` (i.e. without external
279 * linkage). This function may be provided as a function-like macro,
280 * but in this case it must evaluate each of its arguments exactly once.
281 *
282 * \param[out] attributes The attribute structure to write to.
283 * \param bits The key size in bits.
284 */
285static void psa_set_key_bits(psa_key_attributes_t *attributes,
286 size_t bits);
287
Gilles Peskine20628592019-04-19 19:29:50 +0200288/** Retrieve the key type from key attributes.
289 *
290 * This function may be declared as `static` (i.e. without external
291 * linkage). This function may be provided as a function-like macro,
292 * but in this case it must evaluate its argument exactly once.
293 *
294 * \param[in] attributes The key attribute structure to query.
295 *
296 * \return The key type stored in the attribute structure.
297 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200298static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
299
Gilles Peskine20628592019-04-19 19:29:50 +0200300/** Retrieve the key size from key attributes.
301 *
302 * This function may be declared as `static` (i.e. without external
303 * linkage). This function may be provided as a function-like macro,
304 * but in this case it must evaluate its argument exactly once.
305 *
306 * \param[in] attributes The key attribute structure to query.
307 *
308 * \return The key size stored in the attribute structure, in bits.
309 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200310static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
311
Gilles Peskine20628592019-04-19 19:29:50 +0200312/** Retrieve the attributes of a key.
313 *
314 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200315 * psa_reset_key_attributes(). It then copies the attributes of
316 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200317 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200318 * \note This function may allocate memory or other resources.
319 * Once you have called this function on an attribute structure,
320 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200321 *
Gilles Peskine20628592019-04-19 19:29:50 +0200322 * \param[in] handle Handle to the key to query.
323 * \param[in,out] attributes On success, the attributes of the key.
324 * On failure, equivalent to a
325 * freshly-initialized structure.
326 *
327 * \retval #PSA_SUCCESS
328 * \retval #PSA_ERROR_INVALID_HANDLE
329 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
330 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100331 * \retval #PSA_ERROR_CORRUPTION_DETECTED
332 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine20628592019-04-19 19:29:50 +0200333 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200334psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
335 psa_key_attributes_t *attributes);
336
Gilles Peskine20628592019-04-19 19:29:50 +0200337/** Reset a key attribute structure to a freshly initialized state.
338 *
339 * You must initialize the attribute structure as described in the
340 * documentation of the type #psa_key_attributes_t before calling this
341 * function. Once the structure has been initialized, you may call this
342 * function at any time.
343 *
344 * This function frees any auxiliary resources that the structure
345 * may contain.
346 *
347 * \param[in,out] attributes The attribute structure to reset.
348 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200349void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200350
Gilles Peskine87a5e562019-04-17 12:28:25 +0200351/**@}*/
352
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100353/** \defgroup key_management Key management
354 * @{
355 */
356
Gilles Peskinef535eb22018-11-30 14:08:36 +0100357/** Open a handle to an existing persistent key.
358 *
Gilles Peskine4754cde2019-05-21 15:56:29 +0200359 * Open a handle to a persistent key. A key is persistent if it was created
360 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
361 * always has a nonzero key identifier, set with psa_set_key_id() when
362 * creating the key. Implementations may provide additional pre-provisioned
Andrew Thoelke203491c2019-08-21 17:55:30 +0100363 * keys that can be opened with psa_open_key(). Such keys have a key identifier
364 * in the vendor range, as documented in the description of #psa_key_id_t.
Gilles Peskine4754cde2019-05-21 15:56:29 +0200365 *
366 * The application must eventually close the handle with psa_close_key()
367 * to release associated resources. If the application dies without calling
368 * psa_close_key(), the implementation should perform the equivalent of a
369 * call to psa_close_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100370 *
Andrew Thoelke9741b112019-08-21 18:20:41 +0100371 * Some implementations permit an application to open the same key multiple
372 * times. Applications that rely on this behavior will not be portable to
373 * implementations that only permit a single key handle to be opened. See
374 * also :ref:\`key-handles\`.
375 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100376 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100377 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100378 *
379 * \retval #PSA_SUCCESS
380 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100381 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100382 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100383 * The implementation does not have sufficient resources to open the
384 * key. This can be due to reaching an implementation limit on the
385 * number of open keys, the number of open key handles, or available
386 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200387 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100388 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100389 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100390 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100391 * \retval #PSA_ERROR_NOT_PERMITTED
392 * The specified key exists, but the application does not have the
393 * permission to access it. Note that this specification does not
394 * define any way to create such a key, but it may be possible
395 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200396 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
397 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100398 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200399psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100400 psa_key_handle_t *handle);
401
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100402
Gilles Peskinef535eb22018-11-30 14:08:36 +0100403/** Close a key handle.
404 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100405 * If the handle designates a volatile key, this will destroy the key material
406 * and free all associated resources, just like psa_destroy_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100407 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100408 * If this is the last open handle to a persistent key, then closing the handle
409 * will free all resources associated with the key in volatile memory. The key
410 * data in persistent storage is not affected and can be opened again later
411 * with a call to psa_open_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100412 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100413 * Closing the key handle makes the handle invalid, and the key handle
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100414 * must not be used again by the application.
Andrew Thoelke3daba812019-08-21 22:46:56 +0100415 *
416 * If the key is currently in use in a multipart operation, then closing the
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100417 * last remaining handle to the key will abort the multipart operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100418 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100419 * \param handle The key handle to close.
420 *
421 * \retval #PSA_SUCCESS
422 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100423 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100424 */
425psa_status_t psa_close_key(psa_key_handle_t handle);
426
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100427/**@}*/
428
429/** \defgroup import_export Key import and export
430 * @{
431 */
432
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100433/**
434 * \brief Import a key in binary format.
435 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100436 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100437 * documentation of psa_export_public_key() for the format of public keys
438 * and to the documentation of psa_export_key() for the format for
439 * other key types.
440 *
441 * This specification supports a single format for each key type.
442 * Implementations may support other formats as long as the standard
443 * format is supported. Implementations that support other formats
444 * should ensure that the formats are clearly unambiguous so as to
445 * minimize the risk that an invalid input is accidentally interpreted
446 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100447 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100448
Gilles Peskine20628592019-04-19 19:29:50 +0200449 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200450 * The key size is always determined from the
451 * \p data buffer.
452 * If the key size in \p attributes is nonzero,
453 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200454 * \param[out] handle On success, a handle to the newly created key.
455 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100456 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200457 * buffer is interpreted according to the type declared
458 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200459 * All implementations must support at least the format
460 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100461 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200462 * the chosen type. Implementations may allow other
463 * formats, but should be conservative: implementations
464 * should err on the side of rejecting content if it
465 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200466 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100467 *
Gilles Peskine28538492018-07-11 17:34:00 +0200468 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100469 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100470 * If the key is persistent, the key material and the key's metadata
471 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200472 * \retval #PSA_ERROR_ALREADY_EXISTS
473 * This is an attempt to create a persistent key, and there is
474 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200475 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200476 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200477 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200478 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200479 * The key attributes, as a whole, are invalid.
480 * \retval #PSA_ERROR_INVALID_ARGUMENT
481 * The key data is not correctly formatted.
482 * \retval #PSA_ERROR_INVALID_ARGUMENT
483 * The size in \p attributes is nonzero and does not match the size
484 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200485 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
486 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
487 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100488 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200489 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200490 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300491 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300492 * The library has not been previously initialized by psa_crypto_init().
493 * It is implementation-dependent whether a failure to initialize
494 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100495 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200496psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100497 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200498 size_t data_length,
499 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100500
501/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100502 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200503 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100504 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200505 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100506 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200507 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100508 * This function also erases any metadata such as policies and frees all
509 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200510 *
Andrew Thoelke07f16b72019-08-21 22:48:47 +0100511 * Destroying a key will invalidate all existing handles to the key.
512 *
513 * If the key is currently in use in a multipart operation, then destroying the
514 * key will abort the multipart operation.
515 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100516 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100517 *
Gilles Peskine28538492018-07-11 17:34:00 +0200518 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100519 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200520 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100521 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200522 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100523 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200524 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200525 * There was an failure in communication with the cryptoprocessor.
526 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200527 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200528 * The storage is corrupted. Implementations shall make a best effort
529 * to erase key material even in this stage, however applications
530 * should be aware that it may be impossible to guarantee that the
531 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200532 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200533 * An unexpected condition which is not a storage corruption or
534 * a communication failure occurred. The cryptoprocessor may have
535 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300536 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300537 * The library has not been previously initialized by psa_crypto_init().
538 * It is implementation-dependent whether a failure to initialize
539 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100540 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100541psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542
543/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100544 * \brief Export a key in binary format.
545 *
546 * The output of this function can be passed to psa_import_key() to
547 * create an equivalent object.
548 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100549 * If the implementation of psa_import_key() supports other formats
550 * beyond the format specified here, the output from psa_export_key()
551 * must use the representation specified here, not the original
552 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100553 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100554 * For standard key types, the output format is as follows:
555 *
556 * - For symmetric keys (including MAC keys), the format is the
557 * raw bytes of the key.
558 * - For DES, the key data consists of 8 bytes. The parity bits must be
559 * correct.
560 * - For Triple-DES, the format is the concatenation of the
561 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200562 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200563 * is the non-encrypted DER encoding of the representation defined by
564 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
565 * ```
566 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200567 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200568 * modulus INTEGER, -- n
569 * publicExponent INTEGER, -- e
570 * privateExponent INTEGER, -- d
571 * prime1 INTEGER, -- p
572 * prime2 INTEGER, -- q
573 * exponent1 INTEGER, -- d mod (p-1)
574 * exponent2 INTEGER, -- d mod (q-1)
575 * coefficient INTEGER, -- (inverse of q) mod p
576 * }
577 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200578 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200579 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100580 * a representation of the private value as a `ceiling(m/8)`-byte string
581 * where `m` is the bit size associated with the curve, i.e. the bit size
582 * of the order of the curve's coordinate field. This byte string is
583 * in little-endian order for Montgomery curves (curve types
584 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
585 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
586 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100587 * This is the content of the `privateKey` field of the `ECPrivateKey`
588 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200589 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200590 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000591 * format is the representation of the private key `x` as a big-endian byte
592 * string. The length of the byte string is the private key size in bytes
593 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200594 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
595 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100596 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200597 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
598 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100599 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200600 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200601 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200602 * \param[out] data_length On success, the number of bytes
603 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100604 *
Gilles Peskine28538492018-07-11 17:34:00 +0200605 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100606 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200607 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200608 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100609 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200610 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
611 * The size of the \p data buffer is too small. You can determine a
612 * sufficient buffer size by calling
613 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
614 * where \c type is the key type
615 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200616 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
617 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200618 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100619 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100620 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300621 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300622 * The library has not been previously initialized by psa_crypto_init().
623 * It is implementation-dependent whether a failure to initialize
624 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100625 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100626psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100627 uint8_t *data,
628 size_t data_size,
629 size_t *data_length);
630
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100631/**
632 * \brief Export a public key or the public part of a key pair in binary format.
633 *
634 * The output of this function can be passed to psa_import_key() to
635 * create an object that is equivalent to the public key.
636 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000637 * This specification supports a single format for each key type.
638 * Implementations may support other formats as long as the standard
639 * format is supported. Implementations that support other formats
640 * should ensure that the formats are clearly unambiguous so as to
641 * minimize the risk that an invalid input is accidentally interpreted
642 * according to a different format.
643 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000644 * For standard key types, the output format is as follows:
645 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
646 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
647 * ```
648 * RSAPublicKey ::= SEQUENCE {
649 * modulus INTEGER, -- n
650 * publicExponent INTEGER } -- e
651 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000652 * - For elliptic curve public keys (key types for which
653 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
654 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
655 * Let `m` be the bit size associated with the curve, i.e. the bit size of
656 * `q` for a curve over `F_q`. The representation consists of:
657 * - The byte 0x04;
658 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
659 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200660 * - For Diffie-Hellman key exchange public keys (key types for which
661 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000662 * the format is the representation of the public key `y = g^x mod p` as a
663 * big-endian byte string. The length of the byte string is the length of the
664 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100665 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200666 * Exporting a public key object or the public part of a key pair is
667 * always permitted, regardless of the key's usage flags.
668 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100669 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200670 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200671 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200672 * \param[out] data_length On success, the number of bytes
673 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100674 *
Gilles Peskine28538492018-07-11 17:34:00 +0200675 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100676 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200677 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200678 * The key is neither a public key nor a key pair.
679 * \retval #PSA_ERROR_NOT_SUPPORTED
680 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
681 * The size of the \p data buffer is too small. You can determine a
682 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200683 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200684 * where \c type is the key type
685 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200686 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
687 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200688 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100689 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100690 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300691 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300692 * The library has not been previously initialized by psa_crypto_init().
693 * It is implementation-dependent whether a failure to initialize
694 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100695 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100696psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100697 uint8_t *data,
698 size_t data_size,
699 size_t *data_length);
700
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100701/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100702 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100703 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000704 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100705 * This function is primarily useful to copy a key from one location
706 * to another, since it populates a key using the material from
707 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200708 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100709 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100710 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100711 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200712 * The policy on the source key must have the usage flag
713 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200714 * This flag is sufficient to permit the copy if the key has the lifetime
715 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
716 * Some secure elements do not provide a way to copy a key without
717 * making it extractable from the secure element. If a key is located
718 * in such a secure element, then the key must have both usage flags
719 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
720 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200721 *
Gilles Peskine20628592019-04-19 19:29:50 +0200722 * The resulting key may only be used in a way that conforms to
723 * both the policy of the original key and the policy specified in
724 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100725 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200726 * usage flags on the source policy and the usage flags in \p attributes.
727 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100728 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200729 * - If either of the policies allows an algorithm and the other policy
730 * allows a wildcard-based algorithm policy that includes this algorithm,
731 * the resulting key allows the same algorithm.
732 * - If the policies do not allow any algorithm in common, this function
733 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200734 *
Gilles Peskine20628592019-04-19 19:29:50 +0200735 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100736 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200737 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100738 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200739 * \param[in] attributes The attributes for the new key.
740 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200741 * - The key type and size may be 0. If either is
742 * nonzero, it must match the corresponding
743 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200744 * - The key location (the lifetime and, for
745 * persistent keys, the key identifier) is
746 * used directly.
747 * - The policy constraints (usage flags and
748 * algorithm policy) are combined from
749 * the source key and \p attributes so that
750 * both sets of restrictions apply, as
751 * described in the documentation of this function.
752 * \param[out] target_handle On success, a handle to the newly created key.
753 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200754 *
755 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100756 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200757 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200758 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200759 * This is an attempt to create a persistent key, and there is
760 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200761 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200762 * The lifetime or identifier in \p attributes are invalid.
763 * \retval #PSA_ERROR_INVALID_ARGUMENT
764 * The policy constraints on the source and specified in
765 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200766 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200767 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200768 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100769 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200770 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
771 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100772 * The source key is not exportable and its lifetime does not
773 * allow copying it to the target's lifetime.
774 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
775 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200776 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
777 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shaw60b03202019-08-06 17:26:16 +0100778 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200779 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100780 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100781psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200782 const psa_key_attributes_t *attributes,
783 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100784
785/**@}*/
786
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100787/** \defgroup hash Message digests
788 * @{
789 */
790
Gilles Peskine69647a42019-01-14 20:18:12 +0100791/** Calculate the hash (digest) of a message.
792 *
793 * \note To verify the hash of a message against an
794 * expected value, use psa_hash_compare() instead.
795 *
796 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
797 * such that #PSA_ALG_IS_HASH(\p alg) is true).
798 * \param[in] input Buffer containing the message to hash.
799 * \param input_length Size of the \p input buffer in bytes.
800 * \param[out] hash Buffer where the hash is to be written.
801 * \param hash_size Size of the \p hash buffer in bytes.
802 * \param[out] hash_length On success, the number of bytes
803 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100804 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100805 *
806 * \retval #PSA_SUCCESS
807 * Success.
808 * \retval #PSA_ERROR_NOT_SUPPORTED
809 * \p alg is not supported or is not a hash algorithm.
810 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
811 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
812 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200813 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100814 */
815psa_status_t psa_hash_compute(psa_algorithm_t alg,
816 const uint8_t *input,
817 size_t input_length,
818 uint8_t *hash,
819 size_t hash_size,
820 size_t *hash_length);
821
822/** Calculate the hash (digest) of a message and compare it with a
823 * reference value.
824 *
825 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
826 * such that #PSA_ALG_IS_HASH(\p alg) is true).
827 * \param[in] input Buffer containing the message to hash.
828 * \param input_length Size of the \p input buffer in bytes.
829 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100830 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100831 *
832 * \retval #PSA_SUCCESS
833 * The expected hash is identical to the actual hash of the input.
834 * \retval #PSA_ERROR_INVALID_SIGNATURE
835 * The hash of the message was calculated successfully, but it
836 * differs from the expected hash.
837 * \retval #PSA_ERROR_NOT_SUPPORTED
838 * \p alg is not supported or is not a hash algorithm.
839 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
840 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
841 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200842 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100843 */
844psa_status_t psa_hash_compare(psa_algorithm_t alg,
845 const uint8_t *input,
846 size_t input_length,
847 const uint8_t *hash,
848 const size_t hash_length);
849
Gilles Peskine308b91d2018-02-08 09:47:44 +0100850/** The type of the state data structure for multipart hash operations.
851 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000852 * Before calling any function on a hash operation object, the application must
853 * initialize it by any of the following means:
854 * - Set the structure to all-bits-zero, for example:
855 * \code
856 * psa_hash_operation_t operation;
857 * memset(&operation, 0, sizeof(operation));
858 * \endcode
859 * - Initialize the structure to logical zero values, for example:
860 * \code
861 * psa_hash_operation_t operation = {0};
862 * \endcode
863 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
864 * for example:
865 * \code
866 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
867 * \endcode
868 * - Assign the result of the function psa_hash_operation_init()
869 * to the structure, for example:
870 * \code
871 * psa_hash_operation_t operation;
872 * operation = psa_hash_operation_init();
873 * \endcode
874 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100875 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100876 * make any assumptions about the content of this structure except
877 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100878typedef struct psa_hash_operation_s psa_hash_operation_t;
879
Jaeden Amero6a25b412019-01-04 11:47:44 +0000880/** \def PSA_HASH_OPERATION_INIT
881 *
882 * This macro returns a suitable initializer for a hash operation object
883 * of type #psa_hash_operation_t.
884 */
885#ifdef __DOXYGEN_ONLY__
886/* This is an example definition for documentation purposes.
887 * Implementations should define a suitable value in `crypto_struct.h`.
888 */
889#define PSA_HASH_OPERATION_INIT {0}
890#endif
891
892/** Return an initial value for a hash operation object.
893 */
894static psa_hash_operation_t psa_hash_operation_init(void);
895
Gilles Peskinef45adda2019-01-14 18:29:18 +0100896/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100897 *
898 * The sequence of operations to calculate a hash (message digest)
899 * is as follows:
900 * -# Allocate an operation object which will be passed to all the functions
901 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000902 * -# Initialize the operation object with one of the methods described in the
903 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200904 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100905 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100906 * of the message each time. The hash that is calculated is the hash
907 * of the concatenation of these messages in order.
908 * -# To calculate the hash, call psa_hash_finish().
909 * To compare the hash with an expected value, call psa_hash_verify().
910 *
911 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000912 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100913 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200914 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100915 * eventually terminate the operation. The following events terminate an
916 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100917 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100918 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100919 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000920 * \param[in,out] operation The operation object to set up. It must have
921 * been initialized as per the documentation for
922 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200923 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
924 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100925 *
Gilles Peskine28538492018-07-11 17:34:00 +0200926 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100927 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200928 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200929 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100930 * \retval #PSA_ERROR_BAD_STATE
931 * The operation state is not valid (already set up and not
932 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200933 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
934 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
935 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200936 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100937 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200938psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100939 psa_algorithm_t alg);
940
Gilles Peskine308b91d2018-02-08 09:47:44 +0100941/** Add a message fragment to a multipart hash operation.
942 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200943 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100944 *
945 * If this function returns an error status, the operation becomes inactive.
946 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200947 * \param[in,out] operation Active hash operation.
948 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200949 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100950 *
Gilles Peskine28538492018-07-11 17:34:00 +0200951 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100952 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200953 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100954 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200955 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
956 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
957 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200958 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100959 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100960psa_status_t psa_hash_update(psa_hash_operation_t *operation,
961 const uint8_t *input,
962 size_t input_length);
963
Gilles Peskine308b91d2018-02-08 09:47:44 +0100964/** Finish the calculation of the hash of a message.
965 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200966 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100967 * This function calculates the hash of the message formed by concatenating
968 * the inputs passed to preceding calls to psa_hash_update().
969 *
970 * When this function returns, the operation becomes inactive.
971 *
972 * \warning Applications should not call this function if they expect
973 * a specific value for the hash. Call psa_hash_verify() instead.
974 * Beware that comparing integrity or authenticity data such as
975 * hash values with a function such as \c memcmp is risky
976 * because the time taken by the comparison may leak information
977 * about the hashed data which could allow an attacker to guess
978 * a valid hash and thereby bypass security controls.
979 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200980 * \param[in,out] operation Active hash operation.
981 * \param[out] hash Buffer where the hash is to be written.
982 * \param hash_size Size of the \p hash buffer in bytes.
983 * \param[out] hash_length On success, the number of bytes
984 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200985 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200986 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100987 *
Gilles Peskine28538492018-07-11 17:34:00 +0200988 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100989 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200990 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100991 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200992 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200993 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200994 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100995 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200996 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
997 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
998 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200999 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001000 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001001psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1002 uint8_t *hash,
1003 size_t hash_size,
1004 size_t *hash_length);
1005
Gilles Peskine308b91d2018-02-08 09:47:44 +01001006/** Finish the calculation of the hash of a message and compare it with
1007 * an expected value.
1008 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001009 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001010 * This function calculates the hash of the message formed by concatenating
1011 * the inputs passed to preceding calls to psa_hash_update(). It then
1012 * compares the calculated hash with the expected hash passed as a
1013 * parameter to this function.
1014 *
1015 * When this function returns, the operation becomes inactive.
1016 *
Gilles Peskine19067982018-03-20 17:54:53 +01001017 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001018 * comparison between the actual hash and the expected hash is performed
1019 * in constant time.
1020 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001021 * \param[in,out] operation Active hash operation.
1022 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001023 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001024 *
Gilles Peskine28538492018-07-11 17:34:00 +02001025 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001026 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001027 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001028 * The hash of the message was calculated successfully, but it
1029 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001030 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001031 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001032 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1033 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1034 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001035 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001036 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001037psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1038 const uint8_t *hash,
1039 size_t hash_length);
1040
Gilles Peskine308b91d2018-02-08 09:47:44 +01001041/** Abort a hash operation.
1042 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001043 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001044 * \p operation structure itself. Once aborted, the operation object
1045 * can be reused for another operation by calling
1046 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001048 * You may call this function any time after the operation object has
1049 * been initialized by any of the following methods:
1050 * - A call to psa_hash_setup(), whether it succeeds or not.
1051 * - Initializing the \c struct to all-bits-zero.
1052 * - Initializing the \c struct to logical zeros, e.g.
1053 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001054 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001055 * In particular, calling psa_hash_abort() after the operation has been
1056 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1057 * psa_hash_verify() is safe and has no effect.
1058 *
1059 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001060 *
Gilles Peskine28538492018-07-11 17:34:00 +02001061 * \retval #PSA_SUCCESS
1062 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001063 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001064 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1065 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001066 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001067 */
1068psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001069
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001070/** Clone a hash operation.
1071 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001072 * This function copies the state of an ongoing hash operation to
1073 * a new operation object. In other words, this function is equivalent
1074 * to calling psa_hash_setup() on \p target_operation with the same
1075 * algorithm that \p source_operation was set up for, then
1076 * psa_hash_update() on \p target_operation with the same input that
1077 * that was passed to \p source_operation. After this function returns, the
1078 * two objects are independent, i.e. subsequent calls involving one of
1079 * the objects do not affect the other object.
1080 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001081 * \param[in] source_operation The active hash operation to clone.
1082 * \param[in,out] target_operation The operation object to set up.
1083 * It must be initialized but not active.
1084 *
1085 * \retval #PSA_SUCCESS
1086 * \retval #PSA_ERROR_BAD_STATE
1087 * \p source_operation is not an active hash operation.
1088 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001089 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001090 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1091 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001092 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001093 */
1094psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1095 psa_hash_operation_t *target_operation);
1096
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001097/**@}*/
1098
Gilles Peskine8c9def32018-02-08 10:02:12 +01001099/** \defgroup MAC Message authentication codes
1100 * @{
1101 */
1102
Gilles Peskine69647a42019-01-14 20:18:12 +01001103/** Calculate the MAC (message authentication code) of a message.
1104 *
1105 * \note To verify the MAC of a message against an
1106 * expected value, use psa_mac_verify() instead.
1107 * Beware that comparing integrity or authenticity data such as
1108 * MAC values with a function such as \c memcmp is risky
1109 * because the time taken by the comparison may leak information
1110 * about the MAC value which could allow an attacker to guess
1111 * a valid MAC and thereby bypass security controls.
1112 *
1113 * \param handle Handle to the key to use for the operation.
1114 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001115 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001116 * \param[in] input Buffer containing the input message.
1117 * \param input_length Size of the \p input buffer in bytes.
1118 * \param[out] mac Buffer where the MAC value is to be written.
1119 * \param mac_size Size of the \p mac buffer in bytes.
1120 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001121 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001122 *
1123 * \retval #PSA_SUCCESS
1124 * Success.
1125 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001126 * \retval #PSA_ERROR_NOT_PERMITTED
1127 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001128 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001129 * \retval #PSA_ERROR_NOT_SUPPORTED
1130 * \p alg is not supported or is not a MAC algorithm.
1131 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1132 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1133 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001134 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001135 * \retval #PSA_ERROR_BAD_STATE
1136 * The library has not been previously initialized by psa_crypto_init().
1137 * It is implementation-dependent whether a failure to initialize
1138 * results in this error code.
1139 */
1140psa_status_t psa_mac_compute(psa_key_handle_t handle,
1141 psa_algorithm_t alg,
1142 const uint8_t *input,
1143 size_t input_length,
1144 uint8_t *mac,
1145 size_t mac_size,
1146 size_t *mac_length);
1147
1148/** Calculate the MAC of a message and compare it with a reference value.
1149 *
1150 * \param handle Handle to the key to use for the operation.
1151 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001152 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001153 * \param[in] input Buffer containing the input message.
1154 * \param input_length Size of the \p input buffer in bytes.
1155 * \param[out] mac Buffer containing the expected MAC value.
1156 * \param mac_length Size of the \p mac buffer in bytes.
1157 *
1158 * \retval #PSA_SUCCESS
1159 * The expected MAC is identical to the actual MAC of the input.
1160 * \retval #PSA_ERROR_INVALID_SIGNATURE
1161 * The MAC of the message was calculated successfully, but it
1162 * differs from the expected value.
1163 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001164 * \retval #PSA_ERROR_NOT_PERMITTED
1165 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001166 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001167 * \retval #PSA_ERROR_NOT_SUPPORTED
1168 * \p alg is not supported or is not a MAC algorithm.
1169 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1170 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1171 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001172 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001173 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001174psa_status_t psa_mac_verify(psa_key_handle_t handle,
1175 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001176 const uint8_t *input,
1177 size_t input_length,
1178 const uint8_t *mac,
1179 const size_t mac_length);
1180
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001181/** The type of the state data structure for multipart MAC operations.
1182 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001183 * Before calling any function on a MAC operation object, the application must
1184 * initialize it by any of the following means:
1185 * - Set the structure to all-bits-zero, for example:
1186 * \code
1187 * psa_mac_operation_t operation;
1188 * memset(&operation, 0, sizeof(operation));
1189 * \endcode
1190 * - Initialize the structure to logical zero values, for example:
1191 * \code
1192 * psa_mac_operation_t operation = {0};
1193 * \endcode
1194 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1195 * for example:
1196 * \code
1197 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1198 * \endcode
1199 * - Assign the result of the function psa_mac_operation_init()
1200 * to the structure, for example:
1201 * \code
1202 * psa_mac_operation_t operation;
1203 * operation = psa_mac_operation_init();
1204 * \endcode
1205 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001206 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001207 * make any assumptions about the content of this structure except
1208 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001209typedef struct psa_mac_operation_s psa_mac_operation_t;
1210
Jaeden Amero769ce272019-01-04 11:48:03 +00001211/** \def PSA_MAC_OPERATION_INIT
1212 *
1213 * This macro returns a suitable initializer for a MAC operation object of type
1214 * #psa_mac_operation_t.
1215 */
1216#ifdef __DOXYGEN_ONLY__
1217/* This is an example definition for documentation purposes.
1218 * Implementations should define a suitable value in `crypto_struct.h`.
1219 */
1220#define PSA_MAC_OPERATION_INIT {0}
1221#endif
1222
1223/** Return an initial value for a MAC operation object.
1224 */
1225static psa_mac_operation_t psa_mac_operation_init(void);
1226
Gilles Peskinef45adda2019-01-14 18:29:18 +01001227/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001228 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001229 * This function sets up the calculation of the MAC
1230 * (message authentication code) of a byte string.
1231 * To verify the MAC of a message against an
1232 * expected value, use psa_mac_verify_setup() instead.
1233 *
1234 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001235 * -# Allocate an operation object which will be passed to all the functions
1236 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001237 * -# Initialize the operation object with one of the methods described in the
1238 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001239 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001240 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1241 * of the message each time. The MAC that is calculated is the MAC
1242 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001243 * -# At the end of the message, call psa_mac_sign_finish() to finish
1244 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001245 *
1246 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001247 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001248 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001249 * After a successful call to psa_mac_sign_setup(), the application must
1250 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001251 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001252 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001253 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001254 * \param[in,out] operation The operation object to set up. It must have
1255 * been initialized as per the documentation for
1256 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001257 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001258 * It must remain valid until the operation
1259 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001260 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001261 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001262 *
Gilles Peskine28538492018-07-11 17:34:00 +02001263 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001264 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001265 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001266 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001267 * \retval #PSA_ERROR_NOT_PERMITTED
1268 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001269 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001270 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001271 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001272 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1273 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1274 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001275 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001276 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001277 * The operation state is not valid (already set up and not
1278 * subsequently completed).
1279 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001280 * The library has not been previously initialized by psa_crypto_init().
1281 * It is implementation-dependent whether a failure to initialize
1282 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001283 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001284psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001285 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001286 psa_algorithm_t alg);
1287
Gilles Peskinef45adda2019-01-14 18:29:18 +01001288/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001289 *
1290 * This function sets up the verification of the MAC
1291 * (message authentication code) of a byte string against an expected value.
1292 *
1293 * The sequence of operations to verify a MAC is as follows:
1294 * -# Allocate an operation object which will be passed to all the functions
1295 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001296 * -# Initialize the operation object with one of the methods described in the
1297 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001298 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001299 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1300 * of the message each time. The MAC that is calculated is the MAC
1301 * of the concatenation of these messages in order.
1302 * -# At the end of the message, call psa_mac_verify_finish() to finish
1303 * calculating the actual MAC of the message and verify it against
1304 * the expected value.
1305 *
1306 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001307 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001308 *
1309 * After a successful call to psa_mac_verify_setup(), the application must
1310 * eventually terminate the operation through one of the following methods:
1311 * - A failed call to psa_mac_update().
1312 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1313 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001314 * \param[in,out] operation The operation object to set up. It must have
1315 * been initialized as per the documentation for
1316 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001317 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001318 * It must remain valid until the operation
1319 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001320 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1321 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001322 *
Gilles Peskine28538492018-07-11 17:34:00 +02001323 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001324 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001325 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001326 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001327 * \retval #PSA_ERROR_NOT_PERMITTED
1328 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001329 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001330 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001331 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001332 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1333 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1334 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001335 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001336 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001337 * The operation state is not valid (already set up and not
1338 * subsequently completed).
1339 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001340 * The library has not been previously initialized by psa_crypto_init().
1341 * It is implementation-dependent whether a failure to initialize
1342 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001343 */
1344psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001345 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001346 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001347
Gilles Peskinedcd14942018-07-12 00:30:52 +02001348/** Add a message fragment to a multipart MAC operation.
1349 *
1350 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1351 * before calling this function.
1352 *
1353 * If this function returns an error status, the operation becomes inactive.
1354 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001355 * \param[in,out] operation Active MAC operation.
1356 * \param[in] input Buffer containing the message fragment to add to
1357 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001358 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001359 *
1360 * \retval #PSA_SUCCESS
1361 * Success.
1362 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001363 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001364 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1365 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1366 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001367 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001368 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001369psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1370 const uint8_t *input,
1371 size_t input_length);
1372
Gilles Peskinedcd14942018-07-12 00:30:52 +02001373/** Finish the calculation of the MAC of a message.
1374 *
1375 * The application must call psa_mac_sign_setup() before calling this function.
1376 * This function calculates the MAC of the message formed by concatenating
1377 * the inputs passed to preceding calls to psa_mac_update().
1378 *
1379 * When this function returns, the operation becomes inactive.
1380 *
1381 * \warning Applications should not call this function if they expect
1382 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1383 * Beware that comparing integrity or authenticity data such as
1384 * MAC values with a function such as \c memcmp is risky
1385 * because the time taken by the comparison may leak information
1386 * about the MAC value which could allow an attacker to guess
1387 * a valid MAC and thereby bypass security controls.
1388 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001389 * \param[in,out] operation Active MAC operation.
1390 * \param[out] mac Buffer where the MAC value is to be written.
1391 * \param mac_size Size of the \p mac buffer in bytes.
1392 * \param[out] mac_length On success, the number of bytes
1393 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001394 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001395 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001396 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001397 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001398 *
1399 * \retval #PSA_SUCCESS
1400 * Success.
1401 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001402 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001403 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001404 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001405 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1406 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1407 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1408 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001409 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001410 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001411psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1412 uint8_t *mac,
1413 size_t mac_size,
1414 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001415
Gilles Peskinedcd14942018-07-12 00:30:52 +02001416/** Finish the calculation of the MAC of a message and compare it with
1417 * an expected value.
1418 *
1419 * The application must call psa_mac_verify_setup() before calling this function.
1420 * This function calculates the MAC of the message formed by concatenating
1421 * the inputs passed to preceding calls to psa_mac_update(). It then
1422 * compares the calculated MAC with the expected MAC passed as a
1423 * parameter to this function.
1424 *
1425 * When this function returns, the operation becomes inactive.
1426 *
1427 * \note Implementations shall make the best effort to ensure that the
1428 * comparison between the actual MAC and the expected MAC is performed
1429 * in constant time.
1430 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001431 * \param[in,out] operation Active MAC operation.
1432 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001433 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001434 *
1435 * \retval #PSA_SUCCESS
1436 * The expected MAC is identical to the actual MAC of the message.
1437 * \retval #PSA_ERROR_INVALID_SIGNATURE
1438 * The MAC of the message was calculated successfully, but it
1439 * differs from the expected MAC.
1440 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001441 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001442 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1443 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1444 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001445 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001446 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001447psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1448 const uint8_t *mac,
1449 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001450
Gilles Peskinedcd14942018-07-12 00:30:52 +02001451/** Abort a MAC operation.
1452 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001453 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001454 * \p operation structure itself. Once aborted, the operation object
1455 * can be reused for another operation by calling
1456 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001457 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001458 * You may call this function any time after the operation object has
1459 * been initialized by any of the following methods:
1460 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1461 * it succeeds or not.
1462 * - Initializing the \c struct to all-bits-zero.
1463 * - Initializing the \c struct to logical zeros, e.g.
1464 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001465 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001466 * In particular, calling psa_mac_abort() after the operation has been
1467 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1468 * psa_mac_verify_finish() is safe and has no effect.
1469 *
1470 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001471 *
1472 * \retval #PSA_SUCCESS
1473 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001474 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001475 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1476 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001477 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001478 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001479psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1480
1481/**@}*/
1482
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001483/** \defgroup cipher Symmetric ciphers
1484 * @{
1485 */
1486
Gilles Peskine69647a42019-01-14 20:18:12 +01001487/** Encrypt a message using a symmetric cipher.
1488 *
1489 * This function encrypts a message with a random IV (initialization
1490 * vector).
1491 *
1492 * \param handle Handle to the key to use for the operation.
1493 * It must remain valid until the operation
1494 * terminates.
1495 * \param alg The cipher algorithm to compute
1496 * (\c PSA_ALG_XXX value such that
1497 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1498 * \param[in] input Buffer containing the message to encrypt.
1499 * \param input_length Size of the \p input buffer in bytes.
1500 * \param[out] output Buffer where the output is to be written.
1501 * The output contains the IV followed by
1502 * the ciphertext proper.
1503 * \param output_size Size of the \p output buffer in bytes.
1504 * \param[out] output_length On success, the number of bytes
1505 * that make up the output.
1506 *
1507 * \retval #PSA_SUCCESS
1508 * Success.
1509 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001510 * \retval #PSA_ERROR_NOT_PERMITTED
1511 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001512 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001513 * \retval #PSA_ERROR_NOT_SUPPORTED
1514 * \p alg is not supported or is not a cipher algorithm.
1515 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1516 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1517 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1518 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001519 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001520 */
1521psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1522 psa_algorithm_t alg,
1523 const uint8_t *input,
1524 size_t input_length,
1525 uint8_t *output,
1526 size_t output_size,
1527 size_t *output_length);
1528
1529/** Decrypt a message using a symmetric cipher.
1530 *
1531 * This function decrypts a message encrypted with a symmetric cipher.
1532 *
1533 * \param handle Handle to the key to use for the operation.
1534 * It must remain valid until the operation
1535 * terminates.
1536 * \param alg The cipher algorithm to compute
1537 * (\c PSA_ALG_XXX value such that
1538 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1539 * \param[in] input Buffer containing the message to decrypt.
1540 * This consists of the IV followed by the
1541 * ciphertext proper.
1542 * \param input_length Size of the \p input buffer in bytes.
1543 * \param[out] output Buffer where the plaintext is to be written.
1544 * \param output_size Size of the \p output buffer in bytes.
1545 * \param[out] output_length On success, the number of bytes
1546 * that make up the output.
1547 *
1548 * \retval #PSA_SUCCESS
1549 * Success.
1550 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001551 * \retval #PSA_ERROR_NOT_PERMITTED
1552 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001553 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001554 * \retval #PSA_ERROR_NOT_SUPPORTED
1555 * \p alg is not supported or is not a cipher algorithm.
1556 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1557 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1558 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1559 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001560 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001561 */
1562psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1563 psa_algorithm_t alg,
1564 const uint8_t *input,
1565 size_t input_length,
1566 uint8_t *output,
1567 size_t output_size,
1568 size_t *output_length);
1569
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001570/** The type of the state data structure for multipart cipher operations.
1571 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001572 * Before calling any function on a cipher operation object, the application
1573 * must initialize it by any of the following means:
1574 * - Set the structure to all-bits-zero, for example:
1575 * \code
1576 * psa_cipher_operation_t operation;
1577 * memset(&operation, 0, sizeof(operation));
1578 * \endcode
1579 * - Initialize the structure to logical zero values, for example:
1580 * \code
1581 * psa_cipher_operation_t operation = {0};
1582 * \endcode
1583 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1584 * for example:
1585 * \code
1586 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1587 * \endcode
1588 * - Assign the result of the function psa_cipher_operation_init()
1589 * to the structure, for example:
1590 * \code
1591 * psa_cipher_operation_t operation;
1592 * operation = psa_cipher_operation_init();
1593 * \endcode
1594 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001595 * This is an implementation-defined \c struct. Applications should not
1596 * make any assumptions about the content of this structure except
1597 * as directed by the documentation of a specific implementation. */
1598typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1599
Jaeden Amero5bae2272019-01-04 11:48:27 +00001600/** \def PSA_CIPHER_OPERATION_INIT
1601 *
1602 * This macro returns a suitable initializer for a cipher operation object of
1603 * type #psa_cipher_operation_t.
1604 */
1605#ifdef __DOXYGEN_ONLY__
1606/* This is an example definition for documentation purposes.
1607 * Implementations should define a suitable value in `crypto_struct.h`.
1608 */
1609#define PSA_CIPHER_OPERATION_INIT {0}
1610#endif
1611
1612/** Return an initial value for a cipher operation object.
1613 */
1614static psa_cipher_operation_t psa_cipher_operation_init(void);
1615
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001616/** Set the key for a multipart symmetric encryption operation.
1617 *
1618 * The sequence of operations to encrypt a message with a symmetric cipher
1619 * is as follows:
1620 * -# Allocate an operation object which will be passed to all the functions
1621 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001622 * -# Initialize the operation object with one of the methods described in the
1623 * documentation for #psa_cipher_operation_t, e.g.
1624 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001625 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001626 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001627 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001628 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001629 * requires a specific IV value.
1630 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1631 * of the message each time.
1632 * -# Call psa_cipher_finish().
1633 *
1634 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001635 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001636 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001637 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001638 * eventually terminate the operation. The following events terminate an
1639 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001640 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001641 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001642 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001643 * \param[in,out] operation The operation object to set up. It must have
1644 * been initialized as per the documentation for
1645 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001646 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001647 * It must remain valid until the operation
1648 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001649 * \param alg The cipher algorithm to compute
1650 * (\c PSA_ALG_XXX value such that
1651 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001652 *
Gilles Peskine28538492018-07-11 17:34:00 +02001653 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001654 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001655 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001656 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001657 * \retval #PSA_ERROR_NOT_PERMITTED
1658 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001659 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001660 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001661 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001662 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1663 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1664 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001665 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001666 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001667 * The operation state is not valid (already set up and not
1668 * subsequently completed).
1669 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001670 * The library has not been previously initialized by psa_crypto_init().
1671 * It is implementation-dependent whether a failure to initialize
1672 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001673 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001674psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001675 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001676 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001677
1678/** Set the key for a multipart symmetric decryption operation.
1679 *
1680 * The sequence of operations to decrypt a message with a symmetric cipher
1681 * is as follows:
1682 * -# Allocate an operation object which will be passed to all the functions
1683 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001684 * -# Initialize the operation object with one of the methods described in the
1685 * documentation for #psa_cipher_operation_t, e.g.
1686 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001687 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001688 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001689 * decryption. If the IV is prepended to the ciphertext, you can call
1690 * psa_cipher_update() on a buffer containing the IV followed by the
1691 * beginning of the message.
1692 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1693 * of the message each time.
1694 * -# Call psa_cipher_finish().
1695 *
1696 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001697 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001698 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001699 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001700 * eventually terminate the operation. The following events terminate an
1701 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001702 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001703 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001704 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001705 * \param[in,out] operation The operation object to set up. It must have
1706 * been initialized as per the documentation for
1707 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001708 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001709 * It must remain valid until the operation
1710 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001711 * \param alg The cipher algorithm to compute
1712 * (\c PSA_ALG_XXX value such that
1713 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001714 *
Gilles Peskine28538492018-07-11 17:34:00 +02001715 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001716 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001717 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001718 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001719 * \retval #PSA_ERROR_NOT_PERMITTED
1720 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001721 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001722 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001723 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001724 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1725 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1726 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001727 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001728 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001729 * The operation state is not valid (already set up and not
1730 * subsequently completed).
1731 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001732 * The library has not been previously initialized by psa_crypto_init().
1733 * It is implementation-dependent whether a failure to initialize
1734 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001735 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001736psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001737 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001738 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001739
Gilles Peskinedcd14942018-07-12 00:30:52 +02001740/** Generate an IV for a symmetric encryption operation.
1741 *
1742 * This function generates a random IV (initialization vector), nonce
1743 * or initial counter value for the encryption operation as appropriate
1744 * for the chosen algorithm, key type and key size.
1745 *
1746 * The application must call psa_cipher_encrypt_setup() before
1747 * calling this function.
1748 *
1749 * If this function returns an error status, the operation becomes inactive.
1750 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001751 * \param[in,out] operation Active cipher operation.
1752 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001753 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001754 * \param[out] iv_length On success, the number of bytes of the
1755 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001756 *
1757 * \retval #PSA_SUCCESS
1758 * Success.
1759 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001760 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001761 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001762 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001763 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1764 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1765 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001766 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001767 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001768psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001769 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001770 size_t iv_size,
1771 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001772
Gilles Peskinedcd14942018-07-12 00:30:52 +02001773/** Set the IV for a symmetric encryption or decryption operation.
1774 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001775 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001776 * or initial counter value for the encryption or decryption operation.
1777 *
1778 * The application must call psa_cipher_encrypt_setup() before
1779 * calling this function.
1780 *
1781 * If this function returns an error status, the operation becomes inactive.
1782 *
1783 * \note When encrypting, applications should use psa_cipher_generate_iv()
1784 * instead of this function, unless implementing a protocol that requires
1785 * a non-random IV.
1786 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001787 * \param[in,out] operation Active cipher operation.
1788 * \param[in] iv Buffer containing the IV to use.
1789 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001790 *
1791 * \retval #PSA_SUCCESS
1792 * Success.
1793 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001794 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001795 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001796 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001797 * or the chosen algorithm does not use an IV.
1798 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1799 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1800 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001801 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001802 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001803psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001804 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001805 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001806
Gilles Peskinedcd14942018-07-12 00:30:52 +02001807/** Encrypt or decrypt a message fragment in an active cipher operation.
1808 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001809 * Before calling this function, you must:
1810 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1811 * The choice of setup function determines whether this function
1812 * encrypts or decrypts its input.
1813 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1814 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001815 *
1816 * If this function returns an error status, the operation becomes inactive.
1817 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001818 * \param[in,out] operation Active cipher operation.
1819 * \param[in] input Buffer containing the message fragment to
1820 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001821 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001822 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001823 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001824 * \param[out] output_length On success, the number of bytes
1825 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001826 *
1827 * \retval #PSA_SUCCESS
1828 * Success.
1829 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001830 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001831 * not set, or already completed).
1832 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1833 * The size of the \p output buffer is too small.
1834 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1835 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1836 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001837 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001838 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001839psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1840 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001841 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001842 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001843 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001844 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001845
Gilles Peskinedcd14942018-07-12 00:30:52 +02001846/** Finish encrypting or decrypting a message in a cipher operation.
1847 *
1848 * The application must call psa_cipher_encrypt_setup() or
1849 * psa_cipher_decrypt_setup() before calling this function. The choice
1850 * of setup function determines whether this function encrypts or
1851 * decrypts its input.
1852 *
1853 * This function finishes the encryption or decryption of the message
1854 * formed by concatenating the inputs passed to preceding calls to
1855 * psa_cipher_update().
1856 *
1857 * When this function returns, the operation becomes inactive.
1858 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001859 * \param[in,out] operation Active cipher operation.
1860 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001861 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001862 * \param[out] output_length On success, the number of bytes
1863 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001864 *
1865 * \retval #PSA_SUCCESS
1866 * Success.
1867 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001868 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001869 * not set, or already completed).
1870 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1871 * The size of the \p output buffer is too small.
1872 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1873 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1874 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001875 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001876 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001877psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001878 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001879 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001880 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001881
Gilles Peskinedcd14942018-07-12 00:30:52 +02001882/** Abort a cipher operation.
1883 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001884 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001885 * \p operation structure itself. Once aborted, the operation object
1886 * can be reused for another operation by calling
1887 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001888 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001889 * You may call this function any time after the operation object has
1890 * been initialized by any of the following methods:
1891 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1892 * whether it succeeds or not.
1893 * - Initializing the \c struct to all-bits-zero.
1894 * - Initializing the \c struct to logical zeros, e.g.
1895 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001896 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001897 * In particular, calling psa_cipher_abort() after the operation has been
1898 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1899 * is safe and has no effect.
1900 *
1901 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001902 *
1903 * \retval #PSA_SUCCESS
1904 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001905 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001906 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1907 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001908 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001909 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001910psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1911
1912/**@}*/
1913
Gilles Peskine3b555712018-03-03 21:27:57 +01001914/** \defgroup aead Authenticated encryption with associated data (AEAD)
1915 * @{
1916 */
1917
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001918/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001919 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001920 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001921 * \param alg The AEAD algorithm to compute
1922 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001923 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001924 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001925 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001926 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001927 * but not encrypted.
1928 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001929 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001930 * encrypted.
1931 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001932 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001933 * encrypted data. The additional data is not
1934 * part of this output. For algorithms where the
1935 * encrypted data and the authentication tag
1936 * are defined as separate outputs, the
1937 * authentication tag is appended to the
1938 * encrypted data.
1939 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1940 * This must be at least
1941 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1942 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001943 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001944 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001945 *
Gilles Peskine28538492018-07-11 17:34:00 +02001946 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001947 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001948 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001949 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001950 * \retval #PSA_ERROR_NOT_PERMITTED
1951 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001952 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001953 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001954 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001955 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1956 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1957 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001958 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001959 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001960 * The library has not been previously initialized by psa_crypto_init().
1961 * It is implementation-dependent whether a failure to initialize
1962 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001963 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001964psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001965 psa_algorithm_t alg,
1966 const uint8_t *nonce,
1967 size_t nonce_length,
1968 const uint8_t *additional_data,
1969 size_t additional_data_length,
1970 const uint8_t *plaintext,
1971 size_t plaintext_length,
1972 uint8_t *ciphertext,
1973 size_t ciphertext_size,
1974 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001975
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001976/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001977 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001978 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001979 * \param alg The AEAD algorithm to compute
1980 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001981 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001982 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001983 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001984 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001985 * but not encrypted.
1986 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001987 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001988 * encrypted. For algorithms where the
1989 * encrypted data and the authentication tag
1990 * are defined as separate inputs, the buffer
1991 * must contain the encrypted data followed
1992 * by the authentication tag.
1993 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001994 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001995 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1996 * This must be at least
1997 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1998 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001999 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002000 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002001 *
Gilles Peskine28538492018-07-11 17:34:00 +02002002 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002003 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002004 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002005 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002006 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002007 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002008 * \retval #PSA_ERROR_NOT_PERMITTED
2009 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002010 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002011 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002012 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002013 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2014 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2015 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002016 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002017 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002018 * 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 Peskine3b555712018-03-03 21:27:57 +01002021 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002022psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002023 psa_algorithm_t alg,
2024 const uint8_t *nonce,
2025 size_t nonce_length,
2026 const uint8_t *additional_data,
2027 size_t additional_data_length,
2028 const uint8_t *ciphertext,
2029 size_t ciphertext_length,
2030 uint8_t *plaintext,
2031 size_t plaintext_size,
2032 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002033
Gilles Peskine30a9e412019-01-14 18:36:12 +01002034/** The type of the state data structure for multipart AEAD operations.
2035 *
2036 * Before calling any function on an AEAD operation object, the application
2037 * must initialize it by any of the following means:
2038 * - Set the structure to all-bits-zero, for example:
2039 * \code
2040 * psa_aead_operation_t operation;
2041 * memset(&operation, 0, sizeof(operation));
2042 * \endcode
2043 * - Initialize the structure to logical zero values, for example:
2044 * \code
2045 * psa_aead_operation_t operation = {0};
2046 * \endcode
2047 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2048 * for example:
2049 * \code
2050 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2051 * \endcode
2052 * - Assign the result of the function psa_aead_operation_init()
2053 * to the structure, for example:
2054 * \code
2055 * psa_aead_operation_t operation;
2056 * operation = psa_aead_operation_init();
2057 * \endcode
2058 *
2059 * This is an implementation-defined \c struct. Applications should not
2060 * make any assumptions about the content of this structure except
2061 * as directed by the documentation of a specific implementation. */
2062typedef struct psa_aead_operation_s psa_aead_operation_t;
2063
2064/** \def PSA_AEAD_OPERATION_INIT
2065 *
2066 * This macro returns a suitable initializer for an AEAD operation object of
2067 * type #psa_aead_operation_t.
2068 */
2069#ifdef __DOXYGEN_ONLY__
2070/* This is an example definition for documentation purposes.
2071 * Implementations should define a suitable value in `crypto_struct.h`.
2072 */
2073#define PSA_AEAD_OPERATION_INIT {0}
2074#endif
2075
2076/** Return an initial value for an AEAD operation object.
2077 */
2078static psa_aead_operation_t psa_aead_operation_init(void);
2079
2080/** Set the key for a multipart authenticated encryption operation.
2081 *
2082 * The sequence of operations to encrypt a message with authentication
2083 * is as follows:
2084 * -# Allocate an operation object which will be passed to all the functions
2085 * listed here.
2086 * -# Initialize the operation object with one of the methods described in the
2087 * documentation for #psa_aead_operation_t, e.g.
2088 * PSA_AEAD_OPERATION_INIT.
2089 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002090 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2091 * inputs to the subsequent calls to psa_aead_update_ad() and
2092 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2093 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002094 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2095 * generate or set the nonce. You should use
2096 * psa_aead_generate_nonce() unless the protocol you are implementing
2097 * requires a specific nonce value.
2098 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2099 * of the non-encrypted additional authenticated data each time.
2100 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002101 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002102 * -# Call psa_aead_finish().
2103 *
2104 * The application may call psa_aead_abort() at any time after the operation
2105 * has been initialized.
2106 *
2107 * After a successful call to psa_aead_encrypt_setup(), the application must
2108 * eventually terminate the operation. The following events terminate an
2109 * operation:
2110 * - A failed call to any of the \c psa_aead_xxx functions.
2111 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2112 *
2113 * \param[in,out] operation The operation object to set up. It must have
2114 * been initialized as per the documentation for
2115 * #psa_aead_operation_t and not yet in use.
2116 * \param handle Handle to the key to use for the operation.
2117 * It must remain valid until the operation
2118 * terminates.
2119 * \param alg The AEAD algorithm to compute
2120 * (\c PSA_ALG_XXX value such that
2121 * #PSA_ALG_IS_AEAD(\p alg) is true).
2122 *
2123 * \retval #PSA_SUCCESS
2124 * Success.
2125 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002126 * \retval #PSA_ERROR_NOT_PERMITTED
2127 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002128 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002129 * \retval #PSA_ERROR_NOT_SUPPORTED
2130 * \p alg is not supported or is not an AEAD algorithm.
2131 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2132 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2133 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002134 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002135 * \retval #PSA_ERROR_BAD_STATE
2136 * The library has not been previously initialized by psa_crypto_init().
2137 * It is implementation-dependent whether a failure to initialize
2138 * results in this error code.
2139 */
2140psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2141 psa_key_handle_t handle,
2142 psa_algorithm_t alg);
2143
2144/** Set the key for a multipart authenticated decryption operation.
2145 *
2146 * The sequence of operations to decrypt a message with authentication
2147 * is as follows:
2148 * -# Allocate an operation object which will be passed to all the functions
2149 * listed here.
2150 * -# Initialize the operation object with one of the methods described in the
2151 * documentation for #psa_aead_operation_t, e.g.
2152 * PSA_AEAD_OPERATION_INIT.
2153 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002154 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2155 * inputs to the subsequent calls to psa_aead_update_ad() and
2156 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2157 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002158 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2159 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2160 * of the non-encrypted additional authenticated data each time.
2161 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002162 * of the ciphertext to decrypt each time.
2163 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002164 *
2165 * The application may call psa_aead_abort() at any time after the operation
2166 * has been initialized.
2167 *
2168 * After a successful call to psa_aead_decrypt_setup(), the application must
2169 * eventually terminate the operation. The following events terminate an
2170 * operation:
2171 * - A failed call to any of the \c psa_aead_xxx functions.
2172 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2173 *
2174 * \param[in,out] operation The operation object to set up. It must have
2175 * been initialized as per the documentation for
2176 * #psa_aead_operation_t and not yet in use.
2177 * \param handle Handle to the key to use for the operation.
2178 * It must remain valid until the operation
2179 * terminates.
2180 * \param alg The AEAD algorithm to compute
2181 * (\c PSA_ALG_XXX value such that
2182 * #PSA_ALG_IS_AEAD(\p alg) is true).
2183 *
2184 * \retval #PSA_SUCCESS
2185 * Success.
2186 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002187 * \retval #PSA_ERROR_NOT_PERMITTED
2188 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002189 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002190 * \retval #PSA_ERROR_NOT_SUPPORTED
2191 * \p alg is not supported or is not an AEAD algorithm.
2192 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2193 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2194 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002195 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002196 * \retval #PSA_ERROR_BAD_STATE
2197 * The library has not been previously initialized by psa_crypto_init().
2198 * It is implementation-dependent whether a failure to initialize
2199 * results in this error code.
2200 */
2201psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2202 psa_key_handle_t handle,
2203 psa_algorithm_t alg);
2204
2205/** Generate a random nonce for an authenticated encryption operation.
2206 *
2207 * This function generates a random nonce for the authenticated encryption
2208 * operation with an appropriate size for the chosen algorithm, key type
2209 * and key size.
2210 *
2211 * The application must call psa_aead_encrypt_setup() before
2212 * calling this function.
2213 *
2214 * If this function returns an error status, the operation becomes inactive.
2215 *
2216 * \param[in,out] operation Active AEAD operation.
2217 * \param[out] nonce Buffer where the generated nonce is to be
2218 * written.
2219 * \param nonce_size Size of the \p nonce buffer in bytes.
2220 * \param[out] nonce_length On success, the number of bytes of the
2221 * generated nonce.
2222 *
2223 * \retval #PSA_SUCCESS
2224 * Success.
2225 * \retval #PSA_ERROR_BAD_STATE
2226 * The operation state is not valid (not set up, or nonce already set).
2227 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2228 * The size of the \p nonce buffer is too small.
2229 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2230 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2231 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002232 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002233 */
2234psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002235 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002236 size_t nonce_size,
2237 size_t *nonce_length);
2238
2239/** Set the nonce for an authenticated encryption or decryption operation.
2240 *
2241 * This function sets the nonce for the authenticated
2242 * encryption or decryption operation.
2243 *
2244 * The application must call psa_aead_encrypt_setup() before
2245 * calling this function.
2246 *
2247 * If this function returns an error status, the operation becomes inactive.
2248 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002249 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002250 * instead of this function, unless implementing a protocol that requires
2251 * a non-random IV.
2252 *
2253 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002254 * \param[in] nonce Buffer containing the nonce to use.
2255 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002256 *
2257 * \retval #PSA_SUCCESS
2258 * Success.
2259 * \retval #PSA_ERROR_BAD_STATE
2260 * The operation state is not valid (not set up, or nonce already set).
2261 * \retval #PSA_ERROR_INVALID_ARGUMENT
2262 * The size of \p nonce is not acceptable for the chosen algorithm.
2263 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2264 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2265 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002266 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002267 */
2268psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002269 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002270 size_t nonce_length);
2271
Gilles Peskinebc59c852019-01-17 15:26:08 +01002272/** Declare the lengths of the message and additional data for AEAD.
2273 *
2274 * The application must call this function before calling
2275 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2276 * the operation requires it. If the algorithm does not require it,
2277 * calling this function is optional, but if this function is called
2278 * then the implementation must enforce the lengths.
2279 *
2280 * You may call this function before or after setting the nonce with
2281 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2282 *
2283 * - For #PSA_ALG_CCM, calling this function is required.
2284 * - For the other AEAD algorithms defined in this specification, calling
2285 * this function is not required.
2286 * - For vendor-defined algorithm, refer to the vendor documentation.
2287 *
2288 * \param[in,out] operation Active AEAD operation.
2289 * \param ad_length Size of the non-encrypted additional
2290 * authenticated data in bytes.
2291 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2292 *
2293 * \retval #PSA_SUCCESS
2294 * Success.
2295 * \retval #PSA_ERROR_BAD_STATE
2296 * The operation state is not valid (not set up, already completed,
2297 * or psa_aead_update_ad() or psa_aead_update() already called).
2298 * \retval #PSA_ERROR_INVALID_ARGUMENT
2299 * At least one of the lengths is not acceptable for the chosen
2300 * algorithm.
2301 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2302 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2303 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002304 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinebc59c852019-01-17 15:26:08 +01002305 */
2306psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2307 size_t ad_length,
2308 size_t plaintext_length);
2309
Gilles Peskine30a9e412019-01-14 18:36:12 +01002310/** Pass additional data to an active AEAD operation.
2311 *
2312 * Additional data is authenticated, but not encrypted.
2313 *
2314 * You may call this function multiple times to pass successive fragments
2315 * of the additional data. You may not call this function after passing
2316 * data to encrypt or decrypt with psa_aead_update().
2317 *
2318 * Before calling this function, you must:
2319 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2320 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2321 *
2322 * If this function returns an error status, the operation becomes inactive.
2323 *
2324 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2325 * there is no guarantee that the input is valid. Therefore, until
2326 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2327 * treat the input as untrusted and prepare to undo any action that
2328 * depends on the input if psa_aead_verify() returns an error status.
2329 *
2330 * \param[in,out] operation Active AEAD operation.
2331 * \param[in] input Buffer containing the fragment of
2332 * additional data.
2333 * \param input_length Size of the \p input buffer in bytes.
2334 *
2335 * \retval #PSA_SUCCESS
2336 * Success.
2337 * \retval #PSA_ERROR_BAD_STATE
2338 * The operation state is not valid (not set up, nonce not set,
2339 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002340 * \retval #PSA_ERROR_INVALID_ARGUMENT
2341 * The total input length overflows the additional data length that
2342 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002343 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2344 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2345 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002346 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002347 */
2348psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2349 const uint8_t *input,
2350 size_t input_length);
2351
2352/** Encrypt or decrypt a message fragment in an active AEAD operation.
2353 *
2354 * Before calling this function, you must:
2355 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2356 * The choice of setup function determines whether this function
2357 * encrypts or decrypts its input.
2358 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2359 * 3. Call psa_aead_update_ad() to pass all the additional data.
2360 *
2361 * If this function returns an error status, the operation becomes inactive.
2362 *
2363 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2364 * there is no guarantee that the input is valid. Therefore, until
2365 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2366 * - Do not use the output in any way other than storing it in a
2367 * confidential location. If you take any action that depends
2368 * on the tentative decrypted data, this action will need to be
2369 * undone if the input turns out not to be valid. Furthermore,
2370 * if an adversary can observe that this action took place
2371 * (for example through timing), they may be able to use this
2372 * fact as an oracle to decrypt any message encrypted with the
2373 * same key.
2374 * - In particular, do not copy the output anywhere but to a
2375 * memory or storage space that you have exclusive access to.
2376 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002377 * This function does not require the input to be aligned to any
2378 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002379 * a whole block at a time, it must consume all the input provided, but
2380 * it may delay the end of the corresponding output until a subsequent
2381 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2382 * provides sufficient input. The amount of data that can be delayed
2383 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002384 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002385 * \param[in,out] operation Active AEAD operation.
2386 * \param[in] input Buffer containing the message fragment to
2387 * encrypt or decrypt.
2388 * \param input_length Size of the \p input buffer in bytes.
2389 * \param[out] output Buffer where the output is to be written.
2390 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002391 * This must be at least
2392 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2393 * \p input_length) where \c alg is the
2394 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002395 * \param[out] output_length On success, the number of bytes
2396 * that make up the returned output.
2397 *
2398 * \retval #PSA_SUCCESS
2399 * Success.
2400 * \retval #PSA_ERROR_BAD_STATE
2401 * The operation state is not valid (not set up, nonce not set
2402 * or already completed).
2403 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2404 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002405 * You can determine a sufficient buffer size by calling
2406 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2407 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002408 * \retval #PSA_ERROR_INVALID_ARGUMENT
2409 * The total length of input to psa_aead_update_ad() so far is
2410 * less than the additional data length that was previously
2411 * specified with psa_aead_set_lengths().
2412 * \retval #PSA_ERROR_INVALID_ARGUMENT
2413 * The total input length overflows the plaintext length that
2414 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002415 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2416 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2417 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002418 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002419 */
2420psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2421 const uint8_t *input,
2422 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002423 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002424 size_t output_size,
2425 size_t *output_length);
2426
2427/** Finish encrypting a message in an AEAD operation.
2428 *
2429 * The operation must have been set up with psa_aead_encrypt_setup().
2430 *
2431 * This function finishes the authentication of the additional data
2432 * formed by concatenating the inputs passed to preceding calls to
2433 * psa_aead_update_ad() with the plaintext formed by concatenating the
2434 * inputs passed to preceding calls to psa_aead_update().
2435 *
2436 * This function has two output buffers:
2437 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002438 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002439 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002440 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002441 * that the operation performs.
2442 *
2443 * When this function returns, the operation becomes inactive.
2444 *
2445 * \param[in,out] operation Active AEAD operation.
2446 * \param[out] ciphertext Buffer where the last part of the ciphertext
2447 * is to be written.
2448 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002449 * This must be at least
2450 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2451 * \c alg is the algorithm that is being
2452 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002453 * \param[out] ciphertext_length On success, the number of bytes of
2454 * returned ciphertext.
2455 * \param[out] tag Buffer where the authentication tag is
2456 * to be written.
2457 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002458 * This must be at least
2459 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2460 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002461 * \param[out] tag_length On success, the number of bytes
2462 * that make up the returned tag.
2463 *
2464 * \retval #PSA_SUCCESS
2465 * Success.
2466 * \retval #PSA_ERROR_BAD_STATE
2467 * The operation state is not valid (not set up, nonce not set,
2468 * decryption, or already completed).
2469 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002470 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002471 * You can determine a sufficient buffer size for \p ciphertext by
2472 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2473 * where \c alg is the algorithm that is being calculated.
2474 * You can determine a sufficient buffer size for \p tag by
2475 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002476 * \retval #PSA_ERROR_INVALID_ARGUMENT
2477 * The total length of input to psa_aead_update_ad() so far is
2478 * less than the additional data length that was previously
2479 * specified with psa_aead_set_lengths().
2480 * \retval #PSA_ERROR_INVALID_ARGUMENT
2481 * The total length of input to psa_aead_update() so far is
2482 * less than the plaintext length that was previously
2483 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002484 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2485 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2486 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002487 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002488 */
2489psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002490 uint8_t *ciphertext,
2491 size_t ciphertext_size,
2492 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002493 uint8_t *tag,
2494 size_t tag_size,
2495 size_t *tag_length);
2496
2497/** Finish authenticating and decrypting a message in an AEAD operation.
2498 *
2499 * The operation must have been set up with psa_aead_decrypt_setup().
2500 *
2501 * This function finishes the authentication of the additional data
2502 * formed by concatenating the inputs passed to preceding calls to
2503 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2504 * inputs passed to preceding calls to psa_aead_update().
2505 *
2506 * When this function returns, the operation becomes inactive.
2507 *
2508 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002509 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002510 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002511 * from previous calls to psa_aead_update()
2512 * that could not be processed until the end
2513 * of the input.
2514 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002515 * This must be at least
2516 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2517 * \c alg is the algorithm that is being
2518 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002519 * \param[out] plaintext_length On success, the number of bytes of
2520 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002521 * \param[in] tag Buffer containing the authentication tag.
2522 * \param tag_length Size of the \p tag buffer in bytes.
2523 *
2524 * \retval #PSA_SUCCESS
2525 * Success.
2526 * \retval #PSA_ERROR_BAD_STATE
2527 * The operation state is not valid (not set up, nonce not set,
2528 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002529 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2530 * The size of the \p plaintext buffer is too small.
2531 * You can determine a sufficient buffer size for \p plaintext by
2532 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2533 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002534 * \retval #PSA_ERROR_INVALID_ARGUMENT
2535 * The total length of input to psa_aead_update_ad() so far is
2536 * less than the additional data length that was previously
2537 * specified with psa_aead_set_lengths().
2538 * \retval #PSA_ERROR_INVALID_ARGUMENT
2539 * The total length of input to psa_aead_update() so far is
2540 * less than the plaintext length that was previously
2541 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002542 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2543 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2544 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002545 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002546 */
2547psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002548 uint8_t *plaintext,
2549 size_t plaintext_size,
2550 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002551 const uint8_t *tag,
2552 size_t tag_length);
2553
2554/** Abort an AEAD operation.
2555 *
2556 * Aborting an operation frees all associated resources except for the
2557 * \p operation structure itself. Once aborted, the operation object
2558 * can be reused for another operation by calling
2559 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2560 *
2561 * You may call this function any time after the operation object has
2562 * been initialized by any of the following methods:
2563 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2564 * whether it succeeds or not.
2565 * - Initializing the \c struct to all-bits-zero.
2566 * - Initializing the \c struct to logical zeros, e.g.
2567 * `psa_aead_operation_t operation = {0}`.
2568 *
2569 * In particular, calling psa_aead_abort() after the operation has been
2570 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2571 * is safe and has no effect.
2572 *
2573 * \param[in,out] operation Initialized AEAD operation.
2574 *
2575 * \retval #PSA_SUCCESS
2576 * \retval #PSA_ERROR_BAD_STATE
2577 * \p operation is not an active AEAD operation.
2578 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2579 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002580 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002581 */
2582psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2583
Gilles Peskine3b555712018-03-03 21:27:57 +01002584/**@}*/
2585
Gilles Peskine20035e32018-02-03 22:44:14 +01002586/** \defgroup asymmetric Asymmetric cryptography
2587 * @{
2588 */
2589
2590/**
2591 * \brief Sign a hash or short message with a private key.
2592 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002593 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002594 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002595 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2596 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2597 * to determine the hash algorithm to use.
2598 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002599 * \param handle Handle to the key to use for the operation.
2600 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002601 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002602 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002603 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002604 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002605 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002606 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002607 * \param[out] signature_length On success, the number of bytes
2608 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002609 *
Gilles Peskine28538492018-07-11 17:34:00 +02002610 * \retval #PSA_SUCCESS
2611 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002612 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002613 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002614 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002615 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002616 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002617 * \retval #PSA_ERROR_NOT_SUPPORTED
2618 * \retval #PSA_ERROR_INVALID_ARGUMENT
2619 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2620 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2621 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002622 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002623 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002624 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002625 * The library has not been previously initialized by psa_crypto_init().
2626 * It is implementation-dependent whether a failure to initialize
2627 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002628 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002629psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002630 psa_algorithm_t alg,
2631 const uint8_t *hash,
2632 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002633 uint8_t *signature,
2634 size_t signature_size,
2635 size_t *signature_length);
2636
2637/**
2638 * \brief Verify the signature a hash or short message using a public key.
2639 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002640 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002641 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002642 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2643 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2644 * to determine the hash algorithm to use.
2645 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002646 * \param handle Handle to the key to use for the operation.
2647 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002648 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002649 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002650 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002651 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002652 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002653 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002654 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002655 *
Gilles Peskine28538492018-07-11 17:34:00 +02002656 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002657 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002658 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002659 * The calculation was perfomed successfully, but the passed
2660 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002661 * \retval #PSA_ERROR_NOT_SUPPORTED
2662 * \retval #PSA_ERROR_INVALID_ARGUMENT
2663 * \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
itayzafrir90d8c7a2018-09-12 11:44:52 +03002667 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002668 * The library has not been previously initialized by psa_crypto_init().
2669 * It is implementation-dependent whether a failure to initialize
2670 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002671 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002672psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002673 psa_algorithm_t alg,
2674 const uint8_t *hash,
2675 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002676 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002677 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002678
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002679/**
2680 * \brief Encrypt a short message with a public key.
2681 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002682 * \param handle Handle to the key to use for the operation.
2683 * It must be a public key or an asymmetric
2684 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002685 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002686 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002687 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002688 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002689 * \param[in] salt A salt or label, if supported by the
2690 * encryption algorithm.
2691 * If the algorithm does not support a
2692 * salt, pass \c NULL.
2693 * If the algorithm supports an optional
2694 * salt and you do not want to pass a salt,
2695 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002696 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002697 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2698 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002699 * \param salt_length Size of the \p salt buffer in bytes.
2700 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002701 * \param[out] output Buffer where the encrypted message is to
2702 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002703 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002704 * \param[out] output_length On success, the number of bytes
2705 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002706 *
Gilles Peskine28538492018-07-11 17:34:00 +02002707 * \retval #PSA_SUCCESS
2708 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002709 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002710 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002711 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002712 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002713 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002714 * \retval #PSA_ERROR_NOT_SUPPORTED
2715 * \retval #PSA_ERROR_INVALID_ARGUMENT
2716 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2717 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2718 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002719 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002720 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002721 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002722 * The library has not been previously initialized by psa_crypto_init().
2723 * It is implementation-dependent whether a failure to initialize
2724 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002725 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002726psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002727 psa_algorithm_t alg,
2728 const uint8_t *input,
2729 size_t input_length,
2730 const uint8_t *salt,
2731 size_t salt_length,
2732 uint8_t *output,
2733 size_t output_size,
2734 size_t *output_length);
2735
2736/**
2737 * \brief Decrypt a short message with a private key.
2738 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002739 * \param handle Handle to the key to use for the operation.
2740 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002741 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002742 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002743 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002744 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002745 * \param[in] salt A salt or label, if supported by the
2746 * encryption algorithm.
2747 * If the algorithm does not support a
2748 * salt, pass \c NULL.
2749 * If the algorithm supports an optional
2750 * salt and you do not want to pass a salt,
2751 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002752 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002753 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2754 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002755 * \param salt_length Size of the \p salt buffer in bytes.
2756 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002757 * \param[out] output Buffer where the decrypted message is to
2758 * be written.
2759 * \param output_size Size of the \c output buffer in bytes.
2760 * \param[out] output_length On success, the number of bytes
2761 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002762 *
Gilles Peskine28538492018-07-11 17:34:00 +02002763 * \retval #PSA_SUCCESS
2764 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002765 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002766 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002767 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002768 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002769 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002770 * \retval #PSA_ERROR_NOT_SUPPORTED
2771 * \retval #PSA_ERROR_INVALID_ARGUMENT
2772 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2773 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2774 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002775 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002776 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2777 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002778 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002779 * The library has not been previously initialized by psa_crypto_init().
2780 * It is implementation-dependent whether a failure to initialize
2781 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002782 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002783psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002784 psa_algorithm_t alg,
2785 const uint8_t *input,
2786 size_t input_length,
2787 const uint8_t *salt,
2788 size_t salt_length,
2789 uint8_t *output,
2790 size_t output_size,
2791 size_t *output_length);
2792
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002793/**@}*/
2794
Gilles Peskine35675b62019-05-16 17:26:11 +02002795/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002796 * @{
2797 */
2798
Gilles Peskine35675b62019-05-16 17:26:11 +02002799/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002800 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002801 * Before calling any function on a key derivation operation object, the
2802 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002803 * - Set the structure to all-bits-zero, for example:
2804 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002805 * psa_key_derivation_operation_t operation;
2806 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002807 * \endcode
2808 * - Initialize the structure to logical zero values, for example:
2809 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002810 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002811 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002812 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002813 * for example:
2814 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002815 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002816 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002817 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002818 * to the structure, for example:
2819 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002820 * psa_key_derivation_operation_t operation;
2821 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002822 * \endcode
2823 *
2824 * This is an implementation-defined \c struct. Applications should not
2825 * make any assumptions about the content of this structure except
2826 * as directed by the documentation of a specific implementation.
2827 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002828typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002829
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002830/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002831 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002832 * This macro returns a suitable initializer for a key derivation operation
2833 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002834 */
2835#ifdef __DOXYGEN_ONLY__
2836/* This is an example definition for documentation purposes.
2837 * Implementations should define a suitable value in `crypto_struct.h`.
2838 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002839#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02002840#endif
2841
Gilles Peskine35675b62019-05-16 17:26:11 +02002842/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002843 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002844static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002845
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002846/** Set up a key derivation operation.
2847 *
2848 * A key derivation algorithm takes some inputs and uses them to generate
2849 * a byte stream in a deterministic way.
2850 * This byte stream can be used to produce keys and other
2851 * cryptographic material.
2852 *
2853 * To derive a key:
2854 * - Start with an initialized object of type #psa_key_derivation_operation_t.
2855 * - Call psa_key_derivation_setup() to select the algorithm.
2856 * - Provide the inputs for the key derivation by calling
2857 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2858 * as appropriate. Which inputs are needed, in what order, and whether
2859 * they may be keys and if so of what type depends on the algorithm.
2860 * - Optionally set the operation's maximum capacity with
2861 * psa_key_derivation_set_capacity(). You may do this before, in the middle
2862 * of or after providing inputs. For some algorithms, this step is mandatory
2863 * because the output depends on the maximum capacity.
2864 * - To derive a key, call psa_key_derivation_output_key().
2865 * To derive a byte string for a different purpose, call
2866 * - psa_key_derivation_output_bytes().
2867 * Successive calls to these functions use successive output bytes
2868 * calculated by the key derivation algorithm.
2869 * - Clean up the key derivation operation object with
2870 * psa_key_derivation_abort().
2871 *
2872 * \param[in,out] operation The key derivation operation object
2873 * to set up. It must
2874 * have been initialized but not set up yet.
2875 * \param alg The key derivation algorithm to compute
2876 * (\c PSA_ALG_XXX value such that
2877 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2878 *
2879 * \retval #PSA_SUCCESS
2880 * Success.
2881 * \retval #PSA_ERROR_INVALID_ARGUMENT
2882 * \c alg is not a key derivation algorithm.
2883 * \retval #PSA_ERROR_NOT_SUPPORTED
2884 * \c alg is not supported or is not a key derivation algorithm.
2885 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2886 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2887 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002888 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002889 * \retval #PSA_ERROR_BAD_STATE
2890 */
2891psa_status_t psa_key_derivation_setup(
2892 psa_key_derivation_operation_t *operation,
2893 psa_algorithm_t alg);
2894
Gilles Peskine35675b62019-05-16 17:26:11 +02002895/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002896 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002897 * The capacity of a key derivation is the maximum number of bytes that it can
2898 * return. When you get *N* bytes of output from a key derivation operation,
2899 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002900 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002901 * \param[in] operation The operation to query.
2902 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002903 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002904 * \retval #PSA_SUCCESS
2905 * \retval #PSA_ERROR_BAD_STATE
2906 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002907 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002908psa_status_t psa_key_derivation_get_capacity(
2909 const psa_key_derivation_operation_t *operation,
2910 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002911
Gilles Peskine35675b62019-05-16 17:26:11 +02002912/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002913 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002914 * The capacity of a key derivation operation is the maximum number of bytes
2915 * that the key derivation operation can return from this point onwards.
2916 *
2917 * \param[in,out] operation The key derivation operation object to modify.
2918 * \param capacity The new capacity of the operation.
2919 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002920 * current capacity.
2921 *
2922 * \retval #PSA_SUCCESS
2923 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02002924 * \p capacity is larger than the operation's current capacity.
2925 * In this case, the operation object remains valid and its capacity
2926 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002927 * \retval #PSA_ERROR_BAD_STATE
2928 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2929 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002930psa_status_t psa_key_derivation_set_capacity(
2931 psa_key_derivation_operation_t *operation,
2932 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002933
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002934/** Use the maximum possible capacity for a key derivation operation.
2935 *
2936 * Use this value as the capacity argument when setting up a key derivation
2937 * to indicate that the operation should have the maximum possible capacity.
2938 * The value of the maximum possible capacity depends on the key derivation
2939 * algorithm.
2940 */
2941#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
2942
2943/** Provide an input for key derivation or key agreement.
2944 *
2945 * Which inputs are required and in what order depends on the algorithm.
2946 * Refer to the documentation of each key derivation or key agreement
2947 * algorithm for information.
2948 *
2949 * This function passes direct inputs. Some inputs must be passed as keys
2950 * using psa_key_derivation_input_key() instead of this function. Refer to
2951 * the documentation of individual step types for information.
2952 *
2953 * \param[in,out] operation The key derivation operation object to use.
2954 * It must have been set up with
2955 * psa_key_derivation_setup() and must not
2956 * have produced any output yet.
2957 * \param step Which step the input data is for.
2958 * \param[in] data Input data to use.
2959 * \param data_length Size of the \p data buffer in bytes.
2960 *
2961 * \retval #PSA_SUCCESS
2962 * Success.
2963 * \retval #PSA_ERROR_INVALID_ARGUMENT
2964 * \c step is not compatible with the operation's algorithm.
2965 * \retval #PSA_ERROR_INVALID_ARGUMENT
2966 * \c step does not allow direct inputs.
2967 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2968 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2969 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002970 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002971 * \retval #PSA_ERROR_BAD_STATE
2972 * The value of \p step is not valid given the state of \p operation.
2973 * \retval #PSA_ERROR_BAD_STATE
2974 * The library has not been previously initialized by psa_crypto_init().
2975 * It is implementation-dependent whether a failure to initialize
2976 * results in this error code.
2977 */
2978psa_status_t psa_key_derivation_input_bytes(
2979 psa_key_derivation_operation_t *operation,
2980 psa_key_derivation_step_t step,
2981 const uint8_t *data,
2982 size_t data_length);
2983
2984/** Provide an input for key derivation in the form of a key.
2985 *
2986 * Which inputs are required and in what order depends on the algorithm.
2987 * Refer to the documentation of each key derivation or key agreement
2988 * algorithm for information.
2989 *
2990 * This function passes key inputs. Some inputs must be passed as keys
2991 * of the appropriate type using this function, while others must be
2992 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
2993 * the documentation of individual step types for information.
2994 *
2995 * \param[in,out] operation The key derivation operation object to use.
2996 * It must have been set up with
2997 * psa_key_derivation_setup() and must not
2998 * have produced any output yet.
2999 * \param step Which step the input data is for.
3000 * \param handle Handle to the key. It must have an
3001 * appropriate type for \p step and must
3002 * allow the usage #PSA_KEY_USAGE_DERIVE.
3003 *
3004 * \retval #PSA_SUCCESS
3005 * Success.
3006 * \retval #PSA_ERROR_INVALID_HANDLE
3007 * \retval #PSA_ERROR_DOES_NOT_EXIST
3008 * \retval #PSA_ERROR_NOT_PERMITTED
3009 * \retval #PSA_ERROR_INVALID_ARGUMENT
3010 * \c step is not compatible with the operation's algorithm.
3011 * \retval #PSA_ERROR_INVALID_ARGUMENT
3012 * \c step does not allow key inputs.
3013 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3014 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3015 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003016 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003017 * \retval #PSA_ERROR_BAD_STATE
3018 * The value of \p step is not valid given the state of \p operation.
3019 * \retval #PSA_ERROR_BAD_STATE
3020 * The library has not been previously initialized by psa_crypto_init().
3021 * It is implementation-dependent whether a failure to initialize
3022 * results in this error code.
3023 */
3024psa_status_t psa_key_derivation_input_key(
3025 psa_key_derivation_operation_t *operation,
3026 psa_key_derivation_step_t step,
3027 psa_key_handle_t handle);
3028
3029/** Perform a key agreement and use the shared secret as input to a key
3030 * derivation.
3031 *
3032 * A key agreement algorithm takes two inputs: a private key \p private_key
3033 * a public key \p peer_key.
3034 * The result of this function is passed as input to a key derivation.
3035 * The output of this key derivation can be extracted by reading from the
3036 * resulting operation to produce keys and other cryptographic material.
3037 *
3038 * \param[in,out] operation The key derivation operation object to use.
3039 * It must have been set up with
3040 * psa_key_derivation_setup() with a
3041 * key agreement and derivation algorithm
3042 * \c alg (\c PSA_ALG_XXX value such that
3043 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3044 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3045 * is false).
3046 * The operation must be ready for an
3047 * input of the type given by \p step.
3048 * \param step Which step the input data is for.
3049 * \param private_key Handle to the private key to use.
3050 * \param[in] peer_key Public key of the peer. The peer key must be in the
3051 * same format that psa_import_key() accepts for the
3052 * public key type corresponding to the type of
3053 * private_key. That is, this function performs the
3054 * equivalent of
3055 * #psa_import_key(...,
3056 * `peer_key`, `peer_key_length`) where
3057 * with key attributes indicating the public key
3058 * type corresponding to the type of `private_key`.
3059 * For example, for EC keys, this means that peer_key
3060 * is interpreted as a point on the curve that the
3061 * private key is on. The standard formats for public
3062 * keys are documented in the documentation of
3063 * psa_export_public_key().
3064 * \param peer_key_length Size of \p peer_key in bytes.
3065 *
3066 * \retval #PSA_SUCCESS
3067 * Success.
3068 * \retval #PSA_ERROR_INVALID_HANDLE
3069 * \retval #PSA_ERROR_DOES_NOT_EXIST
3070 * \retval #PSA_ERROR_NOT_PERMITTED
3071 * \retval #PSA_ERROR_INVALID_ARGUMENT
3072 * \c private_key is not compatible with \c alg,
3073 * or \p peer_key is not valid for \c alg or not compatible with
3074 * \c private_key.
3075 * \retval #PSA_ERROR_NOT_SUPPORTED
3076 * \c alg is not supported or is not a key derivation algorithm.
3077 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3078 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3079 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003080 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003081 */
3082psa_status_t psa_key_derivation_key_agreement(
3083 psa_key_derivation_operation_t *operation,
3084 psa_key_derivation_step_t step,
3085 psa_key_handle_t private_key,
3086 const uint8_t *peer_key,
3087 size_t peer_key_length);
3088
Gilles Peskine35675b62019-05-16 17:26:11 +02003089/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003090 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003091 * This function calculates output bytes from a key derivation algorithm and
3092 * return those bytes.
3093 * If you view the key derivation's output as a stream of bytes, this
3094 * function destructively reads the requested number of bytes from the
3095 * stream.
3096 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003097 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003098 * \param[in,out] operation The key derivation operation object to read from.
3099 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003100 * \param output_length Number of bytes to output.
3101 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003102 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003103 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003104 * The operation's capacity was less than
3105 * \p output_length bytes. Note that in this case,
3106 * no output is written to the output buffer.
3107 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003108 * subsequent calls to this function will not
3109 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003110 * \retval #PSA_ERROR_BAD_STATE
3111 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3112 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3113 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003114 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003115 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003116psa_status_t psa_key_derivation_output_bytes(
3117 psa_key_derivation_operation_t *operation,
3118 uint8_t *output,
3119 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003120
Gilles Peskine35675b62019-05-16 17:26:11 +02003121/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003122 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003123 * This function calculates output bytes from a key derivation algorithm
3124 * and uses those bytes to generate a key deterministically.
3125 * If you view the key derivation's output as a stream of bytes, this
3126 * function destructively reads as many bytes as required from the
3127 * stream.
3128 * The operation's capacity decreases by the number of bytes read.
3129 *
3130 * How much output is produced and consumed from the operation, and how
3131 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003132 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003133 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003134 * of a given size, this function is functionally equivalent to
3135 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003136 * and passing the resulting output to #psa_import_key.
3137 * However, this function has a security benefit:
3138 * if the implementation provides an isolation boundary then
3139 * the key material is not exposed outside the isolation boundary.
3140 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003141 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003142 * The following key types defined in this specification follow this scheme:
3143 *
3144 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003145 * - #PSA_KEY_TYPE_ARC4;
3146 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003147 * - #PSA_KEY_TYPE_DERIVE;
3148 * - #PSA_KEY_TYPE_HMAC.
3149 *
3150 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003151 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003152 * Montgomery curve), this function always draws a byte string whose
3153 * length is determined by the curve, and sets the mandatory bits
3154 * accordingly. That is:
3155 *
3156 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3157 * and process it as specified in RFC 7748 &sect;5.
3158 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3159 * and process it as specified in RFC 7748 &sect;5.
3160 *
3161 * - For key types for which the key is represented by a single sequence of
3162 * \p bits bits with constraints as to which bit sequences are acceptable,
3163 * this function draws a byte string of length (\p bits / 8) bytes rounded
3164 * up to the nearest whole number of bytes. If the resulting byte string
3165 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3166 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003167 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003168 * for the output produced by psa_export_key().
3169 * The following key types defined in this specification follow this scheme:
3170 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003171 * - #PSA_KEY_TYPE_DES.
3172 * Force-set the parity bits, but discard forbidden weak keys.
3173 * For 2-key and 3-key triple-DES, the three keys are generated
3174 * successively (for example, for 3-key triple-DES,
3175 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3176 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003177 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003178 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003179 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003180 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003181 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003182 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003183 * Weierstrass curve).
3184 * For these key types, interpret the byte string as integer
3185 * in big-endian order. Discard it if it is not in the range
3186 * [0, *N* - 2] where *N* is the boundary of the private key domain
3187 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003188 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003189 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003190 * This method allows compliance to NIST standards, specifically
3191 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003192 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3193 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3194 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3195 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003196 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003197 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003198 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003199 * implementation-defined.
3200 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003201 * In all cases, the data that is read is discarded from the operation.
3202 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003203 *
Gilles Peskine20628592019-04-19 19:29:50 +02003204 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003205 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003206 * \param[out] handle On success, a handle to the newly created key.
3207 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003208 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003209 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003210 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003211 * If the key is persistent, the key material and the key's metadata
3212 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003213 * \retval #PSA_ERROR_ALREADY_EXISTS
3214 * This is an attempt to create a persistent key, and there is
3215 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003216 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003217 * There was not enough data to create the desired key.
3218 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003219 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003220 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003221 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003222 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003223 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003224 * \retval #PSA_ERROR_INVALID_ARGUMENT
3225 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003226 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003227 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3228 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3229 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3230 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003231 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003232 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003233 * The library has not been previously initialized by psa_crypto_init().
3234 * It is implementation-dependent whether a failure to initialize
3235 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003236 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003237psa_status_t psa_key_derivation_output_key(
3238 const psa_key_attributes_t *attributes,
3239 psa_key_derivation_operation_t *operation,
3240 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003241
Gilles Peskine35675b62019-05-16 17:26:11 +02003242/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003243 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003244 * Once a key derivation operation has been aborted, its capacity is zero.
3245 * Aborting an operation frees all associated resources except for the
3246 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003247 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003248 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003249 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003250 * psa_key_derivation_operation_init() or a zero value. In particular,
3251 * it is valid to call psa_key_derivation_abort() twice, or to call
3252 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003253 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003254 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003255 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003256 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003257 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003258 * \retval #PSA_SUCCESS
3259 * \retval #PSA_ERROR_BAD_STATE
3260 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3261 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003262 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003263 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003264psa_status_t psa_key_derivation_abort(
3265 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003266
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003267/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003268 *
3269 * \warning The raw result of a key agreement algorithm such as finite-field
3270 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3271 * not be used directly as key material. It should instead be passed as
3272 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003273 * a key derivation, use psa_key_derivation_key_agreement() and other
3274 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003275 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003276 * \param alg The key agreement algorithm to compute
3277 * (\c PSA_ALG_XXX value such that
3278 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3279 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003280 * \param private_key Handle to the private key to use.
3281 * \param[in] peer_key Public key of the peer. It must be
3282 * in the same format that psa_import_key()
3283 * accepts. The standard formats for public
3284 * keys are documented in the documentation
3285 * of psa_export_public_key().
3286 * \param peer_key_length Size of \p peer_key in bytes.
3287 * \param[out] output Buffer where the decrypted message is to
3288 * be written.
3289 * \param output_size Size of the \c output buffer in bytes.
3290 * \param[out] output_length On success, the number of bytes
3291 * that make up the returned output.
3292 *
3293 * \retval #PSA_SUCCESS
3294 * Success.
3295 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003296 * \retval #PSA_ERROR_NOT_PERMITTED
3297 * \retval #PSA_ERROR_INVALID_ARGUMENT
3298 * \p alg is not a key agreement algorithm
3299 * \retval #PSA_ERROR_INVALID_ARGUMENT
3300 * \p private_key is not compatible with \p alg,
3301 * or \p peer_key is not valid for \p alg or not compatible with
3302 * \p private_key.
3303 * \retval #PSA_ERROR_NOT_SUPPORTED
3304 * \p alg is not a supported key agreement algorithm.
3305 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3306 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3307 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003308 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine769c7a62019-01-18 16:42:29 +01003309 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003310psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3311 psa_key_handle_t private_key,
3312 const uint8_t *peer_key,
3313 size_t peer_key_length,
3314 uint8_t *output,
3315 size_t output_size,
3316 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003317
Gilles Peskineea0fb492018-07-12 17:17:20 +02003318/**@}*/
3319
Gilles Peskineedd76872018-07-20 17:42:05 +02003320/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003321 * @{
3322 */
3323
3324/**
3325 * \brief Generate random bytes.
3326 *
3327 * \warning This function **can** fail! Callers MUST check the return status
3328 * and MUST NOT use the content of the output buffer if the return
3329 * status is not #PSA_SUCCESS.
3330 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003331 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003332 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003333 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003334 * \param output_size Number of bytes to generate and output.
3335 *
Gilles Peskine28538492018-07-11 17:34:00 +02003336 * \retval #PSA_SUCCESS
3337 * \retval #PSA_ERROR_NOT_SUPPORTED
3338 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3339 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3340 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003341 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003342 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003343 * The library has not been previously initialized by psa_crypto_init().
3344 * It is implementation-dependent whether a failure to initialize
3345 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003346 */
3347psa_status_t psa_generate_random(uint8_t *output,
3348 size_t output_size);
3349
3350/**
3351 * \brief Generate a key or key pair.
3352 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003353 * The key is generated randomly.
3354 * Its location, policy, type and size are taken from \p attributes.
3355 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003356 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003357 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003358 * the public exponent is 65537.
3359 * The modulus is a product of two probabilistic primes
3360 * between 2^{n-1} and 2^n where n is the bit size specified in the
3361 * attributes.
3362 *
Gilles Peskine20628592019-04-19 19:29:50 +02003363 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003364 * \param[out] handle On success, a handle to the newly created key.
3365 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003366 *
Gilles Peskine28538492018-07-11 17:34:00 +02003367 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003368 * Success.
3369 * If the key is persistent, the key material and the key's metadata
3370 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003371 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003372 * This is an attempt to create a persistent key, and there is
3373 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003374 * \retval #PSA_ERROR_NOT_SUPPORTED
3375 * \retval #PSA_ERROR_INVALID_ARGUMENT
3376 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3377 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3378 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3379 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003380 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003381 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003382 * The library has not been previously initialized by psa_crypto_init().
3383 * It is implementation-dependent whether a failure to initialize
3384 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003385 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003386psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003387 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003388
3389/**@}*/
3390
Gilles Peskinee59236f2018-01-27 23:32:46 +01003391#ifdef __cplusplus
3392}
3393#endif
3394
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003395/* The file "crypto_sizes.h" contains definitions for size calculation
3396 * macros whose definitions are implementation-specific. */
3397#include "crypto_sizes.h"
3398
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003399/* The file "crypto_struct.h" contains definitions for
3400 * implementation-specific structs that are declared above. */
3401#include "crypto_struct.h"
3402
3403/* The file "crypto_extra.h" contains vendor-specific definitions. This
3404 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003405#include "crypto_extra.h"
3406
3407#endif /* PSA_CRYPTO_H */