blob: 0ec67d7fc5deac864d55e619cd0659368f167492 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +020089 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020090 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine105f67f2019-07-23 18:16:05 +020096/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +020097 * @{
98 */
99
Gilles Peskinea0c06552019-05-21 15:54:54 +0200100/** \def PSA_KEY_ATTRIBUTES_INIT
101 *
102 * This macro returns a suitable initializer for a key attribute structure
103 * of type #psa_key_attributes_t.
104 */
105#ifdef __DOXYGEN_ONLY__
106/* This is an example definition for documentation purposes.
107 * Implementations should define a suitable value in `crypto_struct.h`.
108 */
109#define PSA_KEY_ATTRIBUTES_INIT {0}
110#endif
111
112/** Return an initial value for a key attributes structure.
113 */
114static psa_key_attributes_t psa_key_attributes_init(void);
115
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200116/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200117 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200118 * If the attribute structure currently declares the key as volatile (which
119 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200120 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200121 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200122 * This function does not access storage, it merely stores the given
123 * value in the structure.
124 * The persistent key will be written to storage when the attribute
125 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200126 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200127 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200128 *
Gilles Peskine20628592019-04-19 19:29:50 +0200129 * This function may be declared as `static` (i.e. without external
130 * linkage). This function may be provided as a function-like macro,
131 * but in this case it must evaluate each of its arguments exactly once.
132 *
133 * \param[out] attributes The attribute structure to write to.
134 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200135 */
136static void psa_set_key_id(psa_key_attributes_t *attributes,
137 psa_key_id_t id);
138
139/** Set the location of a persistent key.
140 *
141 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200142 * with psa_set_key_id(). By default, a key that has a persistent identifier
143 * is stored in the default storage area identifier by
144 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
145 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200146 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200147 * This function does not access storage, it merely stores the given
148 * value in the structure.
149 * The persistent key will be written to storage when the attribute
150 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200151 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200152 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200153 *
154 * This function may be declared as `static` (i.e. without external
155 * linkage). This function may be provided as a function-like macro,
156 * but in this case it must evaluate each of its arguments exactly once.
157 *
158 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200159 * \param lifetime The lifetime for the key.
160 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200161 * key will be volatile, and the key identifier
162 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200163 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200164static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
165 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200166
Gilles Peskine20628592019-04-19 19:29:50 +0200167/** Retrieve the key identifier from key attributes.
168 *
169 * This function may be declared as `static` (i.e. without external
170 * linkage). This function may be provided as a function-like macro,
171 * but in this case it must evaluate its argument exactly once.
172 *
173 * \param[in] attributes The key attribute structure to query.
174 *
175 * \return The persistent identifier stored in the attribute structure.
176 * This value is unspecified if the attribute structure declares
177 * the key as volatile.
178 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200179static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
180
Gilles Peskine20628592019-04-19 19:29:50 +0200181/** Retrieve the lifetime from key attributes.
182 *
183 * This function may be declared as `static` (i.e. without external
184 * linkage). This function may be provided as a function-like macro,
185 * but in this case it must evaluate its argument exactly once.
186 *
187 * \param[in] attributes The key attribute structure to query.
188 *
189 * \return The lifetime value stored in the attribute structure.
190 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200191static psa_key_lifetime_t psa_get_key_lifetime(
192 const psa_key_attributes_t *attributes);
193
Gilles Peskine20628592019-04-19 19:29:50 +0200194/** Declare usage flags for a key.
195 *
196 * Usage flags are part of a key's usage policy. They encode what
197 * kind of operations are permitted on the key. For more details,
198 * refer to the documentation of the type #psa_key_usage_t.
199 *
200 * This function overwrites any usage flags
201 * previously set in \p attributes.
202 *
203 * This function may be declared as `static` (i.e. without external
204 * linkage). This function may be provided as a function-like macro,
205 * but in this case it must evaluate each of its arguments exactly once.
206 *
207 * \param[out] attributes The attribute structure to write to.
208 * \param usage_flags The usage flags to write.
209 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200210static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
211 psa_key_usage_t usage_flags);
212
Gilles Peskine20628592019-04-19 19:29:50 +0200213/** Retrieve the usage flags from key attributes.
214 *
215 * This function may be declared as `static` (i.e. without external
216 * linkage). This function may be provided as a function-like macro,
217 * but in this case it must evaluate its argument exactly once.
218 *
219 * \param[in] attributes The key attribute structure to query.
220 *
221 * \return The usage flags stored in the attribute structure.
222 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200223static psa_key_usage_t psa_get_key_usage_flags(
224 const psa_key_attributes_t *attributes);
225
Gilles Peskine20628592019-04-19 19:29:50 +0200226/** Declare the permitted algorithm policy for a key.
227 *
228 * The permitted algorithm policy of a key encodes which algorithm or
229 * algorithms are permitted to be used with this key.
230 *
231 * This function overwrites any algorithm policy
232 * previously set in \p attributes.
233 *
234 * This function may be declared as `static` (i.e. without external
235 * linkage). This function may be provided as a function-like macro,
236 * but in this case it must evaluate each of its arguments exactly once.
237 *
238 * \param[out] attributes The attribute structure to write to.
239 * \param alg The permitted algorithm policy to write.
240 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200241static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
242 psa_algorithm_t alg);
243
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100244
Gilles Peskine20628592019-04-19 19:29:50 +0200245/** Retrieve the algorithm policy from key attributes.
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate its argument exactly once.
250 *
251 * \param[in] attributes The key attribute structure to query.
252 *
253 * \return The algorithm stored in the attribute structure.
254 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200255static psa_algorithm_t psa_get_key_algorithm(
256 const psa_key_attributes_t *attributes);
257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Declare the type of a key.
259 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200260 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200261 * previously set in \p attributes.
262 *
263 * This function may be declared as `static` (i.e. without external
264 * linkage). This function may be provided as a function-like macro,
265 * but in this case it must evaluate each of its arguments exactly once.
266 *
267 * \param[out] attributes The attribute structure to write to.
268 * \param type The key type to write.
269 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200270static void psa_set_key_type(psa_key_attributes_t *attributes,
271 psa_key_type_t type);
272
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100273
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200274/** Declare the size of a key.
275 *
276 * This function overwrites any key size previously set in \p attributes.
277 *
278 * This function may be declared as `static` (i.e. without external
279 * linkage). This function may be provided as a function-like macro,
280 * but in this case it must evaluate each of its arguments exactly once.
281 *
282 * \param[out] attributes The attribute structure to write to.
283 * \param bits The key size in bits.
284 */
285static void psa_set_key_bits(psa_key_attributes_t *attributes,
286 size_t bits);
287
Gilles Peskine20628592019-04-19 19:29:50 +0200288/** Retrieve the key type from key attributes.
289 *
290 * This function may be declared as `static` (i.e. without external
291 * linkage). This function may be provided as a function-like macro,
292 * but in this case it must evaluate its argument exactly once.
293 *
294 * \param[in] attributes The key attribute structure to query.
295 *
296 * \return The key type stored in the attribute structure.
297 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200298static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
299
Gilles Peskine20628592019-04-19 19:29:50 +0200300/** Retrieve the key size from key attributes.
301 *
302 * This function may be declared as `static` (i.e. without external
303 * linkage). This function may be provided as a function-like macro,
304 * but in this case it must evaluate its argument exactly once.
305 *
306 * \param[in] attributes The key attribute structure to query.
307 *
308 * \return The key size stored in the attribute structure, in bits.
309 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200310static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
311
Gilles Peskine20628592019-04-19 19:29:50 +0200312/** Retrieve the attributes of a key.
313 *
314 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200315 * psa_reset_key_attributes(). It then copies the attributes of
316 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200317 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200318 * \note This function may allocate memory or other resources.
319 * Once you have called this function on an attribute structure,
320 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200321 *
Gilles Peskine20628592019-04-19 19:29:50 +0200322 * \param[in] handle Handle to the key to query.
323 * \param[in,out] attributes On success, the attributes of the key.
324 * On failure, equivalent to a
325 * freshly-initialized structure.
326 *
327 * \retval #PSA_SUCCESS
328 * \retval #PSA_ERROR_INVALID_HANDLE
329 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
330 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100331 * \retval #PSA_ERROR_CORRUPTION_DETECTED
332 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100333 * \retval #PSA_ERROR_BAD_STATE
334 * The library has not been previously initialized by psa_crypto_init().
335 * It is implementation-dependent whether a failure to initialize
336 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200337 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200338psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
339 psa_key_attributes_t *attributes);
340
Gilles Peskine20628592019-04-19 19:29:50 +0200341/** Reset a key attribute structure to a freshly initialized state.
342 *
343 * You must initialize the attribute structure as described in the
344 * documentation of the type #psa_key_attributes_t before calling this
345 * function. Once the structure has been initialized, you may call this
346 * function at any time.
347 *
348 * This function frees any auxiliary resources that the structure
349 * may contain.
350 *
351 * \param[in,out] attributes The attribute structure to reset.
352 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200353void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200354
Gilles Peskine87a5e562019-04-17 12:28:25 +0200355/**@}*/
356
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357/** \defgroup key_management Key management
358 * @{
359 */
360
Gilles Peskinef535eb22018-11-30 14:08:36 +0100361/** Open a handle to an existing persistent key.
362 *
Gilles Peskine4754cde2019-05-21 15:56:29 +0200363 * Open a handle to a persistent key. A key is persistent if it was created
364 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
365 * always has a nonzero key identifier, set with psa_set_key_id() when
366 * creating the key. Implementations may provide additional pre-provisioned
Andrew Thoelke203491c2019-08-21 17:55:30 +0100367 * keys that can be opened with psa_open_key(). Such keys have a key identifier
368 * in the vendor range, as documented in the description of #psa_key_id_t.
Gilles Peskine4754cde2019-05-21 15:56:29 +0200369 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100370 * The application must eventually close the handle with psa_close_key() or
371 * psa_destroy_key() to release associated resources. If the application dies
372 * without calling one of these functions, the implementation should perform
373 * the equivalent of a call to psa_close_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100374 *
Andrew Thoelke9741b112019-08-21 18:20:41 +0100375 * Some implementations permit an application to open the same key multiple
Andrew Thoelkede183412019-09-05 09:38:06 +0100376 * times. If this is successful, each call to psa_open_key() will return a
377 * different key handle.
378 *
379 * \note Applications that rely on opening a key multiple times will not be
380 * portable to implementations that only permit a single key handle to be
381 * opened. See also :ref:\`key-handles\`.
Andrew Thoelke9741b112019-08-21 18:20:41 +0100382 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100383 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100384 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100385 *
386 * \retval #PSA_SUCCESS
387 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100388 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100389 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100390 * The implementation does not have sufficient resources to open the
391 * key. This can be due to reaching an implementation limit on the
392 * number of open keys, the number of open key handles, or available
393 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200394 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100395 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100396 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100397 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100398 * \retval #PSA_ERROR_NOT_PERMITTED
399 * The specified key exists, but the application does not have the
400 * permission to access it. Note that this specification does not
401 * define any way to create such a key, but it may be possible
402 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200403 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawd789dc12019-08-12 15:06:48 +0100404 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine225010f2019-05-06 18:44:55 +0200405 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100406 * \retval #PSA_ERROR_BAD_STATE
407 * The library has not been previously initialized by psa_crypto_init().
408 * It is implementation-dependent whether a failure to initialize
409 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100410 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200411psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100412 psa_key_handle_t *handle);
413
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100414
Gilles Peskinef535eb22018-11-30 14:08:36 +0100415/** Close a key handle.
416 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100417 * If the handle designates a volatile key, this will destroy the key material
418 * and free all associated resources, just like psa_destroy_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100419 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100420 * If this is the last open handle to a persistent key, then closing the handle
421 * will free all resources associated with the key in volatile memory. The key
422 * data in persistent storage is not affected and can be opened again later
423 * with a call to psa_open_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100424 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100425 * Closing the key handle makes the handle invalid, and the key handle
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100426 * must not be used again by the application.
Andrew Thoelke3daba812019-08-21 22:46:56 +0100427 *
Andrew Thoelke970629f2019-09-09 09:56:34 +0100428 * \note If the key handle was used to set up an active
Andrew Thoelkede183412019-09-05 09:38:06 +0100429 * :ref:\`multipart operation <multipart-operations>\`, then closing the
430 * key handle can cause the multipart operation to fail. Applications should
431 * maintain the key handle until after the multipart operation has finished.
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100432 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100433 * \param handle The key handle to close.
434 *
435 * \retval #PSA_SUCCESS
436 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100437 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawf97c8522019-08-15 13:27:12 +0100438 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100439 * \retval #PSA_ERROR_BAD_STATE
440 * The library has not been previously initialized by psa_crypto_init().
441 * It is implementation-dependent whether a failure to initialize
442 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100443 */
444psa_status_t psa_close_key(psa_key_handle_t handle);
445
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100446/** Make a copy of a key.
447 *
448 * Copy key material from one location to another.
449 *
450 * This function is primarily useful to copy a key from one location
451 * to another, since it populates a key using the material from
452 * another key which may have a different lifetime.
453 *
454 * This function may be used to share a key with a different party,
455 * subject to implementation-defined restrictions on key sharing.
456 *
457 * The policy on the source key must have the usage flag
458 * #PSA_KEY_USAGE_COPY set.
459 * This flag is sufficient to permit the copy if the key has the lifetime
460 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
461 * Some secure elements do not provide a way to copy a key without
462 * making it extractable from the secure element. If a key is located
463 * in such a secure element, then the key must have both usage flags
464 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
465 * a copy of the key outside the secure element.
466 *
467 * The resulting key may only be used in a way that conforms to
468 * both the policy of the original key and the policy specified in
469 * the \p attributes parameter:
470 * - The usage flags on the resulting key are the bitwise-and of the
471 * usage flags on the source policy and the usage flags in \p attributes.
472 * - If both allow the same algorithm or wildcard-based
473 * algorithm policy, the resulting key has the same algorithm policy.
474 * - If either of the policies allows an algorithm and the other policy
475 * allows a wildcard-based algorithm policy that includes this algorithm,
476 * the resulting key allows the same algorithm.
477 * - If the policies do not allow any algorithm in common, this function
478 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
479 *
480 * The effect of this function on implementation-defined attributes is
481 * implementation-defined.
482 *
483 * \param source_handle The key to copy. It must be a valid key handle.
484 * \param[in] attributes The attributes for the new key.
485 * They are used as follows:
486 * - The key type and size may be 0. If either is
487 * nonzero, it must match the corresponding
488 * attribute of the source key.
489 * - The key location (the lifetime and, for
490 * persistent keys, the key identifier) is
491 * used directly.
492 * - The policy constraints (usage flags and
493 * algorithm policy) are combined from
494 * the source key and \p attributes so that
495 * both sets of restrictions apply, as
496 * described in the documentation of this function.
497 * \param[out] target_handle On success, a handle to the newly created key.
498 * \c 0 on failure.
499 *
500 * \retval #PSA_SUCCESS
501 * \retval #PSA_ERROR_INVALID_HANDLE
502 * \p source_handle is invalid.
503 * \retval #PSA_ERROR_ALREADY_EXISTS
504 * This is an attempt to create a persistent key, and there is
505 * already a persistent key with the given identifier.
506 * \retval #PSA_ERROR_INVALID_ARGUMENT
507 * The lifetime or identifier in \p attributes are invalid.
508 * \retval #PSA_ERROR_INVALID_ARGUMENT
509 * The policy constraints on the source and specified in
510 * \p attributes are incompatible.
511 * \retval #PSA_ERROR_INVALID_ARGUMENT
512 * \p attributes specifies a key type or key size
513 * which does not match the attributes of the source key.
514 * \retval #PSA_ERROR_NOT_PERMITTED
515 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
516 * \retval #PSA_ERROR_NOT_PERMITTED
517 * The source key is not exportable and its lifetime does not
518 * allow copying it to the target's lifetime.
519 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
520 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
521 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
522 * \retval #PSA_ERROR_HARDWARE_FAILURE
523 * \retval #PSA_ERROR_STORAGE_FAILURE
524 * \retval #PSA_ERROR_CORRUPTION_DETECTED
525 * \retval #PSA_ERROR_BAD_STATE
526 * The library has not been previously initialized by psa_crypto_init().
527 * It is implementation-dependent whether a failure to initialize
528 * results in this error code.
529 */
530psa_status_t psa_copy_key(psa_key_handle_t source_handle,
531 const psa_key_attributes_t *attributes,
532 psa_key_handle_t *target_handle);
533
534
535/**
536 * \brief Destroy a key.
537 *
538 * This function destroys a key from both volatile
539 * memory and, if applicable, non-volatile storage. Implementations shall
540 * make a best effort to ensure that that the key material cannot be recovered.
541 *
542 * This function also erases any metadata such as policies and frees
543 * resources associated with the key. To free all resources associated with
544 * the key, all handles to the key must be closed or destroyed.
545 *
546 * Destroying the key makes the handle invalid, and the key handle
547 * must not be used again by the application. Using other open handles to the
548 * destroyed key in a cryptographic operation will result in an error.
549 *
550 * If a key is currently in use in a multipart operation, then destroying the
551 * key will cause the multipart operation to fail.
552 *
553 * \param handle Handle to the key to erase.
554 *
555 * \retval #PSA_SUCCESS
556 * The key material has been erased.
557 * \retval #PSA_ERROR_NOT_PERMITTED
558 * The key cannot be erased because it is
559 * read-only, either due to a policy or due to physical restrictions.
560 * \retval #PSA_ERROR_INVALID_HANDLE
561 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
562 * There was an failure in communication with the cryptoprocessor.
563 * The key material may still be present in the cryptoprocessor.
564 * \retval #PSA_ERROR_STORAGE_FAILURE
565 * The storage is corrupted. Implementations shall make a best effort
566 * to erase key material even in this stage, however applications
567 * should be aware that it may be impossible to guarantee that the
568 * key material is not recoverable in such cases.
569 * \retval #PSA_ERROR_CORRUPTION_DETECTED
570 * An unexpected condition which is not a storage corruption or
571 * a communication failure occurred. The cryptoprocessor may have
572 * been compromised.
573 * \retval #PSA_ERROR_BAD_STATE
574 * The library has not been previously initialized by psa_crypto_init().
575 * It is implementation-dependent whether a failure to initialize
576 * results in this error code.
577 */
578psa_status_t psa_destroy_key(psa_key_handle_t handle);
579
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100580/**@}*/
581
582/** \defgroup import_export Key import and export
583 * @{
584 */
585
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100586/**
587 * \brief Import a key in binary format.
588 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100589 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100590 * documentation of psa_export_public_key() for the format of public keys
591 * and to the documentation of psa_export_key() for the format for
592 * other key types.
593 *
594 * This specification supports a single format for each key type.
595 * Implementations may support other formats as long as the standard
596 * format is supported. Implementations that support other formats
597 * should ensure that the formats are clearly unambiguous so as to
598 * minimize the risk that an invalid input is accidentally interpreted
599 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100600 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100601
Gilles Peskine20628592019-04-19 19:29:50 +0200602 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200603 * The key size is always determined from the
604 * \p data buffer.
605 * If the key size in \p attributes is nonzero,
606 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200607 * \param[out] handle On success, a handle to the newly created key.
608 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100609 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200610 * buffer is interpreted according to the type declared
611 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200612 * All implementations must support at least the format
613 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100614 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200615 * the chosen type. Implementations may allow other
616 * formats, but should be conservative: implementations
617 * should err on the side of rejecting content if it
618 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200619 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100620 *
Gilles Peskine28538492018-07-11 17:34:00 +0200621 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100622 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100623 * If the key is persistent, the key material and the key's metadata
624 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200625 * \retval #PSA_ERROR_ALREADY_EXISTS
626 * This is an attempt to create a persistent key, and there is
627 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200628 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200629 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200630 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200631 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200632 * The key attributes, as a whole, are invalid.
633 * \retval #PSA_ERROR_INVALID_ARGUMENT
634 * The key data is not correctly formatted.
635 * \retval #PSA_ERROR_INVALID_ARGUMENT
636 * The size in \p attributes is nonzero and does not match the size
637 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200638 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
639 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
640 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100641 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200642 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200643 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300644 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300645 * The library has not been previously initialized by psa_crypto_init().
646 * It is implementation-dependent whether a failure to initialize
647 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100648 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200649psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200651 size_t data_length,
652 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100653
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100654
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100655
656/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100657 * \brief Export a key in binary format.
658 *
659 * The output of this function can be passed to psa_import_key() to
660 * create an equivalent object.
661 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100662 * If the implementation of psa_import_key() supports other formats
663 * beyond the format specified here, the output from psa_export_key()
664 * must use the representation specified here, not the original
665 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100666 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100667 * For standard key types, the output format is as follows:
668 *
669 * - For symmetric keys (including MAC keys), the format is the
670 * raw bytes of the key.
671 * - For DES, the key data consists of 8 bytes. The parity bits must be
672 * correct.
673 * - For Triple-DES, the format is the concatenation of the
674 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200675 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200676 * is the non-encrypted DER encoding of the representation defined by
677 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
678 * ```
679 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200680 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200681 * modulus INTEGER, -- n
682 * publicExponent INTEGER, -- e
683 * privateExponent INTEGER, -- d
684 * prime1 INTEGER, -- p
685 * prime2 INTEGER, -- q
686 * exponent1 INTEGER, -- d mod (p-1)
687 * exponent2 INTEGER, -- d mod (q-1)
688 * coefficient INTEGER, -- (inverse of q) mod p
689 * }
690 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200691 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200692 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100693 * a representation of the private value as a `ceiling(m/8)`-byte string
694 * where `m` is the bit size associated with the curve, i.e. the bit size
695 * of the order of the curve's coordinate field. This byte string is
696 * in little-endian order for Montgomery curves (curve types
697 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
698 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
699 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100700 * This is the content of the `privateKey` field of the `ECPrivateKey`
701 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200702 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200703 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000704 * format is the representation of the private key `x` as a big-endian byte
705 * string. The length of the byte string is the private key size in bytes
706 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200707 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
708 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100709 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200710 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
711 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100712 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200713 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200714 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200715 * \param[out] data_length On success, the number of bytes
716 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100717 *
Gilles Peskine28538492018-07-11 17:34:00 +0200718 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100719 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200720 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200721 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100722 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200723 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
724 * The size of the \p data buffer is too small. You can determine a
725 * sufficient buffer size by calling
726 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
727 * where \c type is the key type
728 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200729 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
730 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200731 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100732 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100733 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300734 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300735 * The library has not been previously initialized by psa_crypto_init().
736 * It is implementation-dependent whether a failure to initialize
737 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100739psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 uint8_t *data,
741 size_t data_size,
742 size_t *data_length);
743
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100744/**
745 * \brief Export a public key or the public part of a key pair in binary format.
746 *
747 * The output of this function can be passed to psa_import_key() to
748 * create an object that is equivalent to the public key.
749 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000750 * This specification supports a single format for each key type.
751 * Implementations may support other formats as long as the standard
752 * format is supported. Implementations that support other formats
753 * should ensure that the formats are clearly unambiguous so as to
754 * minimize the risk that an invalid input is accidentally interpreted
755 * according to a different format.
756 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000757 * For standard key types, the output format is as follows:
758 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
759 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
760 * ```
761 * RSAPublicKey ::= SEQUENCE {
762 * modulus INTEGER, -- n
763 * publicExponent INTEGER } -- e
764 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000765 * - For elliptic curve public keys (key types for which
766 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
767 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
768 * Let `m` be the bit size associated with the curve, i.e. the bit size of
769 * `q` for a curve over `F_q`. The representation consists of:
770 * - The byte 0x04;
771 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
772 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200773 * - For Diffie-Hellman key exchange public keys (key types for which
774 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000775 * the format is the representation of the public key `y = g^x mod p` as a
776 * big-endian byte string. The length of the byte string is the length of the
777 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100778 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200779 * Exporting a public key object or the public part of a key pair is
780 * always permitted, regardless of the key's usage flags.
781 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100782 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200783 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200784 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200785 * \param[out] data_length On success, the number of bytes
786 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100787 *
Gilles Peskine28538492018-07-11 17:34:00 +0200788 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100789 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200790 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200791 * The key is neither a public key nor a key pair.
792 * \retval #PSA_ERROR_NOT_SUPPORTED
793 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
794 * The size of the \p data buffer is too small. You can determine a
795 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200796 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200797 * where \c type is the key type
798 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200799 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
800 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200801 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100802 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100803 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300804 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300805 * The library has not been previously initialized by psa_crypto_init().
806 * It is implementation-dependent whether a failure to initialize
807 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100808 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100809psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100810 uint8_t *data,
811 size_t data_size,
812 size_t *data_length);
813
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100814
Gilles Peskine20035e32018-02-03 22:44:14 +0100815
816/**@}*/
817
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100818/** \defgroup hash Message digests
819 * @{
820 */
821
Gilles Peskine69647a42019-01-14 20:18:12 +0100822/** Calculate the hash (digest) of a message.
823 *
824 * \note To verify the hash of a message against an
825 * expected value, use psa_hash_compare() instead.
826 *
827 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
828 * such that #PSA_ALG_IS_HASH(\p alg) is true).
829 * \param[in] input Buffer containing the message to hash.
830 * \param input_length Size of the \p input buffer in bytes.
831 * \param[out] hash Buffer where the hash is to be written.
832 * \param hash_size Size of the \p hash buffer in bytes.
833 * \param[out] hash_length On success, the number of bytes
834 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100835 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100836 *
837 * \retval #PSA_SUCCESS
838 * Success.
839 * \retval #PSA_ERROR_NOT_SUPPORTED
840 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100841 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100842 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
843 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100844 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
845 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
846 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200847 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100848 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100849 * \retval #PSA_ERROR_BAD_STATE
850 * The library has not been previously initialized by psa_crypto_init().
851 * It is implementation-dependent whether a failure to initialize
852 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100853 */
854psa_status_t psa_hash_compute(psa_algorithm_t alg,
855 const uint8_t *input,
856 size_t input_length,
857 uint8_t *hash,
858 size_t hash_size,
859 size_t *hash_length);
860
861/** Calculate the hash (digest) of a message and compare it with a
862 * reference value.
863 *
864 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
865 * such that #PSA_ALG_IS_HASH(\p alg) is true).
866 * \param[in] input Buffer containing the message to hash.
867 * \param input_length Size of the \p input buffer in bytes.
868 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100869 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100870 *
871 * \retval #PSA_SUCCESS
872 * The expected hash is identical to the actual hash of the input.
873 * \retval #PSA_ERROR_INVALID_SIGNATURE
874 * The hash of the message was calculated successfully, but it
875 * differs from the expected hash.
876 * \retval #PSA_ERROR_NOT_SUPPORTED
877 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100878 * \retval #PSA_ERROR_INVALID_ARGUMENT
879 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100880 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
881 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
882 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200883 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100884 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100885 * \retval #PSA_ERROR_BAD_STATE
886 * The library has not been previously initialized by psa_crypto_init().
887 * It is implementation-dependent whether a failure to initialize
888 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100889 */
890psa_status_t psa_hash_compare(psa_algorithm_t alg,
891 const uint8_t *input,
892 size_t input_length,
893 const uint8_t *hash,
894 const size_t hash_length);
895
Gilles Peskine308b91d2018-02-08 09:47:44 +0100896/** The type of the state data structure for multipart hash operations.
897 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000898 * Before calling any function on a hash operation object, the application must
899 * initialize it by any of the following means:
900 * - Set the structure to all-bits-zero, for example:
901 * \code
902 * psa_hash_operation_t operation;
903 * memset(&operation, 0, sizeof(operation));
904 * \endcode
905 * - Initialize the structure to logical zero values, for example:
906 * \code
907 * psa_hash_operation_t operation = {0};
908 * \endcode
909 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
910 * for example:
911 * \code
912 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
913 * \endcode
914 * - Assign the result of the function psa_hash_operation_init()
915 * to the structure, for example:
916 * \code
917 * psa_hash_operation_t operation;
918 * operation = psa_hash_operation_init();
919 * \endcode
920 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100921 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100922 * make any assumptions about the content of this structure except
923 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100924typedef struct psa_hash_operation_s psa_hash_operation_t;
925
Jaeden Amero6a25b412019-01-04 11:47:44 +0000926/** \def PSA_HASH_OPERATION_INIT
927 *
928 * This macro returns a suitable initializer for a hash operation object
929 * of type #psa_hash_operation_t.
930 */
931#ifdef __DOXYGEN_ONLY__
932/* This is an example definition for documentation purposes.
933 * Implementations should define a suitable value in `crypto_struct.h`.
934 */
935#define PSA_HASH_OPERATION_INIT {0}
936#endif
937
938/** Return an initial value for a hash operation object.
939 */
940static psa_hash_operation_t psa_hash_operation_init(void);
941
Gilles Peskinef45adda2019-01-14 18:29:18 +0100942/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100943 *
944 * The sequence of operations to calculate a hash (message digest)
945 * is as follows:
946 * -# Allocate an operation object which will be passed to all the functions
947 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000948 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100949 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200950 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100951 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100952 * of the message each time. The hash that is calculated is the hash
953 * of the concatenation of these messages in order.
954 * -# To calculate the hash, call psa_hash_finish().
955 * To compare the hash with an expected value, call psa_hash_verify().
956 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100957 * If an error occurs at any step after a call to psa_hash_setup(), the
958 * operation will need to be reset by a call to psa_hash_abort(). The
959 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000960 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100961 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200962 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100963 * eventually terminate the operation. The following events terminate an
964 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100965 * - A successful call to psa_hash_finish() or psa_hash_verify().
966 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100967 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000968 * \param[in,out] operation The operation object to set up. It must have
969 * been initialized as per the documentation for
970 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200971 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
972 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100973 *
Gilles Peskine28538492018-07-11 17:34:00 +0200974 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100975 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200976 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100977 * \p alg is not a supported hash algorithm.
978 * \retval #PSA_ERROR_INVALID_ARGUMENT
979 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100980 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100981 * The operation state is not valid (it must be inactive).
Gilles Peskine28538492018-07-11 17:34:00 +0200982 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
983 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
984 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200985 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100986 * \retval #PSA_ERROR_BAD_STATE
987 * The library has not been previously initialized by psa_crypto_init().
988 * It is implementation-dependent whether a failure to initialize
989 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200991psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100992 psa_algorithm_t alg);
993
Gilles Peskine308b91d2018-02-08 09:47:44 +0100994/** Add a message fragment to a multipart hash operation.
995 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200996 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100997 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100998 * If this function returns an error status, the operation enters an error
999 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001000 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001001 * \param[in,out] operation Active hash operation.
1002 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001003 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001004 *
Gilles Peskine28538492018-07-11 17:34:00 +02001005 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001006 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001007 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001008 * The operation state is not valid (it muct be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001009 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1010 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1011 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001012 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001013 * \retval #PSA_ERROR_BAD_STATE
1014 * The library has not been previously initialized by psa_crypto_init().
1015 * It is implementation-dependent whether a failure to initialize
1016 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001017 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001018psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1019 const uint8_t *input,
1020 size_t input_length);
1021
Gilles Peskine308b91d2018-02-08 09:47:44 +01001022/** Finish the calculation of the hash of a message.
1023 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001024 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001025 * This function calculates the hash of the message formed by concatenating
1026 * the inputs passed to preceding calls to psa_hash_update().
1027 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001028 * When this function returns successfuly, the operation becomes inactive.
1029 * If this function returns an error status, the operation enters an error
1030 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001031 *
1032 * \warning Applications should not call this function if they expect
1033 * a specific value for the hash. Call psa_hash_verify() instead.
1034 * Beware that comparing integrity or authenticity data such as
1035 * hash values with a function such as \c memcmp is risky
1036 * because the time taken by the comparison may leak information
1037 * about the hashed data which could allow an attacker to guess
1038 * a valid hash and thereby bypass security controls.
1039 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001040 * \param[in,out] operation Active hash operation.
1041 * \param[out] hash Buffer where the hash is to be written.
1042 * \param hash_size Size of the \p hash buffer in bytes.
1043 * \param[out] hash_length On success, the number of bytes
1044 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001045 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001046 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047 *
Gilles Peskine28538492018-07-11 17:34:00 +02001048 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001049 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001050 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001051 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001052 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001053 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001054 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001055 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001056 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1057 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1058 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001059 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001060 * \retval #PSA_ERROR_BAD_STATE
1061 * The library has not been previously initialized by psa_crypto_init().
1062 * It is implementation-dependent whether a failure to initialize
1063 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001064 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001065psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1066 uint8_t *hash,
1067 size_t hash_size,
1068 size_t *hash_length);
1069
Gilles Peskine308b91d2018-02-08 09:47:44 +01001070/** Finish the calculation of the hash of a message and compare it with
1071 * an expected value.
1072 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001073 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 * This function calculates the hash of the message formed by concatenating
1075 * the inputs passed to preceding calls to psa_hash_update(). It then
1076 * compares the calculated hash with the expected hash passed as a
1077 * parameter to this function.
1078 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001079 * When this function returns successfuly, the operation becomes inactive.
1080 * If this function returns an error status, the operation enters an error
1081 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082 *
Gilles Peskine19067982018-03-20 17:54:53 +01001083 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001084 * comparison between the actual hash and the expected hash is performed
1085 * in constant time.
1086 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001087 * \param[in,out] operation Active hash operation.
1088 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001089 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090 *
Gilles Peskine28538492018-07-11 17:34:00 +02001091 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001092 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001093 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 * The hash of the message was calculated successfully, but it
1095 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001096 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001097 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001098 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1099 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1100 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001101 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001102 * \retval #PSA_ERROR_BAD_STATE
1103 * The library has not been previously initialized by psa_crypto_init().
1104 * It is implementation-dependent whether a failure to initialize
1105 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001106 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001107psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1108 const uint8_t *hash,
1109 size_t hash_length);
1110
Gilles Peskine308b91d2018-02-08 09:47:44 +01001111/** Abort a hash operation.
1112 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001113 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001114 * \p operation structure itself. Once aborted, the operation object
1115 * can be reused for another operation by calling
1116 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001117 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001118 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001119 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001120 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001121 * In particular, calling psa_hash_abort() after the operation has been
1122 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1123 * psa_hash_verify() is safe and has no effect.
1124 *
1125 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001126 *
Gilles Peskine28538492018-07-11 17:34:00 +02001127 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001128 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1129 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001130 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001131 * \retval #PSA_ERROR_BAD_STATE
1132 * The library has not been previously initialized by psa_crypto_init().
1133 * It is implementation-dependent whether a failure to initialize
1134 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001135 */
1136psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001137
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001138/** Clone a hash operation.
1139 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001140 * This function copies the state of an ongoing hash operation to
1141 * a new operation object. In other words, this function is equivalent
1142 * to calling psa_hash_setup() on \p target_operation with the same
1143 * algorithm that \p source_operation was set up for, then
1144 * psa_hash_update() on \p target_operation with the same input that
1145 * that was passed to \p source_operation. After this function returns, the
1146 * two objects are independent, i.e. subsequent calls involving one of
1147 * the objects do not affect the other object.
1148 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001149 * \param[in] source_operation The active hash operation to clone.
1150 * \param[in,out] target_operation The operation object to set up.
1151 * It must be initialized but not active.
1152 *
1153 * \retval #PSA_SUCCESS
1154 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001155 * The \p source_operation state is not valid (it must be active).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001156 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001157 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001158 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1159 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001160 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001161 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001162 * \retval #PSA_ERROR_BAD_STATE
1163 * The library has not been previously initialized by psa_crypto_init().
1164 * It is implementation-dependent whether a failure to initialize
1165 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001166 */
1167psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1168 psa_hash_operation_t *target_operation);
1169
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001170/**@}*/
1171
Gilles Peskine8c9def32018-02-08 10:02:12 +01001172/** \defgroup MAC Message authentication codes
1173 * @{
1174 */
1175
Gilles Peskine69647a42019-01-14 20:18:12 +01001176/** Calculate the MAC (message authentication code) of a message.
1177 *
1178 * \note To verify the MAC of a message against an
1179 * expected value, use psa_mac_verify() instead.
1180 * Beware that comparing integrity or authenticity data such as
1181 * MAC values with a function such as \c memcmp is risky
1182 * because the time taken by the comparison may leak information
1183 * about the MAC value which could allow an attacker to guess
1184 * a valid MAC and thereby bypass security controls.
1185 *
1186 * \param handle Handle to the key to use for the operation.
1187 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001188 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001189 * \param[in] input Buffer containing the input message.
1190 * \param input_length Size of the \p input buffer in bytes.
1191 * \param[out] mac Buffer where the MAC value is to be written.
1192 * \param mac_size Size of the \p mac buffer in bytes.
1193 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001194 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001195 *
1196 * \retval #PSA_SUCCESS
1197 * Success.
1198 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001199 * \retval #PSA_ERROR_NOT_PERMITTED
1200 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001201 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001202 * \retval #PSA_ERROR_NOT_SUPPORTED
1203 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001204 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1205 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001206 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1207 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1208 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001209 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001210 * \retval #PSA_ERROR_STORAGE_FAILURE
1211 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001212 * \retval #PSA_ERROR_BAD_STATE
1213 * The library has not been previously initialized by psa_crypto_init().
1214 * It is implementation-dependent whether a failure to initialize
1215 * results in this error code.
1216 */
1217psa_status_t psa_mac_compute(psa_key_handle_t handle,
1218 psa_algorithm_t alg,
1219 const uint8_t *input,
1220 size_t input_length,
1221 uint8_t *mac,
1222 size_t mac_size,
1223 size_t *mac_length);
1224
1225/** Calculate the MAC of a message and compare it with a reference value.
1226 *
1227 * \param handle Handle to the key to use for the operation.
1228 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001229 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001230 * \param[in] input Buffer containing the input message.
1231 * \param input_length Size of the \p input buffer in bytes.
1232 * \param[out] mac Buffer containing the expected MAC value.
1233 * \param mac_length Size of the \p mac buffer in bytes.
1234 *
1235 * \retval #PSA_SUCCESS
1236 * The expected MAC is identical to the actual MAC of the input.
1237 * \retval #PSA_ERROR_INVALID_SIGNATURE
1238 * The MAC of the message was calculated successfully, but it
1239 * differs from the expected value.
1240 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001241 * \retval #PSA_ERROR_NOT_PERMITTED
1242 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001243 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001244 * \retval #PSA_ERROR_NOT_SUPPORTED
1245 * \p alg is not supported or is not a MAC algorithm.
1246 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1247 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1248 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001249 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001250 * \retval #PSA_ERROR_STORAGE_FAILURE
1251 * The key could not be retrieved from storage.
1252 * \retval #PSA_ERROR_BAD_STATE
1253 * The library has not been previously initialized by psa_crypto_init().
1254 * It is implementation-dependent whether a failure to initialize
1255 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001256 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001257psa_status_t psa_mac_verify(psa_key_handle_t handle,
1258 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001259 const uint8_t *input,
1260 size_t input_length,
1261 const uint8_t *mac,
1262 const size_t mac_length);
1263
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001264/** The type of the state data structure for multipart MAC operations.
1265 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001266 * Before calling any function on a MAC operation object, the application must
1267 * initialize it by any of the following means:
1268 * - Set the structure to all-bits-zero, for example:
1269 * \code
1270 * psa_mac_operation_t operation;
1271 * memset(&operation, 0, sizeof(operation));
1272 * \endcode
1273 * - Initialize the structure to logical zero values, for example:
1274 * \code
1275 * psa_mac_operation_t operation = {0};
1276 * \endcode
1277 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1278 * for example:
1279 * \code
1280 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1281 * \endcode
1282 * - Assign the result of the function psa_mac_operation_init()
1283 * to the structure, for example:
1284 * \code
1285 * psa_mac_operation_t operation;
1286 * operation = psa_mac_operation_init();
1287 * \endcode
1288 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001289 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001290 * make any assumptions about the content of this structure except
1291 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001292typedef struct psa_mac_operation_s psa_mac_operation_t;
1293
Jaeden Amero769ce272019-01-04 11:48:03 +00001294/** \def PSA_MAC_OPERATION_INIT
1295 *
1296 * This macro returns a suitable initializer for a MAC operation object of type
1297 * #psa_mac_operation_t.
1298 */
1299#ifdef __DOXYGEN_ONLY__
1300/* This is an example definition for documentation purposes.
1301 * Implementations should define a suitable value in `crypto_struct.h`.
1302 */
1303#define PSA_MAC_OPERATION_INIT {0}
1304#endif
1305
1306/** Return an initial value for a MAC operation object.
1307 */
1308static psa_mac_operation_t psa_mac_operation_init(void);
1309
Gilles Peskinef45adda2019-01-14 18:29:18 +01001310/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001311 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001312 * This function sets up the calculation of the MAC
1313 * (message authentication code) of a byte string.
1314 * To verify the MAC of a message against an
1315 * expected value, use psa_mac_verify_setup() instead.
1316 *
1317 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001318 * -# Allocate an operation object which will be passed to all the functions
1319 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001320 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001321 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001322 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001323 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1324 * of the message each time. The MAC that is calculated is the MAC
1325 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001326 * -# At the end of the message, call psa_mac_sign_finish() to finish
1327 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001328 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001329 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1330 * operation will need to be reset by a call to psa_mac_abort(). The
1331 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001332 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001333 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001334 * After a successful call to psa_mac_sign_setup(), the application must
1335 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001336 * - A successful call to psa_mac_sign_finish().
1337 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001338 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001339 * \param[in,out] operation The operation object to set up. It must have
1340 * been initialized as per the documentation for
1341 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001342 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001343 * It must remain valid until the operation
1344 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001345 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001346 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001347 *
Gilles Peskine28538492018-07-11 17:34:00 +02001348 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001349 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001350 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001351 * \retval #PSA_ERROR_NOT_PERMITTED
1352 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001353 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001354 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001355 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001356 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1357 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1358 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001359 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001360 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001361 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001362 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001363 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001364 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001365 * The library has not been previously initialized by psa_crypto_init().
1366 * It is implementation-dependent whether a failure to initialize
1367 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001368 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001369psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001370 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001371 psa_algorithm_t alg);
1372
Gilles Peskinef45adda2019-01-14 18:29:18 +01001373/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001374 *
1375 * This function sets up the verification of the MAC
1376 * (message authentication code) of a byte string against an expected value.
1377 *
1378 * The sequence of operations to verify a MAC is as follows:
1379 * -# Allocate an operation object which will be passed to all the functions
1380 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001381 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001382 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001383 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001384 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1385 * of the message each time. The MAC that is calculated is the MAC
1386 * of the concatenation of these messages in order.
1387 * -# At the end of the message, call psa_mac_verify_finish() to finish
1388 * calculating the actual MAC of the message and verify it against
1389 * the expected value.
1390 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001391 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1392 * operation will need to be reset by a call to psa_mac_abort(). The
1393 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001394 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001395 *
1396 * After a successful call to psa_mac_verify_setup(), the application must
1397 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001398 * - A successful call to psa_mac_verify_finish().
1399 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001400 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001401 * \param[in,out] operation The operation object to set up. It must have
1402 * been initialized as per the documentation for
1403 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001404 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001405 * It must remain valid until the operation
1406 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001407 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1408 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001409 *
Gilles Peskine28538492018-07-11 17:34:00 +02001410 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001411 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001412 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001413 * \retval #PSA_ERROR_NOT_PERMITTED
1414 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001415 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001416 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001417 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001418 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1419 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1420 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001421 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001422 * \retval #PSA_ERROR_STORAGE_FAILURE
1423 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001424 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001425 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001426 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001427 * The library has not been previously initialized by psa_crypto_init().
1428 * It is implementation-dependent whether a failure to initialize
1429 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001430 */
1431psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001432 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001433 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001434
Gilles Peskinedcd14942018-07-12 00:30:52 +02001435/** Add a message fragment to a multipart MAC operation.
1436 *
1437 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1438 * before calling this function.
1439 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001440 * If this function returns an error status, the operation enters an error
1441 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001442 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001443 * \param[in,out] operation Active MAC operation.
1444 * \param[in] input Buffer containing the message fragment to add to
1445 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001446 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001447 *
1448 * \retval #PSA_SUCCESS
1449 * Success.
1450 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001451 * The operation state is not valid (it must be active).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001452 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1453 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1454 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001455 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001456 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001457 * \retval #PSA_ERROR_BAD_STATE
1458 * The library has not been previously initialized by psa_crypto_init().
1459 * It is implementation-dependent whether a failure to initialize
1460 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001461 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001462psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1463 const uint8_t *input,
1464 size_t input_length);
1465
Gilles Peskinedcd14942018-07-12 00:30:52 +02001466/** Finish the calculation of the MAC of a message.
1467 *
1468 * The application must call psa_mac_sign_setup() before calling this function.
1469 * This function calculates the MAC of the message formed by concatenating
1470 * the inputs passed to preceding calls to psa_mac_update().
1471 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001472 * When this function returns successfuly, the operation becomes inactive.
1473 * If this function returns an error status, the operation enters an error
1474 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001475 *
1476 * \warning Applications should not call this function if they expect
1477 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1478 * Beware that comparing integrity or authenticity data such as
1479 * MAC values with a function such as \c memcmp is risky
1480 * because the time taken by the comparison may leak information
1481 * about the MAC value which could allow an attacker to guess
1482 * a valid MAC and thereby bypass security controls.
1483 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001484 * \param[in,out] operation Active MAC operation.
1485 * \param[out] mac Buffer where the MAC value is to be written.
1486 * \param mac_size Size of the \p mac buffer in bytes.
1487 * \param[out] mac_length On success, the number of bytes
1488 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001489 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001490 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001491 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001492 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001493 *
1494 * \retval #PSA_SUCCESS
1495 * Success.
1496 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001497 * The operation state is not valid (it must be an active mac sign
1498 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001499 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001500 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001501 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1502 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1503 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1504 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001505 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001506 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001507 * \retval #PSA_ERROR_BAD_STATE
1508 * The library has not been previously initialized by psa_crypto_init().
1509 * It is implementation-dependent whether a failure to initialize
1510 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001511 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001512psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1513 uint8_t *mac,
1514 size_t mac_size,
1515 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001516
Gilles Peskinedcd14942018-07-12 00:30:52 +02001517/** Finish the calculation of the MAC of a message and compare it with
1518 * an expected value.
1519 *
1520 * The application must call psa_mac_verify_setup() before calling this function.
1521 * This function calculates the MAC of the message formed by concatenating
1522 * the inputs passed to preceding calls to psa_mac_update(). It then
1523 * compares the calculated MAC with the expected MAC passed as a
1524 * parameter to this function.
1525 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001526 * When this function returns successfuly, the operation becomes inactive.
1527 * If this function returns an error status, the operation enters an error
1528 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001529 *
1530 * \note Implementations shall make the best effort to ensure that the
1531 * comparison between the actual MAC and the expected MAC is performed
1532 * in constant time.
1533 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001534 * \param[in,out] operation Active MAC operation.
1535 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001536 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001537 *
1538 * \retval #PSA_SUCCESS
1539 * The expected MAC is identical to the actual MAC of the message.
1540 * \retval #PSA_ERROR_INVALID_SIGNATURE
1541 * The MAC of the message was calculated successfully, but it
1542 * differs from the expected MAC.
1543 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001544 * The operation state is not valid (it must be an active mac verify
1545 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001546 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1547 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1548 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001549 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001550 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001551 * \retval #PSA_ERROR_BAD_STATE
1552 * The library has not been previously initialized by psa_crypto_init().
1553 * It is implementation-dependent whether a failure to initialize
1554 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001555 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001556psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1557 const uint8_t *mac,
1558 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001559
Gilles Peskinedcd14942018-07-12 00:30:52 +02001560/** Abort a MAC operation.
1561 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001563 * \p operation structure itself. Once aborted, the operation object
1564 * can be reused for another operation by calling
1565 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001566 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001567 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001568 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001569 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001570 * In particular, calling psa_mac_abort() after the operation has been
1571 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1572 * psa_mac_verify_finish() is safe and has no effect.
1573 *
1574 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001575 *
1576 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001577 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1578 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001579 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001580 * \retval #PSA_ERROR_BAD_STATE
1581 * The library has not been previously initialized by psa_crypto_init().
1582 * It is implementation-dependent whether a failure to initialize
1583 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001584 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001585psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1586
1587/**@}*/
1588
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001589/** \defgroup cipher Symmetric ciphers
1590 * @{
1591 */
1592
Gilles Peskine69647a42019-01-14 20:18:12 +01001593/** Encrypt a message using a symmetric cipher.
1594 *
1595 * This function encrypts a message with a random IV (initialization
1596 * vector).
1597 *
1598 * \param handle Handle to the key to use for the operation.
1599 * It must remain valid until the operation
1600 * terminates.
1601 * \param alg The cipher algorithm to compute
1602 * (\c PSA_ALG_XXX value such that
1603 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1604 * \param[in] input Buffer containing the message to encrypt.
1605 * \param input_length Size of the \p input buffer in bytes.
1606 * \param[out] output Buffer where the output is to be written.
1607 * The output contains the IV followed by
1608 * the ciphertext proper.
1609 * \param output_size Size of the \p output buffer in bytes.
1610 * \param[out] output_length On success, the number of bytes
1611 * that make up the output.
1612 *
1613 * \retval #PSA_SUCCESS
1614 * Success.
1615 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001616 * \retval #PSA_ERROR_NOT_PERMITTED
1617 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001618 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001619 * \retval #PSA_ERROR_NOT_SUPPORTED
1620 * \p alg is not supported or is not a cipher algorithm.
1621 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1622 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1623 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1624 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001625 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001626 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001627 * \retval #PSA_ERROR_BAD_STATE
1628 * The library has not been previously initialized by psa_crypto_init().
1629 * It is implementation-dependent whether a failure to initialize
1630 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001631 */
1632psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1633 psa_algorithm_t alg,
1634 const uint8_t *input,
1635 size_t input_length,
1636 uint8_t *output,
1637 size_t output_size,
1638 size_t *output_length);
1639
1640/** Decrypt a message using a symmetric cipher.
1641 *
1642 * This function decrypts a message encrypted with a symmetric cipher.
1643 *
1644 * \param handle Handle to the key to use for the operation.
1645 * It must remain valid until the operation
1646 * terminates.
1647 * \param alg The cipher algorithm to compute
1648 * (\c PSA_ALG_XXX value such that
1649 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1650 * \param[in] input Buffer containing the message to decrypt.
1651 * This consists of the IV followed by the
1652 * ciphertext proper.
1653 * \param input_length Size of the \p input buffer in bytes.
1654 * \param[out] output Buffer where the plaintext is to be written.
1655 * \param output_size Size of the \p output buffer in bytes.
1656 * \param[out] output_length On success, the number of bytes
1657 * that make up the output.
1658 *
1659 * \retval #PSA_SUCCESS
1660 * Success.
1661 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001662 * \retval #PSA_ERROR_NOT_PERMITTED
1663 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001664 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001665 * \retval #PSA_ERROR_NOT_SUPPORTED
1666 * \p alg is not supported or is not a cipher algorithm.
1667 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1668 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1669 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1670 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001671 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001672 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001673 * \retval #PSA_ERROR_BAD_STATE
1674 * The library has not been previously initialized by psa_crypto_init().
1675 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001676 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001677 */
1678psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1679 psa_algorithm_t alg,
1680 const uint8_t *input,
1681 size_t input_length,
1682 uint8_t *output,
1683 size_t output_size,
1684 size_t *output_length);
1685
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001686/** The type of the state data structure for multipart cipher operations.
1687 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001688 * Before calling any function on a cipher operation object, the application
1689 * must initialize it by any of the following means:
1690 * - Set the structure to all-bits-zero, for example:
1691 * \code
1692 * psa_cipher_operation_t operation;
1693 * memset(&operation, 0, sizeof(operation));
1694 * \endcode
1695 * - Initialize the structure to logical zero values, for example:
1696 * \code
1697 * psa_cipher_operation_t operation = {0};
1698 * \endcode
1699 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1700 * for example:
1701 * \code
1702 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1703 * \endcode
1704 * - Assign the result of the function psa_cipher_operation_init()
1705 * to the structure, for example:
1706 * \code
1707 * psa_cipher_operation_t operation;
1708 * operation = psa_cipher_operation_init();
1709 * \endcode
1710 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001711 * This is an implementation-defined \c struct. Applications should not
1712 * make any assumptions about the content of this structure except
1713 * as directed by the documentation of a specific implementation. */
1714typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1715
Jaeden Amero5bae2272019-01-04 11:48:27 +00001716/** \def PSA_CIPHER_OPERATION_INIT
1717 *
1718 * This macro returns a suitable initializer for a cipher operation object of
1719 * type #psa_cipher_operation_t.
1720 */
1721#ifdef __DOXYGEN_ONLY__
1722/* This is an example definition for documentation purposes.
1723 * Implementations should define a suitable value in `crypto_struct.h`.
1724 */
1725#define PSA_CIPHER_OPERATION_INIT {0}
1726#endif
1727
1728/** Return an initial value for a cipher operation object.
1729 */
1730static psa_cipher_operation_t psa_cipher_operation_init(void);
1731
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001732/** Set the key for a multipart symmetric encryption operation.
1733 *
1734 * The sequence of operations to encrypt a message with a symmetric cipher
1735 * is as follows:
1736 * -# Allocate an operation object which will be passed to all the functions
1737 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001738 * -# Initialize the operation object with one of the methods described in the
1739 * documentation for #psa_cipher_operation_t, e.g.
1740 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001741 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001742 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001743 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001744 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001745 * requires a specific IV value.
1746 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1747 * of the message each time.
1748 * -# Call psa_cipher_finish().
1749 *
1750 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001751 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001752 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001753 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001754 * eventually terminate the operation. The following events terminate an
1755 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001756 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001757 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001758 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001759 * \param[in,out] operation The operation object to set up. It must have
1760 * been initialized as per the documentation for
1761 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001762 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001763 * It must remain valid until the operation
1764 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001765 * \param alg The cipher algorithm to compute
1766 * (\c PSA_ALG_XXX value such that
1767 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001768 *
Gilles Peskine28538492018-07-11 17:34:00 +02001769 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001770 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001771 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001772 * \retval #PSA_ERROR_NOT_PERMITTED
1773 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001774 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001775 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001776 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001777 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1778 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1779 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001780 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001781 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001782 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001783 * The operation state is not valid (already set up and not
1784 * subsequently completed).
1785 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001786 * The library has not been previously initialized by psa_crypto_init().
1787 * It is implementation-dependent whether a failure to initialize
1788 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001789 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001790psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001791 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001792 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001793
1794/** Set the key for a multipart symmetric decryption operation.
1795 *
1796 * The sequence of operations to decrypt a message with a symmetric cipher
1797 * is as follows:
1798 * -# Allocate an operation object which will be passed to all the functions
1799 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001800 * -# Initialize the operation object with one of the methods described in the
1801 * documentation for #psa_cipher_operation_t, e.g.
1802 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001803 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001804 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001805 * decryption. If the IV is prepended to the ciphertext, you can call
1806 * psa_cipher_update() on a buffer containing the IV followed by the
1807 * beginning of the message.
1808 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1809 * of the message each time.
1810 * -# Call psa_cipher_finish().
1811 *
1812 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001813 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001814 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001815 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001816 * eventually terminate the operation. The following events terminate an
1817 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001818 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001819 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001820 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001821 * \param[in,out] operation The operation object to set up. It must have
1822 * been initialized as per the documentation for
1823 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001824 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001825 * It must remain valid until the operation
1826 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001827 * \param alg The cipher algorithm to compute
1828 * (\c PSA_ALG_XXX value such that
1829 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001830 *
Gilles Peskine28538492018-07-11 17:34:00 +02001831 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001832 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001833 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001834 * \retval #PSA_ERROR_NOT_PERMITTED
1835 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001836 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001837 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001838 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001839 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1840 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1841 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001842 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001843 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001844 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001845 * The operation state is not valid (already set up and not
1846 * subsequently completed).
1847 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001848 * The library has not been previously initialized by psa_crypto_init().
1849 * It is implementation-dependent whether a failure to initialize
1850 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001851 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001852psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001853 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001854 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001855
Gilles Peskinedcd14942018-07-12 00:30:52 +02001856/** Generate an IV for a symmetric encryption operation.
1857 *
1858 * This function generates a random IV (initialization vector), nonce
1859 * or initial counter value for the encryption operation as appropriate
1860 * for the chosen algorithm, key type and key size.
1861 *
1862 * The application must call psa_cipher_encrypt_setup() before
1863 * calling this function.
1864 *
1865 * If this function returns an error status, the operation becomes inactive.
1866 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001867 * \param[in,out] operation Active cipher operation.
1868 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001869 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001870 * \param[out] iv_length On success, the number of bytes of the
1871 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001872 *
1873 * \retval #PSA_SUCCESS
1874 * Success.
1875 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001876 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001877 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001878 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001879 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1880 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1881 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001882 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001883 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001884 * \retval #PSA_ERROR_BAD_STATE
1885 * The library has not been previously initialized by psa_crypto_init().
1886 * It is implementation-dependent whether a failure to initialize
1887 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001888 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001889psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001890 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001891 size_t iv_size,
1892 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001893
Gilles Peskinedcd14942018-07-12 00:30:52 +02001894/** Set the IV for a symmetric encryption or decryption operation.
1895 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001896 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001897 * or initial counter value for the encryption or decryption operation.
1898 *
1899 * The application must call psa_cipher_encrypt_setup() before
1900 * calling this function.
1901 *
1902 * If this function returns an error status, the operation becomes inactive.
1903 *
1904 * \note When encrypting, applications should use psa_cipher_generate_iv()
1905 * instead of this function, unless implementing a protocol that requires
1906 * a non-random IV.
1907 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001908 * \param[in,out] operation Active cipher operation.
1909 * \param[in] iv Buffer containing the IV to use.
1910 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001911 *
1912 * \retval #PSA_SUCCESS
1913 * Success.
1914 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001915 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001916 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001917 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001918 * or the chosen algorithm does not use an IV.
1919 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1920 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1921 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001922 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001923 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001924 * \retval #PSA_ERROR_BAD_STATE
1925 * The library has not been previously initialized by psa_crypto_init().
1926 * It is implementation-dependent whether a failure to initialize
1927 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001928 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001929psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001930 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001931 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001932
Gilles Peskinedcd14942018-07-12 00:30:52 +02001933/** Encrypt or decrypt a message fragment in an active cipher operation.
1934 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001935 * Before calling this function, you must:
1936 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1937 * The choice of setup function determines whether this function
1938 * encrypts or decrypts its input.
1939 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1940 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001941 *
1942 * If this function returns an error status, the operation becomes inactive.
1943 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001944 * \param[in,out] operation Active cipher operation.
1945 * \param[in] input Buffer containing the message fragment to
1946 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001947 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001948 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001949 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001950 * \param[out] output_length On success, the number of bytes
1951 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001952 *
1953 * \retval #PSA_SUCCESS
1954 * Success.
1955 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001956 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001957 * not set, or already completed).
1958 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1959 * The size of the \p output buffer is too small.
1960 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1961 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1962 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001963 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001964 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001965 * \retval #PSA_ERROR_BAD_STATE
1966 * The library has not been previously initialized by psa_crypto_init().
1967 * It is implementation-dependent whether a failure to initialize
1968 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001969 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001970psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1971 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001972 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001973 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001974 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001975 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001976
Gilles Peskinedcd14942018-07-12 00:30:52 +02001977/** Finish encrypting or decrypting a message in a cipher operation.
1978 *
1979 * The application must call psa_cipher_encrypt_setup() or
1980 * psa_cipher_decrypt_setup() before calling this function. The choice
1981 * of setup function determines whether this function encrypts or
1982 * decrypts its input.
1983 *
1984 * This function finishes the encryption or decryption of the message
1985 * formed by concatenating the inputs passed to preceding calls to
1986 * psa_cipher_update().
1987 *
1988 * When this function returns, the operation becomes inactive.
1989 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001990 * \param[in,out] operation Active cipher operation.
1991 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001992 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001993 * \param[out] output_length On success, the number of bytes
1994 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001995 *
1996 * \retval #PSA_SUCCESS
1997 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02001998 * \retval #PSA_ERROR_INVALID_ARGUMENT
1999 * The total input size passed to this operation is not valid for
2000 * this particular algorithm. For example, the algorithm is a based
2001 * on block cipher and requires a whole number of blocks, but the
2002 * total input size is not a multiple of the block size.
2003 * \retval #PSA_ERROR_INVALID_PADDING
2004 * This is a decryption operation for an algorithm that includes
2005 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002006 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002007 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002008 * not set, or already completed).
2009 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2010 * The size of the \p output buffer is too small.
2011 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2012 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2013 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002014 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002015 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002016 * \retval #PSA_ERROR_BAD_STATE
2017 * The library has not been previously initialized by psa_crypto_init().
2018 * It is implementation-dependent whether a failure to initialize
2019 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002020 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002021psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002022 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002023 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002024 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002025
Gilles Peskinedcd14942018-07-12 00:30:52 +02002026/** Abort a cipher operation.
2027 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002028 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002029 * \p operation structure itself. Once aborted, the operation object
2030 * can be reused for another operation by calling
2031 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002032 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002033 * You may call this function any time after the operation object has
2034 * been initialized by any of the following methods:
2035 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2036 * whether it succeeds or not.
2037 * - Initializing the \c struct to all-bits-zero.
2038 * - Initializing the \c struct to logical zeros, e.g.
2039 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002040 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002041 * In particular, calling psa_cipher_abort() after the operation has been
2042 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2043 * is safe and has no effect.
2044 *
2045 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002046 *
2047 * \retval #PSA_SUCCESS
2048 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002049 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002050 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2051 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002052 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002053 * \retval #PSA_ERROR_BAD_STATE
2054 * The library has not been previously initialized by psa_crypto_init().
2055 * It is implementation-dependent whether a failure to initialize
2056 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002057 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002058psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2059
2060/**@}*/
2061
Gilles Peskine3b555712018-03-03 21:27:57 +01002062/** \defgroup aead Authenticated encryption with associated data (AEAD)
2063 * @{
2064 */
2065
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002066/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002067 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002068 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002069 * \param alg The AEAD algorithm to compute
2070 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002071 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002072 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002073 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002074 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002075 * but not encrypted.
2076 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002077 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002078 * encrypted.
2079 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002080 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002081 * encrypted data. The additional data is not
2082 * part of this output. For algorithms where the
2083 * encrypted data and the authentication tag
2084 * are defined as separate outputs, the
2085 * authentication tag is appended to the
2086 * encrypted data.
2087 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2088 * This must be at least
2089 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2090 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002091 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002092 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002093 *
Gilles Peskine28538492018-07-11 17:34:00 +02002094 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002095 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002096 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002097 * \retval #PSA_ERROR_NOT_PERMITTED
2098 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002099 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002100 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002101 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002102 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002103 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2104 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002105 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2106 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002107 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002108 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002109 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002110 * The library has not been previously initialized by psa_crypto_init().
2111 * It is implementation-dependent whether a failure to initialize
2112 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002113 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002114psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002115 psa_algorithm_t alg,
2116 const uint8_t *nonce,
2117 size_t nonce_length,
2118 const uint8_t *additional_data,
2119 size_t additional_data_length,
2120 const uint8_t *plaintext,
2121 size_t plaintext_length,
2122 uint8_t *ciphertext,
2123 size_t ciphertext_size,
2124 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002125
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002126/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002127 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002128 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002129 * \param alg The AEAD algorithm to compute
2130 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002131 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002132 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002133 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002134 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002135 * but not encrypted.
2136 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002137 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002138 * encrypted. For algorithms where the
2139 * encrypted data and the authentication tag
2140 * are defined as separate inputs, the buffer
2141 * must contain the encrypted data followed
2142 * by the authentication tag.
2143 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002144 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002145 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2146 * This must be at least
2147 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2148 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002149 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002150 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002151 *
Gilles Peskine28538492018-07-11 17:34:00 +02002152 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002153 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002154 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002155 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002156 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002157 * \retval #PSA_ERROR_NOT_PERMITTED
2158 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002159 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002160 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002161 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002162 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002163 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2164 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002165 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2166 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002167 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002168 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002169 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002170 * The library has not been previously initialized by psa_crypto_init().
2171 * It is implementation-dependent whether a failure to initialize
2172 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002173 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002174psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002175 psa_algorithm_t alg,
2176 const uint8_t *nonce,
2177 size_t nonce_length,
2178 const uint8_t *additional_data,
2179 size_t additional_data_length,
2180 const uint8_t *ciphertext,
2181 size_t ciphertext_length,
2182 uint8_t *plaintext,
2183 size_t plaintext_size,
2184 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002185
Gilles Peskine30a9e412019-01-14 18:36:12 +01002186/** The type of the state data structure for multipart AEAD operations.
2187 *
2188 * Before calling any function on an AEAD operation object, the application
2189 * must initialize it by any of the following means:
2190 * - Set the structure to all-bits-zero, for example:
2191 * \code
2192 * psa_aead_operation_t operation;
2193 * memset(&operation, 0, sizeof(operation));
2194 * \endcode
2195 * - Initialize the structure to logical zero values, for example:
2196 * \code
2197 * psa_aead_operation_t operation = {0};
2198 * \endcode
2199 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2200 * for example:
2201 * \code
2202 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2203 * \endcode
2204 * - Assign the result of the function psa_aead_operation_init()
2205 * to the structure, for example:
2206 * \code
2207 * psa_aead_operation_t operation;
2208 * operation = psa_aead_operation_init();
2209 * \endcode
2210 *
2211 * This is an implementation-defined \c struct. Applications should not
2212 * make any assumptions about the content of this structure except
2213 * as directed by the documentation of a specific implementation. */
2214typedef struct psa_aead_operation_s psa_aead_operation_t;
2215
2216/** \def PSA_AEAD_OPERATION_INIT
2217 *
2218 * This macro returns a suitable initializer for an AEAD operation object of
2219 * type #psa_aead_operation_t.
2220 */
2221#ifdef __DOXYGEN_ONLY__
2222/* This is an example definition for documentation purposes.
2223 * Implementations should define a suitable value in `crypto_struct.h`.
2224 */
2225#define PSA_AEAD_OPERATION_INIT {0}
2226#endif
2227
2228/** Return an initial value for an AEAD operation object.
2229 */
2230static psa_aead_operation_t psa_aead_operation_init(void);
2231
2232/** Set the key for a multipart authenticated encryption operation.
2233 *
2234 * The sequence of operations to encrypt a message with authentication
2235 * is as follows:
2236 * -# Allocate an operation object which will be passed to all the functions
2237 * listed here.
2238 * -# Initialize the operation object with one of the methods described in the
2239 * documentation for #psa_aead_operation_t, e.g.
2240 * PSA_AEAD_OPERATION_INIT.
2241 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002242 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2243 * inputs to the subsequent calls to psa_aead_update_ad() and
2244 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2245 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002246 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2247 * generate or set the nonce. You should use
2248 * psa_aead_generate_nonce() unless the protocol you are implementing
2249 * requires a specific nonce value.
2250 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2251 * of the non-encrypted additional authenticated data each time.
2252 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002253 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002254 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002255 *
2256 * The application may call psa_aead_abort() at any time after the operation
2257 * has been initialized.
2258 *
2259 * After a successful call to psa_aead_encrypt_setup(), the application must
2260 * eventually terminate the operation. The following events terminate an
2261 * operation:
2262 * - A failed call to any of the \c psa_aead_xxx functions.
2263 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2264 *
2265 * \param[in,out] operation The operation object to set up. It must have
2266 * been initialized as per the documentation for
2267 * #psa_aead_operation_t and not yet in use.
2268 * \param handle Handle to the key to use for the operation.
2269 * It must remain valid until the operation
2270 * terminates.
2271 * \param alg The AEAD algorithm to compute
2272 * (\c PSA_ALG_XXX value such that
2273 * #PSA_ALG_IS_AEAD(\p alg) is true).
2274 *
2275 * \retval #PSA_SUCCESS
2276 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002277 * \retval #PSA_ERROR_BAD_STATE
2278 * The operation state is not valid (already set up and not
2279 * subsequently completed).
2280 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002281 * \retval #PSA_ERROR_NOT_PERMITTED
2282 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002283 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002284 * \retval #PSA_ERROR_NOT_SUPPORTED
2285 * \p alg is not supported or is not an AEAD algorithm.
2286 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2287 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2288 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002289 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002290 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002291 * \retval #PSA_ERROR_BAD_STATE
2292 * The library has not been previously initialized by psa_crypto_init().
2293 * It is implementation-dependent whether a failure to initialize
2294 * results in this error code.
2295 */
2296psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2297 psa_key_handle_t handle,
2298 psa_algorithm_t alg);
2299
2300/** Set the key for a multipart authenticated decryption operation.
2301 *
2302 * The sequence of operations to decrypt a message with authentication
2303 * is as follows:
2304 * -# Allocate an operation object which will be passed to all the functions
2305 * listed here.
2306 * -# Initialize the operation object with one of the methods described in the
2307 * documentation for #psa_aead_operation_t, e.g.
2308 * PSA_AEAD_OPERATION_INIT.
2309 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002310 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2311 * inputs to the subsequent calls to psa_aead_update_ad() and
2312 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2313 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002314 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2315 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2316 * of the non-encrypted additional authenticated data each time.
2317 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002318 * of the ciphertext to decrypt each time.
2319 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002320 *
2321 * The application may call psa_aead_abort() at any time after the operation
2322 * has been initialized.
2323 *
2324 * After a successful call to psa_aead_decrypt_setup(), the application must
2325 * eventually terminate the operation. The following events terminate an
2326 * operation:
2327 * - A failed call to any of the \c psa_aead_xxx functions.
2328 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2329 *
2330 * \param[in,out] operation The operation object to set up. It must have
2331 * been initialized as per the documentation for
2332 * #psa_aead_operation_t and not yet in use.
2333 * \param handle Handle to the key to use for the operation.
2334 * It must remain valid until the operation
2335 * terminates.
2336 * \param alg The AEAD algorithm to compute
2337 * (\c PSA_ALG_XXX value such that
2338 * #PSA_ALG_IS_AEAD(\p alg) is true).
2339 *
2340 * \retval #PSA_SUCCESS
2341 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002342 * \retval #PSA_ERROR_BAD_STATE
2343 * The operation state is not valid (already set up and not
2344 * subsequently completed).
2345 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002346 * \retval #PSA_ERROR_NOT_PERMITTED
2347 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002348 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002349 * \retval #PSA_ERROR_NOT_SUPPORTED
2350 * \p alg is not supported or is not an AEAD algorithm.
2351 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2352 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2353 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002354 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002355 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002356 * \retval #PSA_ERROR_BAD_STATE
2357 * The library has not been previously initialized by psa_crypto_init().
2358 * It is implementation-dependent whether a failure to initialize
2359 * results in this error code.
2360 */
2361psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2362 psa_key_handle_t handle,
2363 psa_algorithm_t alg);
2364
2365/** Generate a random nonce for an authenticated encryption operation.
2366 *
2367 * This function generates a random nonce for the authenticated encryption
2368 * operation with an appropriate size for the chosen algorithm, key type
2369 * and key size.
2370 *
2371 * The application must call psa_aead_encrypt_setup() before
2372 * calling this function.
2373 *
2374 * If this function returns an error status, the operation becomes inactive.
2375 *
2376 * \param[in,out] operation Active AEAD operation.
2377 * \param[out] nonce Buffer where the generated nonce is to be
2378 * written.
2379 * \param nonce_size Size of the \p nonce buffer in bytes.
2380 * \param[out] nonce_length On success, the number of bytes of the
2381 * generated nonce.
2382 *
2383 * \retval #PSA_SUCCESS
2384 * Success.
2385 * \retval #PSA_ERROR_BAD_STATE
2386 * The operation state is not valid (not set up, or nonce already set).
2387 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2388 * The size of the \p nonce buffer is too small.
2389 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2390 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2391 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002392 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002393 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002394 * \retval #PSA_ERROR_BAD_STATE
2395 * The library has not been previously initialized by psa_crypto_init().
2396 * It is implementation-dependent whether a failure to initialize
2397 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002398 */
2399psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002400 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002401 size_t nonce_size,
2402 size_t *nonce_length);
2403
2404/** Set the nonce for an authenticated encryption or decryption operation.
2405 *
2406 * This function sets the nonce for the authenticated
2407 * encryption or decryption operation.
2408 *
2409 * The application must call psa_aead_encrypt_setup() before
2410 * calling this function.
2411 *
2412 * If this function returns an error status, the operation becomes inactive.
2413 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002414 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002415 * instead of this function, unless implementing a protocol that requires
2416 * a non-random IV.
2417 *
2418 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002419 * \param[in] nonce Buffer containing the nonce to use.
2420 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002421 *
2422 * \retval #PSA_SUCCESS
2423 * Success.
2424 * \retval #PSA_ERROR_BAD_STATE
2425 * The operation state is not valid (not set up, or nonce already set).
2426 * \retval #PSA_ERROR_INVALID_ARGUMENT
2427 * The size of \p nonce is not acceptable for the chosen algorithm.
2428 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2429 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2430 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002431 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002432 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002433 * \retval #PSA_ERROR_BAD_STATE
2434 * The library has not been previously initialized by psa_crypto_init().
2435 * It is implementation-dependent whether a failure to initialize
2436 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002437 */
2438psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002439 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002440 size_t nonce_length);
2441
Gilles Peskinebc59c852019-01-17 15:26:08 +01002442/** Declare the lengths of the message and additional data for AEAD.
2443 *
2444 * The application must call this function before calling
2445 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2446 * the operation requires it. If the algorithm does not require it,
2447 * calling this function is optional, but if this function is called
2448 * then the implementation must enforce the lengths.
2449 *
2450 * You may call this function before or after setting the nonce with
2451 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2452 *
2453 * - For #PSA_ALG_CCM, calling this function is required.
2454 * - For the other AEAD algorithms defined in this specification, calling
2455 * this function is not required.
2456 * - For vendor-defined algorithm, refer to the vendor documentation.
2457 *
2458 * \param[in,out] operation Active AEAD operation.
2459 * \param ad_length Size of the non-encrypted additional
2460 * authenticated data in bytes.
2461 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2462 *
2463 * \retval #PSA_SUCCESS
2464 * Success.
2465 * \retval #PSA_ERROR_BAD_STATE
2466 * The operation state is not valid (not set up, already completed,
2467 * or psa_aead_update_ad() or psa_aead_update() already called).
2468 * \retval #PSA_ERROR_INVALID_ARGUMENT
2469 * At least one of the lengths is not acceptable for the chosen
2470 * algorithm.
2471 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2472 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2473 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002474 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002475 * \retval #PSA_ERROR_BAD_STATE
2476 * The library has not been previously initialized by psa_crypto_init().
2477 * It is implementation-dependent whether a failure to initialize
2478 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002479 */
2480psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2481 size_t ad_length,
2482 size_t plaintext_length);
2483
Gilles Peskine30a9e412019-01-14 18:36:12 +01002484/** Pass additional data to an active AEAD operation.
2485 *
2486 * Additional data is authenticated, but not encrypted.
2487 *
2488 * You may call this function multiple times to pass successive fragments
2489 * of the additional data. You may not call this function after passing
2490 * data to encrypt or decrypt with psa_aead_update().
2491 *
2492 * Before calling this function, you must:
2493 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2494 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2495 *
2496 * If this function returns an error status, the operation becomes inactive.
2497 *
2498 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2499 * there is no guarantee that the input is valid. Therefore, until
2500 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2501 * treat the input as untrusted and prepare to undo any action that
2502 * depends on the input if psa_aead_verify() returns an error status.
2503 *
2504 * \param[in,out] operation Active AEAD operation.
2505 * \param[in] input Buffer containing the fragment of
2506 * additional data.
2507 * \param input_length Size of the \p input buffer in bytes.
2508 *
2509 * \retval #PSA_SUCCESS
2510 * Success.
2511 * \retval #PSA_ERROR_BAD_STATE
2512 * The operation state is not valid (not set up, nonce not set,
2513 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002514 * \retval #PSA_ERROR_INVALID_ARGUMENT
2515 * The total input length overflows the additional data length that
2516 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002517 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2518 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2519 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002520 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002521 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002522 * \retval #PSA_ERROR_BAD_STATE
2523 * The library has not been previously initialized by psa_crypto_init().
2524 * It is implementation-dependent whether a failure to initialize
2525 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002526 */
2527psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2528 const uint8_t *input,
2529 size_t input_length);
2530
2531/** Encrypt or decrypt a message fragment in an active AEAD operation.
2532 *
2533 * Before calling this function, you must:
2534 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2535 * The choice of setup function determines whether this function
2536 * encrypts or decrypts its input.
2537 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2538 * 3. Call psa_aead_update_ad() to pass all the additional data.
2539 *
2540 * If this function returns an error status, the operation becomes inactive.
2541 *
2542 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2543 * there is no guarantee that the input is valid. Therefore, until
2544 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2545 * - Do not use the output in any way other than storing it in a
2546 * confidential location. If you take any action that depends
2547 * on the tentative decrypted data, this action will need to be
2548 * undone if the input turns out not to be valid. Furthermore,
2549 * if an adversary can observe that this action took place
2550 * (for example through timing), they may be able to use this
2551 * fact as an oracle to decrypt any message encrypted with the
2552 * same key.
2553 * - In particular, do not copy the output anywhere but to a
2554 * memory or storage space that you have exclusive access to.
2555 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002556 * This function does not require the input to be aligned to any
2557 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002558 * a whole block at a time, it must consume all the input provided, but
2559 * it may delay the end of the corresponding output until a subsequent
2560 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2561 * provides sufficient input. The amount of data that can be delayed
2562 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002563 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002564 * \param[in,out] operation Active AEAD operation.
2565 * \param[in] input Buffer containing the message fragment to
2566 * encrypt or decrypt.
2567 * \param input_length Size of the \p input buffer in bytes.
2568 * \param[out] output Buffer where the output is to be written.
2569 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002570 * This must be at least
2571 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2572 * \p input_length) where \c alg is the
2573 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002574 * \param[out] output_length On success, the number of bytes
2575 * that make up the returned output.
2576 *
2577 * \retval #PSA_SUCCESS
2578 * Success.
2579 * \retval #PSA_ERROR_BAD_STATE
2580 * The operation state is not valid (not set up, nonce not set
2581 * or already completed).
2582 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2583 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002584 * You can determine a sufficient buffer size by calling
2585 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2586 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002587 * \retval #PSA_ERROR_INVALID_ARGUMENT
2588 * The total length of input to psa_aead_update_ad() so far is
2589 * less than the additional data length that was previously
2590 * specified with psa_aead_set_lengths().
2591 * \retval #PSA_ERROR_INVALID_ARGUMENT
2592 * The total input length overflows the plaintext length that
2593 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002594 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2595 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2596 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002597 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002598 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002599 * \retval #PSA_ERROR_BAD_STATE
2600 * The library has not been previously initialized by psa_crypto_init().
2601 * It is implementation-dependent whether a failure to initialize
2602 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002603 */
2604psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2605 const uint8_t *input,
2606 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002607 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002608 size_t output_size,
2609 size_t *output_length);
2610
2611/** Finish encrypting a message in an AEAD operation.
2612 *
2613 * The operation must have been set up with psa_aead_encrypt_setup().
2614 *
2615 * This function finishes the authentication of the additional data
2616 * formed by concatenating the inputs passed to preceding calls to
2617 * psa_aead_update_ad() with the plaintext formed by concatenating the
2618 * inputs passed to preceding calls to psa_aead_update().
2619 *
2620 * This function has two output buffers:
2621 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002622 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002623 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002624 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002625 * that the operation performs.
2626 *
2627 * When this function returns, the operation becomes inactive.
2628 *
2629 * \param[in,out] operation Active AEAD operation.
2630 * \param[out] ciphertext Buffer where the last part of the ciphertext
2631 * is to be written.
2632 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002633 * This must be at least
2634 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2635 * \c alg is the algorithm that is being
2636 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002637 * \param[out] ciphertext_length On success, the number of bytes of
2638 * returned ciphertext.
2639 * \param[out] tag Buffer where the authentication tag is
2640 * to be written.
2641 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002642 * This must be at least
2643 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2644 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002645 * \param[out] tag_length On success, the number of bytes
2646 * that make up the returned tag.
2647 *
2648 * \retval #PSA_SUCCESS
2649 * Success.
2650 * \retval #PSA_ERROR_BAD_STATE
2651 * The operation state is not valid (not set up, nonce not set,
2652 * decryption, or already completed).
2653 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002654 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002655 * You can determine a sufficient buffer size for \p ciphertext by
2656 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2657 * where \c alg is the algorithm that is being calculated.
2658 * You can determine a sufficient buffer size for \p tag by
2659 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002660 * \retval #PSA_ERROR_INVALID_ARGUMENT
2661 * The total length of input to psa_aead_update_ad() so far is
2662 * less than the additional data length that was previously
2663 * specified with psa_aead_set_lengths().
2664 * \retval #PSA_ERROR_INVALID_ARGUMENT
2665 * The total length of input to psa_aead_update() so far is
2666 * less than the plaintext length that was previously
2667 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002668 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2669 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2670 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002671 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002672 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002673 * \retval #PSA_ERROR_BAD_STATE
2674 * The library has not been previously initialized by psa_crypto_init().
2675 * It is implementation-dependent whether a failure to initialize
2676 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002677 */
2678psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002679 uint8_t *ciphertext,
2680 size_t ciphertext_size,
2681 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002682 uint8_t *tag,
2683 size_t tag_size,
2684 size_t *tag_length);
2685
2686/** Finish authenticating and decrypting a message in an AEAD operation.
2687 *
2688 * The operation must have been set up with psa_aead_decrypt_setup().
2689 *
2690 * This function finishes the authentication of the additional data
2691 * formed by concatenating the inputs passed to preceding calls to
2692 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2693 * inputs passed to preceding calls to psa_aead_update().
2694 *
2695 * When this function returns, the operation becomes inactive.
2696 *
2697 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002698 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002699 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002700 * from previous calls to psa_aead_update()
2701 * that could not be processed until the end
2702 * of the input.
2703 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002704 * This must be at least
2705 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2706 * \c alg is the algorithm that is being
2707 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002708 * \param[out] plaintext_length On success, the number of bytes of
2709 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002710 * \param[in] tag Buffer containing the authentication tag.
2711 * \param tag_length Size of the \p tag buffer in bytes.
2712 *
2713 * \retval #PSA_SUCCESS
2714 * Success.
2715 * \retval #PSA_ERROR_BAD_STATE
2716 * The operation state is not valid (not set up, nonce not set,
2717 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002718 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2719 * The size of the \p plaintext buffer is too small.
2720 * You can determine a sufficient buffer size for \p plaintext by
2721 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2722 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002723 * \retval #PSA_ERROR_INVALID_ARGUMENT
2724 * The total length of input to psa_aead_update_ad() so far is
2725 * less than the additional data length that was previously
2726 * specified with psa_aead_set_lengths().
2727 * \retval #PSA_ERROR_INVALID_ARGUMENT
2728 * The total length of input to psa_aead_update() so far is
2729 * less than the plaintext length that was previously
2730 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002731 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2732 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2733 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002734 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002735 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002736 * \retval #PSA_ERROR_BAD_STATE
2737 * The library has not been previously initialized by psa_crypto_init().
2738 * It is implementation-dependent whether a failure to initialize
2739 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002740 */
2741psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002742 uint8_t *plaintext,
2743 size_t plaintext_size,
2744 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002745 const uint8_t *tag,
2746 size_t tag_length);
2747
2748/** Abort an AEAD operation.
2749 *
2750 * Aborting an operation frees all associated resources except for the
2751 * \p operation structure itself. Once aborted, the operation object
2752 * can be reused for another operation by calling
2753 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2754 *
2755 * You may call this function any time after the operation object has
2756 * been initialized by any of the following methods:
2757 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2758 * whether it succeeds or not.
2759 * - Initializing the \c struct to all-bits-zero.
2760 * - Initializing the \c struct to logical zeros, e.g.
2761 * `psa_aead_operation_t operation = {0}`.
2762 *
2763 * In particular, calling psa_aead_abort() after the operation has been
2764 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2765 * is safe and has no effect.
2766 *
2767 * \param[in,out] operation Initialized AEAD operation.
2768 *
2769 * \retval #PSA_SUCCESS
2770 * \retval #PSA_ERROR_BAD_STATE
2771 * \p operation is not an active AEAD operation.
2772 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2773 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002774 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002775 * \retval #PSA_ERROR_BAD_STATE
2776 * The library has not been previously initialized by psa_crypto_init().
2777 * It is implementation-dependent whether a failure to initialize
2778 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002779 */
2780psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2781
Gilles Peskine3b555712018-03-03 21:27:57 +01002782/**@}*/
2783
Gilles Peskine20035e32018-02-03 22:44:14 +01002784/** \defgroup asymmetric Asymmetric cryptography
2785 * @{
2786 */
2787
2788/**
2789 * \brief Sign a hash or short message with a private key.
2790 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002791 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002792 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002793 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2794 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2795 * to determine the hash algorithm to use.
2796 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002797 * \param handle Handle to the key to use for the operation.
2798 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002799 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002800 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002801 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002802 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002803 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002804 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002805 * \param[out] signature_length On success, the number of bytes
2806 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002807 *
Gilles Peskine28538492018-07-11 17:34:00 +02002808 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002809 * \retval #PSA_ERROR_INVALID_HANDLE
2810 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002811 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002812 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002813 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002814 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002815 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002816 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002817 * \retval #PSA_ERROR_NOT_SUPPORTED
2818 * \retval #PSA_ERROR_INVALID_ARGUMENT
2819 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2820 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2821 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002822 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002823 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002824 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002825 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002826 * The library has not been previously initialized by psa_crypto_init().
2827 * It is implementation-dependent whether a failure to initialize
2828 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002829 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002830psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002831 psa_algorithm_t alg,
2832 const uint8_t *hash,
2833 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002834 uint8_t *signature,
2835 size_t signature_size,
2836 size_t *signature_length);
2837
2838/**
2839 * \brief Verify the signature a hash or short message using a public key.
2840 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002841 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002842 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002843 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2844 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2845 * to determine the hash algorithm to use.
2846 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002847 * \param handle Handle to the key to use for the operation.
2848 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002849 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002850 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002851 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002852 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002853 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002854 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002855 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002856 *
Gilles Peskine28538492018-07-11 17:34:00 +02002857 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002858 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002859 * \retval #PSA_ERROR_INVALID_HANDLE
2860 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002861 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002862 * The calculation was perfomed successfully, but the passed
2863 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002864 * \retval #PSA_ERROR_NOT_SUPPORTED
2865 * \retval #PSA_ERROR_INVALID_ARGUMENT
2866 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2867 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2868 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002869 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002870 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002871 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002872 * The library has not been previously initialized by psa_crypto_init().
2873 * It is implementation-dependent whether a failure to initialize
2874 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002875 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002876psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002877 psa_algorithm_t alg,
2878 const uint8_t *hash,
2879 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002880 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002881 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002882
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002883/**
2884 * \brief Encrypt a short message with a public key.
2885 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002886 * \param handle Handle to the key to use for the operation.
2887 * It must be a public key or an asymmetric
2888 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002889 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002890 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002891 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002892 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002893 * \param[in] salt A salt or label, if supported by the
2894 * encryption algorithm.
2895 * If the algorithm does not support a
2896 * salt, pass \c NULL.
2897 * If the algorithm supports an optional
2898 * salt and you do not want to pass a salt,
2899 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002900 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002901 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2902 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002903 * \param salt_length Size of the \p salt buffer in bytes.
2904 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002905 * \param[out] output Buffer where the encrypted message is to
2906 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002907 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002908 * \param[out] output_length On success, the number of bytes
2909 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002910 *
Gilles Peskine28538492018-07-11 17:34:00 +02002911 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002912 * \retval #PSA_ERROR_INVALID_HANDLE
2913 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002914 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002915 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002916 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002917 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002918 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002919 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002920 * \retval #PSA_ERROR_NOT_SUPPORTED
2921 * \retval #PSA_ERROR_INVALID_ARGUMENT
2922 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2923 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2924 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002925 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002926 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002927 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002928 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002929 * The library has not been previously initialized by psa_crypto_init().
2930 * It is implementation-dependent whether a failure to initialize
2931 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002932 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002933psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002934 psa_algorithm_t alg,
2935 const uint8_t *input,
2936 size_t input_length,
2937 const uint8_t *salt,
2938 size_t salt_length,
2939 uint8_t *output,
2940 size_t output_size,
2941 size_t *output_length);
2942
2943/**
2944 * \brief Decrypt a short message with a private key.
2945 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002946 * \param handle Handle to the key to use for the operation.
2947 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002948 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002949 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002950 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002951 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002952 * \param[in] salt A salt or label, if supported by the
2953 * encryption algorithm.
2954 * If the algorithm does not support a
2955 * salt, pass \c NULL.
2956 * If the algorithm supports an optional
2957 * salt and you do not want to pass a salt,
2958 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002959 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002960 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2961 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002962 * \param salt_length Size of the \p salt buffer in bytes.
2963 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002964 * \param[out] output Buffer where the decrypted message is to
2965 * be written.
2966 * \param output_size Size of the \c output buffer in bytes.
2967 * \param[out] output_length On success, the number of bytes
2968 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002969 *
Gilles Peskine28538492018-07-11 17:34:00 +02002970 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002971 * \retval #PSA_ERROR_INVALID_HANDLE
2972 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002973 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002974 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002975 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002976 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002977 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002978 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002979 * \retval #PSA_ERROR_NOT_SUPPORTED
2980 * \retval #PSA_ERROR_INVALID_ARGUMENT
2981 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2982 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2983 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002984 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002985 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002986 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2987 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002988 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002989 * The library has not been previously initialized by psa_crypto_init().
2990 * It is implementation-dependent whether a failure to initialize
2991 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002992 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002993psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002994 psa_algorithm_t alg,
2995 const uint8_t *input,
2996 size_t input_length,
2997 const uint8_t *salt,
2998 size_t salt_length,
2999 uint8_t *output,
3000 size_t output_size,
3001 size_t *output_length);
3002
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003003/**@}*/
3004
Gilles Peskine35675b62019-05-16 17:26:11 +02003005/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003006 * @{
3007 */
3008
Gilles Peskine35675b62019-05-16 17:26:11 +02003009/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003010 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003011 * Before calling any function on a key derivation operation object, the
3012 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003013 * - Set the structure to all-bits-zero, for example:
3014 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003015 * psa_key_derivation_operation_t operation;
3016 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003017 * \endcode
3018 * - Initialize the structure to logical zero values, for example:
3019 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003020 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003021 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003022 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003023 * for example:
3024 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003025 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003026 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003027 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003028 * to the structure, for example:
3029 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003030 * psa_key_derivation_operation_t operation;
3031 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003032 * \endcode
3033 *
3034 * This is an implementation-defined \c struct. Applications should not
3035 * make any assumptions about the content of this structure except
3036 * as directed by the documentation of a specific implementation.
3037 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003038typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003039
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003040/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003041 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003042 * This macro returns a suitable initializer for a key derivation operation
3043 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003044 */
3045#ifdef __DOXYGEN_ONLY__
3046/* This is an example definition for documentation purposes.
3047 * Implementations should define a suitable value in `crypto_struct.h`.
3048 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003049#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003050#endif
3051
Gilles Peskine35675b62019-05-16 17:26:11 +02003052/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003053 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003054static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003055
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003056/** Set up a key derivation operation.
3057 *
3058 * A key derivation algorithm takes some inputs and uses them to generate
3059 * a byte stream in a deterministic way.
3060 * This byte stream can be used to produce keys and other
3061 * cryptographic material.
3062 *
3063 * To derive a key:
3064 * - Start with an initialized object of type #psa_key_derivation_operation_t.
3065 * - Call psa_key_derivation_setup() to select the algorithm.
3066 * - Provide the inputs for the key derivation by calling
3067 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3068 * as appropriate. Which inputs are needed, in what order, and whether
3069 * they may be keys and if so of what type depends on the algorithm.
3070 * - Optionally set the operation's maximum capacity with
3071 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3072 * of or after providing inputs. For some algorithms, this step is mandatory
3073 * because the output depends on the maximum capacity.
3074 * - To derive a key, call psa_key_derivation_output_key().
3075 * To derive a byte string for a different purpose, call
3076 * - psa_key_derivation_output_bytes().
3077 * Successive calls to these functions use successive output bytes
3078 * calculated by the key derivation algorithm.
3079 * - Clean up the key derivation operation object with
3080 * psa_key_derivation_abort().
3081 *
3082 * \param[in,out] operation The key derivation operation object
3083 * to set up. It must
3084 * have been initialized but not set up yet.
3085 * \param alg The key derivation algorithm to compute
3086 * (\c PSA_ALG_XXX value such that
3087 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3088 *
3089 * \retval #PSA_SUCCESS
3090 * Success.
3091 * \retval #PSA_ERROR_INVALID_ARGUMENT
3092 * \c alg is not a key derivation algorithm.
3093 * \retval #PSA_ERROR_NOT_SUPPORTED
3094 * \c alg is not supported or is not a key derivation algorithm.
3095 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3096 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3097 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003098 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003099 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003100 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01003101 * The operation state is either not initialized or has already been setup.
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003102 * \retval #PSA_ERROR_BAD_STATE
3103 * The library has not been previously initialized by psa_crypto_init().
3104 * It is implementation-dependent whether a failure to initialize
3105 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003106 */
3107psa_status_t psa_key_derivation_setup(
3108 psa_key_derivation_operation_t *operation,
3109 psa_algorithm_t alg);
3110
Gilles Peskine35675b62019-05-16 17:26:11 +02003111/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003112 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003113 * The capacity of a key derivation is the maximum number of bytes that it can
3114 * return. When you get *N* bytes of output from a key derivation operation,
3115 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003116 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003117 * \param[in] operation The operation to query.
3118 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003119 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003120 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003121 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003122 * \retval #PSA_ERROR_BAD_STATE
3123 * The operation state is not valid.
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003124 * \retval #PSA_ERROR_HARDWARE_FAILURE
3125 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003126 * \retval #PSA_ERROR_BAD_STATE
3127 * The library has not been previously initialized by psa_crypto_init().
3128 * It is implementation-dependent whether a failure to initialize
3129 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003130 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003131psa_status_t psa_key_derivation_get_capacity(
3132 const psa_key_derivation_operation_t *operation,
3133 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003134
Gilles Peskine35675b62019-05-16 17:26:11 +02003135/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003136 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003137 * The capacity of a key derivation operation is the maximum number of bytes
3138 * that the key derivation operation can return from this point onwards.
3139 *
3140 * \param[in,out] operation The key derivation operation object to modify.
3141 * \param capacity The new capacity of the operation.
3142 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003143 * current capacity.
3144 *
3145 * \retval #PSA_SUCCESS
3146 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003147 * \p capacity is larger than the operation's current capacity.
3148 * In this case, the operation object remains valid and its capacity
3149 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003150 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003151 * The operation state is not valid.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003152 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003153 * \retval #PSA_ERROR_HARDWARE_FAILURE
3154 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003155 * \retval #PSA_ERROR_BAD_STATE
3156 * The library has not been previously initialized by psa_crypto_init().
3157 * It is implementation-dependent whether a failure to initialize
3158 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003159 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003160psa_status_t psa_key_derivation_set_capacity(
3161 psa_key_derivation_operation_t *operation,
3162 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003163
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003164/** Use the maximum possible capacity for a key derivation operation.
3165 *
3166 * Use this value as the capacity argument when setting up a key derivation
3167 * to indicate that the operation should have the maximum possible capacity.
3168 * The value of the maximum possible capacity depends on the key derivation
3169 * algorithm.
3170 */
3171#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3172
3173/** Provide an input for key derivation or key agreement.
3174 *
3175 * Which inputs are required and in what order depends on the algorithm.
3176 * Refer to the documentation of each key derivation or key agreement
3177 * algorithm for information.
3178 *
3179 * This function passes direct inputs. Some inputs must be passed as keys
3180 * using psa_key_derivation_input_key() instead of this function. Refer to
3181 * the documentation of individual step types for information.
3182 *
3183 * \param[in,out] operation The key derivation operation object to use.
3184 * It must have been set up with
3185 * psa_key_derivation_setup() and must not
3186 * have produced any output yet.
3187 * \param step Which step the input data is for.
3188 * \param[in] data Input data to use.
3189 * \param data_length Size of the \p data buffer in bytes.
3190 *
3191 * \retval #PSA_SUCCESS
3192 * Success.
3193 * \retval #PSA_ERROR_INVALID_ARGUMENT
3194 * \c step is not compatible with the operation's algorithm.
3195 * \retval #PSA_ERROR_INVALID_ARGUMENT
3196 * \c step does not allow direct inputs.
3197 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3198 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3199 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003200 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003201 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003202 * \retval #PSA_ERROR_BAD_STATE
3203 * The value of \p step is not valid given the state of \p operation.
3204 * \retval #PSA_ERROR_BAD_STATE
3205 * The library has not been previously initialized by psa_crypto_init().
3206 * It is implementation-dependent whether a failure to initialize
3207 * results in this error code.
3208 */
3209psa_status_t psa_key_derivation_input_bytes(
3210 psa_key_derivation_operation_t *operation,
3211 psa_key_derivation_step_t step,
3212 const uint8_t *data,
3213 size_t data_length);
3214
3215/** Provide an input for key derivation in the form of a key.
3216 *
3217 * Which inputs are required and in what order depends on the algorithm.
3218 * Refer to the documentation of each key derivation or key agreement
3219 * algorithm for information.
3220 *
3221 * This function passes key inputs. Some inputs must be passed as keys
3222 * of the appropriate type using this function, while others must be
3223 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3224 * the documentation of individual step types for information.
3225 *
3226 * \param[in,out] operation The key derivation operation object to use.
3227 * It must have been set up with
3228 * psa_key_derivation_setup() and must not
3229 * have produced any output yet.
3230 * \param step Which step the input data is for.
3231 * \param handle Handle to the key. It must have an
3232 * appropriate type for \p step and must
3233 * allow the usage #PSA_KEY_USAGE_DERIVE.
3234 *
3235 * \retval #PSA_SUCCESS
3236 * Success.
3237 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003238 * \retval #PSA_ERROR_NOT_PERMITTED
3239 * \retval #PSA_ERROR_INVALID_ARGUMENT
3240 * \c step is not compatible with the operation's algorithm.
3241 * \retval #PSA_ERROR_INVALID_ARGUMENT
3242 * \c step does not allow key inputs.
3243 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3244 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3245 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003246 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003247 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003248 * \retval #PSA_ERROR_BAD_STATE
3249 * The value of \p step is not valid given the state of \p operation.
3250 * \retval #PSA_ERROR_BAD_STATE
3251 * The library has not been previously initialized by psa_crypto_init().
3252 * It is implementation-dependent whether a failure to initialize
3253 * results in this error code.
3254 */
3255psa_status_t psa_key_derivation_input_key(
3256 psa_key_derivation_operation_t *operation,
3257 psa_key_derivation_step_t step,
3258 psa_key_handle_t handle);
3259
3260/** Perform a key agreement and use the shared secret as input to a key
3261 * derivation.
3262 *
3263 * A key agreement algorithm takes two inputs: a private key \p private_key
3264 * a public key \p peer_key.
3265 * The result of this function is passed as input to a key derivation.
3266 * The output of this key derivation can be extracted by reading from the
3267 * resulting operation to produce keys and other cryptographic material.
3268 *
3269 * \param[in,out] operation The key derivation operation object to use.
3270 * It must have been set up with
3271 * psa_key_derivation_setup() with a
3272 * key agreement and derivation algorithm
3273 * \c alg (\c PSA_ALG_XXX value such that
3274 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3275 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3276 * is false).
3277 * The operation must be ready for an
3278 * input of the type given by \p step.
3279 * \param step Which step the input data is for.
3280 * \param private_key Handle to the private key to use.
3281 * \param[in] peer_key Public key of the peer. The peer key must be in the
3282 * same format that psa_import_key() accepts for the
3283 * public key type corresponding to the type of
3284 * private_key. That is, this function performs the
3285 * equivalent of
3286 * #psa_import_key(...,
3287 * `peer_key`, `peer_key_length`) where
3288 * with key attributes indicating the public key
3289 * type corresponding to the type of `private_key`.
3290 * For example, for EC keys, this means that peer_key
3291 * is interpreted as a point on the curve that the
3292 * private key is on. The standard formats for public
3293 * keys are documented in the documentation of
3294 * psa_export_public_key().
3295 * \param peer_key_length Size of \p peer_key in bytes.
3296 *
3297 * \retval #PSA_SUCCESS
3298 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003299 * \retval #PSA_ERROR_BAD_STATE
3300 * The value of \p step is not valid for a key agreement given the
3301 * state of \p operation.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003302 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003303 * \retval #PSA_ERROR_NOT_PERMITTED
3304 * \retval #PSA_ERROR_INVALID_ARGUMENT
3305 * \c private_key is not compatible with \c alg,
3306 * or \p peer_key is not valid for \c alg or not compatible with
3307 * \c private_key.
3308 * \retval #PSA_ERROR_NOT_SUPPORTED
3309 * \c alg is not supported or is not a key derivation algorithm.
3310 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3311 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3312 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003313 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003314 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003315 * \retval #PSA_ERROR_BAD_STATE
3316 * The library has not been previously initialized by psa_crypto_init().
3317 * It is implementation-dependent whether a failure to initialize
3318 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003319 */
3320psa_status_t psa_key_derivation_key_agreement(
3321 psa_key_derivation_operation_t *operation,
3322 psa_key_derivation_step_t step,
3323 psa_key_handle_t private_key,
3324 const uint8_t *peer_key,
3325 size_t peer_key_length);
3326
Gilles Peskine35675b62019-05-16 17:26:11 +02003327/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003328 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003329 * This function calculates output bytes from a key derivation algorithm and
3330 * return those bytes.
3331 * If you view the key derivation's output as a stream of bytes, this
3332 * function destructively reads the requested number of bytes from the
3333 * stream.
3334 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003335 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003336 * \param[in,out] operation The key derivation operation object to read from.
3337 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003338 * \param output_length Number of bytes to output.
3339 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003340 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003341 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003342 * The operation's capacity was less than
3343 * \p output_length bytes. Note that in this case,
3344 * no output is written to the output buffer.
3345 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003346 * subsequent calls to this function will not
3347 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003348 * \retval #PSA_ERROR_BAD_STATE
3349 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3350 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3351 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003352 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003353 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003354 * \retval #PSA_ERROR_BAD_STATE
3355 * The library has not been previously initialized by psa_crypto_init().
3356 * It is implementation-dependent whether a failure to initialize
3357 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003358 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003359psa_status_t psa_key_derivation_output_bytes(
3360 psa_key_derivation_operation_t *operation,
3361 uint8_t *output,
3362 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003363
Gilles Peskine35675b62019-05-16 17:26:11 +02003364/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003365 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003366 * This function calculates output bytes from a key derivation algorithm
3367 * and uses those bytes to generate a key deterministically.
3368 * If you view the key derivation's output as a stream of bytes, this
3369 * function destructively reads as many bytes as required from the
3370 * stream.
3371 * The operation's capacity decreases by the number of bytes read.
3372 *
3373 * How much output is produced and consumed from the operation, and how
3374 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003375 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003376 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003377 * of a given size, this function is functionally equivalent to
3378 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003379 * and passing the resulting output to #psa_import_key.
3380 * However, this function has a security benefit:
3381 * if the implementation provides an isolation boundary then
3382 * the key material is not exposed outside the isolation boundary.
3383 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003384 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003385 * The following key types defined in this specification follow this scheme:
3386 *
3387 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003388 * - #PSA_KEY_TYPE_ARC4;
3389 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003390 * - #PSA_KEY_TYPE_DERIVE;
3391 * - #PSA_KEY_TYPE_HMAC.
3392 *
3393 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003394 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003395 * Montgomery curve), this function always draws a byte string whose
3396 * length is determined by the curve, and sets the mandatory bits
3397 * accordingly. That is:
3398 *
3399 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3400 * and process it as specified in RFC 7748 &sect;5.
3401 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3402 * and process it as specified in RFC 7748 &sect;5.
3403 *
3404 * - For key types for which the key is represented by a single sequence of
3405 * \p bits bits with constraints as to which bit sequences are acceptable,
3406 * this function draws a byte string of length (\p bits / 8) bytes rounded
3407 * up to the nearest whole number of bytes. If the resulting byte string
3408 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3409 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003410 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003411 * for the output produced by psa_export_key().
3412 * The following key types defined in this specification follow this scheme:
3413 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003414 * - #PSA_KEY_TYPE_DES.
3415 * Force-set the parity bits, but discard forbidden weak keys.
3416 * For 2-key and 3-key triple-DES, the three keys are generated
3417 * successively (for example, for 3-key triple-DES,
3418 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3419 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003420 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003421 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003422 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003423 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003424 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003425 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003426 * Weierstrass curve).
3427 * For these key types, interpret the byte string as integer
3428 * in big-endian order. Discard it if it is not in the range
3429 * [0, *N* - 2] where *N* is the boundary of the private key domain
3430 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003431 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003432 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003433 * This method allows compliance to NIST standards, specifically
3434 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003435 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3436 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3437 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3438 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003439 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003440 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003441 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003442 * implementation-defined.
3443 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003444 * In all cases, the data that is read is discarded from the operation.
3445 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003446 *
Gilles Peskine20628592019-04-19 19:29:50 +02003447 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003448 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003449 * \param[out] handle On success, a handle to the newly created key.
3450 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003451 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003452 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003453 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003454 * If the key is persistent, the key material and the key's metadata
3455 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003456 * \retval #PSA_ERROR_ALREADY_EXISTS
3457 * This is an attempt to create a persistent key, and there is
3458 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003459 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003460 * There was not enough data to create the desired key.
3461 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003462 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003463 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003464 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003465 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003466 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003467 * \retval #PSA_ERROR_INVALID_ARGUMENT
3468 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003469 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003470 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3471 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3472 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3473 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003474 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003475 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003476 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003477 * The library has not been previously initialized by psa_crypto_init().
3478 * It is implementation-dependent whether a failure to initialize
3479 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003480 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003481psa_status_t psa_key_derivation_output_key(
3482 const psa_key_attributes_t *attributes,
3483 psa_key_derivation_operation_t *operation,
3484 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003485
Gilles Peskine35675b62019-05-16 17:26:11 +02003486/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003487 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003488 * Once a key derivation operation has been aborted, its capacity is zero.
3489 * Aborting an operation frees all associated resources except for the
3490 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003491 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003492 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003493 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003494 * psa_key_derivation_operation_init() or a zero value. In particular,
3495 * it is valid to call psa_key_derivation_abort() twice, or to call
3496 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003497 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003498 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003499 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003500 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003501 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003502 * \retval #PSA_SUCCESS
3503 * \retval #PSA_ERROR_BAD_STATE
3504 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3505 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003506 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003507 * \retval #PSA_ERROR_BAD_STATE
3508 * The library has not been previously initialized by psa_crypto_init().
3509 * It is implementation-dependent whether a failure to initialize
3510 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003511 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003512psa_status_t psa_key_derivation_abort(
3513 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003514
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003515/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003516 *
3517 * \warning The raw result of a key agreement algorithm such as finite-field
3518 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3519 * not be used directly as key material. It should instead be passed as
3520 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003521 * a key derivation, use psa_key_derivation_key_agreement() and other
3522 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003523 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003524 * \param alg The key agreement algorithm to compute
3525 * (\c PSA_ALG_XXX value such that
3526 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3527 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003528 * \param private_key Handle to the private key to use.
3529 * \param[in] peer_key Public key of the peer. It must be
3530 * in the same format that psa_import_key()
3531 * accepts. The standard formats for public
3532 * keys are documented in the documentation
3533 * of psa_export_public_key().
3534 * \param peer_key_length Size of \p peer_key in bytes.
3535 * \param[out] output Buffer where the decrypted message is to
3536 * be written.
3537 * \param output_size Size of the \c output buffer in bytes.
3538 * \param[out] output_length On success, the number of bytes
3539 * that make up the returned output.
3540 *
3541 * \retval #PSA_SUCCESS
3542 * Success.
3543 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003544 * \retval #PSA_ERROR_NOT_PERMITTED
3545 * \retval #PSA_ERROR_INVALID_ARGUMENT
3546 * \p alg is not a key agreement algorithm
3547 * \retval #PSA_ERROR_INVALID_ARGUMENT
3548 * \p private_key is not compatible with \p alg,
3549 * or \p peer_key is not valid for \p alg or not compatible with
3550 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003551 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3552 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003553 * \retval #PSA_ERROR_NOT_SUPPORTED
3554 * \p alg is not a supported key agreement algorithm.
3555 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3556 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3557 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003558 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003559 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003560 * \retval #PSA_ERROR_BAD_STATE
3561 * The library has not been previously initialized by psa_crypto_init().
3562 * It is implementation-dependent whether a failure to initialize
3563 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003564 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003565psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3566 psa_key_handle_t private_key,
3567 const uint8_t *peer_key,
3568 size_t peer_key_length,
3569 uint8_t *output,
3570 size_t output_size,
3571 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003572
Gilles Peskineea0fb492018-07-12 17:17:20 +02003573/**@}*/
3574
Gilles Peskineedd76872018-07-20 17:42:05 +02003575/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003576 * @{
3577 */
3578
3579/**
3580 * \brief Generate random bytes.
3581 *
3582 * \warning This function **can** fail! Callers MUST check the return status
3583 * and MUST NOT use the content of the output buffer if the return
3584 * status is not #PSA_SUCCESS.
3585 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003586 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003587 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003588 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003589 * \param output_size Number of bytes to generate and output.
3590 *
Gilles Peskine28538492018-07-11 17:34:00 +02003591 * \retval #PSA_SUCCESS
3592 * \retval #PSA_ERROR_NOT_SUPPORTED
3593 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003594 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003595 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3596 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003597 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003598 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003599 * The library has not been previously initialized by psa_crypto_init().
3600 * It is implementation-dependent whether a failure to initialize
3601 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003602 */
3603psa_status_t psa_generate_random(uint8_t *output,
3604 size_t output_size);
3605
3606/**
3607 * \brief Generate a key or key pair.
3608 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003609 * The key is generated randomly.
3610 * Its location, policy, type and size are taken from \p attributes.
3611 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003612 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003613 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003614 * the public exponent is 65537.
3615 * The modulus is a product of two probabilistic primes
3616 * between 2^{n-1} and 2^n where n is the bit size specified in the
3617 * attributes.
3618 *
Gilles Peskine20628592019-04-19 19:29:50 +02003619 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003620 * \param[out] handle On success, a handle to the newly created key.
3621 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003622 *
Gilles Peskine28538492018-07-11 17:34:00 +02003623 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003624 * Success.
3625 * If the key is persistent, the key material and the key's metadata
3626 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003627 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003628 * This is an attempt to create a persistent key, and there is
3629 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003630 * \retval #PSA_ERROR_NOT_SUPPORTED
3631 * \retval #PSA_ERROR_INVALID_ARGUMENT
3632 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3633 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3634 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3635 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003636 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003637 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3638 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003639 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003640 * The library has not been previously initialized by psa_crypto_init().
3641 * It is implementation-dependent whether a failure to initialize
3642 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003643 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003644psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003645 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003646
3647/**@}*/
3648
Gilles Peskinee59236f2018-01-27 23:32:46 +01003649#ifdef __cplusplus
3650}
3651#endif
3652
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003653/* The file "crypto_sizes.h" contains definitions for size calculation
3654 * macros whose definitions are implementation-specific. */
3655#include "crypto_sizes.h"
3656
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003657/* The file "crypto_struct.h" contains definitions for
3658 * implementation-specific structs that are declared above. */
3659#include "crypto_struct.h"
3660
3661/* The file "crypto_extra.h" contains vendor-specific definitions. This
3662 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003663#include "crypto_extra.h"
3664
3665#endif /* PSA_CRYPTO_H */