blob: 8aa11ce9468129b834d4f85a785f416b13d13675 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +020089 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020090 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine105f67f2019-07-23 18:16:05 +020096/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +020097 * @{
98 */
99
Gilles Peskinea0c06552019-05-21 15:54:54 +0200100/** \def PSA_KEY_ATTRIBUTES_INIT
101 *
102 * This macro returns a suitable initializer for a key attribute structure
103 * of type #psa_key_attributes_t.
104 */
105#ifdef __DOXYGEN_ONLY__
106/* This is an example definition for documentation purposes.
107 * Implementations should define a suitable value in `crypto_struct.h`.
108 */
109#define PSA_KEY_ATTRIBUTES_INIT {0}
110#endif
111
112/** Return an initial value for a key attributes structure.
113 */
114static psa_key_attributes_t psa_key_attributes_init(void);
115
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200116/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200117 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200118 * If the attribute structure currently declares the key as volatile (which
119 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200120 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200121 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200122 * This function does not access storage, it merely stores the given
123 * value in the structure.
124 * The persistent key will be written to storage when the attribute
125 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200126 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200127 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200128 *
Gilles Peskine20628592019-04-19 19:29:50 +0200129 * This function may be declared as `static` (i.e. without external
130 * linkage). This function may be provided as a function-like macro,
131 * but in this case it must evaluate each of its arguments exactly once.
132 *
133 * \param[out] attributes The attribute structure to write to.
134 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200135 */
136static void psa_set_key_id(psa_key_attributes_t *attributes,
137 psa_key_id_t id);
138
139/** Set the location of a persistent key.
140 *
141 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200142 * with psa_set_key_id(). By default, a key that has a persistent identifier
143 * is stored in the default storage area identifier by
144 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
145 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200146 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200147 * This function does not access storage, it merely stores the given
148 * value in the structure.
149 * The persistent key will be written to storage when the attribute
150 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200151 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200152 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200153 *
154 * This function may be declared as `static` (i.e. without external
155 * linkage). This function may be provided as a function-like macro,
156 * but in this case it must evaluate each of its arguments exactly once.
157 *
158 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200159 * \param lifetime The lifetime for the key.
160 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200161 * key will be volatile, and the key identifier
162 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200163 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200164static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
165 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200166
Gilles Peskine20628592019-04-19 19:29:50 +0200167/** Retrieve the key identifier from key attributes.
168 *
169 * This function may be declared as `static` (i.e. without external
170 * linkage). This function may be provided as a function-like macro,
171 * but in this case it must evaluate its argument exactly once.
172 *
173 * \param[in] attributes The key attribute structure to query.
174 *
175 * \return The persistent identifier stored in the attribute structure.
176 * This value is unspecified if the attribute structure declares
177 * the key as volatile.
178 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200179static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
180
Gilles Peskine20628592019-04-19 19:29:50 +0200181/** Retrieve the lifetime from key attributes.
182 *
183 * This function may be declared as `static` (i.e. without external
184 * linkage). This function may be provided as a function-like macro,
185 * but in this case it must evaluate its argument exactly once.
186 *
187 * \param[in] attributes The key attribute structure to query.
188 *
189 * \return The lifetime value stored in the attribute structure.
190 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200191static psa_key_lifetime_t psa_get_key_lifetime(
192 const psa_key_attributes_t *attributes);
193
Gilles Peskine20628592019-04-19 19:29:50 +0200194/** Declare usage flags for a key.
195 *
196 * Usage flags are part of a key's usage policy. They encode what
197 * kind of operations are permitted on the key. For more details,
198 * refer to the documentation of the type #psa_key_usage_t.
199 *
200 * This function overwrites any usage flags
201 * previously set in \p attributes.
202 *
203 * This function may be declared as `static` (i.e. without external
204 * linkage). This function may be provided as a function-like macro,
205 * but in this case it must evaluate each of its arguments exactly once.
206 *
207 * \param[out] attributes The attribute structure to write to.
208 * \param usage_flags The usage flags to write.
209 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200210static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
211 psa_key_usage_t usage_flags);
212
Gilles Peskine20628592019-04-19 19:29:50 +0200213/** Retrieve the usage flags from key attributes.
214 *
215 * This function may be declared as `static` (i.e. without external
216 * linkage). This function may be provided as a function-like macro,
217 * but in this case it must evaluate its argument exactly once.
218 *
219 * \param[in] attributes The key attribute structure to query.
220 *
221 * \return The usage flags stored in the attribute structure.
222 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200223static psa_key_usage_t psa_get_key_usage_flags(
224 const psa_key_attributes_t *attributes);
225
Gilles Peskine20628592019-04-19 19:29:50 +0200226/** Declare the permitted algorithm policy for a key.
227 *
228 * The permitted algorithm policy of a key encodes which algorithm or
229 * algorithms are permitted to be used with this key.
230 *
231 * This function overwrites any algorithm policy
232 * previously set in \p attributes.
233 *
234 * This function may be declared as `static` (i.e. without external
235 * linkage). This function may be provided as a function-like macro,
236 * but in this case it must evaluate each of its arguments exactly once.
237 *
238 * \param[out] attributes The attribute structure to write to.
239 * \param alg The permitted algorithm policy to write.
240 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200241static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
242 psa_algorithm_t alg);
243
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100244
Gilles Peskine20628592019-04-19 19:29:50 +0200245/** Retrieve the algorithm policy from key attributes.
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate its argument exactly once.
250 *
251 * \param[in] attributes The key attribute structure to query.
252 *
253 * \return The algorithm stored in the attribute structure.
254 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200255static psa_algorithm_t psa_get_key_algorithm(
256 const psa_key_attributes_t *attributes);
257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Declare the type of a key.
259 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200260 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200261 * previously set in \p attributes.
262 *
263 * This function may be declared as `static` (i.e. without external
264 * linkage). This function may be provided as a function-like macro,
265 * but in this case it must evaluate each of its arguments exactly once.
266 *
267 * \param[out] attributes The attribute structure to write to.
268 * \param type The key type to write.
269 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200270static void psa_set_key_type(psa_key_attributes_t *attributes,
271 psa_key_type_t type);
272
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100273
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200274/** Declare the size of a key.
275 *
276 * This function overwrites any key size previously set in \p attributes.
277 *
278 * This function may be declared as `static` (i.e. without external
279 * linkage). This function may be provided as a function-like macro,
280 * but in this case it must evaluate each of its arguments exactly once.
281 *
282 * \param[out] attributes The attribute structure to write to.
283 * \param bits The key size in bits.
284 */
285static void psa_set_key_bits(psa_key_attributes_t *attributes,
286 size_t bits);
287
Gilles Peskine20628592019-04-19 19:29:50 +0200288/** Retrieve the key type from key attributes.
289 *
290 * This function may be declared as `static` (i.e. without external
291 * linkage). This function may be provided as a function-like macro,
292 * but in this case it must evaluate its argument exactly once.
293 *
294 * \param[in] attributes The key attribute structure to query.
295 *
296 * \return The key type stored in the attribute structure.
297 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200298static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
299
Gilles Peskine20628592019-04-19 19:29:50 +0200300/** Retrieve the key size from key attributes.
301 *
302 * This function may be declared as `static` (i.e. without external
303 * linkage). This function may be provided as a function-like macro,
304 * but in this case it must evaluate its argument exactly once.
305 *
306 * \param[in] attributes The key attribute structure to query.
307 *
308 * \return The key size stored in the attribute structure, in bits.
309 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200310static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
311
Gilles Peskine20628592019-04-19 19:29:50 +0200312/** Retrieve the attributes of a key.
313 *
314 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200315 * psa_reset_key_attributes(). It then copies the attributes of
316 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200317 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200318 * \note This function may allocate memory or other resources.
319 * Once you have called this function on an attribute structure,
320 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200321 *
Gilles Peskine20628592019-04-19 19:29:50 +0200322 * \param[in] handle Handle to the key to query.
323 * \param[in,out] attributes On success, the attributes of the key.
324 * On failure, equivalent to a
325 * freshly-initialized structure.
326 *
327 * \retval #PSA_SUCCESS
328 * \retval #PSA_ERROR_INVALID_HANDLE
329 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
330 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
331 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200332psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
333 psa_key_attributes_t *attributes);
334
Gilles Peskine20628592019-04-19 19:29:50 +0200335/** Reset a key attribute structure to a freshly initialized state.
336 *
337 * You must initialize the attribute structure as described in the
338 * documentation of the type #psa_key_attributes_t before calling this
339 * function. Once the structure has been initialized, you may call this
340 * function at any time.
341 *
342 * This function frees any auxiliary resources that the structure
343 * may contain.
344 *
345 * \param[in,out] attributes The attribute structure to reset.
346 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200347void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200348
Gilles Peskine87a5e562019-04-17 12:28:25 +0200349/**@}*/
350
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100351/** \defgroup key_management Key management
352 * @{
353 */
354
Gilles Peskinef535eb22018-11-30 14:08:36 +0100355/** Open a handle to an existing persistent key.
356 *
Gilles Peskine4754cde2019-05-21 15:56:29 +0200357 * Open a handle to a persistent key. A key is persistent if it was created
358 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
359 * always has a nonzero key identifier, set with psa_set_key_id() when
360 * creating the key. Implementations may provide additional pre-provisioned
Andrew Thoelke203491c2019-08-21 17:55:30 +0100361 * keys that can be opened with psa_open_key(). Such keys have a key identifier
362 * in the vendor range, as documented in the description of #psa_key_id_t.
Gilles Peskine4754cde2019-05-21 15:56:29 +0200363 *
364 * The application must eventually close the handle with psa_close_key()
365 * to release associated resources. If the application dies without calling
366 * psa_close_key(), the implementation should perform the equivalent of a
367 * call to psa_close_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100368 *
Andrew Thoelke9741b112019-08-21 18:20:41 +0100369 * Some implementations permit an application to open the same key multiple
370 * times. Applications that rely on this behavior will not be portable to
371 * implementations that only permit a single key handle to be opened. See
372 * also :ref:\`key-handles\`.
373 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100374 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100375 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100376 *
377 * \retval #PSA_SUCCESS
378 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100379 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100380 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100381 * The implementation does not have sufficient resources to open the
382 * key. This can be due to reaching an implementation limit on the
383 * number of open keys, the number of open key handles, or available
384 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200385 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100386 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100387 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100388 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100389 * \retval #PSA_ERROR_NOT_PERMITTED
390 * The specified key exists, but the application does not have the
391 * permission to access it. Note that this specification does not
392 * define any way to create such a key, but it may be possible
393 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200394 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
395 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100396 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200397psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100398 psa_key_handle_t *handle);
399
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100400
Gilles Peskinef535eb22018-11-30 14:08:36 +0100401/** Close a key handle.
402 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100403 * If the handle designates a volatile key, this will destroy the key material
404 * and free all associated resources, just like psa_destroy_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100405 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100406 * If this is the last open handle to a persistent key, then closing the handle
407 * will free all resources associated with the key in volatile memory. The key
408 * data in persistent storage is not affected and can be opened again later
409 * with a call to psa_open_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100410 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100411 * Closing the key handle makes the handle invalid, and the key handle
412 * must not be used again by the application..
413 *
414 * If the key is currently in use in a multipart operation, then closing the
415 * last handle to the key will abort the multipart operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100416 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100417 * \param handle The key handle to close.
418 *
419 * \retval #PSA_SUCCESS
420 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100421 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100422 */
423psa_status_t psa_close_key(psa_key_handle_t handle);
424
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100425/**@}*/
426
427/** \defgroup import_export Key import and export
428 * @{
429 */
430
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100431/**
432 * \brief Import a key in binary format.
433 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100434 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100435 * documentation of psa_export_public_key() for the format of public keys
436 * and to the documentation of psa_export_key() for the format for
437 * other key types.
438 *
439 * This specification supports a single format for each key type.
440 * Implementations may support other formats as long as the standard
441 * format is supported. Implementations that support other formats
442 * should ensure that the formats are clearly unambiguous so as to
443 * minimize the risk that an invalid input is accidentally interpreted
444 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100445 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100446
Gilles Peskine20628592019-04-19 19:29:50 +0200447 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200448 * The key size is always determined from the
449 * \p data buffer.
450 * If the key size in \p attributes is nonzero,
451 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200452 * \param[out] handle On success, a handle to the newly created key.
453 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100454 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200455 * buffer is interpreted according to the type declared
456 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200457 * All implementations must support at least the format
458 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100459 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200460 * the chosen type. Implementations may allow other
461 * formats, but should be conservative: implementations
462 * should err on the side of rejecting content if it
463 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200464 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100465 *
Gilles Peskine28538492018-07-11 17:34:00 +0200466 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100467 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100468 * If the key is persistent, the key material and the key's metadata
469 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200470 * \retval #PSA_ERROR_ALREADY_EXISTS
471 * This is an attempt to create a persistent key, and there is
472 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200473 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200474 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200475 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200476 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200477 * The key attributes, as a whole, are invalid.
478 * \retval #PSA_ERROR_INVALID_ARGUMENT
479 * The key data is not correctly formatted.
480 * \retval #PSA_ERROR_INVALID_ARGUMENT
481 * The size in \p attributes is nonzero and does not match the size
482 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200483 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
484 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
485 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100486 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200487 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200488 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300489 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300490 * The library has not been previously initialized by psa_crypto_init().
491 * It is implementation-dependent whether a failure to initialize
492 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100493 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200494psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100495 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200496 size_t data_length,
497 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100498
499/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100500 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200501 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100502 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200503 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100504 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200505 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100506 * This function also erases any metadata such as policies and frees all
507 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200508 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100509 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100510 *
Gilles Peskine28538492018-07-11 17:34:00 +0200511 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100512 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200513 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100514 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200515 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100516 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200517 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200518 * There was an failure in communication with the cryptoprocessor.
519 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200520 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200521 * The storage is corrupted. Implementations shall make a best effort
522 * to erase key material even in this stage, however applications
523 * should be aware that it may be impossible to guarantee that the
524 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200525 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200526 * An unexpected condition which is not a storage corruption or
527 * a communication failure occurred. The cryptoprocessor may have
528 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300529 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300530 * The library has not been previously initialized by psa_crypto_init().
531 * It is implementation-dependent whether a failure to initialize
532 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100533 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100534psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100535
536/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100537 * \brief Export a key in binary format.
538 *
539 * The output of this function can be passed to psa_import_key() to
540 * create an equivalent object.
541 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100542 * If the implementation of psa_import_key() supports other formats
543 * beyond the format specified here, the output from psa_export_key()
544 * must use the representation specified here, not the original
545 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100546 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100547 * For standard key types, the output format is as follows:
548 *
549 * - For symmetric keys (including MAC keys), the format is the
550 * raw bytes of the key.
551 * - For DES, the key data consists of 8 bytes. The parity bits must be
552 * correct.
553 * - For Triple-DES, the format is the concatenation of the
554 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200555 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200556 * is the non-encrypted DER encoding of the representation defined by
557 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
558 * ```
559 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200560 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200561 * modulus INTEGER, -- n
562 * publicExponent INTEGER, -- e
563 * privateExponent INTEGER, -- d
564 * prime1 INTEGER, -- p
565 * prime2 INTEGER, -- q
566 * exponent1 INTEGER, -- d mod (p-1)
567 * exponent2 INTEGER, -- d mod (q-1)
568 * coefficient INTEGER, -- (inverse of q) mod p
569 * }
570 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200571 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200572 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100573 * a representation of the private value as a `ceiling(m/8)`-byte string
574 * where `m` is the bit size associated with the curve, i.e. the bit size
575 * of the order of the curve's coordinate field. This byte string is
576 * in little-endian order for Montgomery curves (curve types
577 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
578 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
579 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100580 * This is the content of the `privateKey` field of the `ECPrivateKey`
581 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200582 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200583 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000584 * format is the representation of the private key `x` as a big-endian byte
585 * string. The length of the byte string is the private key size in bytes
586 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200587 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
588 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100589 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200590 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
591 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100592 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200593 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200594 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200595 * \param[out] data_length On success, the number of bytes
596 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100597 *
Gilles Peskine28538492018-07-11 17:34:00 +0200598 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100599 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200600 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200601 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200602 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100603 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200604 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
605 * The size of the \p data buffer is too small. You can determine a
606 * sufficient buffer size by calling
607 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
608 * where \c type is the key type
609 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200610 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
611 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200612 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300613 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300614 * The library has not been previously initialized by psa_crypto_init().
615 * It is implementation-dependent whether a failure to initialize
616 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100617 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100618psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619 uint8_t *data,
620 size_t data_size,
621 size_t *data_length);
622
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100623/**
624 * \brief Export a public key or the public part of a key pair in binary format.
625 *
626 * The output of this function can be passed to psa_import_key() to
627 * create an object that is equivalent to the public key.
628 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000629 * This specification supports a single format for each key type.
630 * Implementations may support other formats as long as the standard
631 * format is supported. Implementations that support other formats
632 * should ensure that the formats are clearly unambiguous so as to
633 * minimize the risk that an invalid input is accidentally interpreted
634 * according to a different format.
635 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000636 * For standard key types, the output format is as follows:
637 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
638 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
639 * ```
640 * RSAPublicKey ::= SEQUENCE {
641 * modulus INTEGER, -- n
642 * publicExponent INTEGER } -- e
643 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000644 * - For elliptic curve public keys (key types for which
645 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
646 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
647 * Let `m` be the bit size associated with the curve, i.e. the bit size of
648 * `q` for a curve over `F_q`. The representation consists of:
649 * - The byte 0x04;
650 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
651 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200652 * - For Diffie-Hellman key exchange public keys (key types for which
653 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000654 * the format is the representation of the public key `y = g^x mod p` as a
655 * big-endian byte string. The length of the byte string is the length of the
656 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100657 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200658 * Exporting a public key object or the public part of a key pair is
659 * always permitted, regardless of the key's usage flags.
660 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100661 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200662 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200663 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200664 * \param[out] data_length On success, the number of bytes
665 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100666 *
Gilles Peskine28538492018-07-11 17:34:00 +0200667 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100668 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200669 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200670 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200671 * The key is neither a public key nor a key pair.
672 * \retval #PSA_ERROR_NOT_SUPPORTED
673 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
674 * The size of the \p data buffer is too small. You can determine a
675 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200676 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200677 * where \c type is the key type
678 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200679 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
680 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200681 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300682 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300683 * The library has not been previously initialized by psa_crypto_init().
684 * It is implementation-dependent whether a failure to initialize
685 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100686 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100687psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100688 uint8_t *data,
689 size_t data_size,
690 size_t *data_length);
691
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100692/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100693 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100694 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000695 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100696 * This function is primarily useful to copy a key from one location
697 * to another, since it populates a key using the material from
698 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200699 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100700 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100701 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100702 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200703 * The policy on the source key must have the usage flag
704 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200705 * This flag is sufficient to permit the copy if the key has the lifetime
706 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
707 * Some secure elements do not provide a way to copy a key without
708 * making it extractable from the secure element. If a key is located
709 * in such a secure element, then the key must have both usage flags
710 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
711 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200712 *
Gilles Peskine20628592019-04-19 19:29:50 +0200713 * The resulting key may only be used in a way that conforms to
714 * both the policy of the original key and the policy specified in
715 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100716 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200717 * usage flags on the source policy and the usage flags in \p attributes.
718 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100719 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200720 * - If either of the policies allows an algorithm and the other policy
721 * allows a wildcard-based algorithm policy that includes this algorithm,
722 * the resulting key allows the same algorithm.
723 * - If the policies do not allow any algorithm in common, this function
724 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200725 *
Gilles Peskine20628592019-04-19 19:29:50 +0200726 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100727 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200728 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100729 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200730 * \param[in] attributes The attributes for the new key.
731 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200732 * - The key type and size may be 0. If either is
733 * nonzero, it must match the corresponding
734 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200735 * - The key location (the lifetime and, for
736 * persistent keys, the key identifier) is
737 * used directly.
738 * - The policy constraints (usage flags and
739 * algorithm policy) are combined from
740 * the source key and \p attributes so that
741 * both sets of restrictions apply, as
742 * described in the documentation of this function.
743 * \param[out] target_handle On success, a handle to the newly created key.
744 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200745 *
746 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100747 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200748 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200749 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200750 * This is an attempt to create a persistent key, and there is
751 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200752 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200753 * The lifetime or identifier in \p attributes are invalid.
754 * \retval #PSA_ERROR_INVALID_ARGUMENT
755 * The policy constraints on the source and specified in
756 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200757 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200758 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200759 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100760 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200761 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
762 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100763 * The source key is not exportable and its lifetime does not
764 * allow copying it to the target's lifetime.
765 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
766 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200767 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
768 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200769 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100770 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100771psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200772 const psa_key_attributes_t *attributes,
773 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100774
775/**@}*/
776
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100777/** \defgroup hash Message digests
778 * @{
779 */
780
Gilles Peskine69647a42019-01-14 20:18:12 +0100781/** Calculate the hash (digest) of a message.
782 *
783 * \note To verify the hash of a message against an
784 * expected value, use psa_hash_compare() instead.
785 *
786 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
787 * such that #PSA_ALG_IS_HASH(\p alg) is true).
788 * \param[in] input Buffer containing the message to hash.
789 * \param input_length Size of the \p input buffer in bytes.
790 * \param[out] hash Buffer where the hash is to be written.
791 * \param hash_size Size of the \p hash buffer in bytes.
792 * \param[out] hash_length On success, the number of bytes
793 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100794 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100795 *
796 * \retval #PSA_SUCCESS
797 * Success.
798 * \retval #PSA_ERROR_NOT_SUPPORTED
799 * \p alg is not supported or is not a hash algorithm.
800 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
801 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
802 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200803 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100804 */
805psa_status_t psa_hash_compute(psa_algorithm_t alg,
806 const uint8_t *input,
807 size_t input_length,
808 uint8_t *hash,
809 size_t hash_size,
810 size_t *hash_length);
811
812/** Calculate the hash (digest) of a message and compare it with a
813 * reference value.
814 *
815 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
816 * such that #PSA_ALG_IS_HASH(\p alg) is true).
817 * \param[in] input Buffer containing the message to hash.
818 * \param input_length Size of the \p input buffer in bytes.
819 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100820 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100821 *
822 * \retval #PSA_SUCCESS
823 * The expected hash is identical to the actual hash of the input.
824 * \retval #PSA_ERROR_INVALID_SIGNATURE
825 * The hash of the message was calculated successfully, but it
826 * differs from the expected hash.
827 * \retval #PSA_ERROR_NOT_SUPPORTED
828 * \p alg is not supported or is not a hash algorithm.
829 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
830 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
831 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200832 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +0100833 */
834psa_status_t psa_hash_compare(psa_algorithm_t alg,
835 const uint8_t *input,
836 size_t input_length,
837 const uint8_t *hash,
838 const size_t hash_length);
839
Gilles Peskine308b91d2018-02-08 09:47:44 +0100840/** The type of the state data structure for multipart hash operations.
841 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000842 * Before calling any function on a hash operation object, the application must
843 * initialize it by any of the following means:
844 * - Set the structure to all-bits-zero, for example:
845 * \code
846 * psa_hash_operation_t operation;
847 * memset(&operation, 0, sizeof(operation));
848 * \endcode
849 * - Initialize the structure to logical zero values, for example:
850 * \code
851 * psa_hash_operation_t operation = {0};
852 * \endcode
853 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
854 * for example:
855 * \code
856 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
857 * \endcode
858 * - Assign the result of the function psa_hash_operation_init()
859 * to the structure, for example:
860 * \code
861 * psa_hash_operation_t operation;
862 * operation = psa_hash_operation_init();
863 * \endcode
864 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100865 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100866 * make any assumptions about the content of this structure except
867 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100868typedef struct psa_hash_operation_s psa_hash_operation_t;
869
Jaeden Amero6a25b412019-01-04 11:47:44 +0000870/** \def PSA_HASH_OPERATION_INIT
871 *
872 * This macro returns a suitable initializer for a hash operation object
873 * of type #psa_hash_operation_t.
874 */
875#ifdef __DOXYGEN_ONLY__
876/* This is an example definition for documentation purposes.
877 * Implementations should define a suitable value in `crypto_struct.h`.
878 */
879#define PSA_HASH_OPERATION_INIT {0}
880#endif
881
882/** Return an initial value for a hash operation object.
883 */
884static psa_hash_operation_t psa_hash_operation_init(void);
885
Gilles Peskinef45adda2019-01-14 18:29:18 +0100886/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100887 *
888 * The sequence of operations to calculate a hash (message digest)
889 * is as follows:
890 * -# Allocate an operation object which will be passed to all the functions
891 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000892 * -# Initialize the operation object with one of the methods described in the
893 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200894 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100895 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100896 * of the message each time. The hash that is calculated is the hash
897 * of the concatenation of these messages in order.
898 * -# To calculate the hash, call psa_hash_finish().
899 * To compare the hash with an expected value, call psa_hash_verify().
900 *
901 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000902 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100903 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200904 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100905 * eventually terminate the operation. The following events terminate an
906 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100907 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100908 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100909 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000910 * \param[in,out] operation The operation object to set up. It must have
911 * been initialized as per the documentation for
912 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200913 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
914 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100915 *
Gilles Peskine28538492018-07-11 17:34:00 +0200916 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100917 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200918 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200919 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100920 * \retval #PSA_ERROR_BAD_STATE
921 * The operation state is not valid (already set up and not
922 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200923 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
924 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
925 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200926 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100927 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200928psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100929 psa_algorithm_t alg);
930
Gilles Peskine308b91d2018-02-08 09:47:44 +0100931/** Add a message fragment to a multipart hash operation.
932 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200933 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100934 *
935 * If this function returns an error status, the operation becomes inactive.
936 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200937 * \param[in,out] operation Active hash operation.
938 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200939 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100940 *
Gilles Peskine28538492018-07-11 17:34:00 +0200941 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100942 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200943 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100944 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200945 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
946 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
947 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200948 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100949 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100950psa_status_t psa_hash_update(psa_hash_operation_t *operation,
951 const uint8_t *input,
952 size_t input_length);
953
Gilles Peskine308b91d2018-02-08 09:47:44 +0100954/** Finish the calculation of the hash of a message.
955 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200956 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100957 * This function calculates the hash of the message formed by concatenating
958 * the inputs passed to preceding calls to psa_hash_update().
959 *
960 * When this function returns, the operation becomes inactive.
961 *
962 * \warning Applications should not call this function if they expect
963 * a specific value for the hash. Call psa_hash_verify() instead.
964 * Beware that comparing integrity or authenticity data such as
965 * hash values with a function such as \c memcmp is risky
966 * because the time taken by the comparison may leak information
967 * about the hashed data which could allow an attacker to guess
968 * a valid hash and thereby bypass security controls.
969 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200970 * \param[in,out] operation Active hash operation.
971 * \param[out] hash Buffer where the hash is to be written.
972 * \param hash_size Size of the \p hash buffer in bytes.
973 * \param[out] hash_length On success, the number of bytes
974 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200975 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200976 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100977 *
Gilles Peskine28538492018-07-11 17:34:00 +0200978 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100979 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200980 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100981 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200982 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200983 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200984 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100985 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200986 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
987 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
988 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200989 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100991psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
992 uint8_t *hash,
993 size_t hash_size,
994 size_t *hash_length);
995
Gilles Peskine308b91d2018-02-08 09:47:44 +0100996/** Finish the calculation of the hash of a message and compare it with
997 * an expected value.
998 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200999 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001000 * This function calculates the hash of the message formed by concatenating
1001 * the inputs passed to preceding calls to psa_hash_update(). It then
1002 * compares the calculated hash with the expected hash passed as a
1003 * parameter to this function.
1004 *
1005 * When this function returns, the operation becomes inactive.
1006 *
Gilles Peskine19067982018-03-20 17:54:53 +01001007 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001008 * comparison between the actual hash and the expected hash is performed
1009 * in constant time.
1010 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001011 * \param[in,out] operation Active hash operation.
1012 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001013 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001014 *
Gilles Peskine28538492018-07-11 17:34:00 +02001015 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001016 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001017 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001018 * The hash of the message was calculated successfully, but it
1019 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001020 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001021 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001022 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1023 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1024 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001025 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001026 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001027psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1028 const uint8_t *hash,
1029 size_t hash_length);
1030
Gilles Peskine308b91d2018-02-08 09:47:44 +01001031/** Abort a hash operation.
1032 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001033 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001034 * \p operation structure itself. Once aborted, the operation object
1035 * can be reused for another operation by calling
1036 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001037 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001038 * You may call this function any time after the operation object has
1039 * been initialized by any of the following methods:
1040 * - A call to psa_hash_setup(), whether it succeeds or not.
1041 * - Initializing the \c struct to all-bits-zero.
1042 * - Initializing the \c struct to logical zeros, e.g.
1043 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001044 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001045 * In particular, calling psa_hash_abort() after the operation has been
1046 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1047 * psa_hash_verify() is safe and has no effect.
1048 *
1049 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001050 *
Gilles Peskine28538492018-07-11 17:34:00 +02001051 * \retval #PSA_SUCCESS
1052 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001053 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001054 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1055 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001056 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001057 */
1058psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001059
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001060/** Clone a hash operation.
1061 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001062 * This function copies the state of an ongoing hash operation to
1063 * a new operation object. In other words, this function is equivalent
1064 * to calling psa_hash_setup() on \p target_operation with the same
1065 * algorithm that \p source_operation was set up for, then
1066 * psa_hash_update() on \p target_operation with the same input that
1067 * that was passed to \p source_operation. After this function returns, the
1068 * two objects are independent, i.e. subsequent calls involving one of
1069 * the objects do not affect the other object.
1070 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001071 * \param[in] source_operation The active hash operation to clone.
1072 * \param[in,out] target_operation The operation object to set up.
1073 * It must be initialized but not active.
1074 *
1075 * \retval #PSA_SUCCESS
1076 * \retval #PSA_ERROR_BAD_STATE
1077 * \p source_operation is not an active hash operation.
1078 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001079 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001080 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1081 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001082 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001083 */
1084psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1085 psa_hash_operation_t *target_operation);
1086
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001087/**@}*/
1088
Gilles Peskine8c9def32018-02-08 10:02:12 +01001089/** \defgroup MAC Message authentication codes
1090 * @{
1091 */
1092
Gilles Peskine69647a42019-01-14 20:18:12 +01001093/** Calculate the MAC (message authentication code) of a message.
1094 *
1095 * \note To verify the MAC of a message against an
1096 * expected value, use psa_mac_verify() instead.
1097 * Beware that comparing integrity or authenticity data such as
1098 * MAC values with a function such as \c memcmp is risky
1099 * because the time taken by the comparison may leak information
1100 * about the MAC value which could allow an attacker to guess
1101 * a valid MAC and thereby bypass security controls.
1102 *
1103 * \param handle Handle to the key to use for the operation.
1104 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001105 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001106 * \param[in] input Buffer containing the input message.
1107 * \param input_length Size of the \p input buffer in bytes.
1108 * \param[out] mac Buffer where the MAC value is to be written.
1109 * \param mac_size Size of the \p mac buffer in bytes.
1110 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001111 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001112 *
1113 * \retval #PSA_SUCCESS
1114 * Success.
1115 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001116 * \retval #PSA_ERROR_NOT_PERMITTED
1117 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001118 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001119 * \retval #PSA_ERROR_NOT_SUPPORTED
1120 * \p alg is not supported or is not a MAC algorithm.
1121 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1122 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1123 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001124 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001125 * \retval #PSA_ERROR_BAD_STATE
1126 * The library has not been previously initialized by psa_crypto_init().
1127 * It is implementation-dependent whether a failure to initialize
1128 * results in this error code.
1129 */
1130psa_status_t psa_mac_compute(psa_key_handle_t handle,
1131 psa_algorithm_t alg,
1132 const uint8_t *input,
1133 size_t input_length,
1134 uint8_t *mac,
1135 size_t mac_size,
1136 size_t *mac_length);
1137
1138/** Calculate the MAC of a message and compare it with a reference value.
1139 *
1140 * \param handle Handle to the key to use for the operation.
1141 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001142 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001143 * \param[in] input Buffer containing the input message.
1144 * \param input_length Size of the \p input buffer in bytes.
1145 * \param[out] mac Buffer containing the expected MAC value.
1146 * \param mac_length Size of the \p mac buffer in bytes.
1147 *
1148 * \retval #PSA_SUCCESS
1149 * The expected MAC is identical to the actual MAC of the input.
1150 * \retval #PSA_ERROR_INVALID_SIGNATURE
1151 * The MAC of the message was calculated successfully, but it
1152 * differs from the expected value.
1153 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001154 * \retval #PSA_ERROR_NOT_PERMITTED
1155 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001156 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001157 * \retval #PSA_ERROR_NOT_SUPPORTED
1158 * \p alg is not supported or is not a MAC algorithm.
1159 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1160 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1161 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001162 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001163 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001164psa_status_t psa_mac_verify(psa_key_handle_t handle,
1165 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001166 const uint8_t *input,
1167 size_t input_length,
1168 const uint8_t *mac,
1169 const size_t mac_length);
1170
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001171/** The type of the state data structure for multipart MAC operations.
1172 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001173 * Before calling any function on a MAC operation object, the application must
1174 * initialize it by any of the following means:
1175 * - Set the structure to all-bits-zero, for example:
1176 * \code
1177 * psa_mac_operation_t operation;
1178 * memset(&operation, 0, sizeof(operation));
1179 * \endcode
1180 * - Initialize the structure to logical zero values, for example:
1181 * \code
1182 * psa_mac_operation_t operation = {0};
1183 * \endcode
1184 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1185 * for example:
1186 * \code
1187 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1188 * \endcode
1189 * - Assign the result of the function psa_mac_operation_init()
1190 * to the structure, for example:
1191 * \code
1192 * psa_mac_operation_t operation;
1193 * operation = psa_mac_operation_init();
1194 * \endcode
1195 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001196 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001197 * make any assumptions about the content of this structure except
1198 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001199typedef struct psa_mac_operation_s psa_mac_operation_t;
1200
Jaeden Amero769ce272019-01-04 11:48:03 +00001201/** \def PSA_MAC_OPERATION_INIT
1202 *
1203 * This macro returns a suitable initializer for a MAC operation object of type
1204 * #psa_mac_operation_t.
1205 */
1206#ifdef __DOXYGEN_ONLY__
1207/* This is an example definition for documentation purposes.
1208 * Implementations should define a suitable value in `crypto_struct.h`.
1209 */
1210#define PSA_MAC_OPERATION_INIT {0}
1211#endif
1212
1213/** Return an initial value for a MAC operation object.
1214 */
1215static psa_mac_operation_t psa_mac_operation_init(void);
1216
Gilles Peskinef45adda2019-01-14 18:29:18 +01001217/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001218 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001219 * This function sets up the calculation of the MAC
1220 * (message authentication code) of a byte string.
1221 * To verify the MAC of a message against an
1222 * expected value, use psa_mac_verify_setup() instead.
1223 *
1224 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001225 * -# Allocate an operation object which will be passed to all the functions
1226 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001227 * -# Initialize the operation object with one of the methods described in the
1228 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001229 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001230 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1231 * of the message each time. The MAC that is calculated is the MAC
1232 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001233 * -# At the end of the message, call psa_mac_sign_finish() to finish
1234 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001235 *
1236 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001237 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001238 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001239 * After a successful call to psa_mac_sign_setup(), the application must
1240 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001241 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001242 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001243 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001244 * \param[in,out] operation The operation object to set up. It must have
1245 * been initialized as per the documentation for
1246 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001247 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001248 * It must remain valid until the operation
1249 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001250 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001251 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001252 *
Gilles Peskine28538492018-07-11 17:34:00 +02001253 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001254 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001255 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001256 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001257 * \retval #PSA_ERROR_NOT_PERMITTED
1258 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001259 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001260 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001261 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001262 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1263 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1264 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001265 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001266 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001267 * The operation state is not valid (already set up and not
1268 * subsequently completed).
1269 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001270 * The library has not been previously initialized by psa_crypto_init().
1271 * It is implementation-dependent whether a failure to initialize
1272 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001273 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001274psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001275 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001276 psa_algorithm_t alg);
1277
Gilles Peskinef45adda2019-01-14 18:29:18 +01001278/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001279 *
1280 * This function sets up the verification of the MAC
1281 * (message authentication code) of a byte string against an expected value.
1282 *
1283 * The sequence of operations to verify a MAC is as follows:
1284 * -# Allocate an operation object which will be passed to all the functions
1285 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001286 * -# Initialize the operation object with one of the methods described in the
1287 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001288 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001289 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1290 * of the message each time. The MAC that is calculated is the MAC
1291 * of the concatenation of these messages in order.
1292 * -# At the end of the message, call psa_mac_verify_finish() to finish
1293 * calculating the actual MAC of the message and verify it against
1294 * the expected value.
1295 *
1296 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001297 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001298 *
1299 * After a successful call to psa_mac_verify_setup(), the application must
1300 * eventually terminate the operation through one of the following methods:
1301 * - A failed call to psa_mac_update().
1302 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1303 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001304 * \param[in,out] operation The operation object to set up. It must have
1305 * been initialized as per the documentation for
1306 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001307 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001308 * It must remain valid until the operation
1309 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001310 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1311 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001312 *
Gilles Peskine28538492018-07-11 17:34:00 +02001313 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001314 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001315 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001316 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001317 * \retval #PSA_ERROR_NOT_PERMITTED
1318 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001319 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001320 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001321 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001322 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1323 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1324 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001325 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001326 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001327 * The operation state is not valid (already set up and not
1328 * subsequently completed).
1329 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001330 * The library has not been previously initialized by psa_crypto_init().
1331 * It is implementation-dependent whether a failure to initialize
1332 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001333 */
1334psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001335 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001336 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001337
Gilles Peskinedcd14942018-07-12 00:30:52 +02001338/** Add a message fragment to a multipart MAC operation.
1339 *
1340 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1341 * before calling this function.
1342 *
1343 * If this function returns an error status, the operation becomes inactive.
1344 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001345 * \param[in,out] operation Active MAC operation.
1346 * \param[in] input Buffer containing the message fragment to add to
1347 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001348 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001349 *
1350 * \retval #PSA_SUCCESS
1351 * Success.
1352 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001353 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001354 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1355 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1356 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001357 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001358 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001359psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1360 const uint8_t *input,
1361 size_t input_length);
1362
Gilles Peskinedcd14942018-07-12 00:30:52 +02001363/** Finish the calculation of the MAC of a message.
1364 *
1365 * The application must call psa_mac_sign_setup() before calling this function.
1366 * This function calculates the MAC of the message formed by concatenating
1367 * the inputs passed to preceding calls to psa_mac_update().
1368 *
1369 * When this function returns, the operation becomes inactive.
1370 *
1371 * \warning Applications should not call this function if they expect
1372 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1373 * Beware that comparing integrity or authenticity data such as
1374 * MAC values with a function such as \c memcmp is risky
1375 * because the time taken by the comparison may leak information
1376 * about the MAC value which could allow an attacker to guess
1377 * a valid MAC and thereby bypass security controls.
1378 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001379 * \param[in,out] operation Active MAC operation.
1380 * \param[out] mac Buffer where the MAC value is to be written.
1381 * \param mac_size Size of the \p mac buffer in bytes.
1382 * \param[out] mac_length On success, the number of bytes
1383 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001384 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001385 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001386 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001387 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001388 *
1389 * \retval #PSA_SUCCESS
1390 * Success.
1391 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001392 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001393 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001394 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001395 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1396 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1397 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1398 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001399 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001400 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001401psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1402 uint8_t *mac,
1403 size_t mac_size,
1404 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001405
Gilles Peskinedcd14942018-07-12 00:30:52 +02001406/** Finish the calculation of the MAC of a message and compare it with
1407 * an expected value.
1408 *
1409 * The application must call psa_mac_verify_setup() before calling this function.
1410 * This function calculates the MAC of the message formed by concatenating
1411 * the inputs passed to preceding calls to psa_mac_update(). It then
1412 * compares the calculated MAC with the expected MAC passed as a
1413 * parameter to this function.
1414 *
1415 * When this function returns, the operation becomes inactive.
1416 *
1417 * \note Implementations shall make the best effort to ensure that the
1418 * comparison between the actual MAC and the expected MAC is performed
1419 * in constant time.
1420 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001421 * \param[in,out] operation Active MAC operation.
1422 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001423 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001424 *
1425 * \retval #PSA_SUCCESS
1426 * The expected MAC is identical to the actual MAC of the message.
1427 * \retval #PSA_ERROR_INVALID_SIGNATURE
1428 * The MAC of the message was calculated successfully, but it
1429 * differs from the expected MAC.
1430 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001431 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001432 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1433 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1434 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001435 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001436 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001437psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1438 const uint8_t *mac,
1439 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001440
Gilles Peskinedcd14942018-07-12 00:30:52 +02001441/** Abort a MAC operation.
1442 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001443 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001444 * \p operation structure itself. Once aborted, the operation object
1445 * can be reused for another operation by calling
1446 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001447 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001448 * You may call this function any time after the operation object has
1449 * been initialized by any of the following methods:
1450 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1451 * it succeeds or not.
1452 * - Initializing the \c struct to all-bits-zero.
1453 * - Initializing the \c struct to logical zeros, e.g.
1454 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001455 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001456 * In particular, calling psa_mac_abort() after the operation has been
1457 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1458 * psa_mac_verify_finish() is safe and has no effect.
1459 *
1460 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001461 *
1462 * \retval #PSA_SUCCESS
1463 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001464 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001465 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1466 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001467 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001468 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001469psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1470
1471/**@}*/
1472
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001473/** \defgroup cipher Symmetric ciphers
1474 * @{
1475 */
1476
Gilles Peskine69647a42019-01-14 20:18:12 +01001477/** Encrypt a message using a symmetric cipher.
1478 *
1479 * This function encrypts a message with a random IV (initialization
1480 * vector).
1481 *
1482 * \param handle Handle to the key to use for the operation.
1483 * It must remain valid until the operation
1484 * terminates.
1485 * \param alg The cipher algorithm to compute
1486 * (\c PSA_ALG_XXX value such that
1487 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1488 * \param[in] input Buffer containing the message to encrypt.
1489 * \param input_length Size of the \p input buffer in bytes.
1490 * \param[out] output Buffer where the output is to be written.
1491 * The output contains the IV followed by
1492 * the ciphertext proper.
1493 * \param output_size Size of the \p output buffer in bytes.
1494 * \param[out] output_length On success, the number of bytes
1495 * that make up the output.
1496 *
1497 * \retval #PSA_SUCCESS
1498 * Success.
1499 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001500 * \retval #PSA_ERROR_NOT_PERMITTED
1501 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001502 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001503 * \retval #PSA_ERROR_NOT_SUPPORTED
1504 * \p alg is not supported or is not a cipher algorithm.
1505 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1506 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1507 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1508 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001509 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001510 */
1511psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1512 psa_algorithm_t alg,
1513 const uint8_t *input,
1514 size_t input_length,
1515 uint8_t *output,
1516 size_t output_size,
1517 size_t *output_length);
1518
1519/** Decrypt a message using a symmetric cipher.
1520 *
1521 * This function decrypts a message encrypted with a symmetric cipher.
1522 *
1523 * \param handle Handle to the key to use for the operation.
1524 * It must remain valid until the operation
1525 * terminates.
1526 * \param alg The cipher algorithm to compute
1527 * (\c PSA_ALG_XXX value such that
1528 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1529 * \param[in] input Buffer containing the message to decrypt.
1530 * This consists of the IV followed by the
1531 * ciphertext proper.
1532 * \param input_length Size of the \p input buffer in bytes.
1533 * \param[out] output Buffer where the plaintext is to be written.
1534 * \param output_size Size of the \p output buffer in bytes.
1535 * \param[out] output_length On success, the number of bytes
1536 * that make up the output.
1537 *
1538 * \retval #PSA_SUCCESS
1539 * Success.
1540 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001541 * \retval #PSA_ERROR_NOT_PERMITTED
1542 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001543 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001544 * \retval #PSA_ERROR_NOT_SUPPORTED
1545 * \p alg is not supported or is not a cipher algorithm.
1546 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1547 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1548 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1549 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001550 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine69647a42019-01-14 20:18:12 +01001551 */
1552psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1553 psa_algorithm_t alg,
1554 const uint8_t *input,
1555 size_t input_length,
1556 uint8_t *output,
1557 size_t output_size,
1558 size_t *output_length);
1559
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001560/** The type of the state data structure for multipart cipher operations.
1561 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001562 * Before calling any function on a cipher operation object, the application
1563 * must initialize it by any of the following means:
1564 * - Set the structure to all-bits-zero, for example:
1565 * \code
1566 * psa_cipher_operation_t operation;
1567 * memset(&operation, 0, sizeof(operation));
1568 * \endcode
1569 * - Initialize the structure to logical zero values, for example:
1570 * \code
1571 * psa_cipher_operation_t operation = {0};
1572 * \endcode
1573 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1574 * for example:
1575 * \code
1576 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1577 * \endcode
1578 * - Assign the result of the function psa_cipher_operation_init()
1579 * to the structure, for example:
1580 * \code
1581 * psa_cipher_operation_t operation;
1582 * operation = psa_cipher_operation_init();
1583 * \endcode
1584 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001585 * This is an implementation-defined \c struct. Applications should not
1586 * make any assumptions about the content of this structure except
1587 * as directed by the documentation of a specific implementation. */
1588typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1589
Jaeden Amero5bae2272019-01-04 11:48:27 +00001590/** \def PSA_CIPHER_OPERATION_INIT
1591 *
1592 * This macro returns a suitable initializer for a cipher operation object of
1593 * type #psa_cipher_operation_t.
1594 */
1595#ifdef __DOXYGEN_ONLY__
1596/* This is an example definition for documentation purposes.
1597 * Implementations should define a suitable value in `crypto_struct.h`.
1598 */
1599#define PSA_CIPHER_OPERATION_INIT {0}
1600#endif
1601
1602/** Return an initial value for a cipher operation object.
1603 */
1604static psa_cipher_operation_t psa_cipher_operation_init(void);
1605
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001606/** Set the key for a multipart symmetric encryption operation.
1607 *
1608 * The sequence of operations to encrypt a message with a symmetric cipher
1609 * is as follows:
1610 * -# Allocate an operation object which will be passed to all the functions
1611 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001612 * -# Initialize the operation object with one of the methods described in the
1613 * documentation for #psa_cipher_operation_t, e.g.
1614 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001615 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001616 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001617 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001618 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001619 * requires a specific IV value.
1620 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1621 * of the message each time.
1622 * -# Call psa_cipher_finish().
1623 *
1624 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001625 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001626 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001627 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001628 * eventually terminate the operation. The following events terminate an
1629 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001630 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001631 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001632 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001633 * \param[in,out] operation The operation object to set up. It must have
1634 * been initialized as per the documentation for
1635 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001636 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001637 * It must remain valid until the operation
1638 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001639 * \param alg The cipher algorithm to compute
1640 * (\c PSA_ALG_XXX value such that
1641 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001642 *
Gilles Peskine28538492018-07-11 17:34:00 +02001643 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001644 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001645 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001646 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001647 * \retval #PSA_ERROR_NOT_PERMITTED
1648 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001649 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001650 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001651 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001652 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1653 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1654 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001655 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001656 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001657 * The operation state is not valid (already set up and not
1658 * subsequently completed).
1659 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001660 * The library has not been previously initialized by psa_crypto_init().
1661 * It is implementation-dependent whether a failure to initialize
1662 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001663 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001664psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001665 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001666 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001667
1668/** Set the key for a multipart symmetric decryption operation.
1669 *
1670 * The sequence of operations to decrypt a message with a symmetric cipher
1671 * is as follows:
1672 * -# Allocate an operation object which will be passed to all the functions
1673 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001674 * -# Initialize the operation object with one of the methods described in the
1675 * documentation for #psa_cipher_operation_t, e.g.
1676 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001677 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001678 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001679 * decryption. If the IV is prepended to the ciphertext, you can call
1680 * psa_cipher_update() on a buffer containing the IV followed by the
1681 * beginning of the message.
1682 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1683 * of the message each time.
1684 * -# Call psa_cipher_finish().
1685 *
1686 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001687 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001688 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001689 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001690 * eventually terminate the operation. The following events terminate an
1691 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001692 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001693 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001694 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001695 * \param[in,out] operation The operation object to set up. It must have
1696 * been initialized as per the documentation for
1697 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001698 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001699 * It must remain valid until the operation
1700 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001701 * \param alg The cipher algorithm to compute
1702 * (\c PSA_ALG_XXX value such that
1703 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001704 *
Gilles Peskine28538492018-07-11 17:34:00 +02001705 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001706 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001707 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001708 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001709 * \retval #PSA_ERROR_NOT_PERMITTED
1710 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001711 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001712 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001713 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001714 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1715 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1716 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001717 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001718 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001719 * The operation state is not valid (already set up and not
1720 * subsequently completed).
1721 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001722 * The library has not been previously initialized by psa_crypto_init().
1723 * It is implementation-dependent whether a failure to initialize
1724 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001725 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001726psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001727 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001728 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001729
Gilles Peskinedcd14942018-07-12 00:30:52 +02001730/** Generate an IV for a symmetric encryption operation.
1731 *
1732 * This function generates a random IV (initialization vector), nonce
1733 * or initial counter value for the encryption operation as appropriate
1734 * for the chosen algorithm, key type and key size.
1735 *
1736 * The application must call psa_cipher_encrypt_setup() before
1737 * calling this function.
1738 *
1739 * If this function returns an error status, the operation becomes inactive.
1740 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001741 * \param[in,out] operation Active cipher operation.
1742 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001743 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001744 * \param[out] iv_length On success, the number of bytes of the
1745 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001746 *
1747 * \retval #PSA_SUCCESS
1748 * Success.
1749 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001750 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001751 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001752 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001753 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1754 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1755 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001756 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001757 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001758psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001759 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001760 size_t iv_size,
1761 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001762
Gilles Peskinedcd14942018-07-12 00:30:52 +02001763/** Set the IV for a symmetric encryption or decryption operation.
1764 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001765 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001766 * or initial counter value for the encryption or decryption operation.
1767 *
1768 * The application must call psa_cipher_encrypt_setup() before
1769 * calling this function.
1770 *
1771 * If this function returns an error status, the operation becomes inactive.
1772 *
1773 * \note When encrypting, applications should use psa_cipher_generate_iv()
1774 * instead of this function, unless implementing a protocol that requires
1775 * a non-random IV.
1776 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001777 * \param[in,out] operation Active cipher operation.
1778 * \param[in] iv Buffer containing the IV to use.
1779 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001780 *
1781 * \retval #PSA_SUCCESS
1782 * Success.
1783 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001784 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001785 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001786 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001787 * or the chosen algorithm does not use an IV.
1788 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1789 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1790 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001791 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001792 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001793psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001794 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001795 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001796
Gilles Peskinedcd14942018-07-12 00:30:52 +02001797/** Encrypt or decrypt a message fragment in an active cipher operation.
1798 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001799 * Before calling this function, you must:
1800 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1801 * The choice of setup function determines whether this function
1802 * encrypts or decrypts its input.
1803 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1804 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001805 *
1806 * If this function returns an error status, the operation becomes inactive.
1807 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001808 * \param[in,out] operation Active cipher operation.
1809 * \param[in] input Buffer containing the message fragment to
1810 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001811 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001812 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001813 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001814 * \param[out] output_length On success, the number of bytes
1815 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001816 *
1817 * \retval #PSA_SUCCESS
1818 * Success.
1819 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001820 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001821 * not set, or already completed).
1822 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1823 * The size of the \p output buffer is too small.
1824 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1825 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1826 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001827 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001828 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001829psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1830 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001831 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001832 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001833 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001834 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001835
Gilles Peskinedcd14942018-07-12 00:30:52 +02001836/** Finish encrypting or decrypting a message in a cipher operation.
1837 *
1838 * The application must call psa_cipher_encrypt_setup() or
1839 * psa_cipher_decrypt_setup() before calling this function. The choice
1840 * of setup function determines whether this function encrypts or
1841 * decrypts its input.
1842 *
1843 * This function finishes the encryption or decryption of the message
1844 * formed by concatenating the inputs passed to preceding calls to
1845 * psa_cipher_update().
1846 *
1847 * When this function returns, the operation becomes inactive.
1848 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001849 * \param[in,out] operation Active cipher operation.
1850 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001851 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001852 * \param[out] output_length On success, the number of bytes
1853 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001854 *
1855 * \retval #PSA_SUCCESS
1856 * Success.
1857 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001858 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001859 * not set, or already completed).
1860 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1861 * The size of the \p output buffer is too small.
1862 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1863 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1864 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001865 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001866 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001867psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001868 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001869 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001870 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001871
Gilles Peskinedcd14942018-07-12 00:30:52 +02001872/** Abort a cipher operation.
1873 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001874 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001875 * \p operation structure itself. Once aborted, the operation object
1876 * can be reused for another operation by calling
1877 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001878 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001879 * You may call this function any time after the operation object has
1880 * been initialized by any of the following methods:
1881 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1882 * whether it succeeds or not.
1883 * - Initializing the \c struct to all-bits-zero.
1884 * - Initializing the \c struct to logical zeros, e.g.
1885 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001886 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001887 * In particular, calling psa_cipher_abort() after the operation has been
1888 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1889 * is safe and has no effect.
1890 *
1891 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001892 *
1893 * \retval #PSA_SUCCESS
1894 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001895 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001896 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1897 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001898 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinedcd14942018-07-12 00:30:52 +02001899 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001900psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1901
1902/**@}*/
1903
Gilles Peskine3b555712018-03-03 21:27:57 +01001904/** \defgroup aead Authenticated encryption with associated data (AEAD)
1905 * @{
1906 */
1907
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001908/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001909 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001910 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001911 * \param alg The AEAD algorithm to compute
1912 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001913 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001914 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001915 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001916 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001917 * but not encrypted.
1918 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001919 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001920 * encrypted.
1921 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001922 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001923 * encrypted data. The additional data is not
1924 * part of this output. For algorithms where the
1925 * encrypted data and the authentication tag
1926 * are defined as separate outputs, the
1927 * authentication tag is appended to the
1928 * encrypted data.
1929 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1930 * This must be at least
1931 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1932 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001933 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001934 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001935 *
Gilles Peskine28538492018-07-11 17:34:00 +02001936 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001937 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001938 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001939 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001940 * \retval #PSA_ERROR_NOT_PERMITTED
1941 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001942 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001943 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001944 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001945 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1946 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1947 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001948 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001949 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001950 * The library has not been previously initialized by psa_crypto_init().
1951 * It is implementation-dependent whether a failure to initialize
1952 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001953 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001954psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001955 psa_algorithm_t alg,
1956 const uint8_t *nonce,
1957 size_t nonce_length,
1958 const uint8_t *additional_data,
1959 size_t additional_data_length,
1960 const uint8_t *plaintext,
1961 size_t plaintext_length,
1962 uint8_t *ciphertext,
1963 size_t ciphertext_size,
1964 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001965
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001966/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001967 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001968 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001969 * \param alg The AEAD algorithm to compute
1970 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001971 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001972 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001973 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001974 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001975 * but not encrypted.
1976 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001977 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001978 * encrypted. For algorithms where the
1979 * encrypted data and the authentication tag
1980 * are defined as separate inputs, the buffer
1981 * must contain the encrypted data followed
1982 * by the authentication tag.
1983 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001984 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001985 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1986 * This must be at least
1987 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1988 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001989 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001990 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001991 *
Gilles Peskine28538492018-07-11 17:34:00 +02001992 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001993 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001994 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001995 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001996 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001997 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001998 * \retval #PSA_ERROR_NOT_PERMITTED
1999 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002000 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002001 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002002 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002003 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2004 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2005 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002006 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002007 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002008 * The library has not been previously initialized by psa_crypto_init().
2009 * It is implementation-dependent whether a failure to initialize
2010 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002011 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002012psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002013 psa_algorithm_t alg,
2014 const uint8_t *nonce,
2015 size_t nonce_length,
2016 const uint8_t *additional_data,
2017 size_t additional_data_length,
2018 const uint8_t *ciphertext,
2019 size_t ciphertext_length,
2020 uint8_t *plaintext,
2021 size_t plaintext_size,
2022 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002023
Gilles Peskine30a9e412019-01-14 18:36:12 +01002024/** The type of the state data structure for multipart AEAD operations.
2025 *
2026 * Before calling any function on an AEAD operation object, the application
2027 * must initialize it by any of the following means:
2028 * - Set the structure to all-bits-zero, for example:
2029 * \code
2030 * psa_aead_operation_t operation;
2031 * memset(&operation, 0, sizeof(operation));
2032 * \endcode
2033 * - Initialize the structure to logical zero values, for example:
2034 * \code
2035 * psa_aead_operation_t operation = {0};
2036 * \endcode
2037 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2038 * for example:
2039 * \code
2040 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2041 * \endcode
2042 * - Assign the result of the function psa_aead_operation_init()
2043 * to the structure, for example:
2044 * \code
2045 * psa_aead_operation_t operation;
2046 * operation = psa_aead_operation_init();
2047 * \endcode
2048 *
2049 * This is an implementation-defined \c struct. Applications should not
2050 * make any assumptions about the content of this structure except
2051 * as directed by the documentation of a specific implementation. */
2052typedef struct psa_aead_operation_s psa_aead_operation_t;
2053
2054/** \def PSA_AEAD_OPERATION_INIT
2055 *
2056 * This macro returns a suitable initializer for an AEAD operation object of
2057 * type #psa_aead_operation_t.
2058 */
2059#ifdef __DOXYGEN_ONLY__
2060/* This is an example definition for documentation purposes.
2061 * Implementations should define a suitable value in `crypto_struct.h`.
2062 */
2063#define PSA_AEAD_OPERATION_INIT {0}
2064#endif
2065
2066/** Return an initial value for an AEAD operation object.
2067 */
2068static psa_aead_operation_t psa_aead_operation_init(void);
2069
2070/** Set the key for a multipart authenticated encryption operation.
2071 *
2072 * The sequence of operations to encrypt a message with authentication
2073 * is as follows:
2074 * -# Allocate an operation object which will be passed to all the functions
2075 * listed here.
2076 * -# Initialize the operation object with one of the methods described in the
2077 * documentation for #psa_aead_operation_t, e.g.
2078 * PSA_AEAD_OPERATION_INIT.
2079 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002080 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2081 * inputs to the subsequent calls to psa_aead_update_ad() and
2082 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2083 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002084 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2085 * generate or set the nonce. You should use
2086 * psa_aead_generate_nonce() unless the protocol you are implementing
2087 * requires a specific nonce value.
2088 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2089 * of the non-encrypted additional authenticated data each time.
2090 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002091 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002092 * -# Call psa_aead_finish().
2093 *
2094 * The application may call psa_aead_abort() at any time after the operation
2095 * has been initialized.
2096 *
2097 * After a successful call to psa_aead_encrypt_setup(), the application must
2098 * eventually terminate the operation. The following events terminate an
2099 * operation:
2100 * - A failed call to any of the \c psa_aead_xxx functions.
2101 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2102 *
2103 * \param[in,out] operation The operation object to set up. It must have
2104 * been initialized as per the documentation for
2105 * #psa_aead_operation_t and not yet in use.
2106 * \param handle Handle to the key to use for the operation.
2107 * It must remain valid until the operation
2108 * terminates.
2109 * \param alg The AEAD algorithm to compute
2110 * (\c PSA_ALG_XXX value such that
2111 * #PSA_ALG_IS_AEAD(\p alg) is true).
2112 *
2113 * \retval #PSA_SUCCESS
2114 * Success.
2115 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002116 * \retval #PSA_ERROR_NOT_PERMITTED
2117 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002118 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002119 * \retval #PSA_ERROR_NOT_SUPPORTED
2120 * \p alg is not supported or is not an AEAD algorithm.
2121 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2122 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2123 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002124 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002125 * \retval #PSA_ERROR_BAD_STATE
2126 * The library has not been previously initialized by psa_crypto_init().
2127 * It is implementation-dependent whether a failure to initialize
2128 * results in this error code.
2129 */
2130psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2131 psa_key_handle_t handle,
2132 psa_algorithm_t alg);
2133
2134/** Set the key for a multipart authenticated decryption operation.
2135 *
2136 * The sequence of operations to decrypt a message with authentication
2137 * is as follows:
2138 * -# Allocate an operation object which will be passed to all the functions
2139 * listed here.
2140 * -# Initialize the operation object with one of the methods described in the
2141 * documentation for #psa_aead_operation_t, e.g.
2142 * PSA_AEAD_OPERATION_INIT.
2143 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002144 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2145 * inputs to the subsequent calls to psa_aead_update_ad() and
2146 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2147 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002148 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2149 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2150 * of the non-encrypted additional authenticated data each time.
2151 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002152 * of the ciphertext to decrypt each time.
2153 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002154 *
2155 * The application may call psa_aead_abort() at any time after the operation
2156 * has been initialized.
2157 *
2158 * After a successful call to psa_aead_decrypt_setup(), the application must
2159 * eventually terminate the operation. The following events terminate an
2160 * operation:
2161 * - A failed call to any of the \c psa_aead_xxx functions.
2162 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2163 *
2164 * \param[in,out] operation The operation object to set up. It must have
2165 * been initialized as per the documentation for
2166 * #psa_aead_operation_t and not yet in use.
2167 * \param handle Handle to the key to use for the operation.
2168 * It must remain valid until the operation
2169 * terminates.
2170 * \param alg The AEAD algorithm to compute
2171 * (\c PSA_ALG_XXX value such that
2172 * #PSA_ALG_IS_AEAD(\p alg) is true).
2173 *
2174 * \retval #PSA_SUCCESS
2175 * Success.
2176 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002177 * \retval #PSA_ERROR_NOT_PERMITTED
2178 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002179 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002180 * \retval #PSA_ERROR_NOT_SUPPORTED
2181 * \p alg is not supported or is not an AEAD algorithm.
2182 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2183 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2184 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002185 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002186 * \retval #PSA_ERROR_BAD_STATE
2187 * The library has not been previously initialized by psa_crypto_init().
2188 * It is implementation-dependent whether a failure to initialize
2189 * results in this error code.
2190 */
2191psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2192 psa_key_handle_t handle,
2193 psa_algorithm_t alg);
2194
2195/** Generate a random nonce for an authenticated encryption operation.
2196 *
2197 * This function generates a random nonce for the authenticated encryption
2198 * operation with an appropriate size for the chosen algorithm, key type
2199 * and key size.
2200 *
2201 * The application must call psa_aead_encrypt_setup() before
2202 * calling this function.
2203 *
2204 * If this function returns an error status, the operation becomes inactive.
2205 *
2206 * \param[in,out] operation Active AEAD operation.
2207 * \param[out] nonce Buffer where the generated nonce is to be
2208 * written.
2209 * \param nonce_size Size of the \p nonce buffer in bytes.
2210 * \param[out] nonce_length On success, the number of bytes of the
2211 * generated nonce.
2212 *
2213 * \retval #PSA_SUCCESS
2214 * Success.
2215 * \retval #PSA_ERROR_BAD_STATE
2216 * The operation state is not valid (not set up, or nonce already set).
2217 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2218 * The size of the \p nonce buffer is too small.
2219 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2220 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2221 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002222 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002223 */
2224psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002225 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002226 size_t nonce_size,
2227 size_t *nonce_length);
2228
2229/** Set the nonce for an authenticated encryption or decryption operation.
2230 *
2231 * This function sets the nonce for the authenticated
2232 * encryption or decryption operation.
2233 *
2234 * The application must call psa_aead_encrypt_setup() before
2235 * calling this function.
2236 *
2237 * If this function returns an error status, the operation becomes inactive.
2238 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002239 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002240 * instead of this function, unless implementing a protocol that requires
2241 * a non-random IV.
2242 *
2243 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002244 * \param[in] nonce Buffer containing the nonce to use.
2245 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002246 *
2247 * \retval #PSA_SUCCESS
2248 * Success.
2249 * \retval #PSA_ERROR_BAD_STATE
2250 * The operation state is not valid (not set up, or nonce already set).
2251 * \retval #PSA_ERROR_INVALID_ARGUMENT
2252 * The size of \p nonce is not acceptable for the chosen algorithm.
2253 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2254 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2255 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002256 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002257 */
2258psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002259 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002260 size_t nonce_length);
2261
Gilles Peskinebc59c852019-01-17 15:26:08 +01002262/** Declare the lengths of the message and additional data for AEAD.
2263 *
2264 * The application must call this function before calling
2265 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2266 * the operation requires it. If the algorithm does not require it,
2267 * calling this function is optional, but if this function is called
2268 * then the implementation must enforce the lengths.
2269 *
2270 * You may call this function before or after setting the nonce with
2271 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2272 *
2273 * - For #PSA_ALG_CCM, calling this function is required.
2274 * - For the other AEAD algorithms defined in this specification, calling
2275 * this function is not required.
2276 * - For vendor-defined algorithm, refer to the vendor documentation.
2277 *
2278 * \param[in,out] operation Active AEAD operation.
2279 * \param ad_length Size of the non-encrypted additional
2280 * authenticated data in bytes.
2281 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2282 *
2283 * \retval #PSA_SUCCESS
2284 * Success.
2285 * \retval #PSA_ERROR_BAD_STATE
2286 * The operation state is not valid (not set up, already completed,
2287 * or psa_aead_update_ad() or psa_aead_update() already called).
2288 * \retval #PSA_ERROR_INVALID_ARGUMENT
2289 * At least one of the lengths is not acceptable for the chosen
2290 * algorithm.
2291 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2292 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2293 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002294 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskinebc59c852019-01-17 15:26:08 +01002295 */
2296psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2297 size_t ad_length,
2298 size_t plaintext_length);
2299
Gilles Peskine30a9e412019-01-14 18:36:12 +01002300/** Pass additional data to an active AEAD operation.
2301 *
2302 * Additional data is authenticated, but not encrypted.
2303 *
2304 * You may call this function multiple times to pass successive fragments
2305 * of the additional data. You may not call this function after passing
2306 * data to encrypt or decrypt with psa_aead_update().
2307 *
2308 * Before calling this function, you must:
2309 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2310 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2311 *
2312 * If this function returns an error status, the operation becomes inactive.
2313 *
2314 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2315 * there is no guarantee that the input is valid. Therefore, until
2316 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2317 * treat the input as untrusted and prepare to undo any action that
2318 * depends on the input if psa_aead_verify() returns an error status.
2319 *
2320 * \param[in,out] operation Active AEAD operation.
2321 * \param[in] input Buffer containing the fragment of
2322 * additional data.
2323 * \param input_length Size of the \p input buffer in bytes.
2324 *
2325 * \retval #PSA_SUCCESS
2326 * Success.
2327 * \retval #PSA_ERROR_BAD_STATE
2328 * The operation state is not valid (not set up, nonce not set,
2329 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002330 * \retval #PSA_ERROR_INVALID_ARGUMENT
2331 * The total input length overflows the additional data length that
2332 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002333 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2334 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2335 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002336 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002337 */
2338psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2339 const uint8_t *input,
2340 size_t input_length);
2341
2342/** Encrypt or decrypt a message fragment in an active AEAD operation.
2343 *
2344 * Before calling this function, you must:
2345 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2346 * The choice of setup function determines whether this function
2347 * encrypts or decrypts its input.
2348 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2349 * 3. Call psa_aead_update_ad() to pass all the additional data.
2350 *
2351 * If this function returns an error status, the operation becomes inactive.
2352 *
2353 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2354 * there is no guarantee that the input is valid. Therefore, until
2355 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2356 * - Do not use the output in any way other than storing it in a
2357 * confidential location. If you take any action that depends
2358 * on the tentative decrypted data, this action will need to be
2359 * undone if the input turns out not to be valid. Furthermore,
2360 * if an adversary can observe that this action took place
2361 * (for example through timing), they may be able to use this
2362 * fact as an oracle to decrypt any message encrypted with the
2363 * same key.
2364 * - In particular, do not copy the output anywhere but to a
2365 * memory or storage space that you have exclusive access to.
2366 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002367 * This function does not require the input to be aligned to any
2368 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002369 * a whole block at a time, it must consume all the input provided, but
2370 * it may delay the end of the corresponding output until a subsequent
2371 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2372 * provides sufficient input. The amount of data that can be delayed
2373 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002374 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002375 * \param[in,out] operation Active AEAD operation.
2376 * \param[in] input Buffer containing the message fragment to
2377 * encrypt or decrypt.
2378 * \param input_length Size of the \p input buffer in bytes.
2379 * \param[out] output Buffer where the output is to be written.
2380 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002381 * This must be at least
2382 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2383 * \p input_length) where \c alg is the
2384 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002385 * \param[out] output_length On success, the number of bytes
2386 * that make up the returned output.
2387 *
2388 * \retval #PSA_SUCCESS
2389 * Success.
2390 * \retval #PSA_ERROR_BAD_STATE
2391 * The operation state is not valid (not set up, nonce not set
2392 * or already completed).
2393 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2394 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002395 * You can determine a sufficient buffer size by calling
2396 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2397 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002398 * \retval #PSA_ERROR_INVALID_ARGUMENT
2399 * The total length of input to psa_aead_update_ad() so far is
2400 * less than the additional data length that was previously
2401 * specified with psa_aead_set_lengths().
2402 * \retval #PSA_ERROR_INVALID_ARGUMENT
2403 * The total input length overflows the plaintext length that
2404 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002405 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2406 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2407 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002408 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002409 */
2410psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2411 const uint8_t *input,
2412 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002413 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002414 size_t output_size,
2415 size_t *output_length);
2416
2417/** Finish encrypting a message in an AEAD operation.
2418 *
2419 * The operation must have been set up with psa_aead_encrypt_setup().
2420 *
2421 * This function finishes the authentication of the additional data
2422 * formed by concatenating the inputs passed to preceding calls to
2423 * psa_aead_update_ad() with the plaintext formed by concatenating the
2424 * inputs passed to preceding calls to psa_aead_update().
2425 *
2426 * This function has two output buffers:
2427 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002428 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002429 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002430 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002431 * that the operation performs.
2432 *
2433 * When this function returns, the operation becomes inactive.
2434 *
2435 * \param[in,out] operation Active AEAD operation.
2436 * \param[out] ciphertext Buffer where the last part of the ciphertext
2437 * is to be written.
2438 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002439 * This must be at least
2440 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2441 * \c alg is the algorithm that is being
2442 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002443 * \param[out] ciphertext_length On success, the number of bytes of
2444 * returned ciphertext.
2445 * \param[out] tag Buffer where the authentication tag is
2446 * to be written.
2447 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002448 * This must be at least
2449 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2450 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002451 * \param[out] tag_length On success, the number of bytes
2452 * that make up the returned tag.
2453 *
2454 * \retval #PSA_SUCCESS
2455 * Success.
2456 * \retval #PSA_ERROR_BAD_STATE
2457 * The operation state is not valid (not set up, nonce not set,
2458 * decryption, or already completed).
2459 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002460 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002461 * You can determine a sufficient buffer size for \p ciphertext by
2462 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2463 * where \c alg is the algorithm that is being calculated.
2464 * You can determine a sufficient buffer size for \p tag by
2465 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002466 * \retval #PSA_ERROR_INVALID_ARGUMENT
2467 * The total length of input to psa_aead_update_ad() so far is
2468 * less than the additional data length that was previously
2469 * specified with psa_aead_set_lengths().
2470 * \retval #PSA_ERROR_INVALID_ARGUMENT
2471 * The total length of input to psa_aead_update() so far is
2472 * less than the plaintext length that was previously
2473 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002474 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2475 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2476 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002477 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002478 */
2479psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002480 uint8_t *ciphertext,
2481 size_t ciphertext_size,
2482 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002483 uint8_t *tag,
2484 size_t tag_size,
2485 size_t *tag_length);
2486
2487/** Finish authenticating and decrypting a message in an AEAD operation.
2488 *
2489 * The operation must have been set up with psa_aead_decrypt_setup().
2490 *
2491 * This function finishes the authentication of the additional data
2492 * formed by concatenating the inputs passed to preceding calls to
2493 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2494 * inputs passed to preceding calls to psa_aead_update().
2495 *
2496 * When this function returns, the operation becomes inactive.
2497 *
2498 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002499 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002500 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002501 * from previous calls to psa_aead_update()
2502 * that could not be processed until the end
2503 * of the input.
2504 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002505 * This must be at least
2506 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2507 * \c alg is the algorithm that is being
2508 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002509 * \param[out] plaintext_length On success, the number of bytes of
2510 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002511 * \param[in] tag Buffer containing the authentication tag.
2512 * \param tag_length Size of the \p tag buffer in bytes.
2513 *
2514 * \retval #PSA_SUCCESS
2515 * Success.
2516 * \retval #PSA_ERROR_BAD_STATE
2517 * The operation state is not valid (not set up, nonce not set,
2518 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002519 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2520 * The size of the \p plaintext buffer is too small.
2521 * You can determine a sufficient buffer size for \p plaintext by
2522 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2523 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002524 * \retval #PSA_ERROR_INVALID_ARGUMENT
2525 * The total length of input to psa_aead_update_ad() so far is
2526 * less than the additional data length that was previously
2527 * specified with psa_aead_set_lengths().
2528 * \retval #PSA_ERROR_INVALID_ARGUMENT
2529 * The total length of input to psa_aead_update() so far is
2530 * less than the plaintext length that was previously
2531 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002532 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2533 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2534 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002535 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002536 */
2537psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002538 uint8_t *plaintext,
2539 size_t plaintext_size,
2540 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002541 const uint8_t *tag,
2542 size_t tag_length);
2543
2544/** Abort an AEAD operation.
2545 *
2546 * Aborting an operation frees all associated resources except for the
2547 * \p operation structure itself. Once aborted, the operation object
2548 * can be reused for another operation by calling
2549 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2550 *
2551 * You may call this function any time after the operation object has
2552 * been initialized by any of the following methods:
2553 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2554 * whether it succeeds or not.
2555 * - Initializing the \c struct to all-bits-zero.
2556 * - Initializing the \c struct to logical zeros, e.g.
2557 * `psa_aead_operation_t operation = {0}`.
2558 *
2559 * In particular, calling psa_aead_abort() after the operation has been
2560 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2561 * is safe and has no effect.
2562 *
2563 * \param[in,out] operation Initialized AEAD operation.
2564 *
2565 * \retval #PSA_SUCCESS
2566 * \retval #PSA_ERROR_BAD_STATE
2567 * \p operation is not an active AEAD operation.
2568 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2569 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002570 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002571 */
2572psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2573
Gilles Peskine3b555712018-03-03 21:27:57 +01002574/**@}*/
2575
Gilles Peskine20035e32018-02-03 22:44:14 +01002576/** \defgroup asymmetric Asymmetric cryptography
2577 * @{
2578 */
2579
2580/**
2581 * \brief Sign a hash or short message with a private key.
2582 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002583 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002584 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002585 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2586 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2587 * to determine the hash algorithm to use.
2588 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002589 * \param handle Handle to the key to use for the operation.
2590 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002591 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002592 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002593 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002594 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002595 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002596 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002597 * \param[out] signature_length On success, the number of bytes
2598 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002599 *
Gilles Peskine28538492018-07-11 17:34:00 +02002600 * \retval #PSA_SUCCESS
2601 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002602 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002603 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002604 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002605 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002606 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002607 * \retval #PSA_ERROR_NOT_SUPPORTED
2608 * \retval #PSA_ERROR_INVALID_ARGUMENT
2609 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2610 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2611 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002612 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002613 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002614 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002615 * The library has not been previously initialized by psa_crypto_init().
2616 * It is implementation-dependent whether a failure to initialize
2617 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002618 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002619psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002620 psa_algorithm_t alg,
2621 const uint8_t *hash,
2622 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002623 uint8_t *signature,
2624 size_t signature_size,
2625 size_t *signature_length);
2626
2627/**
2628 * \brief Verify the signature a hash or short message using a public key.
2629 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002630 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002631 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002632 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2633 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2634 * to determine the hash algorithm to use.
2635 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002636 * \param handle Handle to the key to use for the operation.
2637 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002638 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002639 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002640 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002641 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002642 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002643 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002644 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002645 *
Gilles Peskine28538492018-07-11 17:34:00 +02002646 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002647 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002648 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002649 * The calculation was perfomed successfully, but the passed
2650 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002651 * \retval #PSA_ERROR_NOT_SUPPORTED
2652 * \retval #PSA_ERROR_INVALID_ARGUMENT
2653 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2654 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2655 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002656 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002657 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002658 * The library has not been previously initialized by psa_crypto_init().
2659 * It is implementation-dependent whether a failure to initialize
2660 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002661 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002662psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002663 psa_algorithm_t alg,
2664 const uint8_t *hash,
2665 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002666 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002667 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002668
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002669/**
2670 * \brief Encrypt a short message with a public key.
2671 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002672 * \param handle Handle to the key to use for the operation.
2673 * It must be a public key or an asymmetric
2674 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002675 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002676 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002677 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002678 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002679 * \param[in] salt A salt or label, if supported by the
2680 * encryption algorithm.
2681 * If the algorithm does not support a
2682 * salt, pass \c NULL.
2683 * If the algorithm supports an optional
2684 * salt and you do not want to pass a salt,
2685 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002686 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002687 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2688 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002689 * \param salt_length Size of the \p salt buffer in bytes.
2690 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002691 * \param[out] output Buffer where the encrypted message is to
2692 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002693 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002694 * \param[out] output_length On success, the number of bytes
2695 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002696 *
Gilles Peskine28538492018-07-11 17:34:00 +02002697 * \retval #PSA_SUCCESS
2698 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002699 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002700 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002701 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002702 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002703 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002704 * \retval #PSA_ERROR_NOT_SUPPORTED
2705 * \retval #PSA_ERROR_INVALID_ARGUMENT
2706 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2707 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2708 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002709 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002710 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002711 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002712 * The library has not been previously initialized by psa_crypto_init().
2713 * It is implementation-dependent whether a failure to initialize
2714 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002715 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002716psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002717 psa_algorithm_t alg,
2718 const uint8_t *input,
2719 size_t input_length,
2720 const uint8_t *salt,
2721 size_t salt_length,
2722 uint8_t *output,
2723 size_t output_size,
2724 size_t *output_length);
2725
2726/**
2727 * \brief Decrypt a short message with a private key.
2728 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002729 * \param handle Handle to the key to use for the operation.
2730 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002731 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002732 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002733 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002734 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002735 * \param[in] salt A salt or label, if supported by the
2736 * encryption algorithm.
2737 * If the algorithm does not support a
2738 * salt, pass \c NULL.
2739 * If the algorithm supports an optional
2740 * salt and you do not want to pass a salt,
2741 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002742 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002743 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2744 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002745 * \param salt_length Size of the \p salt buffer in bytes.
2746 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002747 * \param[out] output Buffer where the decrypted message is to
2748 * be written.
2749 * \param output_size Size of the \c output buffer in bytes.
2750 * \param[out] output_length On success, the number of bytes
2751 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002752 *
Gilles Peskine28538492018-07-11 17:34:00 +02002753 * \retval #PSA_SUCCESS
2754 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002755 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002756 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002757 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002758 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002759 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002760 * \retval #PSA_ERROR_NOT_SUPPORTED
2761 * \retval #PSA_ERROR_INVALID_ARGUMENT
2762 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2763 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2764 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002765 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +02002766 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2767 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002768 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002769 * The library has not been previously initialized by psa_crypto_init().
2770 * It is implementation-dependent whether a failure to initialize
2771 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002772 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002773psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002774 psa_algorithm_t alg,
2775 const uint8_t *input,
2776 size_t input_length,
2777 const uint8_t *salt,
2778 size_t salt_length,
2779 uint8_t *output,
2780 size_t output_size,
2781 size_t *output_length);
2782
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002783/**@}*/
2784
Gilles Peskine35675b62019-05-16 17:26:11 +02002785/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002786 * @{
2787 */
2788
Gilles Peskine35675b62019-05-16 17:26:11 +02002789/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002790 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002791 * Before calling any function on a key derivation operation object, the
2792 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002793 * - Set the structure to all-bits-zero, for example:
2794 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002795 * psa_key_derivation_operation_t operation;
2796 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002797 * \endcode
2798 * - Initialize the structure to logical zero values, for example:
2799 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002800 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002801 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002802 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002803 * for example:
2804 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002805 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002806 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002807 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002808 * to the structure, for example:
2809 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002810 * psa_key_derivation_operation_t operation;
2811 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002812 * \endcode
2813 *
2814 * This is an implementation-defined \c struct. Applications should not
2815 * make any assumptions about the content of this structure except
2816 * as directed by the documentation of a specific implementation.
2817 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002818typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002819
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002820/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002821 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002822 * This macro returns a suitable initializer for a key derivation operation
2823 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002824 */
2825#ifdef __DOXYGEN_ONLY__
2826/* This is an example definition for documentation purposes.
2827 * Implementations should define a suitable value in `crypto_struct.h`.
2828 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002829#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02002830#endif
2831
Gilles Peskine35675b62019-05-16 17:26:11 +02002832/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002833 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002834static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002835
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002836/** Set up a key derivation operation.
2837 *
2838 * A key derivation algorithm takes some inputs and uses them to generate
2839 * a byte stream in a deterministic way.
2840 * This byte stream can be used to produce keys and other
2841 * cryptographic material.
2842 *
2843 * To derive a key:
2844 * - Start with an initialized object of type #psa_key_derivation_operation_t.
2845 * - Call psa_key_derivation_setup() to select the algorithm.
2846 * - Provide the inputs for the key derivation by calling
2847 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2848 * as appropriate. Which inputs are needed, in what order, and whether
2849 * they may be keys and if so of what type depends on the algorithm.
2850 * - Optionally set the operation's maximum capacity with
2851 * psa_key_derivation_set_capacity(). You may do this before, in the middle
2852 * of or after providing inputs. For some algorithms, this step is mandatory
2853 * because the output depends on the maximum capacity.
2854 * - To derive a key, call psa_key_derivation_output_key().
2855 * To derive a byte string for a different purpose, call
2856 * - psa_key_derivation_output_bytes().
2857 * Successive calls to these functions use successive output bytes
2858 * calculated by the key derivation algorithm.
2859 * - Clean up the key derivation operation object with
2860 * psa_key_derivation_abort().
2861 *
2862 * \param[in,out] operation The key derivation operation object
2863 * to set up. It must
2864 * have been initialized but not set up yet.
2865 * \param alg The key derivation algorithm to compute
2866 * (\c PSA_ALG_XXX value such that
2867 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2868 *
2869 * \retval #PSA_SUCCESS
2870 * Success.
2871 * \retval #PSA_ERROR_INVALID_ARGUMENT
2872 * \c alg is not a key derivation algorithm.
2873 * \retval #PSA_ERROR_NOT_SUPPORTED
2874 * \c alg is not supported or is not a key derivation algorithm.
2875 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2876 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2877 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002878 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002879 * \retval #PSA_ERROR_BAD_STATE
2880 */
2881psa_status_t psa_key_derivation_setup(
2882 psa_key_derivation_operation_t *operation,
2883 psa_algorithm_t alg);
2884
Gilles Peskine35675b62019-05-16 17:26:11 +02002885/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002886 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002887 * The capacity of a key derivation is the maximum number of bytes that it can
2888 * return. When you get *N* bytes of output from a key derivation operation,
2889 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002890 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002891 * \param[in] operation The operation to query.
2892 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002893 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002894 * \retval #PSA_SUCCESS
2895 * \retval #PSA_ERROR_BAD_STATE
2896 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002897 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002898psa_status_t psa_key_derivation_get_capacity(
2899 const psa_key_derivation_operation_t *operation,
2900 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002901
Gilles Peskine35675b62019-05-16 17:26:11 +02002902/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002903 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002904 * The capacity of a key derivation operation is the maximum number of bytes
2905 * that the key derivation operation can return from this point onwards.
2906 *
2907 * \param[in,out] operation The key derivation operation object to modify.
2908 * \param capacity The new capacity of the operation.
2909 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002910 * current capacity.
2911 *
2912 * \retval #PSA_SUCCESS
2913 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02002914 * \p capacity is larger than the operation's current capacity.
2915 * In this case, the operation object remains valid and its capacity
2916 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002917 * \retval #PSA_ERROR_BAD_STATE
2918 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2919 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002920psa_status_t psa_key_derivation_set_capacity(
2921 psa_key_derivation_operation_t *operation,
2922 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002923
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002924/** Use the maximum possible capacity for a key derivation operation.
2925 *
2926 * Use this value as the capacity argument when setting up a key derivation
2927 * to indicate that the operation should have the maximum possible capacity.
2928 * The value of the maximum possible capacity depends on the key derivation
2929 * algorithm.
2930 */
2931#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
2932
2933/** Provide an input for key derivation or key agreement.
2934 *
2935 * Which inputs are required and in what order depends on the algorithm.
2936 * Refer to the documentation of each key derivation or key agreement
2937 * algorithm for information.
2938 *
2939 * This function passes direct inputs. Some inputs must be passed as keys
2940 * using psa_key_derivation_input_key() instead of this function. Refer to
2941 * the documentation of individual step types for information.
2942 *
2943 * \param[in,out] operation The key derivation operation object to use.
2944 * It must have been set up with
2945 * psa_key_derivation_setup() and must not
2946 * have produced any output yet.
2947 * \param step Which step the input data is for.
2948 * \param[in] data Input data to use.
2949 * \param data_length Size of the \p data buffer in bytes.
2950 *
2951 * \retval #PSA_SUCCESS
2952 * Success.
2953 * \retval #PSA_ERROR_INVALID_ARGUMENT
2954 * \c step is not compatible with the operation's algorithm.
2955 * \retval #PSA_ERROR_INVALID_ARGUMENT
2956 * \c step does not allow direct inputs.
2957 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2958 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2959 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002960 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002961 * \retval #PSA_ERROR_BAD_STATE
2962 * The value of \p step is not valid given the state of \p operation.
2963 * \retval #PSA_ERROR_BAD_STATE
2964 * The library has not been previously initialized by psa_crypto_init().
2965 * It is implementation-dependent whether a failure to initialize
2966 * results in this error code.
2967 */
2968psa_status_t psa_key_derivation_input_bytes(
2969 psa_key_derivation_operation_t *operation,
2970 psa_key_derivation_step_t step,
2971 const uint8_t *data,
2972 size_t data_length);
2973
2974/** Provide an input for key derivation in the form of a key.
2975 *
2976 * Which inputs are required and in what order depends on the algorithm.
2977 * Refer to the documentation of each key derivation or key agreement
2978 * algorithm for information.
2979 *
2980 * This function passes key inputs. Some inputs must be passed as keys
2981 * of the appropriate type using this function, while others must be
2982 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
2983 * the documentation of individual step types for information.
2984 *
2985 * \param[in,out] operation The key derivation operation object to use.
2986 * It must have been set up with
2987 * psa_key_derivation_setup() and must not
2988 * have produced any output yet.
2989 * \param step Which step the input data is for.
2990 * \param handle Handle to the key. It must have an
2991 * appropriate type for \p step and must
2992 * allow the usage #PSA_KEY_USAGE_DERIVE.
2993 *
2994 * \retval #PSA_SUCCESS
2995 * Success.
2996 * \retval #PSA_ERROR_INVALID_HANDLE
2997 * \retval #PSA_ERROR_DOES_NOT_EXIST
2998 * \retval #PSA_ERROR_NOT_PERMITTED
2999 * \retval #PSA_ERROR_INVALID_ARGUMENT
3000 * \c step is not compatible with the operation's algorithm.
3001 * \retval #PSA_ERROR_INVALID_ARGUMENT
3002 * \c step does not allow key inputs.
3003 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3004 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3005 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003006 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003007 * \retval #PSA_ERROR_BAD_STATE
3008 * The value of \p step is not valid given the state of \p operation.
3009 * \retval #PSA_ERROR_BAD_STATE
3010 * The library has not been previously initialized by psa_crypto_init().
3011 * It is implementation-dependent whether a failure to initialize
3012 * results in this error code.
3013 */
3014psa_status_t psa_key_derivation_input_key(
3015 psa_key_derivation_operation_t *operation,
3016 psa_key_derivation_step_t step,
3017 psa_key_handle_t handle);
3018
3019/** Perform a key agreement and use the shared secret as input to a key
3020 * derivation.
3021 *
3022 * A key agreement algorithm takes two inputs: a private key \p private_key
3023 * a public key \p peer_key.
3024 * The result of this function is passed as input to a key derivation.
3025 * The output of this key derivation can be extracted by reading from the
3026 * resulting operation to produce keys and other cryptographic material.
3027 *
3028 * \param[in,out] operation The key derivation operation object to use.
3029 * It must have been set up with
3030 * psa_key_derivation_setup() with a
3031 * key agreement and derivation algorithm
3032 * \c alg (\c PSA_ALG_XXX value such that
3033 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3034 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3035 * is false).
3036 * The operation must be ready for an
3037 * input of the type given by \p step.
3038 * \param step Which step the input data is for.
3039 * \param private_key Handle to the private key to use.
3040 * \param[in] peer_key Public key of the peer. The peer key must be in the
3041 * same format that psa_import_key() accepts for the
3042 * public key type corresponding to the type of
3043 * private_key. That is, this function performs the
3044 * equivalent of
3045 * #psa_import_key(...,
3046 * `peer_key`, `peer_key_length`) where
3047 * with key attributes indicating the public key
3048 * type corresponding to the type of `private_key`.
3049 * For example, for EC keys, this means that peer_key
3050 * is interpreted as a point on the curve that the
3051 * private key is on. The standard formats for public
3052 * keys are documented in the documentation of
3053 * psa_export_public_key().
3054 * \param peer_key_length Size of \p peer_key in bytes.
3055 *
3056 * \retval #PSA_SUCCESS
3057 * Success.
3058 * \retval #PSA_ERROR_INVALID_HANDLE
3059 * \retval #PSA_ERROR_DOES_NOT_EXIST
3060 * \retval #PSA_ERROR_NOT_PERMITTED
3061 * \retval #PSA_ERROR_INVALID_ARGUMENT
3062 * \c private_key is not compatible with \c alg,
3063 * or \p peer_key is not valid for \c alg or not compatible with
3064 * \c private_key.
3065 * \retval #PSA_ERROR_NOT_SUPPORTED
3066 * \c alg is not supported or is not a key derivation algorithm.
3067 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3068 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3069 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003070 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003071 */
3072psa_status_t psa_key_derivation_key_agreement(
3073 psa_key_derivation_operation_t *operation,
3074 psa_key_derivation_step_t step,
3075 psa_key_handle_t private_key,
3076 const uint8_t *peer_key,
3077 size_t peer_key_length);
3078
Gilles Peskine35675b62019-05-16 17:26:11 +02003079/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003080 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003081 * This function calculates output bytes from a key derivation algorithm and
3082 * return those bytes.
3083 * If you view the key derivation's output as a stream of bytes, this
3084 * function destructively reads the requested number of bytes from the
3085 * stream.
3086 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003087 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003088 * \param[in,out] operation The key derivation operation object to read from.
3089 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003090 * \param output_length Number of bytes to output.
3091 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003092 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003093 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003094 * The operation's capacity was less than
3095 * \p output_length bytes. Note that in this case,
3096 * no output is written to the output buffer.
3097 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003098 * subsequent calls to this function will not
3099 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003100 * \retval #PSA_ERROR_BAD_STATE
3101 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3102 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3103 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003104 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003105 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003106psa_status_t psa_key_derivation_output_bytes(
3107 psa_key_derivation_operation_t *operation,
3108 uint8_t *output,
3109 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003110
Gilles Peskine35675b62019-05-16 17:26:11 +02003111/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003112 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003113 * This function calculates output bytes from a key derivation algorithm
3114 * and uses those bytes to generate a key deterministically.
3115 * If you view the key derivation's output as a stream of bytes, this
3116 * function destructively reads as many bytes as required from the
3117 * stream.
3118 * The operation's capacity decreases by the number of bytes read.
3119 *
3120 * How much output is produced and consumed from the operation, and how
3121 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003122 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003123 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003124 * of a given size, this function is functionally equivalent to
3125 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003126 * and passing the resulting output to #psa_import_key.
3127 * However, this function has a security benefit:
3128 * if the implementation provides an isolation boundary then
3129 * the key material is not exposed outside the isolation boundary.
3130 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003131 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003132 * The following key types defined in this specification follow this scheme:
3133 *
3134 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003135 * - #PSA_KEY_TYPE_ARC4;
3136 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003137 * - #PSA_KEY_TYPE_DERIVE;
3138 * - #PSA_KEY_TYPE_HMAC.
3139 *
3140 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003141 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003142 * Montgomery curve), this function always draws a byte string whose
3143 * length is determined by the curve, and sets the mandatory bits
3144 * accordingly. That is:
3145 *
3146 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3147 * and process it as specified in RFC 7748 &sect;5.
3148 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3149 * and process it as specified in RFC 7748 &sect;5.
3150 *
3151 * - For key types for which the key is represented by a single sequence of
3152 * \p bits bits with constraints as to which bit sequences are acceptable,
3153 * this function draws a byte string of length (\p bits / 8) bytes rounded
3154 * up to the nearest whole number of bytes. If the resulting byte string
3155 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3156 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003157 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003158 * for the output produced by psa_export_key().
3159 * The following key types defined in this specification follow this scheme:
3160 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003161 * - #PSA_KEY_TYPE_DES.
3162 * Force-set the parity bits, but discard forbidden weak keys.
3163 * For 2-key and 3-key triple-DES, the three keys are generated
3164 * successively (for example, for 3-key triple-DES,
3165 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3166 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003167 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003168 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003169 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003170 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003171 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003172 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003173 * Weierstrass curve).
3174 * For these key types, interpret the byte string as integer
3175 * in big-endian order. Discard it if it is not in the range
3176 * [0, *N* - 2] where *N* is the boundary of the private key domain
3177 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003178 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003179 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003180 * This method allows compliance to NIST standards, specifically
3181 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003182 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3183 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3184 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3185 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003186 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003187 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003188 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003189 * implementation-defined.
3190 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003191 * In all cases, the data that is read is discarded from the operation.
3192 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003193 *
Gilles Peskine20628592019-04-19 19:29:50 +02003194 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003195 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003196 * \param[out] handle On success, a handle to the newly created key.
3197 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003198 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003199 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003200 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003201 * If the key is persistent, the key material and the key's metadata
3202 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003203 * \retval #PSA_ERROR_ALREADY_EXISTS
3204 * This is an attempt to create a persistent key, and there is
3205 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003206 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003207 * There was not enough data to create the desired key.
3208 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003209 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003210 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003211 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003212 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003213 * implementation in general or in this particular location.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003214 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003215 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3216 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3217 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3218 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003219 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003220 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003221 * The library has not been previously initialized by psa_crypto_init().
3222 * It is implementation-dependent whether a failure to initialize
3223 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003224 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003225psa_status_t psa_key_derivation_output_key(
3226 const psa_key_attributes_t *attributes,
3227 psa_key_derivation_operation_t *operation,
3228 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003229
Gilles Peskine35675b62019-05-16 17:26:11 +02003230/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003231 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003232 * Once a key derivation operation has been aborted, its capacity is zero.
3233 * Aborting an operation frees all associated resources except for the
3234 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003235 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003236 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003237 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003238 * psa_key_derivation_operation_init() or a zero value. In particular,
3239 * it is valid to call psa_key_derivation_abort() twice, or to call
3240 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003241 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003242 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003243 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003244 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003245 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003246 * \retval #PSA_SUCCESS
3247 * \retval #PSA_ERROR_BAD_STATE
3248 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3249 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003250 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003251 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003252psa_status_t psa_key_derivation_abort(
3253 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003254
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003255/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003256 *
3257 * \warning The raw result of a key agreement algorithm such as finite-field
3258 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3259 * not be used directly as key material. It should instead be passed as
3260 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003261 * a key derivation, use psa_key_derivation_key_agreement() and other
3262 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003263 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003264 * \param alg The key agreement algorithm to compute
3265 * (\c PSA_ALG_XXX value such that
3266 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3267 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003268 * \param private_key Handle to the private key to use.
3269 * \param[in] peer_key Public key of the peer. It must be
3270 * in the same format that psa_import_key()
3271 * accepts. The standard formats for public
3272 * keys are documented in the documentation
3273 * of psa_export_public_key().
3274 * \param peer_key_length Size of \p peer_key in bytes.
3275 * \param[out] output Buffer where the decrypted message is to
3276 * be written.
3277 * \param output_size Size of the \c output buffer in bytes.
3278 * \param[out] output_length On success, the number of bytes
3279 * that make up the returned output.
3280 *
3281 * \retval #PSA_SUCCESS
3282 * Success.
3283 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003284 * \retval #PSA_ERROR_NOT_PERMITTED
3285 * \retval #PSA_ERROR_INVALID_ARGUMENT
3286 * \p alg is not a key agreement algorithm
3287 * \retval #PSA_ERROR_INVALID_ARGUMENT
3288 * \p private_key is not compatible with \p alg,
3289 * or \p peer_key is not valid for \p alg or not compatible with
3290 * \p private_key.
3291 * \retval #PSA_ERROR_NOT_SUPPORTED
3292 * \p alg is not a supported key agreement algorithm.
3293 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3294 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3295 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003296 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine769c7a62019-01-18 16:42:29 +01003297 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003298psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3299 psa_key_handle_t private_key,
3300 const uint8_t *peer_key,
3301 size_t peer_key_length,
3302 uint8_t *output,
3303 size_t output_size,
3304 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003305
Gilles Peskineea0fb492018-07-12 17:17:20 +02003306/**@}*/
3307
Gilles Peskineedd76872018-07-20 17:42:05 +02003308/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003309 * @{
3310 */
3311
3312/**
3313 * \brief Generate random bytes.
3314 *
3315 * \warning This function **can** fail! Callers MUST check the return status
3316 * and MUST NOT use the content of the output buffer if the return
3317 * status is not #PSA_SUCCESS.
3318 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003319 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003320 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003321 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003322 * \param output_size Number of bytes to generate and output.
3323 *
Gilles Peskine28538492018-07-11 17:34:00 +02003324 * \retval #PSA_SUCCESS
3325 * \retval #PSA_ERROR_NOT_SUPPORTED
3326 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3327 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3328 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003329 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003330 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003331 * The library has not been previously initialized by psa_crypto_init().
3332 * It is implementation-dependent whether a failure to initialize
3333 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003334 */
3335psa_status_t psa_generate_random(uint8_t *output,
3336 size_t output_size);
3337
3338/**
3339 * \brief Generate a key or key pair.
3340 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003341 * The key is generated randomly.
3342 * Its location, policy, type and size are taken from \p attributes.
3343 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003344 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003345 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003346 * the public exponent is 65537.
3347 * The modulus is a product of two probabilistic primes
3348 * between 2^{n-1} and 2^n where n is the bit size specified in the
3349 * attributes.
3350 *
Gilles Peskine20628592019-04-19 19:29:50 +02003351 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003352 * \param[out] handle On success, a handle to the newly created key.
3353 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003354 *
Gilles Peskine28538492018-07-11 17:34:00 +02003355 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003356 * Success.
3357 * If the key is persistent, the key material and the key's metadata
3358 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003359 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003360 * This is an attempt to create a persistent key, and there is
3361 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003362 * \retval #PSA_ERROR_NOT_SUPPORTED
3363 * \retval #PSA_ERROR_INVALID_ARGUMENT
3364 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3365 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3366 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3367 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003368 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003369 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003370 * The library has not been previously initialized by psa_crypto_init().
3371 * It is implementation-dependent whether a failure to initialize
3372 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003373 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003374psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003375 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003376
3377/**@}*/
3378
Gilles Peskinee59236f2018-01-27 23:32:46 +01003379#ifdef __cplusplus
3380}
3381#endif
3382
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003383/* The file "crypto_sizes.h" contains definitions for size calculation
3384 * macros whose definitions are implementation-specific. */
3385#include "crypto_sizes.h"
3386
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003387/* The file "crypto_struct.h" contains definitions for
3388 * implementation-specific structs that are declared above. */
3389#include "crypto_struct.h"
3390
3391/* The file "crypto_extra.h" contains vendor-specific definitions. This
3392 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003393#include "crypto_extra.h"
3394
3395#endif /* PSA_CRYPTO_H */