blob: e5af9c605f2e6e43a7ce735fc69177c96b8811ad [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +020089 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +020090 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine105f67f2019-07-23 18:16:05 +020096/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +020097 * @{
98 */
99
Gilles Peskinea0c06552019-05-21 15:54:54 +0200100/** \def PSA_KEY_ATTRIBUTES_INIT
101 *
102 * This macro returns a suitable initializer for a key attribute structure
103 * of type #psa_key_attributes_t.
104 */
105#ifdef __DOXYGEN_ONLY__
106/* This is an example definition for documentation purposes.
107 * Implementations should define a suitable value in `crypto_struct.h`.
108 */
109#define PSA_KEY_ATTRIBUTES_INIT {0}
110#endif
111
112/** Return an initial value for a key attributes structure.
113 */
114static psa_key_attributes_t psa_key_attributes_init(void);
115
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200116/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200117 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200118 * If the attribute structure currently declares the key as volatile (which
119 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200120 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200121 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200122 * This function does not access storage, it merely stores the given
123 * value in the structure.
124 * The persistent key will be written to storage when the attribute
125 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200126 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200127 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200128 *
Gilles Peskine20628592019-04-19 19:29:50 +0200129 * This function may be declared as `static` (i.e. without external
130 * linkage). This function may be provided as a function-like macro,
131 * but in this case it must evaluate each of its arguments exactly once.
132 *
133 * \param[out] attributes The attribute structure to write to.
134 * \param id The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200135 */
136static void psa_set_key_id(psa_key_attributes_t *attributes,
137 psa_key_id_t id);
138
139/** Set the location of a persistent key.
140 *
141 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200142 * with psa_set_key_id(). By default, a key that has a persistent identifier
143 * is stored in the default storage area identifier by
144 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
145 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200146 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200147 * This function does not access storage, it merely stores the given
148 * value in the structure.
149 * The persistent key will be written to storage when the attribute
150 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200151 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200152 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200153 *
154 * This function may be declared as `static` (i.e. without external
155 * linkage). This function may be provided as a function-like macro,
156 * but in this case it must evaluate each of its arguments exactly once.
157 *
158 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200159 * \param lifetime The lifetime for the key.
160 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200161 * key will be volatile, and the key identifier
162 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200163 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200164static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
165 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200166
Gilles Peskine20628592019-04-19 19:29:50 +0200167/** Retrieve the key identifier from key attributes.
168 *
169 * This function may be declared as `static` (i.e. without external
170 * linkage). This function may be provided as a function-like macro,
171 * but in this case it must evaluate its argument exactly once.
172 *
173 * \param[in] attributes The key attribute structure to query.
174 *
175 * \return The persistent identifier stored in the attribute structure.
176 * This value is unspecified if the attribute structure declares
177 * the key as volatile.
178 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200179static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
180
Gilles Peskine20628592019-04-19 19:29:50 +0200181/** Retrieve the lifetime from key attributes.
182 *
183 * This function may be declared as `static` (i.e. without external
184 * linkage). This function may be provided as a function-like macro,
185 * but in this case it must evaluate its argument exactly once.
186 *
187 * \param[in] attributes The key attribute structure to query.
188 *
189 * \return The lifetime value stored in the attribute structure.
190 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200191static psa_key_lifetime_t psa_get_key_lifetime(
192 const psa_key_attributes_t *attributes);
193
Gilles Peskine20628592019-04-19 19:29:50 +0200194/** Declare usage flags for a key.
195 *
196 * Usage flags are part of a key's usage policy. They encode what
197 * kind of operations are permitted on the key. For more details,
198 * refer to the documentation of the type #psa_key_usage_t.
199 *
200 * This function overwrites any usage flags
201 * previously set in \p attributes.
202 *
203 * This function may be declared as `static` (i.e. without external
204 * linkage). This function may be provided as a function-like macro,
205 * but in this case it must evaluate each of its arguments exactly once.
206 *
207 * \param[out] attributes The attribute structure to write to.
208 * \param usage_flags The usage flags to write.
209 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200210static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
211 psa_key_usage_t usage_flags);
212
Gilles Peskine20628592019-04-19 19:29:50 +0200213/** Retrieve the usage flags from key attributes.
214 *
215 * This function may be declared as `static` (i.e. without external
216 * linkage). This function may be provided as a function-like macro,
217 * but in this case it must evaluate its argument exactly once.
218 *
219 * \param[in] attributes The key attribute structure to query.
220 *
221 * \return The usage flags stored in the attribute structure.
222 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200223static psa_key_usage_t psa_get_key_usage_flags(
224 const psa_key_attributes_t *attributes);
225
Gilles Peskine20628592019-04-19 19:29:50 +0200226/** Declare the permitted algorithm policy for a key.
227 *
228 * The permitted algorithm policy of a key encodes which algorithm or
229 * algorithms are permitted to be used with this key.
230 *
231 * This function overwrites any algorithm policy
232 * previously set in \p attributes.
233 *
234 * This function may be declared as `static` (i.e. without external
235 * linkage). This function may be provided as a function-like macro,
236 * but in this case it must evaluate each of its arguments exactly once.
237 *
238 * \param[out] attributes The attribute structure to write to.
239 * \param alg The permitted algorithm policy to write.
240 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200241static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
242 psa_algorithm_t alg);
243
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100244
Gilles Peskine20628592019-04-19 19:29:50 +0200245/** Retrieve the algorithm policy from key attributes.
246 *
247 * This function may be declared as `static` (i.e. without external
248 * linkage). This function may be provided as a function-like macro,
249 * but in this case it must evaluate its argument exactly once.
250 *
251 * \param[in] attributes The key attribute structure to query.
252 *
253 * \return The algorithm stored in the attribute structure.
254 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200255static psa_algorithm_t psa_get_key_algorithm(
256 const psa_key_attributes_t *attributes);
257
Gilles Peskine20628592019-04-19 19:29:50 +0200258/** Declare the type of a key.
259 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200260 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200261 * previously set in \p attributes.
262 *
263 * This function may be declared as `static` (i.e. without external
264 * linkage). This function may be provided as a function-like macro,
265 * but in this case it must evaluate each of its arguments exactly once.
266 *
267 * \param[out] attributes The attribute structure to write to.
268 * \param type The key type to write.
269 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200270static void psa_set_key_type(psa_key_attributes_t *attributes,
271 psa_key_type_t type);
272
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100273
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200274/** Declare the size of a key.
275 *
276 * This function overwrites any key size previously set in \p attributes.
277 *
278 * This function may be declared as `static` (i.e. without external
279 * linkage). This function may be provided as a function-like macro,
280 * but in this case it must evaluate each of its arguments exactly once.
281 *
282 * \param[out] attributes The attribute structure to write to.
283 * \param bits The key size in bits.
284 */
285static void psa_set_key_bits(psa_key_attributes_t *attributes,
286 size_t bits);
287
Gilles Peskine20628592019-04-19 19:29:50 +0200288/** Retrieve the key type from key attributes.
289 *
290 * This function may be declared as `static` (i.e. without external
291 * linkage). This function may be provided as a function-like macro,
292 * but in this case it must evaluate its argument exactly once.
293 *
294 * \param[in] attributes The key attribute structure to query.
295 *
296 * \return The key type stored in the attribute structure.
297 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200298static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
299
Gilles Peskine20628592019-04-19 19:29:50 +0200300/** Retrieve the key size from key attributes.
301 *
302 * This function may be declared as `static` (i.e. without external
303 * linkage). This function may be provided as a function-like macro,
304 * but in this case it must evaluate its argument exactly once.
305 *
306 * \param[in] attributes The key attribute structure to query.
307 *
308 * \return The key size stored in the attribute structure, in bits.
309 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200310static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
311
Gilles Peskine20628592019-04-19 19:29:50 +0200312/** Retrieve the attributes of a key.
313 *
314 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200315 * psa_reset_key_attributes(). It then copies the attributes of
316 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200317 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200318 * \note This function may allocate memory or other resources.
319 * Once you have called this function on an attribute structure,
320 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200321 *
Gilles Peskine20628592019-04-19 19:29:50 +0200322 * \param[in] handle Handle to the key to query.
323 * \param[in,out] attributes On success, the attributes of the key.
324 * On failure, equivalent to a
325 * freshly-initialized structure.
326 *
327 * \retval #PSA_SUCCESS
328 * \retval #PSA_ERROR_INVALID_HANDLE
329 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
330 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100331 * \retval #PSA_ERROR_CORRUPTION_DETECTED
332 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100333 * \retval #PSA_ERROR_BAD_STATE
334 * The library has not been previously initialized by psa_crypto_init().
335 * It is implementation-dependent whether a failure to initialize
336 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200337 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200338psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
339 psa_key_attributes_t *attributes);
340
Gilles Peskine20628592019-04-19 19:29:50 +0200341/** Reset a key attribute structure to a freshly initialized state.
342 *
343 * You must initialize the attribute structure as described in the
344 * documentation of the type #psa_key_attributes_t before calling this
345 * function. Once the structure has been initialized, you may call this
346 * function at any time.
347 *
348 * This function frees any auxiliary resources that the structure
349 * may contain.
350 *
351 * \param[in,out] attributes The attribute structure to reset.
352 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200353void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200354
Gilles Peskine87a5e562019-04-17 12:28:25 +0200355/**@}*/
356
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357/** \defgroup key_management Key management
358 * @{
359 */
360
Gilles Peskinef535eb22018-11-30 14:08:36 +0100361/** Open a handle to an existing persistent key.
362 *
Gilles Peskine4754cde2019-05-21 15:56:29 +0200363 * Open a handle to a persistent key. A key is persistent if it was created
364 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
365 * always has a nonzero key identifier, set with psa_set_key_id() when
366 * creating the key. Implementations may provide additional pre-provisioned
Andrew Thoelke203491c2019-08-21 17:55:30 +0100367 * keys that can be opened with psa_open_key(). Such keys have a key identifier
368 * in the vendor range, as documented in the description of #psa_key_id_t.
Gilles Peskine4754cde2019-05-21 15:56:29 +0200369 *
370 * The application must eventually close the handle with psa_close_key()
371 * to release associated resources. If the application dies without calling
372 * psa_close_key(), the implementation should perform the equivalent of a
373 * call to psa_close_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100374 *
Andrew Thoelke9741b112019-08-21 18:20:41 +0100375 * Some implementations permit an application to open the same key multiple
376 * times. Applications that rely on this behavior will not be portable to
377 * implementations that only permit a single key handle to be opened. See
378 * also :ref:\`key-handles\`.
379 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100380 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100381 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100382 *
383 * \retval #PSA_SUCCESS
384 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100385 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100386 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100387 * The implementation does not have sufficient resources to open the
388 * key. This can be due to reaching an implementation limit on the
389 * number of open keys, the number of open key handles, or available
390 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200391 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100392 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100393 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100394 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100395 * \retval #PSA_ERROR_NOT_PERMITTED
396 * The specified key exists, but the application does not have the
397 * permission to access it. Note that this specification does not
398 * define any way to create such a key, but it may be possible
399 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200400 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
401 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100402 * \retval #PSA_ERROR_BAD_STATE
403 * The library has not been previously initialized by psa_crypto_init().
404 * It is implementation-dependent whether a failure to initialize
405 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100406 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200407psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100408 psa_key_handle_t *handle);
409
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100410
Gilles Peskinef535eb22018-11-30 14:08:36 +0100411/** Close a key handle.
412 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100413 * If the handle designates a volatile key, this will destroy the key material
414 * and free all associated resources, just like psa_destroy_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100415 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100416 * If this is the last open handle to a persistent key, then closing the handle
417 * will free all resources associated with the key in volatile memory. The key
418 * data in persistent storage is not affected and can be opened again later
419 * with a call to psa_open_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100420 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100421 * Closing the key handle makes the handle invalid, and the key handle
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100422 * must not be used again by the application.
Andrew Thoelke3daba812019-08-21 22:46:56 +0100423 *
424 * If the key is currently in use in a multipart operation, then closing the
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100425 * last remaining handle to the key will abort the multipart operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100426 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100427 * \param handle The key handle to close.
428 *
429 * \retval #PSA_SUCCESS
430 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100431 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100432 * \retval #PSA_ERROR_BAD_STATE
433 * The library has not been previously initialized by psa_crypto_init().
434 * It is implementation-dependent whether a failure to initialize
435 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100436 */
437psa_status_t psa_close_key(psa_key_handle_t handle);
438
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100439/**@}*/
440
441/** \defgroup import_export Key import and export
442 * @{
443 */
444
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100445/**
446 * \brief Import a key in binary format.
447 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100448 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100449 * documentation of psa_export_public_key() for the format of public keys
450 * and to the documentation of psa_export_key() for the format for
451 * other key types.
452 *
453 * This specification supports a single format for each key type.
454 * Implementations may support other formats as long as the standard
455 * format is supported. Implementations that support other formats
456 * should ensure that the formats are clearly unambiguous so as to
457 * minimize the risk that an invalid input is accidentally interpreted
458 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100459 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100460
Gilles Peskine20628592019-04-19 19:29:50 +0200461 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200462 * The key size is always determined from the
463 * \p data buffer.
464 * If the key size in \p attributes is nonzero,
465 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200466 * \param[out] handle On success, a handle to the newly created key.
467 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100468 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200469 * buffer is interpreted according to the type declared
470 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200471 * All implementations must support at least the format
472 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100473 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200474 * the chosen type. Implementations may allow other
475 * formats, but should be conservative: implementations
476 * should err on the side of rejecting content if it
477 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200478 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100479 *
Gilles Peskine28538492018-07-11 17:34:00 +0200480 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100481 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100482 * If the key is persistent, the key material and the key's metadata
483 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200484 * \retval #PSA_ERROR_ALREADY_EXISTS
485 * This is an attempt to create a persistent key, and there is
486 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200487 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200488 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200489 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200490 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200491 * The key attributes, as a whole, are invalid.
492 * \retval #PSA_ERROR_INVALID_ARGUMENT
493 * The key data is not correctly formatted.
494 * \retval #PSA_ERROR_INVALID_ARGUMENT
495 * The size in \p attributes is nonzero and does not match the size
496 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200497 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
498 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
499 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100500 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200501 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200502 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300503 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300504 * The library has not been previously initialized by psa_crypto_init().
505 * It is implementation-dependent whether a failure to initialize
506 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200508psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100509 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200510 size_t data_length,
511 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100512
513/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100514 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200515 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100516 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200517 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100518 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200519 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100520 * This function also erases any metadata such as policies and frees all
521 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200522 *
Andrew Thoelke07f16b72019-08-21 22:48:47 +0100523 * Destroying a key will invalidate all existing handles to the key.
524 *
525 * If the key is currently in use in a multipart operation, then destroying the
526 * key will abort the multipart operation.
527 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100528 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100529 *
Gilles Peskine28538492018-07-11 17:34:00 +0200530 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100531 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200532 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100533 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200534 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100535 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200536 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200537 * There was an failure in communication with the cryptoprocessor.
538 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200539 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200540 * The storage is corrupted. Implementations shall make a best effort
541 * to erase key material even in this stage, however applications
542 * should be aware that it may be impossible to guarantee that the
543 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200544 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200545 * An unexpected condition which is not a storage corruption or
546 * a communication failure occurred. The cryptoprocessor may have
547 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300548 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300549 * The library has not been previously initialized by psa_crypto_init().
550 * It is implementation-dependent whether a failure to initialize
551 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100553psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554
555/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556 * \brief Export a key in binary format.
557 *
558 * The output of this function can be passed to psa_import_key() to
559 * create an equivalent object.
560 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100561 * If the implementation of psa_import_key() supports other formats
562 * beyond the format specified here, the output from psa_export_key()
563 * must use the representation specified here, not the original
564 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100565 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100566 * For standard key types, the output format is as follows:
567 *
568 * - For symmetric keys (including MAC keys), the format is the
569 * raw bytes of the key.
570 * - For DES, the key data consists of 8 bytes. The parity bits must be
571 * correct.
572 * - For Triple-DES, the format is the concatenation of the
573 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200574 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200575 * is the non-encrypted DER encoding of the representation defined by
576 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
577 * ```
578 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200579 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200580 * modulus INTEGER, -- n
581 * publicExponent INTEGER, -- e
582 * privateExponent INTEGER, -- d
583 * prime1 INTEGER, -- p
584 * prime2 INTEGER, -- q
585 * exponent1 INTEGER, -- d mod (p-1)
586 * exponent2 INTEGER, -- d mod (q-1)
587 * coefficient INTEGER, -- (inverse of q) mod p
588 * }
589 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200590 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200591 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100592 * a representation of the private value as a `ceiling(m/8)`-byte string
593 * where `m` is the bit size associated with the curve, i.e. the bit size
594 * of the order of the curve's coordinate field. This byte string is
595 * in little-endian order for Montgomery curves (curve types
596 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
597 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
598 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100599 * This is the content of the `privateKey` field of the `ECPrivateKey`
600 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200601 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200602 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000603 * format is the representation of the private key `x` as a big-endian byte
604 * string. The length of the byte string is the private key size in bytes
605 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200606 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
607 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100608 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200609 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
610 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100611 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200612 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200613 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200614 * \param[out] data_length On success, the number of bytes
615 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100616 *
Gilles Peskine28538492018-07-11 17:34:00 +0200617 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100618 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200619 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200620 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100621 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200622 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
623 * The size of the \p data buffer is too small. You can determine a
624 * sufficient buffer size by calling
625 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
626 * where \c type is the key type
627 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200628 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
629 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200630 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100631 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100632 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300633 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300634 * The library has not been previously initialized by psa_crypto_init().
635 * It is implementation-dependent whether a failure to initialize
636 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100637 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100638psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100639 uint8_t *data,
640 size_t data_size,
641 size_t *data_length);
642
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100643/**
644 * \brief Export a public key or the public part of a key pair in binary format.
645 *
646 * The output of this function can be passed to psa_import_key() to
647 * create an object that is equivalent to the public key.
648 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000649 * This specification supports a single format for each key type.
650 * Implementations may support other formats as long as the standard
651 * format is supported. Implementations that support other formats
652 * should ensure that the formats are clearly unambiguous so as to
653 * minimize the risk that an invalid input is accidentally interpreted
654 * according to a different format.
655 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000656 * For standard key types, the output format is as follows:
657 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
658 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
659 * ```
660 * RSAPublicKey ::= SEQUENCE {
661 * modulus INTEGER, -- n
662 * publicExponent INTEGER } -- e
663 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000664 * - For elliptic curve public keys (key types for which
665 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
666 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
667 * Let `m` be the bit size associated with the curve, i.e. the bit size of
668 * `q` for a curve over `F_q`. The representation consists of:
669 * - The byte 0x04;
670 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
671 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200672 * - For Diffie-Hellman key exchange public keys (key types for which
673 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000674 * the format is the representation of the public key `y = g^x mod p` as a
675 * big-endian byte string. The length of the byte string is the length of the
676 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100677 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200678 * Exporting a public key object or the public part of a key pair is
679 * always permitted, regardless of the key's usage flags.
680 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100681 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200682 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200683 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200684 * \param[out] data_length On success, the number of bytes
685 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100686 *
Gilles Peskine28538492018-07-11 17:34:00 +0200687 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100688 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200689 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200690 * The key is neither a public key nor a key pair.
691 * \retval #PSA_ERROR_NOT_SUPPORTED
692 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
693 * The size of the \p data buffer is too small. You can determine a
694 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200695 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200696 * where \c type is the key type
697 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200698 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
699 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200700 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100701 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100702 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300703 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300704 * The library has not been previously initialized by psa_crypto_init().
705 * It is implementation-dependent whether a failure to initialize
706 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100707 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100708psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100709 uint8_t *data,
710 size_t data_size,
711 size_t *data_length);
712
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100713/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100714 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100715 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000716 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100717 * This function is primarily useful to copy a key from one location
718 * to another, since it populates a key using the material from
719 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200720 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100721 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100722 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100723 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200724 * The policy on the source key must have the usage flag
725 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200726 * This flag is sufficient to permit the copy if the key has the lifetime
727 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
728 * Some secure elements do not provide a way to copy a key without
729 * making it extractable from the secure element. If a key is located
730 * in such a secure element, then the key must have both usage flags
731 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
732 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200733 *
Gilles Peskine20628592019-04-19 19:29:50 +0200734 * The resulting key may only be used in a way that conforms to
735 * both the policy of the original key and the policy specified in
736 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100737 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200738 * usage flags on the source policy and the usage flags in \p attributes.
739 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100740 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200741 * - If either of the policies allows an algorithm and the other policy
742 * allows a wildcard-based algorithm policy that includes this algorithm,
743 * the resulting key allows the same algorithm.
744 * - If the policies do not allow any algorithm in common, this function
745 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200746 *
Gilles Peskine20628592019-04-19 19:29:50 +0200747 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100748 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200749 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100750 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200751 * \param[in] attributes The attributes for the new key.
752 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200753 * - The key type and size may be 0. If either is
754 * nonzero, it must match the corresponding
755 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200756 * - The key location (the lifetime and, for
757 * persistent keys, the key identifier) is
758 * used directly.
759 * - The policy constraints (usage flags and
760 * algorithm policy) are combined from
761 * the source key and \p attributes so that
762 * both sets of restrictions apply, as
763 * described in the documentation of this function.
764 * \param[out] target_handle On success, a handle to the newly created key.
765 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200766 *
767 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100768 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200769 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200770 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200771 * This is an attempt to create a persistent key, and there is
772 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200773 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200774 * The lifetime or identifier in \p attributes are invalid.
775 * \retval #PSA_ERROR_INVALID_ARGUMENT
776 * The policy constraints on the source and specified in
777 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200778 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200779 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200780 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100781 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200782 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
783 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100784 * The source key is not exportable and its lifetime does not
785 * allow copying it to the target's lifetime.
786 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
787 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200788 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
789 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shaw60b03202019-08-06 17:26:16 +0100790 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200791 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100792 * \retval #PSA_ERROR_BAD_STATE
793 * The library has not been previously initialized by psa_crypto_init().
794 * It is implementation-dependent whether a failure to initialize
795 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100796 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100797psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200798 const psa_key_attributes_t *attributes,
799 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100800
801/**@}*/
802
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100803/** \defgroup hash Message digests
804 * @{
805 */
806
Gilles Peskine69647a42019-01-14 20:18:12 +0100807/** Calculate the hash (digest) of a message.
808 *
809 * \note To verify the hash of a message against an
810 * expected value, use psa_hash_compare() instead.
811 *
812 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
813 * such that #PSA_ALG_IS_HASH(\p alg) is true).
814 * \param[in] input Buffer containing the message to hash.
815 * \param input_length Size of the \p input buffer in bytes.
816 * \param[out] hash Buffer where the hash is to be written.
817 * \param hash_size Size of the \p hash buffer in bytes.
818 * \param[out] hash_length On success, the number of bytes
819 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100820 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100821 *
822 * \retval #PSA_SUCCESS
823 * Success.
824 * \retval #PSA_ERROR_NOT_SUPPORTED
825 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100826 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
827 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100828 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
829 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
830 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200831 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100832 * \retval #PSA_ERROR_BAD_STATE
833 * The library has not been previously initialized by psa_crypto_init().
834 * It is implementation-dependent whether a failure to initialize
835 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100836 */
837psa_status_t psa_hash_compute(psa_algorithm_t alg,
838 const uint8_t *input,
839 size_t input_length,
840 uint8_t *hash,
841 size_t hash_size,
842 size_t *hash_length);
843
844/** Calculate the hash (digest) of a message and compare it with a
845 * reference value.
846 *
847 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
848 * such that #PSA_ALG_IS_HASH(\p alg) is true).
849 * \param[in] input Buffer containing the message to hash.
850 * \param input_length Size of the \p input buffer in bytes.
851 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100852 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100853 *
854 * \retval #PSA_SUCCESS
855 * The expected hash is identical to the actual hash of the input.
856 * \retval #PSA_ERROR_INVALID_SIGNATURE
857 * The hash of the message was calculated successfully, but it
858 * differs from the expected hash.
859 * \retval #PSA_ERROR_NOT_SUPPORTED
860 * \p alg is not supported or is not a hash algorithm.
861 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
862 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
863 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200864 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100865 * \retval #PSA_ERROR_BAD_STATE
866 * The library has not been previously initialized by psa_crypto_init().
867 * It is implementation-dependent whether a failure to initialize
868 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100869 */
870psa_status_t psa_hash_compare(psa_algorithm_t alg,
871 const uint8_t *input,
872 size_t input_length,
873 const uint8_t *hash,
874 const size_t hash_length);
875
Gilles Peskine308b91d2018-02-08 09:47:44 +0100876/** The type of the state data structure for multipart hash operations.
877 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000878 * Before calling any function on a hash operation object, the application must
879 * initialize it by any of the following means:
880 * - Set the structure to all-bits-zero, for example:
881 * \code
882 * psa_hash_operation_t operation;
883 * memset(&operation, 0, sizeof(operation));
884 * \endcode
885 * - Initialize the structure to logical zero values, for example:
886 * \code
887 * psa_hash_operation_t operation = {0};
888 * \endcode
889 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
890 * for example:
891 * \code
892 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
893 * \endcode
894 * - Assign the result of the function psa_hash_operation_init()
895 * to the structure, for example:
896 * \code
897 * psa_hash_operation_t operation;
898 * operation = psa_hash_operation_init();
899 * \endcode
900 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100901 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100902 * make any assumptions about the content of this structure except
903 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100904typedef struct psa_hash_operation_s psa_hash_operation_t;
905
Jaeden Amero6a25b412019-01-04 11:47:44 +0000906/** \def PSA_HASH_OPERATION_INIT
907 *
908 * This macro returns a suitable initializer for a hash operation object
909 * of type #psa_hash_operation_t.
910 */
911#ifdef __DOXYGEN_ONLY__
912/* This is an example definition for documentation purposes.
913 * Implementations should define a suitable value in `crypto_struct.h`.
914 */
915#define PSA_HASH_OPERATION_INIT {0}
916#endif
917
918/** Return an initial value for a hash operation object.
919 */
920static psa_hash_operation_t psa_hash_operation_init(void);
921
Gilles Peskinef45adda2019-01-14 18:29:18 +0100922/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100923 *
924 * The sequence of operations to calculate a hash (message digest)
925 * is as follows:
926 * -# Allocate an operation object which will be passed to all the functions
927 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000928 * -# Initialize the operation object with one of the methods described in the
929 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200930 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100931 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100932 * of the message each time. The hash that is calculated is the hash
933 * of the concatenation of these messages in order.
934 * -# To calculate the hash, call psa_hash_finish().
935 * To compare the hash with an expected value, call psa_hash_verify().
936 *
937 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000938 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100939 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200940 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100941 * eventually terminate the operation. The following events terminate an
942 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100943 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100944 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100945 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000946 * \param[in,out] operation The operation object to set up. It must have
947 * been initialized as per the documentation for
948 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200949 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
950 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100951 *
Gilles Peskine28538492018-07-11 17:34:00 +0200952 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100953 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200954 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200955 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100956 * \retval #PSA_ERROR_BAD_STATE
957 * The operation state is not valid (already set up and not
958 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200959 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
960 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
961 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200962 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100963 * \retval #PSA_ERROR_BAD_STATE
964 * The library has not been previously initialized by psa_crypto_init().
965 * It is implementation-dependent whether a failure to initialize
966 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100967 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200968psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100969 psa_algorithm_t alg);
970
Gilles Peskine308b91d2018-02-08 09:47:44 +0100971/** Add a message fragment to a multipart hash operation.
972 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200973 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100974 *
975 * If this function returns an error status, the operation becomes inactive.
976 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200977 * \param[in,out] operation Active hash operation.
978 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200979 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100980 *
Gilles Peskine28538492018-07-11 17:34:00 +0200981 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100982 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200983 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100984 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200985 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
986 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
987 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200988 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100989 * \retval #PSA_ERROR_BAD_STATE
990 * The library has not been previously initialized by psa_crypto_init().
991 * It is implementation-dependent whether a failure to initialize
992 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100993 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100994psa_status_t psa_hash_update(psa_hash_operation_t *operation,
995 const uint8_t *input,
996 size_t input_length);
997
Gilles Peskine308b91d2018-02-08 09:47:44 +0100998/** Finish the calculation of the hash of a message.
999 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001000 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001001 * This function calculates the hash of the message formed by concatenating
1002 * the inputs passed to preceding calls to psa_hash_update().
1003 *
1004 * When this function returns, the operation becomes inactive.
1005 *
1006 * \warning Applications should not call this function if they expect
1007 * a specific value for the hash. Call psa_hash_verify() instead.
1008 * Beware that comparing integrity or authenticity data such as
1009 * hash values with a function such as \c memcmp is risky
1010 * because the time taken by the comparison may leak information
1011 * about the hashed data which could allow an attacker to guess
1012 * a valid hash and thereby bypass security controls.
1013 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001014 * \param[in,out] operation Active hash operation.
1015 * \param[out] hash Buffer where the hash is to be written.
1016 * \param hash_size Size of the \p hash buffer in bytes.
1017 * \param[out] hash_length On success, the number of bytes
1018 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001019 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001020 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001021 *
Gilles Peskine28538492018-07-11 17:34:00 +02001022 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001023 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001024 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001025 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001026 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001027 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001028 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001029 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001030 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1031 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1032 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001033 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001034 * \retval #PSA_ERROR_BAD_STATE
1035 * The library has not been previously initialized by psa_crypto_init().
1036 * It is implementation-dependent whether a failure to initialize
1037 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001038 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001039psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1040 uint8_t *hash,
1041 size_t hash_size,
1042 size_t *hash_length);
1043
Gilles Peskine308b91d2018-02-08 09:47:44 +01001044/** Finish the calculation of the hash of a message and compare it with
1045 * an expected value.
1046 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001047 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001048 * This function calculates the hash of the message formed by concatenating
1049 * the inputs passed to preceding calls to psa_hash_update(). It then
1050 * compares the calculated hash with the expected hash passed as a
1051 * parameter to this function.
1052 *
1053 * When this function returns, the operation becomes inactive.
1054 *
Gilles Peskine19067982018-03-20 17:54:53 +01001055 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001056 * comparison between the actual hash and the expected hash is performed
1057 * in constant time.
1058 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001059 * \param[in,out] operation Active hash operation.
1060 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001061 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001062 *
Gilles Peskine28538492018-07-11 17:34:00 +02001063 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001064 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001065 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001066 * The hash of the message was calculated successfully, but it
1067 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001068 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001069 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001070 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1071 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1072 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001073 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001074 * \retval #PSA_ERROR_BAD_STATE
1075 * The library has not been previously initialized by psa_crypto_init().
1076 * It is implementation-dependent whether a failure to initialize
1077 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001078 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001079psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1080 const uint8_t *hash,
1081 size_t hash_length);
1082
Gilles Peskine308b91d2018-02-08 09:47:44 +01001083/** Abort a hash operation.
1084 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001085 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001086 * \p operation structure itself. Once aborted, the operation object
1087 * can be reused for another operation by calling
1088 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001089 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001090 * You may call this function any time after the operation object has
1091 * been initialized by any of the following methods:
1092 * - A call to psa_hash_setup(), whether it succeeds or not.
1093 * - Initializing the \c struct to all-bits-zero.
1094 * - Initializing the \c struct to logical zeros, e.g.
1095 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001096 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001097 * In particular, calling psa_hash_abort() after the operation has been
1098 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1099 * psa_hash_verify() is safe and has no effect.
1100 *
1101 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001102 *
Gilles Peskine28538492018-07-11 17:34:00 +02001103 * \retval #PSA_SUCCESS
1104 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001105 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001106 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1107 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001108 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001109 * \retval #PSA_ERROR_BAD_STATE
1110 * The library has not been previously initialized by psa_crypto_init().
1111 * It is implementation-dependent whether a failure to initialize
1112 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001113 */
1114psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001115
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001116/** Clone a hash operation.
1117 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001118 * This function copies the state of an ongoing hash operation to
1119 * a new operation object. In other words, this function is equivalent
1120 * to calling psa_hash_setup() on \p target_operation with the same
1121 * algorithm that \p source_operation was set up for, then
1122 * psa_hash_update() on \p target_operation with the same input that
1123 * that was passed to \p source_operation. After this function returns, the
1124 * two objects are independent, i.e. subsequent calls involving one of
1125 * the objects do not affect the other object.
1126 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001127 * \param[in] source_operation The active hash operation to clone.
1128 * \param[in,out] target_operation The operation object to set up.
1129 * It must be initialized but not active.
1130 *
1131 * \retval #PSA_SUCCESS
1132 * \retval #PSA_ERROR_BAD_STATE
1133 * \p source_operation is not an active hash operation.
1134 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001135 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001136 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1137 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001138 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001139 * \retval #PSA_ERROR_BAD_STATE
1140 * The library has not been previously initialized by psa_crypto_init().
1141 * It is implementation-dependent whether a failure to initialize
1142 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001143 */
1144psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1145 psa_hash_operation_t *target_operation);
1146
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001147/**@}*/
1148
Gilles Peskine8c9def32018-02-08 10:02:12 +01001149/** \defgroup MAC Message authentication codes
1150 * @{
1151 */
1152
Gilles Peskine69647a42019-01-14 20:18:12 +01001153/** Calculate the MAC (message authentication code) of a message.
1154 *
1155 * \note To verify the MAC of a message against an
1156 * expected value, use psa_mac_verify() instead.
1157 * Beware that comparing integrity or authenticity data such as
1158 * MAC values with a function such as \c memcmp is risky
1159 * because the time taken by the comparison may leak information
1160 * about the MAC value which could allow an attacker to guess
1161 * a valid MAC and thereby bypass security controls.
1162 *
1163 * \param handle Handle to the key to use for the operation.
1164 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001165 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001166 * \param[in] input Buffer containing the input message.
1167 * \param input_length Size of the \p input buffer in bytes.
1168 * \param[out] mac Buffer where the MAC value is to be written.
1169 * \param mac_size Size of the \p mac buffer in bytes.
1170 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001171 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001172 *
1173 * \retval #PSA_SUCCESS
1174 * Success.
1175 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001176 * \retval #PSA_ERROR_NOT_PERMITTED
1177 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001178 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001179 * \retval #PSA_ERROR_NOT_SUPPORTED
1180 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001181 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1182 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001183 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1184 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1185 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001186 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001187 * \retval #PSA_ERROR_STORAGE_FAILURE
1188 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001189 * \retval #PSA_ERROR_BAD_STATE
1190 * The library has not been previously initialized by psa_crypto_init().
1191 * It is implementation-dependent whether a failure to initialize
1192 * results in this error code.
1193 */
1194psa_status_t psa_mac_compute(psa_key_handle_t handle,
1195 psa_algorithm_t alg,
1196 const uint8_t *input,
1197 size_t input_length,
1198 uint8_t *mac,
1199 size_t mac_size,
1200 size_t *mac_length);
1201
1202/** Calculate the MAC of a message and compare it with a reference value.
1203 *
1204 * \param handle Handle to the key to use for the operation.
1205 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001206 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001207 * \param[in] input Buffer containing the input message.
1208 * \param input_length Size of the \p input buffer in bytes.
1209 * \param[out] mac Buffer containing the expected MAC value.
1210 * \param mac_length Size of the \p mac buffer in bytes.
1211 *
1212 * \retval #PSA_SUCCESS
1213 * The expected MAC is identical to the actual MAC of the input.
1214 * \retval #PSA_ERROR_INVALID_SIGNATURE
1215 * The MAC of the message was calculated successfully, but it
1216 * differs from the expected value.
1217 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001218 * \retval #PSA_ERROR_NOT_PERMITTED
1219 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001220 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001221 * \retval #PSA_ERROR_NOT_SUPPORTED
1222 * \p alg is not supported or is not a MAC algorithm.
1223 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1224 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1225 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001226 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001227 * \retval #PSA_ERROR_STORAGE_FAILURE
1228 * The key could not be retrieved from storage.
1229 * \retval #PSA_ERROR_BAD_STATE
1230 * The library has not been previously initialized by psa_crypto_init().
1231 * It is implementation-dependent whether a failure to initialize
1232 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001233 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001234psa_status_t psa_mac_verify(psa_key_handle_t handle,
1235 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001236 const uint8_t *input,
1237 size_t input_length,
1238 const uint8_t *mac,
1239 const size_t mac_length);
1240
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001241/** The type of the state data structure for multipart MAC operations.
1242 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001243 * Before calling any function on a MAC operation object, the application must
1244 * initialize it by any of the following means:
1245 * - Set the structure to all-bits-zero, for example:
1246 * \code
1247 * psa_mac_operation_t operation;
1248 * memset(&operation, 0, sizeof(operation));
1249 * \endcode
1250 * - Initialize the structure to logical zero values, for example:
1251 * \code
1252 * psa_mac_operation_t operation = {0};
1253 * \endcode
1254 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1255 * for example:
1256 * \code
1257 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1258 * \endcode
1259 * - Assign the result of the function psa_mac_operation_init()
1260 * to the structure, for example:
1261 * \code
1262 * psa_mac_operation_t operation;
1263 * operation = psa_mac_operation_init();
1264 * \endcode
1265 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001266 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001267 * make any assumptions about the content of this structure except
1268 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001269typedef struct psa_mac_operation_s psa_mac_operation_t;
1270
Jaeden Amero769ce272019-01-04 11:48:03 +00001271/** \def PSA_MAC_OPERATION_INIT
1272 *
1273 * This macro returns a suitable initializer for a MAC operation object of type
1274 * #psa_mac_operation_t.
1275 */
1276#ifdef __DOXYGEN_ONLY__
1277/* This is an example definition for documentation purposes.
1278 * Implementations should define a suitable value in `crypto_struct.h`.
1279 */
1280#define PSA_MAC_OPERATION_INIT {0}
1281#endif
1282
1283/** Return an initial value for a MAC operation object.
1284 */
1285static psa_mac_operation_t psa_mac_operation_init(void);
1286
Gilles Peskinef45adda2019-01-14 18:29:18 +01001287/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001288 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001289 * This function sets up the calculation of the MAC
1290 * (message authentication code) of a byte string.
1291 * To verify the MAC of a message against an
1292 * expected value, use psa_mac_verify_setup() instead.
1293 *
1294 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001295 * -# Allocate an operation object which will be passed to all the functions
1296 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001297 * -# Initialize the operation object with one of the methods described in the
1298 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001299 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001300 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1301 * of the message each time. The MAC that is calculated is the MAC
1302 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001303 * -# At the end of the message, call psa_mac_sign_finish() to finish
1304 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001305 *
1306 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001307 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001308 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001309 * After a successful call to psa_mac_sign_setup(), the application must
1310 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001311 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001312 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001313 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001314 * \param[in,out] operation The operation object to set up. It must have
1315 * been initialized as per the documentation for
1316 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001317 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001318 * It must remain valid until the operation
1319 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001320 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001321 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001322 *
Gilles Peskine28538492018-07-11 17:34:00 +02001323 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001324 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001325 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001326 * \retval #PSA_ERROR_NOT_PERMITTED
1327 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001328 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001329 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001330 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001331 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1332 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1333 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001334 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001335 * \retval #PSA_ERROR_STORAGE_FAILURE
1336 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001337 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001338 * The operation state is not valid (already set up and not
1339 * subsequently completed).
1340 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001341 * The library has not been previously initialized by psa_crypto_init().
1342 * It is implementation-dependent whether a failure to initialize
1343 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001344 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001345psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001346 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001347 psa_algorithm_t alg);
1348
Gilles Peskinef45adda2019-01-14 18:29:18 +01001349/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001350 *
1351 * This function sets up the verification of the MAC
1352 * (message authentication code) of a byte string against an expected value.
1353 *
1354 * The sequence of operations to verify a MAC is as follows:
1355 * -# Allocate an operation object which will be passed to all the functions
1356 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001357 * -# Initialize the operation object with one of the methods described in the
1358 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001359 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001360 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1361 * of the message each time. The MAC that is calculated is the MAC
1362 * of the concatenation of these messages in order.
1363 * -# At the end of the message, call psa_mac_verify_finish() to finish
1364 * calculating the actual MAC of the message and verify it against
1365 * the expected value.
1366 *
1367 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001368 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001369 *
1370 * After a successful call to psa_mac_verify_setup(), the application must
1371 * eventually terminate the operation through one of the following methods:
1372 * - A failed call to psa_mac_update().
1373 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1374 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001375 * \param[in,out] operation The operation object to set up. It must have
1376 * been initialized as per the documentation for
1377 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001378 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001379 * It must remain valid until the operation
1380 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001381 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1382 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001383 *
Gilles Peskine28538492018-07-11 17:34:00 +02001384 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001385 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001386 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001387 * \retval #PSA_ERROR_NOT_PERMITTED
1388 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001389 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001390 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001391 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001392 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1393 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1394 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001395 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001396 * \retval #PSA_ERROR_STORAGE_FAILURE
1397 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001398 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001399 * The operation state is not valid (already set up and not
1400 * subsequently completed).
1401 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001402 * The library has not been previously initialized by psa_crypto_init().
1403 * It is implementation-dependent whether a failure to initialize
1404 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001405 */
1406psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001407 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001408 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001409
Gilles Peskinedcd14942018-07-12 00:30:52 +02001410/** Add a message fragment to a multipart MAC operation.
1411 *
1412 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1413 * before calling this function.
1414 *
1415 * If this function returns an error status, the operation becomes inactive.
1416 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001417 * \param[in,out] operation Active MAC operation.
1418 * \param[in] input Buffer containing the message fragment to add to
1419 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001420 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001421 *
1422 * \retval #PSA_SUCCESS
1423 * Success.
1424 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001425 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001426 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1427 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1428 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001429 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001430 * \retval #PSA_ERROR_BAD_STATE
1431 * The library has not been previously initialized by psa_crypto_init().
1432 * It is implementation-dependent whether a failure to initialize
1433 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001434 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001435psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1436 const uint8_t *input,
1437 size_t input_length);
1438
Gilles Peskinedcd14942018-07-12 00:30:52 +02001439/** Finish the calculation of the MAC of a message.
1440 *
1441 * The application must call psa_mac_sign_setup() before calling this function.
1442 * This function calculates the MAC of the message formed by concatenating
1443 * the inputs passed to preceding calls to psa_mac_update().
1444 *
1445 * When this function returns, the operation becomes inactive.
1446 *
1447 * \warning Applications should not call this function if they expect
1448 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1449 * Beware that comparing integrity or authenticity data such as
1450 * MAC values with a function such as \c memcmp is risky
1451 * because the time taken by the comparison may leak information
1452 * about the MAC value which could allow an attacker to guess
1453 * a valid MAC and thereby bypass security controls.
1454 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001455 * \param[in,out] operation Active MAC operation.
1456 * \param[out] mac Buffer where the MAC value is to be written.
1457 * \param mac_size Size of the \p mac buffer in bytes.
1458 * \param[out] mac_length On success, the number of bytes
1459 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001460 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001461 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001462 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001463 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001464 *
1465 * \retval #PSA_SUCCESS
1466 * Success.
1467 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001468 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001469 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001470 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001471 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1472 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1473 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1474 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001475 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001476 * \retval #PSA_ERROR_BAD_STATE
1477 * The library has not been previously initialized by psa_crypto_init().
1478 * It is implementation-dependent whether a failure to initialize
1479 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001480 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001481psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1482 uint8_t *mac,
1483 size_t mac_size,
1484 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001485
Gilles Peskinedcd14942018-07-12 00:30:52 +02001486/** Finish the calculation of the MAC of a message and compare it with
1487 * an expected value.
1488 *
1489 * The application must call psa_mac_verify_setup() before calling this function.
1490 * This function calculates the MAC of the message formed by concatenating
1491 * the inputs passed to preceding calls to psa_mac_update(). It then
1492 * compares the calculated MAC with the expected MAC passed as a
1493 * parameter to this function.
1494 *
1495 * When this function returns, the operation becomes inactive.
1496 *
1497 * \note Implementations shall make the best effort to ensure that the
1498 * comparison between the actual MAC and the expected MAC is performed
1499 * in constant time.
1500 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001501 * \param[in,out] operation Active MAC operation.
1502 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001503 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001504 *
1505 * \retval #PSA_SUCCESS
1506 * The expected MAC is identical to the actual MAC of the message.
1507 * \retval #PSA_ERROR_INVALID_SIGNATURE
1508 * The MAC of the message was calculated successfully, but it
1509 * differs from the expected MAC.
1510 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001511 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001512 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1513 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1514 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001515 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001516 * \retval #PSA_ERROR_BAD_STATE
1517 * The library has not been previously initialized by psa_crypto_init().
1518 * It is implementation-dependent whether a failure to initialize
1519 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001520 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001521psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1522 const uint8_t *mac,
1523 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001524
Gilles Peskinedcd14942018-07-12 00:30:52 +02001525/** Abort a MAC operation.
1526 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001527 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001528 * \p operation structure itself. Once aborted, the operation object
1529 * can be reused for another operation by calling
1530 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001531 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001532 * You may call this function any time after the operation object has
1533 * been initialized by any of the following methods:
1534 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1535 * it succeeds or not.
1536 * - Initializing the \c struct to all-bits-zero.
1537 * - Initializing the \c struct to logical zeros, e.g.
1538 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001539 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001540 * In particular, calling psa_mac_abort() after the operation has been
1541 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1542 * psa_mac_verify_finish() is safe and has no effect.
1543 *
1544 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001545 *
1546 * \retval #PSA_SUCCESS
1547 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001548 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001549 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1550 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001551 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001552 * \retval #PSA_ERROR_BAD_STATE
1553 * The library has not been previously initialized by psa_crypto_init().
1554 * It is implementation-dependent whether a failure to initialize
1555 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001556 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001557psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1558
1559/**@}*/
1560
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001561/** \defgroup cipher Symmetric ciphers
1562 * @{
1563 */
1564
Gilles Peskine69647a42019-01-14 20:18:12 +01001565/** Encrypt a message using a symmetric cipher.
1566 *
1567 * This function encrypts a message with a random IV (initialization
1568 * vector).
1569 *
1570 * \param handle Handle to the key to use for the operation.
1571 * It must remain valid until the operation
1572 * terminates.
1573 * \param alg The cipher algorithm to compute
1574 * (\c PSA_ALG_XXX value such that
1575 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1576 * \param[in] input Buffer containing the message to encrypt.
1577 * \param input_length Size of the \p input buffer in bytes.
1578 * \param[out] output Buffer where the output is to be written.
1579 * The output contains the IV followed by
1580 * the ciphertext proper.
1581 * \param output_size Size of the \p output buffer in bytes.
1582 * \param[out] output_length On success, the number of bytes
1583 * that make up the output.
1584 *
1585 * \retval #PSA_SUCCESS
1586 * Success.
1587 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001588 * \retval #PSA_ERROR_NOT_PERMITTED
1589 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001590 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001591 * \retval #PSA_ERROR_NOT_SUPPORTED
1592 * \p alg is not supported or is not a cipher algorithm.
1593 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1594 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1595 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1596 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001597 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001598 * \retval #PSA_ERROR_BAD_STATE
1599 * The library has not been previously initialized by psa_crypto_init().
1600 * It is implementation-dependent whether a failure to initialize
1601 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001602 */
1603psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1604 psa_algorithm_t alg,
1605 const uint8_t *input,
1606 size_t input_length,
1607 uint8_t *output,
1608 size_t output_size,
1609 size_t *output_length);
1610
1611/** Decrypt a message using a symmetric cipher.
1612 *
1613 * This function decrypts a message encrypted with a symmetric cipher.
1614 *
1615 * \param handle Handle to the key to use for the operation.
1616 * It must remain valid until the operation
1617 * terminates.
1618 * \param alg The cipher algorithm to compute
1619 * (\c PSA_ALG_XXX value such that
1620 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1621 * \param[in] input Buffer containing the message to decrypt.
1622 * This consists of the IV followed by the
1623 * ciphertext proper.
1624 * \param input_length Size of the \p input buffer in bytes.
1625 * \param[out] output Buffer where the plaintext is to be written.
1626 * \param output_size Size of the \p output buffer in bytes.
1627 * \param[out] output_length On success, the number of bytes
1628 * that make up the output.
1629 *
1630 * \retval #PSA_SUCCESS
1631 * Success.
1632 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001633 * \retval #PSA_ERROR_NOT_PERMITTED
1634 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001635 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001636 * \retval #PSA_ERROR_NOT_SUPPORTED
1637 * \p alg is not supported or is not a cipher algorithm.
1638 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1639 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1640 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1641 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001642 * \retval #PSA_ERROR_BAD_STATE
1643 * The library has not been previously initialized by psa_crypto_init().
1644 * It is implementation-dependent whether a failure to initialize
1645 * results in this error code. * \retval #PSA_ERROR_CORRUPTION_DETECTED
1646
Gilles Peskine69647a42019-01-14 20:18:12 +01001647 */
1648psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1649 psa_algorithm_t alg,
1650 const uint8_t *input,
1651 size_t input_length,
1652 uint8_t *output,
1653 size_t output_size,
1654 size_t *output_length);
1655
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001656/** The type of the state data structure for multipart cipher operations.
1657 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001658 * Before calling any function on a cipher operation object, the application
1659 * must initialize it by any of the following means:
1660 * - Set the structure to all-bits-zero, for example:
1661 * \code
1662 * psa_cipher_operation_t operation;
1663 * memset(&operation, 0, sizeof(operation));
1664 * \endcode
1665 * - Initialize the structure to logical zero values, for example:
1666 * \code
1667 * psa_cipher_operation_t operation = {0};
1668 * \endcode
1669 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1670 * for example:
1671 * \code
1672 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1673 * \endcode
1674 * - Assign the result of the function psa_cipher_operation_init()
1675 * to the structure, for example:
1676 * \code
1677 * psa_cipher_operation_t operation;
1678 * operation = psa_cipher_operation_init();
1679 * \endcode
1680 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001681 * This is an implementation-defined \c struct. Applications should not
1682 * make any assumptions about the content of this structure except
1683 * as directed by the documentation of a specific implementation. */
1684typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1685
Jaeden Amero5bae2272019-01-04 11:48:27 +00001686/** \def PSA_CIPHER_OPERATION_INIT
1687 *
1688 * This macro returns a suitable initializer for a cipher operation object of
1689 * type #psa_cipher_operation_t.
1690 */
1691#ifdef __DOXYGEN_ONLY__
1692/* This is an example definition for documentation purposes.
1693 * Implementations should define a suitable value in `crypto_struct.h`.
1694 */
1695#define PSA_CIPHER_OPERATION_INIT {0}
1696#endif
1697
1698/** Return an initial value for a cipher operation object.
1699 */
1700static psa_cipher_operation_t psa_cipher_operation_init(void);
1701
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001702/** Set the key for a multipart symmetric encryption operation.
1703 *
1704 * The sequence of operations to encrypt a message with a symmetric cipher
1705 * is as follows:
1706 * -# Allocate an operation object which will be passed to all the functions
1707 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001708 * -# Initialize the operation object with one of the methods described in the
1709 * documentation for #psa_cipher_operation_t, e.g.
1710 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001711 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001712 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001713 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001714 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001715 * requires a specific IV value.
1716 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1717 * of the message each time.
1718 * -# Call psa_cipher_finish().
1719 *
1720 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001721 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001722 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001723 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001724 * eventually terminate the operation. The following events terminate an
1725 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001726 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001727 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001728 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001729 * \param[in,out] operation The operation object to set up. It must have
1730 * been initialized as per the documentation for
1731 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001732 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001733 * It must remain valid until the operation
1734 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001735 * \param alg The cipher algorithm to compute
1736 * (\c PSA_ALG_XXX value such that
1737 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001738 *
Gilles Peskine28538492018-07-11 17:34:00 +02001739 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001740 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001741 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001742 * \retval #PSA_ERROR_NOT_PERMITTED
1743 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001744 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001745 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001746 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001747 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1748 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1749 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001750 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001751 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001752 * The operation state is not valid (already set up and not
1753 * subsequently completed).
1754 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001755 * The library has not been previously initialized by psa_crypto_init().
1756 * It is implementation-dependent whether a failure to initialize
1757 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001758 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001759psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001760 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001761 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001762
1763/** Set the key for a multipart symmetric decryption operation.
1764 *
1765 * The sequence of operations to decrypt a message with a symmetric cipher
1766 * is as follows:
1767 * -# Allocate an operation object which will be passed to all the functions
1768 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001769 * -# Initialize the operation object with one of the methods described in the
1770 * documentation for #psa_cipher_operation_t, e.g.
1771 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001772 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001773 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001774 * decryption. If the IV is prepended to the ciphertext, you can call
1775 * psa_cipher_update() on a buffer containing the IV followed by the
1776 * beginning of the message.
1777 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1778 * of the message each time.
1779 * -# Call psa_cipher_finish().
1780 *
1781 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001782 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001783 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001784 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001785 * eventually terminate the operation. The following events terminate an
1786 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001787 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001788 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001789 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001790 * \param[in,out] operation The operation object to set up. It must have
1791 * been initialized as per the documentation for
1792 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001793 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001794 * It must remain valid until the operation
1795 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001796 * \param alg The cipher algorithm to compute
1797 * (\c PSA_ALG_XXX value such that
1798 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001799 *
Gilles Peskine28538492018-07-11 17:34:00 +02001800 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001801 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001802 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001803 * \retval #PSA_ERROR_NOT_PERMITTED
1804 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001805 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001806 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001807 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001808 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1809 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1810 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001811 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001812 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001813 * The operation state is not valid (already set up and not
1814 * subsequently completed).
1815 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001816 * The library has not been previously initialized by psa_crypto_init().
1817 * It is implementation-dependent whether a failure to initialize
1818 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001819 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001820psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001821 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001822 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001823
Gilles Peskinedcd14942018-07-12 00:30:52 +02001824/** Generate an IV for a symmetric encryption operation.
1825 *
1826 * This function generates a random IV (initialization vector), nonce
1827 * or initial counter value for the encryption operation as appropriate
1828 * for the chosen algorithm, key type and key size.
1829 *
1830 * The application must call psa_cipher_encrypt_setup() before
1831 * calling this function.
1832 *
1833 * If this function returns an error status, the operation becomes inactive.
1834 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001835 * \param[in,out] operation Active cipher operation.
1836 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001837 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001838 * \param[out] iv_length On success, the number of bytes of the
1839 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001840 *
1841 * \retval #PSA_SUCCESS
1842 * Success.
1843 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001844 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001845 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001846 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001847 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1848 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1849 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001850 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001851 * \retval #PSA_ERROR_BAD_STATE
1852 * The library has not been previously initialized by psa_crypto_init().
1853 * It is implementation-dependent whether a failure to initialize
1854 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001855 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001856psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001857 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001858 size_t iv_size,
1859 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001860
Gilles Peskinedcd14942018-07-12 00:30:52 +02001861/** Set the IV for a symmetric encryption or decryption operation.
1862 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001863 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001864 * or initial counter value for the encryption or decryption operation.
1865 *
1866 * The application must call psa_cipher_encrypt_setup() before
1867 * calling this function.
1868 *
1869 * If this function returns an error status, the operation becomes inactive.
1870 *
1871 * \note When encrypting, applications should use psa_cipher_generate_iv()
1872 * instead of this function, unless implementing a protocol that requires
1873 * a non-random IV.
1874 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001875 * \param[in,out] operation Active cipher operation.
1876 * \param[in] iv Buffer containing the IV to use.
1877 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001878 *
1879 * \retval #PSA_SUCCESS
1880 * Success.
1881 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001882 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001883 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001884 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001885 * or the chosen algorithm does not use an IV.
1886 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1887 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1888 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001889 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001890 * \retval #PSA_ERROR_BAD_STATE
1891 * The library has not been previously initialized by psa_crypto_init().
1892 * It is implementation-dependent whether a failure to initialize
1893 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001894 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001895psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001896 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001897 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001898
Gilles Peskinedcd14942018-07-12 00:30:52 +02001899/** Encrypt or decrypt a message fragment in an active cipher operation.
1900 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001901 * Before calling this function, you must:
1902 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1903 * The choice of setup function determines whether this function
1904 * encrypts or decrypts its input.
1905 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1906 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001907 *
1908 * If this function returns an error status, the operation becomes inactive.
1909 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001910 * \param[in,out] operation Active cipher operation.
1911 * \param[in] input Buffer containing the message fragment to
1912 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001913 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001914 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001915 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001916 * \param[out] output_length On success, the number of bytes
1917 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001918 *
1919 * \retval #PSA_SUCCESS
1920 * Success.
1921 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001922 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001923 * not set, or already completed).
1924 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1925 * The size of the \p output buffer is too small.
1926 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1927 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1928 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001929 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001930 * \retval #PSA_ERROR_BAD_STATE
1931 * The library has not been previously initialized by psa_crypto_init().
1932 * It is implementation-dependent whether a failure to initialize
1933 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001934 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001935psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1936 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001937 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001938 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001939 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001940 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001941
Gilles Peskinedcd14942018-07-12 00:30:52 +02001942/** Finish encrypting or decrypting a message in a cipher operation.
1943 *
1944 * The application must call psa_cipher_encrypt_setup() or
1945 * psa_cipher_decrypt_setup() before calling this function. The choice
1946 * of setup function determines whether this function encrypts or
1947 * decrypts its input.
1948 *
1949 * This function finishes the encryption or decryption of the message
1950 * formed by concatenating the inputs passed to preceding calls to
1951 * psa_cipher_update().
1952 *
1953 * When this function returns, the operation becomes inactive.
1954 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001955 * \param[in,out] operation Active cipher operation.
1956 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001957 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001958 * \param[out] output_length On success, the number of bytes
1959 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001960 *
1961 * \retval #PSA_SUCCESS
1962 * Success.
1963 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001964 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001965 * not set, or already completed).
1966 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1967 * The size of the \p output buffer is too small.
1968 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1969 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1970 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001971 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001972 * \retval #PSA_ERROR_BAD_STATE
1973 * The library has not been previously initialized by psa_crypto_init().
1974 * It is implementation-dependent whether a failure to initialize
1975 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001976 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001977psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001978 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001979 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001980 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001981
Gilles Peskinedcd14942018-07-12 00:30:52 +02001982/** Abort a cipher operation.
1983 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001984 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001985 * \p operation structure itself. Once aborted, the operation object
1986 * can be reused for another operation by calling
1987 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001988 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001989 * You may call this function any time after the operation object has
1990 * been initialized by any of the following methods:
1991 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1992 * whether it succeeds or not.
1993 * - Initializing the \c struct to all-bits-zero.
1994 * - Initializing the \c struct to logical zeros, e.g.
1995 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001996 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001997 * In particular, calling psa_cipher_abort() after the operation has been
1998 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1999 * is safe and has no effect.
2000 *
2001 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002002 *
2003 * \retval #PSA_SUCCESS
2004 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002005 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002006 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2007 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002008 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002009 * \retval #PSA_ERROR_BAD_STATE
2010 * The library has not been previously initialized by psa_crypto_init().
2011 * It is implementation-dependent whether a failure to initialize
2012 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002013 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002014psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2015
2016/**@}*/
2017
Gilles Peskine3b555712018-03-03 21:27:57 +01002018/** \defgroup aead Authenticated encryption with associated data (AEAD)
2019 * @{
2020 */
2021
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002022/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002023 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002024 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002025 * \param alg The AEAD algorithm to compute
2026 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002027 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002028 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002029 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002030 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002031 * but not encrypted.
2032 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002033 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002034 * encrypted.
2035 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002036 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002037 * encrypted data. The additional data is not
2038 * part of this output. For algorithms where the
2039 * encrypted data and the authentication tag
2040 * are defined as separate outputs, the
2041 * authentication tag is appended to the
2042 * encrypted data.
2043 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2044 * This must be at least
2045 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2046 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002047 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002048 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002049 *
Gilles Peskine28538492018-07-11 17:34:00 +02002050 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002051 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002052 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002053 * \retval #PSA_ERROR_NOT_PERMITTED
2054 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002055 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002056 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002057 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002058 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2059 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2060 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002061 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002062 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002063 * The library has not been previously initialized by psa_crypto_init().
2064 * It is implementation-dependent whether a failure to initialize
2065 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002066 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002067psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002068 psa_algorithm_t alg,
2069 const uint8_t *nonce,
2070 size_t nonce_length,
2071 const uint8_t *additional_data,
2072 size_t additional_data_length,
2073 const uint8_t *plaintext,
2074 size_t plaintext_length,
2075 uint8_t *ciphertext,
2076 size_t ciphertext_size,
2077 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002078
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002079/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002080 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002081 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002082 * \param alg The AEAD algorithm to compute
2083 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002084 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002085 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002086 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002087 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002088 * but not encrypted.
2089 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002090 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002091 * encrypted. For algorithms where the
2092 * encrypted data and the authentication tag
2093 * are defined as separate inputs, the buffer
2094 * must contain the encrypted data followed
2095 * by the authentication tag.
2096 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002097 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002098 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2099 * This must be at least
2100 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2101 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002102 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002103 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002104 *
Gilles Peskine28538492018-07-11 17:34:00 +02002105 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002106 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002107 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002108 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002109 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002110 * \retval #PSA_ERROR_NOT_PERMITTED
2111 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002112 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002113 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002114 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002115 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002116 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2117 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002118 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2119 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002120 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002121 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002122 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002123 * The library has not been previously initialized by psa_crypto_init().
2124 * It is implementation-dependent whether a failure to initialize
2125 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002126 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002127psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002128 psa_algorithm_t alg,
2129 const uint8_t *nonce,
2130 size_t nonce_length,
2131 const uint8_t *additional_data,
2132 size_t additional_data_length,
2133 const uint8_t *ciphertext,
2134 size_t ciphertext_length,
2135 uint8_t *plaintext,
2136 size_t plaintext_size,
2137 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002138
Gilles Peskine30a9e412019-01-14 18:36:12 +01002139/** The type of the state data structure for multipart AEAD operations.
2140 *
2141 * Before calling any function on an AEAD operation object, the application
2142 * must initialize it by any of the following means:
2143 * - Set the structure to all-bits-zero, for example:
2144 * \code
2145 * psa_aead_operation_t operation;
2146 * memset(&operation, 0, sizeof(operation));
2147 * \endcode
2148 * - Initialize the structure to logical zero values, for example:
2149 * \code
2150 * psa_aead_operation_t operation = {0};
2151 * \endcode
2152 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2153 * for example:
2154 * \code
2155 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2156 * \endcode
2157 * - Assign the result of the function psa_aead_operation_init()
2158 * to the structure, for example:
2159 * \code
2160 * psa_aead_operation_t operation;
2161 * operation = psa_aead_operation_init();
2162 * \endcode
2163 *
2164 * This is an implementation-defined \c struct. Applications should not
2165 * make any assumptions about the content of this structure except
2166 * as directed by the documentation of a specific implementation. */
2167typedef struct psa_aead_operation_s psa_aead_operation_t;
2168
2169/** \def PSA_AEAD_OPERATION_INIT
2170 *
2171 * This macro returns a suitable initializer for an AEAD operation object of
2172 * type #psa_aead_operation_t.
2173 */
2174#ifdef __DOXYGEN_ONLY__
2175/* This is an example definition for documentation purposes.
2176 * Implementations should define a suitable value in `crypto_struct.h`.
2177 */
2178#define PSA_AEAD_OPERATION_INIT {0}
2179#endif
2180
2181/** Return an initial value for an AEAD operation object.
2182 */
2183static psa_aead_operation_t psa_aead_operation_init(void);
2184
2185/** Set the key for a multipart authenticated encryption operation.
2186 *
2187 * The sequence of operations to encrypt a message with authentication
2188 * is as follows:
2189 * -# Allocate an operation object which will be passed to all the functions
2190 * listed here.
2191 * -# Initialize the operation object with one of the methods described in the
2192 * documentation for #psa_aead_operation_t, e.g.
2193 * PSA_AEAD_OPERATION_INIT.
2194 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002195 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2196 * inputs to the subsequent calls to psa_aead_update_ad() and
2197 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2198 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002199 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2200 * generate or set the nonce. You should use
2201 * psa_aead_generate_nonce() unless the protocol you are implementing
2202 * requires a specific nonce value.
2203 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2204 * of the non-encrypted additional authenticated data each time.
2205 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002206 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002207 * -# Call psa_aead_finish().
2208 *
2209 * The application may call psa_aead_abort() at any time after the operation
2210 * has been initialized.
2211 *
2212 * After a successful call to psa_aead_encrypt_setup(), the application must
2213 * eventually terminate the operation. The following events terminate an
2214 * operation:
2215 * - A failed call to any of the \c psa_aead_xxx functions.
2216 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2217 *
2218 * \param[in,out] operation The operation object to set up. It must have
2219 * been initialized as per the documentation for
2220 * #psa_aead_operation_t and not yet in use.
2221 * \param handle Handle to the key to use for the operation.
2222 * It must remain valid until the operation
2223 * terminates.
2224 * \param alg The AEAD algorithm to compute
2225 * (\c PSA_ALG_XXX value such that
2226 * #PSA_ALG_IS_AEAD(\p alg) is true).
2227 *
2228 * \retval #PSA_SUCCESS
2229 * Success.
2230 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002231 * \retval #PSA_ERROR_NOT_PERMITTED
2232 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002233 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002234 * \retval #PSA_ERROR_NOT_SUPPORTED
2235 * \p alg is not supported or is not an AEAD algorithm.
2236 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2237 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2238 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002239 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002240 * \retval #PSA_ERROR_BAD_STATE
2241 * The library has not been previously initialized by psa_crypto_init().
2242 * It is implementation-dependent whether a failure to initialize
2243 * results in this error code.
2244 */
2245psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2246 psa_key_handle_t handle,
2247 psa_algorithm_t alg);
2248
2249/** Set the key for a multipart authenticated decryption operation.
2250 *
2251 * The sequence of operations to decrypt a message with authentication
2252 * is as follows:
2253 * -# Allocate an operation object which will be passed to all the functions
2254 * listed here.
2255 * -# Initialize the operation object with one of the methods described in the
2256 * documentation for #psa_aead_operation_t, e.g.
2257 * PSA_AEAD_OPERATION_INIT.
2258 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002259 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2260 * inputs to the subsequent calls to psa_aead_update_ad() and
2261 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2262 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002263 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2264 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2265 * of the non-encrypted additional authenticated data each time.
2266 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002267 * of the ciphertext to decrypt each time.
2268 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002269 *
2270 * The application may call psa_aead_abort() at any time after the operation
2271 * has been initialized.
2272 *
2273 * After a successful call to psa_aead_decrypt_setup(), the application must
2274 * eventually terminate the operation. The following events terminate an
2275 * operation:
2276 * - A failed call to any of the \c psa_aead_xxx functions.
2277 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2278 *
2279 * \param[in,out] operation The operation object to set up. It must have
2280 * been initialized as per the documentation for
2281 * #psa_aead_operation_t and not yet in use.
2282 * \param handle Handle to the key to use for the operation.
2283 * It must remain valid until the operation
2284 * terminates.
2285 * \param alg The AEAD algorithm to compute
2286 * (\c PSA_ALG_XXX value such that
2287 * #PSA_ALG_IS_AEAD(\p alg) is true).
2288 *
2289 * \retval #PSA_SUCCESS
2290 * Success.
2291 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002292 * \retval #PSA_ERROR_NOT_PERMITTED
2293 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002294 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002295 * \retval #PSA_ERROR_NOT_SUPPORTED
2296 * \p alg is not supported or is not an AEAD algorithm.
2297 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2298 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2299 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002300 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine30a9e412019-01-14 18:36:12 +01002301 * \retval #PSA_ERROR_BAD_STATE
2302 * The library has not been previously initialized by psa_crypto_init().
2303 * It is implementation-dependent whether a failure to initialize
2304 * results in this error code.
2305 */
2306psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2307 psa_key_handle_t handle,
2308 psa_algorithm_t alg);
2309
2310/** Generate a random nonce for an authenticated encryption operation.
2311 *
2312 * This function generates a random nonce for the authenticated encryption
2313 * operation with an appropriate size for the chosen algorithm, key type
2314 * and key size.
2315 *
2316 * The application must call psa_aead_encrypt_setup() before
2317 * calling this function.
2318 *
2319 * If this function returns an error status, the operation becomes inactive.
2320 *
2321 * \param[in,out] operation Active AEAD operation.
2322 * \param[out] nonce Buffer where the generated nonce is to be
2323 * written.
2324 * \param nonce_size Size of the \p nonce buffer in bytes.
2325 * \param[out] nonce_length On success, the number of bytes of the
2326 * generated nonce.
2327 *
2328 * \retval #PSA_SUCCESS
2329 * Success.
2330 * \retval #PSA_ERROR_BAD_STATE
2331 * The operation state is not valid (not set up, or nonce already set).
2332 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2333 * The size of the \p nonce buffer is too small.
2334 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2335 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2336 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002337 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002338 * \retval #PSA_ERROR_BAD_STATE
2339 * The library has not been previously initialized by psa_crypto_init().
2340 * It is implementation-dependent whether a failure to initialize
2341 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002342 */
2343psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002344 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002345 size_t nonce_size,
2346 size_t *nonce_length);
2347
2348/** Set the nonce for an authenticated encryption or decryption operation.
2349 *
2350 * This function sets the nonce for the authenticated
2351 * encryption or decryption operation.
2352 *
2353 * The application must call psa_aead_encrypt_setup() before
2354 * calling this function.
2355 *
2356 * If this function returns an error status, the operation becomes inactive.
2357 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002358 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002359 * instead of this function, unless implementing a protocol that requires
2360 * a non-random IV.
2361 *
2362 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002363 * \param[in] nonce Buffer containing the nonce to use.
2364 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002365 *
2366 * \retval #PSA_SUCCESS
2367 * Success.
2368 * \retval #PSA_ERROR_BAD_STATE
2369 * The operation state is not valid (not set up, or nonce already set).
2370 * \retval #PSA_ERROR_INVALID_ARGUMENT
2371 * The size of \p nonce is not acceptable for the chosen algorithm.
2372 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2373 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2374 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002375 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002376 * \retval #PSA_ERROR_BAD_STATE
2377 * The library has not been previously initialized by psa_crypto_init().
2378 * It is implementation-dependent whether a failure to initialize
2379 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002380 */
2381psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002382 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002383 size_t nonce_length);
2384
Gilles Peskinebc59c852019-01-17 15:26:08 +01002385/** Declare the lengths of the message and additional data for AEAD.
2386 *
2387 * The application must call this function before calling
2388 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2389 * the operation requires it. If the algorithm does not require it,
2390 * calling this function is optional, but if this function is called
2391 * then the implementation must enforce the lengths.
2392 *
2393 * You may call this function before or after setting the nonce with
2394 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2395 *
2396 * - For #PSA_ALG_CCM, calling this function is required.
2397 * - For the other AEAD algorithms defined in this specification, calling
2398 * this function is not required.
2399 * - For vendor-defined algorithm, refer to the vendor documentation.
2400 *
2401 * \param[in,out] operation Active AEAD operation.
2402 * \param ad_length Size of the non-encrypted additional
2403 * authenticated data in bytes.
2404 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2405 *
2406 * \retval #PSA_SUCCESS
2407 * Success.
2408 * \retval #PSA_ERROR_BAD_STATE
2409 * The operation state is not valid (not set up, already completed,
2410 * or psa_aead_update_ad() or psa_aead_update() already called).
2411 * \retval #PSA_ERROR_INVALID_ARGUMENT
2412 * At least one of the lengths is not acceptable for the chosen
2413 * algorithm.
2414 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2415 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2416 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002417 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002418 * \retval #PSA_ERROR_BAD_STATE
2419 * The library has not been previously initialized by psa_crypto_init().
2420 * It is implementation-dependent whether a failure to initialize
2421 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002422 */
2423psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2424 size_t ad_length,
2425 size_t plaintext_length);
2426
Gilles Peskine30a9e412019-01-14 18:36:12 +01002427/** Pass additional data to an active AEAD operation.
2428 *
2429 * Additional data is authenticated, but not encrypted.
2430 *
2431 * You may call this function multiple times to pass successive fragments
2432 * of the additional data. You may not call this function after passing
2433 * data to encrypt or decrypt with psa_aead_update().
2434 *
2435 * Before calling this function, you must:
2436 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2437 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2438 *
2439 * If this function returns an error status, the operation becomes inactive.
2440 *
2441 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2442 * there is no guarantee that the input is valid. Therefore, until
2443 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2444 * treat the input as untrusted and prepare to undo any action that
2445 * depends on the input if psa_aead_verify() returns an error status.
2446 *
2447 * \param[in,out] operation Active AEAD operation.
2448 * \param[in] input Buffer containing the fragment of
2449 * additional data.
2450 * \param input_length Size of the \p input buffer in bytes.
2451 *
2452 * \retval #PSA_SUCCESS
2453 * Success.
2454 * \retval #PSA_ERROR_BAD_STATE
2455 * The operation state is not valid (not set up, nonce not set,
2456 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002457 * \retval #PSA_ERROR_INVALID_ARGUMENT
2458 * The total input length overflows the additional data length that
2459 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002460 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2461 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2462 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002463 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002464 * \retval #PSA_ERROR_BAD_STATE
2465 * The library has not been previously initialized by psa_crypto_init().
2466 * It is implementation-dependent whether a failure to initialize
2467 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002468 */
2469psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2470 const uint8_t *input,
2471 size_t input_length);
2472
2473/** Encrypt or decrypt a message fragment in an active AEAD operation.
2474 *
2475 * Before calling this function, you must:
2476 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2477 * The choice of setup function determines whether this function
2478 * encrypts or decrypts its input.
2479 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2480 * 3. Call psa_aead_update_ad() to pass all the additional data.
2481 *
2482 * If this function returns an error status, the operation becomes inactive.
2483 *
2484 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2485 * there is no guarantee that the input is valid. Therefore, until
2486 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2487 * - Do not use the output in any way other than storing it in a
2488 * confidential location. If you take any action that depends
2489 * on the tentative decrypted data, this action will need to be
2490 * undone if the input turns out not to be valid. Furthermore,
2491 * if an adversary can observe that this action took place
2492 * (for example through timing), they may be able to use this
2493 * fact as an oracle to decrypt any message encrypted with the
2494 * same key.
2495 * - In particular, do not copy the output anywhere but to a
2496 * memory or storage space that you have exclusive access to.
2497 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002498 * This function does not require the input to be aligned to any
2499 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002500 * a whole block at a time, it must consume all the input provided, but
2501 * it may delay the end of the corresponding output until a subsequent
2502 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2503 * provides sufficient input. The amount of data that can be delayed
2504 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002505 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002506 * \param[in,out] operation Active AEAD operation.
2507 * \param[in] input Buffer containing the message fragment to
2508 * encrypt or decrypt.
2509 * \param input_length Size of the \p input buffer in bytes.
2510 * \param[out] output Buffer where the output is to be written.
2511 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002512 * This must be at least
2513 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2514 * \p input_length) where \c alg is the
2515 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002516 * \param[out] output_length On success, the number of bytes
2517 * that make up the returned output.
2518 *
2519 * \retval #PSA_SUCCESS
2520 * Success.
2521 * \retval #PSA_ERROR_BAD_STATE
2522 * The operation state is not valid (not set up, nonce not set
2523 * or already completed).
2524 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2525 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002526 * You can determine a sufficient buffer size by calling
2527 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2528 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002529 * \retval #PSA_ERROR_INVALID_ARGUMENT
2530 * The total length of input to psa_aead_update_ad() so far is
2531 * less than the additional data length that was previously
2532 * specified with psa_aead_set_lengths().
2533 * \retval #PSA_ERROR_INVALID_ARGUMENT
2534 * The total input length overflows the plaintext length that
2535 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002536 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2537 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2538 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002539 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002540 * \retval #PSA_ERROR_BAD_STATE
2541 * The library has not been previously initialized by psa_crypto_init().
2542 * It is implementation-dependent whether a failure to initialize
2543 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002544 */
2545psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2546 const uint8_t *input,
2547 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002548 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002549 size_t output_size,
2550 size_t *output_length);
2551
2552/** Finish encrypting a message in an AEAD operation.
2553 *
2554 * The operation must have been set up with psa_aead_encrypt_setup().
2555 *
2556 * This function finishes the authentication of the additional data
2557 * formed by concatenating the inputs passed to preceding calls to
2558 * psa_aead_update_ad() with the plaintext formed by concatenating the
2559 * inputs passed to preceding calls to psa_aead_update().
2560 *
2561 * This function has two output buffers:
2562 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002563 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002564 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002565 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002566 * that the operation performs.
2567 *
2568 * When this function returns, the operation becomes inactive.
2569 *
2570 * \param[in,out] operation Active AEAD operation.
2571 * \param[out] ciphertext Buffer where the last part of the ciphertext
2572 * is to be written.
2573 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002574 * This must be at least
2575 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2576 * \c alg is the algorithm that is being
2577 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002578 * \param[out] ciphertext_length On success, the number of bytes of
2579 * returned ciphertext.
2580 * \param[out] tag Buffer where the authentication tag is
2581 * to be written.
2582 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002583 * This must be at least
2584 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2585 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002586 * \param[out] tag_length On success, the number of bytes
2587 * that make up the returned tag.
2588 *
2589 * \retval #PSA_SUCCESS
2590 * Success.
2591 * \retval #PSA_ERROR_BAD_STATE
2592 * The operation state is not valid (not set up, nonce not set,
2593 * decryption, or already completed).
2594 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002595 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002596 * You can determine a sufficient buffer size for \p ciphertext by
2597 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2598 * where \c alg is the algorithm that is being calculated.
2599 * You can determine a sufficient buffer size for \p tag by
2600 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002601 * \retval #PSA_ERROR_INVALID_ARGUMENT
2602 * The total length of input to psa_aead_update_ad() so far is
2603 * less than the additional data length that was previously
2604 * specified with psa_aead_set_lengths().
2605 * \retval #PSA_ERROR_INVALID_ARGUMENT
2606 * The total length of input to psa_aead_update() so far is
2607 * less than the plaintext length that was previously
2608 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002609 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2610 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2611 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002612 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002613 * \retval #PSA_ERROR_BAD_STATE
2614 * The library has not been previously initialized by psa_crypto_init().
2615 * It is implementation-dependent whether a failure to initialize
2616 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002617 */
2618psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002619 uint8_t *ciphertext,
2620 size_t ciphertext_size,
2621 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002622 uint8_t *tag,
2623 size_t tag_size,
2624 size_t *tag_length);
2625
2626/** Finish authenticating and decrypting a message in an AEAD operation.
2627 *
2628 * The operation must have been set up with psa_aead_decrypt_setup().
2629 *
2630 * This function finishes the authentication of the additional data
2631 * formed by concatenating the inputs passed to preceding calls to
2632 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2633 * inputs passed to preceding calls to psa_aead_update().
2634 *
2635 * When this function returns, the operation becomes inactive.
2636 *
2637 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002638 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002639 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002640 * from previous calls to psa_aead_update()
2641 * that could not be processed until the end
2642 * of the input.
2643 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002644 * This must be at least
2645 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2646 * \c alg is the algorithm that is being
2647 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002648 * \param[out] plaintext_length On success, the number of bytes of
2649 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002650 * \param[in] tag Buffer containing the authentication tag.
2651 * \param tag_length Size of the \p tag buffer in bytes.
2652 *
2653 * \retval #PSA_SUCCESS
2654 * Success.
2655 * \retval #PSA_ERROR_BAD_STATE
2656 * The operation state is not valid (not set up, nonce not set,
2657 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002658 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2659 * The size of the \p plaintext buffer is too small.
2660 * You can determine a sufficient buffer size for \p plaintext by
2661 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2662 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002663 * \retval #PSA_ERROR_INVALID_ARGUMENT
2664 * The total length of input to psa_aead_update_ad() so far is
2665 * less than the additional data length that was previously
2666 * specified with psa_aead_set_lengths().
2667 * \retval #PSA_ERROR_INVALID_ARGUMENT
2668 * The total length of input to psa_aead_update() so far is
2669 * less than the plaintext length that was previously
2670 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002671 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2672 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2673 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002674 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002675 * \retval #PSA_ERROR_BAD_STATE
2676 * The library has not been previously initialized by psa_crypto_init().
2677 * It is implementation-dependent whether a failure to initialize
2678 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002679 */
2680psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002681 uint8_t *plaintext,
2682 size_t plaintext_size,
2683 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002684 const uint8_t *tag,
2685 size_t tag_length);
2686
2687/** Abort an AEAD operation.
2688 *
2689 * Aborting an operation frees all associated resources except for the
2690 * \p operation structure itself. Once aborted, the operation object
2691 * can be reused for another operation by calling
2692 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2693 *
2694 * You may call this function any time after the operation object has
2695 * been initialized by any of the following methods:
2696 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2697 * whether it succeeds or not.
2698 * - Initializing the \c struct to all-bits-zero.
2699 * - Initializing the \c struct to logical zeros, e.g.
2700 * `psa_aead_operation_t operation = {0}`.
2701 *
2702 * In particular, calling psa_aead_abort() after the operation has been
2703 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2704 * is safe and has no effect.
2705 *
2706 * \param[in,out] operation Initialized AEAD operation.
2707 *
2708 * \retval #PSA_SUCCESS
2709 * \retval #PSA_ERROR_BAD_STATE
2710 * \p operation is not an active AEAD operation.
2711 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2712 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002713 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002714 * \retval #PSA_ERROR_BAD_STATE
2715 * The library has not been previously initialized by psa_crypto_init().
2716 * It is implementation-dependent whether a failure to initialize
2717 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002718 */
2719psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2720
Gilles Peskine3b555712018-03-03 21:27:57 +01002721/**@}*/
2722
Gilles Peskine20035e32018-02-03 22:44:14 +01002723/** \defgroup asymmetric Asymmetric cryptography
2724 * @{
2725 */
2726
2727/**
2728 * \brief Sign a hash or short message with a private key.
2729 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002730 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002731 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002732 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2733 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2734 * to determine the hash algorithm to use.
2735 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002736 * \param handle Handle to the key to use for the operation.
2737 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002738 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002739 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002740 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002741 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002742 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002743 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002744 * \param[out] signature_length On success, the number of bytes
2745 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002746 *
Gilles Peskine28538492018-07-11 17:34:00 +02002747 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002748 * \retval #PSA_ERROR_INVALID_HANDLE
2749 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002750 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002751 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002752 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002753 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002754 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002755 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002756 * \retval #PSA_ERROR_NOT_SUPPORTED
2757 * \retval #PSA_ERROR_INVALID_ARGUMENT
2758 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2759 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2760 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002761 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002762 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002763 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002764 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002765 * The library has not been previously initialized by psa_crypto_init().
2766 * It is implementation-dependent whether a failure to initialize
2767 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002768 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002769psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002770 psa_algorithm_t alg,
2771 const uint8_t *hash,
2772 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002773 uint8_t *signature,
2774 size_t signature_size,
2775 size_t *signature_length);
2776
2777/**
2778 * \brief Verify the signature a hash or short message using a public key.
2779 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002780 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002781 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002782 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2783 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2784 * to determine the hash algorithm to use.
2785 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002786 * \param handle Handle to the key to use for the operation.
2787 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002788 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002789 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002790 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002791 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002792 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002793 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002794 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002795 *
Gilles Peskine28538492018-07-11 17:34:00 +02002796 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002797 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002798 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002799 * The calculation was perfomed successfully, but the passed
2800 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002801 * \retval #PSA_ERROR_NOT_SUPPORTED
2802 * \retval #PSA_ERROR_INVALID_ARGUMENT
2803 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2804 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2805 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002806 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002807 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002808 * The library has not been previously initialized by psa_crypto_init().
2809 * It is implementation-dependent whether a failure to initialize
2810 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002811 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002812psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002813 psa_algorithm_t alg,
2814 const uint8_t *hash,
2815 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002816 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002817 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002818
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002819/**
2820 * \brief Encrypt a short message with a public key.
2821 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002822 * \param handle Handle to the key to use for the operation.
2823 * It must be a public key or an asymmetric
2824 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002825 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002826 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002827 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002828 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002829 * \param[in] salt A salt or label, if supported by the
2830 * encryption algorithm.
2831 * If the algorithm does not support a
2832 * salt, pass \c NULL.
2833 * If the algorithm supports an optional
2834 * salt and you do not want to pass a salt,
2835 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002836 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002837 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2838 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002839 * \param salt_length Size of the \p salt buffer in bytes.
2840 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002841 * \param[out] output Buffer where the encrypted message is to
2842 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002843 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002844 * \param[out] output_length On success, the number of bytes
2845 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002846 *
Gilles Peskine28538492018-07-11 17:34:00 +02002847 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002848 * \retval #PSA_ERROR_INVALID_HANDLE
2849 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002850 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002851 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002852 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002853 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002854 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002855 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002856 * \retval #PSA_ERROR_NOT_SUPPORTED
2857 * \retval #PSA_ERROR_INVALID_ARGUMENT
2858 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2859 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2860 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002861 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002862 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002863 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002864 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002865 * The library has not been previously initialized by psa_crypto_init().
2866 * It is implementation-dependent whether a failure to initialize
2867 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002868 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002869psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002870 psa_algorithm_t alg,
2871 const uint8_t *input,
2872 size_t input_length,
2873 const uint8_t *salt,
2874 size_t salt_length,
2875 uint8_t *output,
2876 size_t output_size,
2877 size_t *output_length);
2878
2879/**
2880 * \brief Decrypt a short message with a private key.
2881 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002882 * \param handle Handle to the key to use for the operation.
2883 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002884 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002885 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002886 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002887 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002888 * \param[in] salt A salt or label, if supported by the
2889 * encryption algorithm.
2890 * If the algorithm does not support a
2891 * salt, pass \c NULL.
2892 * If the algorithm supports an optional
2893 * salt and you do not want to pass a salt,
2894 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002895 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002896 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2897 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002898 * \param salt_length Size of the \p salt buffer in bytes.
2899 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002900 * \param[out] output Buffer where the decrypted message is to
2901 * be written.
2902 * \param output_size Size of the \c output buffer in bytes.
2903 * \param[out] output_length On success, the number of bytes
2904 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002905 *
Gilles Peskine28538492018-07-11 17:34:00 +02002906 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002907 * \retval #PSA_ERROR_INVALID_HANDLE
2908 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002909 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002910 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002911 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002912 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002913 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002914 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002915 * \retval #PSA_ERROR_NOT_SUPPORTED
2916 * \retval #PSA_ERROR_INVALID_ARGUMENT
2917 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2918 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2919 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002920 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002921 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002922 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2923 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002924 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002925 * The library has not been previously initialized by psa_crypto_init().
2926 * It is implementation-dependent whether a failure to initialize
2927 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002928 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002929psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002930 psa_algorithm_t alg,
2931 const uint8_t *input,
2932 size_t input_length,
2933 const uint8_t *salt,
2934 size_t salt_length,
2935 uint8_t *output,
2936 size_t output_size,
2937 size_t *output_length);
2938
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002939/**@}*/
2940
Gilles Peskine35675b62019-05-16 17:26:11 +02002941/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02002942 * @{
2943 */
2944
Gilles Peskine35675b62019-05-16 17:26:11 +02002945/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002946 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002947 * Before calling any function on a key derivation operation object, the
2948 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02002949 * - Set the structure to all-bits-zero, for example:
2950 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002951 * psa_key_derivation_operation_t operation;
2952 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02002953 * \endcode
2954 * - Initialize the structure to logical zero values, for example:
2955 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002956 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02002957 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002958 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002959 * for example:
2960 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002961 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002962 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002963 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02002964 * to the structure, for example:
2965 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02002966 * psa_key_derivation_operation_t operation;
2967 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02002968 * \endcode
2969 *
2970 * This is an implementation-defined \c struct. Applications should not
2971 * make any assumptions about the content of this structure except
2972 * as directed by the documentation of a specific implementation.
2973 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02002974typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02002975
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002976/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002977 *
Gilles Peskine35675b62019-05-16 17:26:11 +02002978 * This macro returns a suitable initializer for a key derivation operation
2979 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002980 */
2981#ifdef __DOXYGEN_ONLY__
2982/* This is an example definition for documentation purposes.
2983 * Implementations should define a suitable value in `crypto_struct.h`.
2984 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002985#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02002986#endif
2987
Gilles Peskine35675b62019-05-16 17:26:11 +02002988/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002989 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02002990static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02002991
Gilles Peskine1cb9a082019-05-16 17:56:47 +02002992/** Set up a key derivation operation.
2993 *
2994 * A key derivation algorithm takes some inputs and uses them to generate
2995 * a byte stream in a deterministic way.
2996 * This byte stream can be used to produce keys and other
2997 * cryptographic material.
2998 *
2999 * To derive a key:
3000 * - Start with an initialized object of type #psa_key_derivation_operation_t.
3001 * - Call psa_key_derivation_setup() to select the algorithm.
3002 * - Provide the inputs for the key derivation by calling
3003 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3004 * as appropriate. Which inputs are needed, in what order, and whether
3005 * they may be keys and if so of what type depends on the algorithm.
3006 * - Optionally set the operation's maximum capacity with
3007 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3008 * of or after providing inputs. For some algorithms, this step is mandatory
3009 * because the output depends on the maximum capacity.
3010 * - To derive a key, call psa_key_derivation_output_key().
3011 * To derive a byte string for a different purpose, call
3012 * - psa_key_derivation_output_bytes().
3013 * Successive calls to these functions use successive output bytes
3014 * calculated by the key derivation algorithm.
3015 * - Clean up the key derivation operation object with
3016 * psa_key_derivation_abort().
3017 *
3018 * \param[in,out] operation The key derivation operation object
3019 * to set up. It must
3020 * have been initialized but not set up yet.
3021 * \param alg The key derivation algorithm to compute
3022 * (\c PSA_ALG_XXX value such that
3023 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3024 *
3025 * \retval #PSA_SUCCESS
3026 * Success.
3027 * \retval #PSA_ERROR_INVALID_ARGUMENT
3028 * \c alg is not a key derivation algorithm.
3029 * \retval #PSA_ERROR_NOT_SUPPORTED
3030 * \c alg is not supported or is not a key derivation algorithm.
3031 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3032 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3033 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003034 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003035 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003036 * The operation state is either not initialized or has been setup.
3037 * \retval #PSA_ERROR_BAD_STATE
3038 * The library has not been previously initialized by psa_crypto_init().
3039 * It is implementation-dependent whether a failure to initialize
3040 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003041 */
3042psa_status_t psa_key_derivation_setup(
3043 psa_key_derivation_operation_t *operation,
3044 psa_algorithm_t alg);
3045
Gilles Peskine35675b62019-05-16 17:26:11 +02003046/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003047 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003048 * The capacity of a key derivation is the maximum number of bytes that it can
3049 * return. When you get *N* bytes of output from a key derivation operation,
3050 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003051 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003052 * \param[in] operation The operation to query.
3053 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003054 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003055 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003056 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003057 * \retval #PSA_ERROR_BAD_STATE
3058 * The operation state is not valid.
3059 * \retval #PSA_ERROR_BAD_STATE
3060 * The library has not been previously initialized by psa_crypto_init().
3061 * It is implementation-dependent whether a failure to initialize
3062 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003063 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003064psa_status_t psa_key_derivation_get_capacity(
3065 const psa_key_derivation_operation_t *operation,
3066 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003067
Gilles Peskine35675b62019-05-16 17:26:11 +02003068/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003069 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003070 * The capacity of a key derivation operation is the maximum number of bytes
3071 * that the key derivation operation can return from this point onwards.
3072 *
3073 * \param[in,out] operation The key derivation operation object to modify.
3074 * \param capacity The new capacity of the operation.
3075 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003076 * current capacity.
3077 *
3078 * \retval #PSA_SUCCESS
3079 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003080 * \p capacity is larger than the operation's current capacity.
3081 * In this case, the operation object remains valid and its capacity
3082 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003083 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003084 * The operation state is not valid.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003085 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003086 * \retval #PSA_ERROR_BAD_STATE
3087 * The library has not been previously initialized by psa_crypto_init().
3088 * It is implementation-dependent whether a failure to initialize
3089 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003090 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003091psa_status_t psa_key_derivation_set_capacity(
3092 psa_key_derivation_operation_t *operation,
3093 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003094
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003095/** Use the maximum possible capacity for a key derivation operation.
3096 *
3097 * Use this value as the capacity argument when setting up a key derivation
3098 * to indicate that the operation should have the maximum possible capacity.
3099 * The value of the maximum possible capacity depends on the key derivation
3100 * algorithm.
3101 */
3102#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3103
3104/** Provide an input for key derivation or key agreement.
3105 *
3106 * Which inputs are required and in what order depends on the algorithm.
3107 * Refer to the documentation of each key derivation or key agreement
3108 * algorithm for information.
3109 *
3110 * This function passes direct inputs. Some inputs must be passed as keys
3111 * using psa_key_derivation_input_key() instead of this function. Refer to
3112 * the documentation of individual step types for information.
3113 *
3114 * \param[in,out] operation The key derivation operation object to use.
3115 * It must have been set up with
3116 * psa_key_derivation_setup() and must not
3117 * have produced any output yet.
3118 * \param step Which step the input data is for.
3119 * \param[in] data Input data to use.
3120 * \param data_length Size of the \p data buffer in bytes.
3121 *
3122 * \retval #PSA_SUCCESS
3123 * Success.
3124 * \retval #PSA_ERROR_INVALID_ARGUMENT
3125 * \c step is not compatible with the operation's algorithm.
3126 * \retval #PSA_ERROR_INVALID_ARGUMENT
3127 * \c step does not allow direct inputs.
3128 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3129 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3130 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003131 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003132 * \retval #PSA_ERROR_BAD_STATE
3133 * The value of \p step is not valid given the state of \p operation.
3134 * \retval #PSA_ERROR_BAD_STATE
3135 * The library has not been previously initialized by psa_crypto_init().
3136 * It is implementation-dependent whether a failure to initialize
3137 * results in this error code.
3138 */
3139psa_status_t psa_key_derivation_input_bytes(
3140 psa_key_derivation_operation_t *operation,
3141 psa_key_derivation_step_t step,
3142 const uint8_t *data,
3143 size_t data_length);
3144
3145/** Provide an input for key derivation in the form of a key.
3146 *
3147 * Which inputs are required and in what order depends on the algorithm.
3148 * Refer to the documentation of each key derivation or key agreement
3149 * algorithm for information.
3150 *
3151 * This function passes key inputs. Some inputs must be passed as keys
3152 * of the appropriate type using this function, while others must be
3153 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3154 * the documentation of individual step types for information.
3155 *
3156 * \param[in,out] operation The key derivation operation object to use.
3157 * It must have been set up with
3158 * psa_key_derivation_setup() and must not
3159 * have produced any output yet.
3160 * \param step Which step the input data is for.
3161 * \param handle Handle to the key. It must have an
3162 * appropriate type for \p step and must
3163 * allow the usage #PSA_KEY_USAGE_DERIVE.
3164 *
3165 * \retval #PSA_SUCCESS
3166 * Success.
3167 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003168 * \retval #PSA_ERROR_NOT_PERMITTED
3169 * \retval #PSA_ERROR_INVALID_ARGUMENT
3170 * \c step is not compatible with the operation's algorithm.
3171 * \retval #PSA_ERROR_INVALID_ARGUMENT
3172 * \c step does not allow key inputs.
3173 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3174 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3175 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003176 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003177 * \retval #PSA_ERROR_BAD_STATE
3178 * The value of \p step is not valid given the state of \p operation.
3179 * \retval #PSA_ERROR_BAD_STATE
3180 * The library has not been previously initialized by psa_crypto_init().
3181 * It is implementation-dependent whether a failure to initialize
3182 * results in this error code.
3183 */
3184psa_status_t psa_key_derivation_input_key(
3185 psa_key_derivation_operation_t *operation,
3186 psa_key_derivation_step_t step,
3187 psa_key_handle_t handle);
3188
3189/** Perform a key agreement and use the shared secret as input to a key
3190 * derivation.
3191 *
3192 * A key agreement algorithm takes two inputs: a private key \p private_key
3193 * a public key \p peer_key.
3194 * The result of this function is passed as input to a key derivation.
3195 * The output of this key derivation can be extracted by reading from the
3196 * resulting operation to produce keys and other cryptographic material.
3197 *
3198 * \param[in,out] operation The key derivation operation object to use.
3199 * It must have been set up with
3200 * psa_key_derivation_setup() with a
3201 * key agreement and derivation algorithm
3202 * \c alg (\c PSA_ALG_XXX value such that
3203 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3204 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3205 * is false).
3206 * The operation must be ready for an
3207 * input of the type given by \p step.
3208 * \param step Which step the input data is for.
3209 * \param private_key Handle to the private key to use.
3210 * \param[in] peer_key Public key of the peer. The peer key must be in the
3211 * same format that psa_import_key() accepts for the
3212 * public key type corresponding to the type of
3213 * private_key. That is, this function performs the
3214 * equivalent of
3215 * #psa_import_key(...,
3216 * `peer_key`, `peer_key_length`) where
3217 * with key attributes indicating the public key
3218 * type corresponding to the type of `private_key`.
3219 * For example, for EC keys, this means that peer_key
3220 * is interpreted as a point on the curve that the
3221 * private key is on. The standard formats for public
3222 * keys are documented in the documentation of
3223 * psa_export_public_key().
3224 * \param peer_key_length Size of \p peer_key in bytes.
3225 *
3226 * \retval #PSA_SUCCESS
3227 * Success.
3228 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003229 * \retval #PSA_ERROR_NOT_PERMITTED
3230 * \retval #PSA_ERROR_INVALID_ARGUMENT
3231 * \c private_key is not compatible with \c alg,
3232 * or \p peer_key is not valid for \c alg or not compatible with
3233 * \c private_key.
3234 * \retval #PSA_ERROR_NOT_SUPPORTED
3235 * \c alg is not supported or is not a key derivation algorithm.
3236 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3237 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3238 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003239 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003240 * \retval #PSA_ERROR_BAD_STATE
3241 * The library has not been previously initialized by psa_crypto_init().
3242 * It is implementation-dependent whether a failure to initialize
3243 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003244 */
3245psa_status_t psa_key_derivation_key_agreement(
3246 psa_key_derivation_operation_t *operation,
3247 psa_key_derivation_step_t step,
3248 psa_key_handle_t private_key,
3249 const uint8_t *peer_key,
3250 size_t peer_key_length);
3251
Gilles Peskine35675b62019-05-16 17:26:11 +02003252/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003253 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003254 * This function calculates output bytes from a key derivation algorithm and
3255 * return those bytes.
3256 * If you view the key derivation's output as a stream of bytes, this
3257 * function destructively reads the requested number of bytes from the
3258 * stream.
3259 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003260 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003261 * \param[in,out] operation The key derivation operation object to read from.
3262 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003263 * \param output_length Number of bytes to output.
3264 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003265 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003266 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003267 * The operation's capacity was less than
3268 * \p output_length bytes. Note that in this case,
3269 * no output is written to the output buffer.
3270 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003271 * subsequent calls to this function will not
3272 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003273 * \retval #PSA_ERROR_BAD_STATE
3274 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3275 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3276 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003277 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003278 * \retval #PSA_ERROR_BAD_STATE
3279 * The library has not been previously initialized by psa_crypto_init().
3280 * It is implementation-dependent whether a failure to initialize
3281 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003282 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003283psa_status_t psa_key_derivation_output_bytes(
3284 psa_key_derivation_operation_t *operation,
3285 uint8_t *output,
3286 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003287
Gilles Peskine35675b62019-05-16 17:26:11 +02003288/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003289 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003290 * This function calculates output bytes from a key derivation algorithm
3291 * and uses those bytes to generate a key deterministically.
3292 * If you view the key derivation's output as a stream of bytes, this
3293 * function destructively reads as many bytes as required from the
3294 * stream.
3295 * The operation's capacity decreases by the number of bytes read.
3296 *
3297 * How much output is produced and consumed from the operation, and how
3298 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003299 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003300 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003301 * of a given size, this function is functionally equivalent to
3302 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003303 * and passing the resulting output to #psa_import_key.
3304 * However, this function has a security benefit:
3305 * if the implementation provides an isolation boundary then
3306 * the key material is not exposed outside the isolation boundary.
3307 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003308 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003309 * The following key types defined in this specification follow this scheme:
3310 *
3311 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003312 * - #PSA_KEY_TYPE_ARC4;
3313 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003314 * - #PSA_KEY_TYPE_DERIVE;
3315 * - #PSA_KEY_TYPE_HMAC.
3316 *
3317 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003318 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003319 * Montgomery curve), this function always draws a byte string whose
3320 * length is determined by the curve, and sets the mandatory bits
3321 * accordingly. That is:
3322 *
3323 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3324 * and process it as specified in RFC 7748 &sect;5.
3325 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3326 * and process it as specified in RFC 7748 &sect;5.
3327 *
3328 * - For key types for which the key is represented by a single sequence of
3329 * \p bits bits with constraints as to which bit sequences are acceptable,
3330 * this function draws a byte string of length (\p bits / 8) bytes rounded
3331 * up to the nearest whole number of bytes. If the resulting byte string
3332 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3333 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003334 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003335 * for the output produced by psa_export_key().
3336 * The following key types defined in this specification follow this scheme:
3337 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003338 * - #PSA_KEY_TYPE_DES.
3339 * Force-set the parity bits, but discard forbidden weak keys.
3340 * For 2-key and 3-key triple-DES, the three keys are generated
3341 * successively (for example, for 3-key triple-DES,
3342 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3343 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003344 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003345 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003346 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003347 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003348 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003349 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003350 * Weierstrass curve).
3351 * For these key types, interpret the byte string as integer
3352 * in big-endian order. Discard it if it is not in the range
3353 * [0, *N* - 2] where *N* is the boundary of the private key domain
3354 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003355 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003356 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003357 * This method allows compliance to NIST standards, specifically
3358 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003359 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3360 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3361 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3362 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003363 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003364 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003365 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003366 * implementation-defined.
3367 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003368 * In all cases, the data that is read is discarded from the operation.
3369 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003370 *
Gilles Peskine20628592019-04-19 19:29:50 +02003371 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003372 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003373 * \param[out] handle On success, a handle to the newly created key.
3374 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003375 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003376 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003377 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003378 * If the key is persistent, the key material and the key's metadata
3379 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003380 * \retval #PSA_ERROR_ALREADY_EXISTS
3381 * This is an attempt to create a persistent key, and there is
3382 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003383 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003384 * There was not enough data to create the desired key.
3385 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003386 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003387 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003388 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003389 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003390 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003391 * \retval #PSA_ERROR_INVALID_ARGUMENT
3392 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003393 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003394 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3395 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3396 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3397 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003398 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003399 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003400 * The library has not been previously initialized by psa_crypto_init().
3401 * It is implementation-dependent whether a failure to initialize
3402 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003403 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003404psa_status_t psa_key_derivation_output_key(
3405 const psa_key_attributes_t *attributes,
3406 psa_key_derivation_operation_t *operation,
3407 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003408
Gilles Peskine35675b62019-05-16 17:26:11 +02003409/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003410 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003411 * Once a key derivation operation has been aborted, its capacity is zero.
3412 * Aborting an operation frees all associated resources except for the
3413 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003414 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003415 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003416 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003417 * psa_key_derivation_operation_init() or a zero value. In particular,
3418 * it is valid to call psa_key_derivation_abort() twice, or to call
3419 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003420 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003421 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003422 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003423 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003424 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003425 * \retval #PSA_SUCCESS
3426 * \retval #PSA_ERROR_BAD_STATE
3427 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3428 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003429 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003430 * \retval #PSA_ERROR_BAD_STATE
3431 * The library has not been previously initialized by psa_crypto_init().
3432 * It is implementation-dependent whether a failure to initialize
3433 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003434 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003435psa_status_t psa_key_derivation_abort(
3436 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003437
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003438/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003439 *
3440 * \warning The raw result of a key agreement algorithm such as finite-field
3441 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3442 * not be used directly as key material. It should instead be passed as
3443 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003444 * a key derivation, use psa_key_derivation_key_agreement() and other
3445 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003446 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003447 * \param alg The key agreement algorithm to compute
3448 * (\c PSA_ALG_XXX value such that
3449 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3450 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003451 * \param private_key Handle to the private key to use.
3452 * \param[in] peer_key Public key of the peer. It must be
3453 * in the same format that psa_import_key()
3454 * accepts. The standard formats for public
3455 * keys are documented in the documentation
3456 * of psa_export_public_key().
3457 * \param peer_key_length Size of \p peer_key in bytes.
3458 * \param[out] output Buffer where the decrypted message is to
3459 * be written.
3460 * \param output_size Size of the \c output buffer in bytes.
3461 * \param[out] output_length On success, the number of bytes
3462 * that make up the returned output.
3463 *
3464 * \retval #PSA_SUCCESS
3465 * Success.
3466 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003467 * \retval #PSA_ERROR_NOT_PERMITTED
3468 * \retval #PSA_ERROR_INVALID_ARGUMENT
3469 * \p alg is not a key agreement algorithm
3470 * \retval #PSA_ERROR_INVALID_ARGUMENT
3471 * \p private_key is not compatible with \p alg,
3472 * or \p peer_key is not valid for \p alg or not compatible with
3473 * \p private_key.
3474 * \retval #PSA_ERROR_NOT_SUPPORTED
3475 * \p alg is not a supported key agreement algorithm.
3476 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3477 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3478 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003479 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003480 * \retval #PSA_ERROR_BAD_STATE
3481 * The library has not been previously initialized by psa_crypto_init().
3482 * It is implementation-dependent whether a failure to initialize
3483 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003484 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003485psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3486 psa_key_handle_t private_key,
3487 const uint8_t *peer_key,
3488 size_t peer_key_length,
3489 uint8_t *output,
3490 size_t output_size,
3491 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003492
Gilles Peskineea0fb492018-07-12 17:17:20 +02003493/**@}*/
3494
Gilles Peskineedd76872018-07-20 17:42:05 +02003495/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003496 * @{
3497 */
3498
3499/**
3500 * \brief Generate random bytes.
3501 *
3502 * \warning This function **can** fail! Callers MUST check the return status
3503 * and MUST NOT use the content of the output buffer if the return
3504 * status is not #PSA_SUCCESS.
3505 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003506 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003507 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003508 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003509 * \param output_size Number of bytes to generate and output.
3510 *
Gilles Peskine28538492018-07-11 17:34:00 +02003511 * \retval #PSA_SUCCESS
3512 * \retval #PSA_ERROR_NOT_SUPPORTED
3513 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3514 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3515 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003516 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003517 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003518 * The library has not been previously initialized by psa_crypto_init().
3519 * It is implementation-dependent whether a failure to initialize
3520 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003521 */
3522psa_status_t psa_generate_random(uint8_t *output,
3523 size_t output_size);
3524
3525/**
3526 * \brief Generate a key or key pair.
3527 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003528 * The key is generated randomly.
3529 * Its location, policy, type and size are taken from \p attributes.
3530 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003531 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003532 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003533 * the public exponent is 65537.
3534 * The modulus is a product of two probabilistic primes
3535 * between 2^{n-1} and 2^n where n is the bit size specified in the
3536 * attributes.
3537 *
Gilles Peskine20628592019-04-19 19:29:50 +02003538 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003539 * \param[out] handle On success, a handle to the newly created key.
3540 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003541 *
Gilles Peskine28538492018-07-11 17:34:00 +02003542 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003543 * Success.
3544 * If the key is persistent, the key material and the key's metadata
3545 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003546 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003547 * This is an attempt to create a persistent key, and there is
3548 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003549 * \retval #PSA_ERROR_NOT_SUPPORTED
3550 * \retval #PSA_ERROR_INVALID_ARGUMENT
3551 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3552 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3553 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3554 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003555 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003556 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3557 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003558 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003559 * The library has not been previously initialized by psa_crypto_init().
3560 * It is implementation-dependent whether a failure to initialize
3561 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003562 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003563psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003564 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003565
3566/**@}*/
3567
Gilles Peskinee59236f2018-01-27 23:32:46 +01003568#ifdef __cplusplus
3569}
3570#endif
3571
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003572/* The file "crypto_sizes.h" contains definitions for size calculation
3573 * macros whose definitions are implementation-specific. */
3574#include "crypto_sizes.h"
3575
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003576/* The file "crypto_struct.h" contains definitions for
3577 * implementation-specific structs that are declared above. */
3578#include "crypto_struct.h"
3579
3580/* The file "crypto_extra.h" contains vendor-specific definitions. This
3581 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003582#include "crypto_extra.h"
3583
3584#endif /* PSA_CRYPTO_H */