blob: 7c88bd680b39ecc0e96065302d63c7bdcb9a319a [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
Gilles Peskinea170d922019-09-12 16:59:37 +0200229 * algorithms are permitted to be used with this key. The following
230 * algorithm policies are supported:
231 * - 0 does not allow any cryptographic operation with the key. The key
232 * may be used for non-cryptographic actions such as exporting (if
233 * permitted by the usage flags).
234 * - An algorithm value permits this particular algorithm.
235 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
236 * signature scheme with any hash algorithm.
Gilles Peskine20628592019-04-19 19:29:50 +0200237 *
238 * This function overwrites any algorithm policy
239 * previously set in \p attributes.
240 *
241 * This function may be declared as `static` (i.e. without external
242 * linkage). This function may be provided as a function-like macro,
243 * but in this case it must evaluate each of its arguments exactly once.
244 *
245 * \param[out] attributes The attribute structure to write to.
246 * \param alg The permitted algorithm policy to write.
247 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200248static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
249 psa_algorithm_t alg);
250
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100251
Gilles Peskine20628592019-04-19 19:29:50 +0200252/** Retrieve the algorithm policy from key attributes.
253 *
254 * This function may be declared as `static` (i.e. without external
255 * linkage). This function may be provided as a function-like macro,
256 * but in this case it must evaluate its argument exactly once.
257 *
258 * \param[in] attributes The key attribute structure to query.
259 *
260 * \return The algorithm stored in the attribute structure.
261 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200262static psa_algorithm_t psa_get_key_algorithm(
263 const psa_key_attributes_t *attributes);
264
Gilles Peskine20628592019-04-19 19:29:50 +0200265/** Declare the type of a key.
266 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200267 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200268 * previously set in \p attributes.
269 *
270 * This function may be declared as `static` (i.e. without external
271 * linkage). This function may be provided as a function-like macro,
272 * but in this case it must evaluate each of its arguments exactly once.
273 *
274 * \param[out] attributes The attribute structure to write to.
275 * \param type The key type to write.
Gilles Peskinea170d922019-09-12 16:59:37 +0200276 * If this is 0, the key type in \p attributes
277 * becomes unspecified.
Gilles Peskine20628592019-04-19 19:29:50 +0200278 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200279static void psa_set_key_type(psa_key_attributes_t *attributes,
280 psa_key_type_t type);
281
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100282
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200283/** Declare the size of a key.
284 *
285 * This function overwrites any key size previously set in \p attributes.
286 *
287 * This function may be declared as `static` (i.e. without external
288 * linkage). This function may be provided as a function-like macro,
289 * but in this case it must evaluate each of its arguments exactly once.
290 *
291 * \param[out] attributes The attribute structure to write to.
292 * \param bits The key size in bits.
Gilles Peskinea170d922019-09-12 16:59:37 +0200293 * If this is 0, the key size in \p attributes
Gilles Peskine05c900b2019-09-12 18:29:43 +0200294 * becomes unspecified. Keys of size 0 are
295 * not supported.
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200296 */
297static void psa_set_key_bits(psa_key_attributes_t *attributes,
298 size_t bits);
299
Gilles Peskine20628592019-04-19 19:29:50 +0200300/** Retrieve the key type 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 type stored in the attribute structure.
309 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200310static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
311
Gilles Peskine20628592019-04-19 19:29:50 +0200312/** Retrieve the key size from key attributes.
313 *
314 * This function may be declared as `static` (i.e. without external
315 * linkage). This function may be provided as a function-like macro,
316 * but in this case it must evaluate its argument exactly once.
317 *
318 * \param[in] attributes The key attribute structure to query.
319 *
320 * \return The key size stored in the attribute structure, in bits.
321 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200322static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
323
Gilles Peskine20628592019-04-19 19:29:50 +0200324/** Retrieve the attributes of a key.
325 *
326 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200327 * psa_reset_key_attributes(). It then copies the attributes of
328 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200329 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200330 * \note This function may allocate memory or other resources.
331 * Once you have called this function on an attribute structure,
332 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200333 *
Gilles Peskine20628592019-04-19 19:29:50 +0200334 * \param[in] handle Handle to the key to query.
335 * \param[in,out] attributes On success, the attributes of the key.
336 * On failure, equivalent to a
337 * freshly-initialized structure.
338 *
339 * \retval #PSA_SUCCESS
340 * \retval #PSA_ERROR_INVALID_HANDLE
341 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
342 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100343 * \retval #PSA_ERROR_CORRUPTION_DETECTED
344 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100345 * \retval #PSA_ERROR_BAD_STATE
346 * The library has not been previously initialized by psa_crypto_init().
347 * It is implementation-dependent whether a failure to initialize
348 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200349 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200350psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
351 psa_key_attributes_t *attributes);
352
Gilles Peskine20628592019-04-19 19:29:50 +0200353/** Reset a key attribute structure to a freshly initialized state.
354 *
355 * You must initialize the attribute structure as described in the
356 * documentation of the type #psa_key_attributes_t before calling this
357 * function. Once the structure has been initialized, you may call this
358 * function at any time.
359 *
360 * This function frees any auxiliary resources that the structure
361 * may contain.
362 *
363 * \param[in,out] attributes The attribute structure to reset.
364 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200365void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200366
Gilles Peskine87a5e562019-04-17 12:28:25 +0200367/**@}*/
368
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100369/** \defgroup key_management Key management
370 * @{
371 */
372
Gilles Peskinef535eb22018-11-30 14:08:36 +0100373/** Open a handle to an existing persistent key.
374 *
Gilles Peskine4754cde2019-05-21 15:56:29 +0200375 * Open a handle to a persistent key. A key is persistent if it was created
376 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
377 * always has a nonzero key identifier, set with psa_set_key_id() when
378 * creating the key. Implementations may provide additional pre-provisioned
Andrew Thoelke203491c2019-08-21 17:55:30 +0100379 * keys that can be opened with psa_open_key(). Such keys have a key identifier
380 * in the vendor range, as documented in the description of #psa_key_id_t.
Gilles Peskine4754cde2019-05-21 15:56:29 +0200381 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100382 * The application must eventually close the handle with psa_close_key() or
383 * psa_destroy_key() to release associated resources. If the application dies
384 * without calling one of these functions, the implementation should perform
385 * the equivalent of a call to psa_close_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100386 *
Andrew Thoelke9741b112019-08-21 18:20:41 +0100387 * Some implementations permit an application to open the same key multiple
Andrew Thoelkede183412019-09-05 09:38:06 +0100388 * times. If this is successful, each call to psa_open_key() will return a
389 * different key handle.
390 *
391 * \note Applications that rely on opening a key multiple times will not be
392 * portable to implementations that only permit a single key handle to be
393 * opened. See also :ref:\`key-handles\`.
Andrew Thoelke9741b112019-08-21 18:20:41 +0100394 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100395 * \param id The persistent identifier of the key.
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100396 * \param[out] handle On success, a handle to the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100397 *
398 * \retval #PSA_SUCCESS
399 * Success. The application can now use the value of `*handle`
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100400 * to access the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100401 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Andrew Thoelke9741b112019-08-21 18:20:41 +0100402 * The implementation does not have sufficient resources to open the
403 * key. This can be due to reaching an implementation limit on the
404 * number of open keys, the number of open key handles, or available
405 * memory.
David Saadab4ecc272019-02-14 13:48:10 +0200406 * \retval #PSA_ERROR_DOES_NOT_EXIST
Andrew Thoelke9741b112019-08-21 18:20:41 +0100407 * There is no persistent key with key identifier \p id.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100408 * \retval #PSA_ERROR_INVALID_ARGUMENT
Andrew Thoelke9741b112019-08-21 18:20:41 +0100409 * \p id is not a valid persistent key identifier.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100410 * \retval #PSA_ERROR_NOT_PERMITTED
411 * The specified key exists, but the application does not have the
412 * permission to access it. Note that this specification does not
413 * define any way to create such a key, but it may be possible
414 * through implementation-specific means.
Gilles Peskine225010f2019-05-06 18:44:55 +0200415 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawd789dc12019-08-12 15:06:48 +0100416 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine225010f2019-05-06 18:44:55 +0200417 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100418 * \retval #PSA_ERROR_BAD_STATE
419 * The library has not been previously initialized by psa_crypto_init().
420 * It is implementation-dependent whether a failure to initialize
421 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100422 */
Gilles Peskine225010f2019-05-06 18:44:55 +0200423psa_status_t psa_open_key(psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100424 psa_key_handle_t *handle);
425
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100426
Gilles Peskinef535eb22018-11-30 14:08:36 +0100427/** Close a key handle.
428 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100429 * If the handle designates a volatile key, this will destroy the key material
430 * and free all associated resources, just like psa_destroy_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100431 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100432 * If this is the last open handle to a persistent key, then closing the handle
433 * will free all resources associated with the key in volatile memory. The key
434 * data in persistent storage is not affected and can be opened again later
435 * with a call to psa_open_key().
Gilles Peskinef535eb22018-11-30 14:08:36 +0100436 *
Andrew Thoelke3daba812019-08-21 22:46:56 +0100437 * Closing the key handle makes the handle invalid, and the key handle
Andrew Thoelke8824dae2019-08-22 15:04:48 +0100438 * must not be used again by the application.
Andrew Thoelke3daba812019-08-21 22:46:56 +0100439 *
Andrew Thoelke970629f2019-09-09 09:56:34 +0100440 * \note If the key handle was used to set up an active
Andrew Thoelkede183412019-09-05 09:38:06 +0100441 * :ref:\`multipart operation <multipart-operations>\`, then closing the
442 * key handle can cause the multipart operation to fail. Applications should
443 * maintain the key handle until after the multipart operation has finished.
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100444 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100445 * \param handle The key handle to close.
446 *
447 * \retval #PSA_SUCCESS
448 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100449 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawf97c8522019-08-15 13:27:12 +0100450 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100451 * \retval #PSA_ERROR_BAD_STATE
452 * The library has not been previously initialized by psa_crypto_init().
453 * It is implementation-dependent whether a failure to initialize
454 * results in this error code.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100455 */
456psa_status_t psa_close_key(psa_key_handle_t handle);
457
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100458/**@}*/
459
460/** \defgroup import_export Key import and export
461 * @{
462 */
463
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100464/**
465 * \brief Import a key in binary format.
466 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100467 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100468 * documentation of psa_export_public_key() for the format of public keys
469 * and to the documentation of psa_export_key() for the format for
470 * other key types.
471 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200472 * The key data determines the key size. The attributes may optionally
473 * specify a key size; in this case it must match the size determined
474 * from the key data. A key size of 0 in \p attributes indicates that
475 * the key size is solely determined by the key data.
476 *
477 * Implementations must reject an attempt to import a key of size 0.
478 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100479 * This specification supports a single format for each key type.
480 * Implementations may support other formats as long as the standard
481 * format is supported. Implementations that support other formats
482 * should ensure that the formats are clearly unambiguous so as to
483 * minimize the risk that an invalid input is accidentally interpreted
484 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100485 *
Gilles Peskine20628592019-04-19 19:29:50 +0200486 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200487 * The key size is always determined from the
488 * \p data buffer.
489 * If the key size in \p attributes is nonzero,
490 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200491 * \param[out] handle On success, a handle to the newly created key.
492 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100493 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200494 * buffer is interpreted according to the type declared
495 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200496 * All implementations must support at least the format
497 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100498 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200499 * the chosen type. Implementations may allow other
500 * formats, but should be conservative: implementations
501 * should err on the side of rejecting content if it
502 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200503 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100504 *
Gilles Peskine28538492018-07-11 17:34:00 +0200505 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100506 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100507 * If the key is persistent, the key material and the key's metadata
508 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200509 * \retval #PSA_ERROR_ALREADY_EXISTS
510 * This is an attempt to create a persistent key, and there is
511 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200512 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200513 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200514 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200515 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200516 * The key attributes, as a whole, are invalid.
517 * \retval #PSA_ERROR_INVALID_ARGUMENT
518 * The key data is not correctly formatted.
519 * \retval #PSA_ERROR_INVALID_ARGUMENT
520 * The size in \p attributes is nonzero and does not match the size
521 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200522 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
523 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
524 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100525 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200526 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200527 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300528 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw97d3bc32019-08-08 14:40:04 +0100529 * \p operation is either not initialized or is in use
530 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300531 * The library has not been previously initialized by psa_crypto_init().
532 * It is implementation-dependent whether a failure to initialize
533 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100534 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200535psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100536 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200537 size_t data_length,
538 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100539
540/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100541 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200542 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100543 * This function destroys a key from both volatile
Gilles Peskine154bd952018-04-19 08:38:16 +0200544 * memory and, if applicable, non-volatile storage. Implementations shall
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100545 * make a best effort to ensure that that the key material cannot be recovered.
Gilles Peskine154bd952018-04-19 08:38:16 +0200546 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100547 * This function also erases any metadata such as policies and frees
548 * resources associated with the key. To free all resources associated with
549 * the key, all handles to the key must be closed or destroyed.
Gilles Peskine154bd952018-04-19 08:38:16 +0200550 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100551 * Destroying the key makes the handle invalid, and the key handle
552 * must not be used again by the application. Using other open handles to the
553 * destroyed key in a cryptographic operation will result in an error.
Andrew Thoelke07f16b72019-08-21 22:48:47 +0100554 *
Andrew Thoelkede183412019-09-05 09:38:06 +0100555 * If a key is currently in use in a multipart operation, then destroying the
556 * key will cause the multipart operation to fail.
Andrew Thoelke07f16b72019-08-21 22:48:47 +0100557 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100558 * \param handle Handle to the key to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559 *
Gilles Peskine28538492018-07-11 17:34:00 +0200560 * \retval #PSA_SUCCESS
Adrian L. Shawd56456c2019-05-15 11:36:13 +0100561 * The key material has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200562 * \retval #PSA_ERROR_NOT_PERMITTED
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100563 * The key cannot be erased because it is
Gilles Peskine65eb8582018-04-19 08:28:58 +0200564 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100565 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200566 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200567 * There was an failure in communication with the cryptoprocessor.
568 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200569 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200570 * The storage is corrupted. Implementations shall make a best effort
571 * to erase key material even in this stage, however applications
572 * should be aware that it may be impossible to guarantee that the
573 * key material is not recoverable in such cases.
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200574 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200575 * An unexpected condition which is not a storage corruption or
576 * a communication failure occurred. The cryptoprocessor may have
577 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300578 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300579 * The library has not been previously initialized by psa_crypto_init().
580 * It is implementation-dependent whether a failure to initialize
581 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100582 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100583psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584
585/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100586 * \brief Export a key in binary format.
587 *
588 * The output of this function can be passed to psa_import_key() to
589 * create an equivalent object.
590 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100591 * If the implementation of psa_import_key() supports other formats
592 * beyond the format specified here, the output from psa_export_key()
593 * must use the representation specified here, not the original
594 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100595 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100596 * For standard key types, the output format is as follows:
597 *
598 * - For symmetric keys (including MAC keys), the format is the
599 * raw bytes of the key.
600 * - For DES, the key data consists of 8 bytes. The parity bits must be
601 * correct.
602 * - For Triple-DES, the format is the concatenation of the
603 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200604 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200605 * is the non-encrypted DER encoding of the representation defined by
606 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
607 * ```
608 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200609 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200610 * modulus INTEGER, -- n
611 * publicExponent INTEGER, -- e
612 * privateExponent INTEGER, -- d
613 * prime1 INTEGER, -- p
614 * prime2 INTEGER, -- q
615 * exponent1 INTEGER, -- d mod (p-1)
616 * exponent2 INTEGER, -- d mod (q-1)
617 * coefficient INTEGER, -- (inverse of q) mod p
618 * }
619 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200620 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200621 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100622 * a representation of the private value as a `ceiling(m/8)`-byte string
623 * where `m` is the bit size associated with the curve, i.e. the bit size
624 * of the order of the curve's coordinate field. This byte string is
625 * in little-endian order for Montgomery curves (curve types
626 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
627 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
628 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100629 * This is the content of the `privateKey` field of the `ECPrivateKey`
630 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200631 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200632 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000633 * format is the representation of the private key `x` as a big-endian byte
634 * string. The length of the byte string is the private key size in bytes
635 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200636 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
637 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100638 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200639 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
640 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100641 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200642 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200643 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200644 * \param[out] data_length On success, the number of bytes
645 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100646 *
Gilles Peskine28538492018-07-11 17:34:00 +0200647 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100648 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200649 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200650 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100651 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200652 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
653 * The size of the \p data buffer is too small. You can determine a
654 * sufficient buffer size by calling
655 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
656 * where \c type is the key type
657 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200658 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
659 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200660 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100661 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100662 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300663 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300664 * The library has not been previously initialized by psa_crypto_init().
665 * It is implementation-dependent whether a failure to initialize
666 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100668psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100669 uint8_t *data,
670 size_t data_size,
671 size_t *data_length);
672
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100673/**
674 * \brief Export a public key or the public part of a key pair in binary format.
675 *
676 * The output of this function can be passed to psa_import_key() to
677 * create an object that is equivalent to the public key.
678 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000679 * This specification supports a single format for each key type.
680 * Implementations may support other formats as long as the standard
681 * format is supported. Implementations that support other formats
682 * should ensure that the formats are clearly unambiguous so as to
683 * minimize the risk that an invalid input is accidentally interpreted
684 * according to a different format.
685 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000686 * For standard key types, the output format is as follows:
687 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
688 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
689 * ```
690 * RSAPublicKey ::= SEQUENCE {
691 * modulus INTEGER, -- n
692 * publicExponent INTEGER } -- e
693 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000694 * - For elliptic curve public keys (key types for which
695 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
696 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
697 * Let `m` be the bit size associated with the curve, i.e. the bit size of
698 * `q` for a curve over `F_q`. The representation consists of:
699 * - The byte 0x04;
700 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
701 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200702 * - For Diffie-Hellman key exchange public keys (key types for which
703 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000704 * the format is the representation of the public key `y = g^x mod p` as a
705 * big-endian byte string. The length of the byte string is the length of the
706 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100707 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200708 * Exporting a public key object or the public part of a key pair is
709 * always permitted, regardless of the key's usage flags.
710 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100711 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200712 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200713 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200714 * \param[out] data_length On success, the number of bytes
715 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100716 *
Gilles Peskine28538492018-07-11 17:34:00 +0200717 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100718 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200719 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200720 * The key is neither a public key nor a key pair.
721 * \retval #PSA_ERROR_NOT_SUPPORTED
722 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
723 * The size of the \p data buffer is too small. You can determine a
724 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200725 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200726 * where \c type is the key type
727 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200728 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
729 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200730 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100731 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100732 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300733 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300734 * The library has not been previously initialized by psa_crypto_init().
735 * It is implementation-dependent whether a failure to initialize
736 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100737 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100738psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100739 uint8_t *data,
740 size_t data_size,
741 size_t *data_length);
742
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100743/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100744 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100745 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000746 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100747 * This function is primarily useful to copy a key from one location
748 * to another, since it populates a key using the material from
749 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200750 *
Adrian L. Shaw0a695bd2019-05-15 13:28:41 +0100751 * This function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100752 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100753 *
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200754 * The policy on the source key must have the usage flag
755 * #PSA_KEY_USAGE_COPY set.
Gilles Peskined6a8f5f2019-05-14 16:25:50 +0200756 * This flag is sufficient to permit the copy if the key has the lifetime
757 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
758 * Some secure elements do not provide a way to copy a key without
759 * making it extractable from the secure element. If a key is located
760 * in such a secure element, then the key must have both usage flags
761 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
762 * a copy of the key outside the secure element.
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200763 *
Gilles Peskine20628592019-04-19 19:29:50 +0200764 * The resulting key may only be used in a way that conforms to
765 * both the policy of the original key and the policy specified in
766 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100767 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200768 * usage flags on the source policy and the usage flags in \p attributes.
769 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100770 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200771 * - If either of the policies allows an algorithm and the other policy
772 * allows a wildcard-based algorithm policy that includes this algorithm,
773 * the resulting key allows the same algorithm.
774 * - If the policies do not allow any algorithm in common, this function
775 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200776 *
Gilles Peskine20628592019-04-19 19:29:50 +0200777 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100778 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200779 *
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100780 * \param source_handle The key to copy. It must be a valid key handle.
Gilles Peskine20628592019-04-19 19:29:50 +0200781 * \param[in] attributes The attributes for the new key.
782 * They are used as follows:
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200783 * - The key type and size may be 0. If either is
784 * nonzero, it must match the corresponding
785 * attribute of the source key.
Gilles Peskine20628592019-04-19 19:29:50 +0200786 * - The key location (the lifetime and, for
787 * persistent keys, the key identifier) is
788 * used directly.
789 * - The policy constraints (usage flags and
790 * algorithm policy) are combined from
791 * the source key and \p attributes so that
792 * both sets of restrictions apply, as
793 * described in the documentation of this function.
794 * \param[out] target_handle On success, a handle to the newly created key.
795 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200796 *
797 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100798 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200799 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200800 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200801 * This is an attempt to create a persistent key, and there is
802 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200803 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200804 * The lifetime or identifier in \p attributes are invalid.
805 * \retval #PSA_ERROR_INVALID_ARGUMENT
806 * The policy constraints on the source and specified in
807 * \p attributes are incompatible.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200808 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine24f10f82019-05-16 12:18:32 +0200809 * \p attributes specifies a key type or key size
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200810 * which does not match the attributes of the source key.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100811 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine8e0206a2019-05-14 14:24:28 +0200812 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
813 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100814 * The source key is not exportable and its lifetime does not
815 * allow copying it to the target's lifetime.
816 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
817 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200818 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
819 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shaw60b03202019-08-06 17:26:16 +0100820 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200821 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100822 * \retval #PSA_ERROR_BAD_STATE
823 * The library has not been previously initialized by psa_crypto_init().
824 * It is implementation-dependent whether a failure to initialize
825 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100826 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100827psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200828 const psa_key_attributes_t *attributes,
829 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100830
831/**@}*/
832
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100833/** \defgroup hash Message digests
834 * @{
835 */
836
Gilles Peskine69647a42019-01-14 20:18:12 +0100837/** Calculate the hash (digest) of a message.
838 *
839 * \note To verify the hash of a message against an
840 * expected value, use psa_hash_compare() instead.
841 *
842 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
843 * such that #PSA_ALG_IS_HASH(\p alg) is true).
844 * \param[in] input Buffer containing the message to hash.
845 * \param input_length Size of the \p input buffer in bytes.
846 * \param[out] hash Buffer where the hash is to be written.
847 * \param hash_size Size of the \p hash buffer in bytes.
848 * \param[out] hash_length On success, the number of bytes
849 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100850 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100851 *
852 * \retval #PSA_SUCCESS
853 * Success.
854 * \retval #PSA_ERROR_NOT_SUPPORTED
855 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100856 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100857 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
858 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100859 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
860 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
861 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200862 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100863 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100864 * \retval #PSA_ERROR_BAD_STATE
865 * The library has not been previously initialized by psa_crypto_init().
866 * It is implementation-dependent whether a failure to initialize
867 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100868 */
869psa_status_t psa_hash_compute(psa_algorithm_t alg,
870 const uint8_t *input,
871 size_t input_length,
872 uint8_t *hash,
873 size_t hash_size,
874 size_t *hash_length);
875
876/** Calculate the hash (digest) of a message and compare it with a
877 * reference value.
878 *
879 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
880 * such that #PSA_ALG_IS_HASH(\p alg) is true).
881 * \param[in] input Buffer containing the message to hash.
882 * \param input_length Size of the \p input buffer in bytes.
883 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100884 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100885 *
886 * \retval #PSA_SUCCESS
887 * The expected hash is identical to the actual hash of the input.
888 * \retval #PSA_ERROR_INVALID_SIGNATURE
889 * The hash of the message was calculated successfully, but it
890 * differs from the expected hash.
891 * \retval #PSA_ERROR_NOT_SUPPORTED
892 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100893 * \retval #PSA_ERROR_INVALID_ARGUMENT
894 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100895 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
896 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
897 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200898 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100899 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100900 * \retval #PSA_ERROR_BAD_STATE
901 * The library has not been previously initialized by psa_crypto_init().
902 * It is implementation-dependent whether a failure to initialize
903 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100904 */
905psa_status_t psa_hash_compare(psa_algorithm_t alg,
906 const uint8_t *input,
907 size_t input_length,
908 const uint8_t *hash,
909 const size_t hash_length);
910
Gilles Peskine308b91d2018-02-08 09:47:44 +0100911/** The type of the state data structure for multipart hash operations.
912 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000913 * Before calling any function on a hash operation object, the application must
914 * initialize it by any of the following means:
915 * - Set the structure to all-bits-zero, for example:
916 * \code
917 * psa_hash_operation_t operation;
918 * memset(&operation, 0, sizeof(operation));
919 * \endcode
920 * - Initialize the structure to logical zero values, for example:
921 * \code
922 * psa_hash_operation_t operation = {0};
923 * \endcode
924 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
925 * for example:
926 * \code
927 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
928 * \endcode
929 * - Assign the result of the function psa_hash_operation_init()
930 * to the structure, for example:
931 * \code
932 * psa_hash_operation_t operation;
933 * operation = psa_hash_operation_init();
934 * \endcode
935 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100936 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100937 * make any assumptions about the content of this structure except
938 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100939typedef struct psa_hash_operation_s psa_hash_operation_t;
940
Jaeden Amero6a25b412019-01-04 11:47:44 +0000941/** \def PSA_HASH_OPERATION_INIT
942 *
943 * This macro returns a suitable initializer for a hash operation object
944 * of type #psa_hash_operation_t.
945 */
946#ifdef __DOXYGEN_ONLY__
947/* This is an example definition for documentation purposes.
948 * Implementations should define a suitable value in `crypto_struct.h`.
949 */
950#define PSA_HASH_OPERATION_INIT {0}
951#endif
952
953/** Return an initial value for a hash operation object.
954 */
955static psa_hash_operation_t psa_hash_operation_init(void);
956
Gilles Peskinef45adda2019-01-14 18:29:18 +0100957/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100958 *
959 * The sequence of operations to calculate a hash (message digest)
960 * is as follows:
961 * -# Allocate an operation object which will be passed to all the functions
962 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000963 * -# Initialize the operation object with one of the methods described in the
964 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200965 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100966 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100967 * of the message each time. The hash that is calculated is the hash
968 * of the concatenation of these messages in order.
969 * -# To calculate the hash, call psa_hash_finish().
970 * To compare the hash with an expected value, call psa_hash_verify().
971 *
972 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000973 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100974 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200975 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100976 * eventually terminate the operation. The following events terminate an
977 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100978 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100979 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100980 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000981 * \param[in,out] operation The operation object to set up. It must have
982 * been initialized as per the documentation for
983 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200984 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
985 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100986 *
Gilles Peskine28538492018-07-11 17:34:00 +0200987 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100988 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200989 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100990 * \p alg is not a supported hash algorithm.
991 * \retval #PSA_ERROR_INVALID_ARGUMENT
992 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100993 * \retval #PSA_ERROR_BAD_STATE
994 * The operation state is not valid (already set up and not
995 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200996 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
997 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
998 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200999 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001000 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawe970d652019-08-08 14:40:04 +01001001 * \p operation is either not initialized or is in use
1002 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001003 * The library has not been previously initialized by psa_crypto_init().
1004 * It is implementation-dependent whether a failure to initialize
1005 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001006 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001007psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001008 psa_algorithm_t alg);
1009
Gilles Peskine308b91d2018-02-08 09:47:44 +01001010/** Add a message fragment to a multipart hash operation.
1011 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001012 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001013 *
1014 * If this function returns an error status, the operation becomes inactive.
1015 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001016 * \param[in,out] operation Active hash operation.
1017 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001018 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001019 *
Gilles Peskine28538492018-07-11 17:34:00 +02001020 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001021 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001022 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001023 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001024 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1025 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1026 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001027 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001028 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001029 * The operation state is not valid.
1030 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001031 * The library has not been previously initialized by psa_crypto_init().
1032 * It is implementation-dependent whether a failure to initialize
1033 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001034 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001035psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1036 const uint8_t *input,
1037 size_t input_length);
1038
Gilles Peskine308b91d2018-02-08 09:47:44 +01001039/** Finish the calculation of the hash of a message.
1040 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001041 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001042 * This function calculates the hash of the message formed by concatenating
1043 * the inputs passed to preceding calls to psa_hash_update().
1044 *
1045 * When this function returns, the operation becomes inactive.
1046 *
1047 * \warning Applications should not call this function if they expect
1048 * a specific value for the hash. Call psa_hash_verify() instead.
1049 * Beware that comparing integrity or authenticity data such as
1050 * hash values with a function such as \c memcmp is risky
1051 * because the time taken by the comparison may leak information
1052 * about the hashed data which could allow an attacker to guess
1053 * a valid hash and thereby bypass security controls.
1054 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001055 * \param[in,out] operation Active hash operation.
1056 * \param[out] hash Buffer where the hash is to be written.
1057 * \param hash_size Size of the \p hash buffer in bytes.
1058 * \param[out] hash_length On success, the number of bytes
1059 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001060 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001061 * hash algorithm that is calculated.
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 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001065 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001066 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001067 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001068 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001069 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001070 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001071 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1072 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1073 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001074 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001075 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001076 * The operation state is not valid.
1077 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001078 * The library has not been previously initialized by psa_crypto_init().
1079 * It is implementation-dependent whether a failure to initialize
1080 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001081 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001082psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1083 uint8_t *hash,
1084 size_t hash_size,
1085 size_t *hash_length);
1086
Gilles Peskine308b91d2018-02-08 09:47:44 +01001087/** Finish the calculation of the hash of a message and compare it with
1088 * an expected value.
1089 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001090 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001091 * This function calculates the hash of the message formed by concatenating
1092 * the inputs passed to preceding calls to psa_hash_update(). It then
1093 * compares the calculated hash with the expected hash passed as a
1094 * parameter to this function.
1095 *
1096 * When this function returns, the operation becomes inactive.
1097 *
Gilles Peskine19067982018-03-20 17:54:53 +01001098 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001099 * comparison between the actual hash and the expected hash is performed
1100 * in constant time.
1101 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001102 * \param[in,out] operation Active hash operation.
1103 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001104 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001105 *
Gilles Peskine28538492018-07-11 17:34:00 +02001106 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001107 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001108 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001109 * The hash of the message was calculated successfully, but it
1110 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001111 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001112 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001113 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1114 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1115 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001116 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001117 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001118 * The operation state is not valid.
1119 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001120 * The library has not been previously initialized by psa_crypto_init().
1121 * It is implementation-dependent whether a failure to initialize
1122 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001123 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001124psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1125 const uint8_t *hash,
1126 size_t hash_length);
1127
Gilles Peskine308b91d2018-02-08 09:47:44 +01001128/** Abort a hash operation.
1129 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001130 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001131 * \p operation structure itself. Once aborted, the operation object
1132 * can be reused for another operation by calling
1133 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001134 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001135 * You may call this function any time after the operation object has
1136 * been initialized by any of the following methods:
1137 * - A call to psa_hash_setup(), whether it succeeds or not.
1138 * - Initializing the \c struct to all-bits-zero.
1139 * - Initializing the \c struct to logical zeros, e.g.
1140 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001141 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001142 * In particular, calling psa_hash_abort() after the operation has been
1143 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1144 * psa_hash_verify() is safe and has no effect.
1145 *
1146 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001147 *
Gilles Peskine28538492018-07-11 17:34:00 +02001148 * \retval #PSA_SUCCESS
1149 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001150 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001151 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1152 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001153 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001154 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001155 * The operation state is not valid.
1156 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001157 * The library has not been previously initialized by psa_crypto_init().
1158 * It is implementation-dependent whether a failure to initialize
1159 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001160 */
1161psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001162
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001163/** Clone a hash operation.
1164 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001165 * This function copies the state of an ongoing hash operation to
1166 * a new operation object. In other words, this function is equivalent
1167 * to calling psa_hash_setup() on \p target_operation with the same
1168 * algorithm that \p source_operation was set up for, then
1169 * psa_hash_update() on \p target_operation with the same input that
1170 * that was passed to \p source_operation. After this function returns, the
1171 * two objects are independent, i.e. subsequent calls involving one of
1172 * the objects do not affect the other object.
1173 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001174 * \param[in] source_operation The active hash operation to clone.
1175 * \param[in,out] target_operation The operation object to set up.
1176 * It must be initialized but not active.
1177 *
1178 * \retval #PSA_SUCCESS
1179 * \retval #PSA_ERROR_BAD_STATE
1180 * \p source_operation is not an active hash operation.
1181 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001182 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001183 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1184 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001185 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001186 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001187 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01001188 * The operation state is either not initialized or has already been setup.
1189 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw8f7cd1e2019-08-08 14:49:01 +01001190 * The operation state is either not initialized or has already been setup.
1191 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001192 * The library has not been previously initialized by psa_crypto_init().
1193 * It is implementation-dependent whether a failure to initialize
1194 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001195 */
1196psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1197 psa_hash_operation_t *target_operation);
1198
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001199/**@}*/
1200
Gilles Peskine8c9def32018-02-08 10:02:12 +01001201/** \defgroup MAC Message authentication codes
1202 * @{
1203 */
1204
Gilles Peskine69647a42019-01-14 20:18:12 +01001205/** Calculate the MAC (message authentication code) of a message.
1206 *
1207 * \note To verify the MAC of a message against an
1208 * expected value, use psa_mac_verify() instead.
1209 * Beware that comparing integrity or authenticity data such as
1210 * MAC values with a function such as \c memcmp is risky
1211 * because the time taken by the comparison may leak information
1212 * about the MAC value which could allow an attacker to guess
1213 * a valid MAC and thereby bypass security controls.
1214 *
1215 * \param handle Handle to the key to use for the operation.
1216 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001217 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001218 * \param[in] input Buffer containing the input message.
1219 * \param input_length Size of the \p input buffer in bytes.
1220 * \param[out] mac Buffer where the MAC value is to be written.
1221 * \param mac_size Size of the \p mac buffer in bytes.
1222 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001223 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001224 *
1225 * \retval #PSA_SUCCESS
1226 * Success.
1227 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001228 * \retval #PSA_ERROR_NOT_PERMITTED
1229 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001230 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001231 * \retval #PSA_ERROR_NOT_SUPPORTED
1232 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001233 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1234 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001235 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1236 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1237 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001238 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001239 * \retval #PSA_ERROR_STORAGE_FAILURE
1240 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001241 * \retval #PSA_ERROR_BAD_STATE
1242 * The library has not been previously initialized by psa_crypto_init().
1243 * It is implementation-dependent whether a failure to initialize
1244 * results in this error code.
1245 */
1246psa_status_t psa_mac_compute(psa_key_handle_t handle,
1247 psa_algorithm_t alg,
1248 const uint8_t *input,
1249 size_t input_length,
1250 uint8_t *mac,
1251 size_t mac_size,
1252 size_t *mac_length);
1253
1254/** Calculate the MAC of a message and compare it with a reference value.
1255 *
1256 * \param handle Handle to the key to use for the operation.
1257 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001258 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001259 * \param[in] input Buffer containing the input message.
1260 * \param input_length Size of the \p input buffer in bytes.
1261 * \param[out] mac Buffer containing the expected MAC value.
1262 * \param mac_length Size of the \p mac buffer in bytes.
1263 *
1264 * \retval #PSA_SUCCESS
1265 * The expected MAC is identical to the actual MAC of the input.
1266 * \retval #PSA_ERROR_INVALID_SIGNATURE
1267 * The MAC of the message was calculated successfully, but it
1268 * differs from the expected value.
1269 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001270 * \retval #PSA_ERROR_NOT_PERMITTED
1271 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001272 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001273 * \retval #PSA_ERROR_NOT_SUPPORTED
1274 * \p alg is not supported or is not a MAC algorithm.
1275 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1276 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1277 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001278 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001279 * \retval #PSA_ERROR_STORAGE_FAILURE
1280 * The key could not be retrieved from storage.
1281 * \retval #PSA_ERROR_BAD_STATE
1282 * The library has not been previously initialized by psa_crypto_init().
1283 * It is implementation-dependent whether a failure to initialize
1284 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001285 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001286psa_status_t psa_mac_verify(psa_key_handle_t handle,
1287 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001288 const uint8_t *input,
1289 size_t input_length,
1290 const uint8_t *mac,
1291 const size_t mac_length);
1292
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001293/** The type of the state data structure for multipart MAC operations.
1294 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001295 * Before calling any function on a MAC operation object, the application must
1296 * initialize it by any of the following means:
1297 * - Set the structure to all-bits-zero, for example:
1298 * \code
1299 * psa_mac_operation_t operation;
1300 * memset(&operation, 0, sizeof(operation));
1301 * \endcode
1302 * - Initialize the structure to logical zero values, for example:
1303 * \code
1304 * psa_mac_operation_t operation = {0};
1305 * \endcode
1306 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1307 * for example:
1308 * \code
1309 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1310 * \endcode
1311 * - Assign the result of the function psa_mac_operation_init()
1312 * to the structure, for example:
1313 * \code
1314 * psa_mac_operation_t operation;
1315 * operation = psa_mac_operation_init();
1316 * \endcode
1317 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001318 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001319 * make any assumptions about the content of this structure except
1320 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001321typedef struct psa_mac_operation_s psa_mac_operation_t;
1322
Jaeden Amero769ce272019-01-04 11:48:03 +00001323/** \def PSA_MAC_OPERATION_INIT
1324 *
1325 * This macro returns a suitable initializer for a MAC operation object of type
1326 * #psa_mac_operation_t.
1327 */
1328#ifdef __DOXYGEN_ONLY__
1329/* This is an example definition for documentation purposes.
1330 * Implementations should define a suitable value in `crypto_struct.h`.
1331 */
1332#define PSA_MAC_OPERATION_INIT {0}
1333#endif
1334
1335/** Return an initial value for a MAC operation object.
1336 */
1337static psa_mac_operation_t psa_mac_operation_init(void);
1338
Gilles Peskinef45adda2019-01-14 18:29:18 +01001339/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001340 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001341 * This function sets up the calculation of the MAC
1342 * (message authentication code) of a byte string.
1343 * To verify the MAC of a message against an
1344 * expected value, use psa_mac_verify_setup() instead.
1345 *
1346 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001347 * -# Allocate an operation object which will be passed to all the functions
1348 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001349 * -# Initialize the operation object with one of the methods described in the
1350 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001351 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001352 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1353 * of the message each time. The MAC that is calculated is the MAC
1354 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001355 * -# At the end of the message, call psa_mac_sign_finish() to finish
1356 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001357 *
1358 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001359 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001360 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001361 * After a successful call to psa_mac_sign_setup(), the application must
1362 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001363 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001364 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001365 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001366 * \param[in,out] operation The operation object to set up. It must have
1367 * been initialized as per the documentation for
1368 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001369 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001370 * It must remain valid until the operation
1371 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001372 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001373 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001374 *
Gilles Peskine28538492018-07-11 17:34:00 +02001375 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001376 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001377 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001378 * \retval #PSA_ERROR_NOT_PERMITTED
1379 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001380 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001381 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001382 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001383 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1384 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1385 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001386 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001387 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001388 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001389 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001390 * The operation state is not valid (already set up and not
1391 * subsequently completed).
1392 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001393 * The library has not been previously initialized by psa_crypto_init().
1394 * It is implementation-dependent whether a failure to initialize
1395 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001396 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001397psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001398 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001399 psa_algorithm_t alg);
1400
Gilles Peskinef45adda2019-01-14 18:29:18 +01001401/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001402 *
1403 * This function sets up the verification of the MAC
1404 * (message authentication code) of a byte string against an expected value.
1405 *
1406 * The sequence of operations to verify a MAC is as follows:
1407 * -# Allocate an operation object which will be passed to all the functions
1408 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001409 * -# Initialize the operation object with one of the methods described in the
1410 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001411 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001412 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1413 * of the message each time. The MAC that is calculated is the MAC
1414 * of the concatenation of these messages in order.
1415 * -# At the end of the message, call psa_mac_verify_finish() to finish
1416 * calculating the actual MAC of the message and verify it against
1417 * the expected value.
1418 *
1419 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001420 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001421 *
1422 * After a successful call to psa_mac_verify_setup(), the application must
1423 * eventually terminate the operation through one of the following methods:
1424 * - A failed call to psa_mac_update().
1425 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1426 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001427 * \param[in,out] operation The operation object to set up. It must have
1428 * been initialized as per the documentation for
1429 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001430 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001431 * It must remain valid until the operation
1432 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001433 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1434 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001435 *
Gilles Peskine28538492018-07-11 17:34:00 +02001436 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001437 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001438 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001439 * \retval #PSA_ERROR_NOT_PERMITTED
1440 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001441 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001442 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001443 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001444 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1445 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1446 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001447 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001448 * \retval #PSA_ERROR_STORAGE_FAILURE
1449 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001450 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001451 * The operation state is not valid (already set up and not
1452 * subsequently completed).
1453 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001454 * The library has not been previously initialized by psa_crypto_init().
1455 * It is implementation-dependent whether a failure to initialize
1456 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001457 */
1458psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001459 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001460 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001461
Gilles Peskinedcd14942018-07-12 00:30:52 +02001462/** Add a message fragment to a multipart MAC operation.
1463 *
1464 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1465 * before calling this function.
1466 *
1467 * If this function returns an error status, the operation becomes inactive.
1468 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001469 * \param[in,out] operation Active MAC operation.
1470 * \param[in] input Buffer containing the message fragment to add to
1471 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001472 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001473 *
1474 * \retval #PSA_SUCCESS
1475 * Success.
1476 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001477 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001478 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1479 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1480 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001481 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001482 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001483 * \retval #PSA_ERROR_BAD_STATE
1484 * The library has not been previously initialized by psa_crypto_init().
1485 * It is implementation-dependent whether a failure to initialize
1486 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001487 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001488psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1489 const uint8_t *input,
1490 size_t input_length);
1491
Gilles Peskinedcd14942018-07-12 00:30:52 +02001492/** Finish the calculation of the MAC of a message.
1493 *
1494 * The application must call psa_mac_sign_setup() before calling this function.
1495 * This function calculates the MAC of the message formed by concatenating
1496 * the inputs passed to preceding calls to psa_mac_update().
1497 *
1498 * When this function returns, the operation becomes inactive.
1499 *
1500 * \warning Applications should not call this function if they expect
1501 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1502 * Beware that comparing integrity or authenticity data such as
1503 * MAC values with a function such as \c memcmp is risky
1504 * because the time taken by the comparison may leak information
1505 * about the MAC value which could allow an attacker to guess
1506 * a valid MAC and thereby bypass security controls.
1507 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001508 * \param[in,out] operation Active MAC operation.
1509 * \param[out] mac Buffer where the MAC value is to be written.
1510 * \param mac_size Size of the \p mac buffer in bytes.
1511 * \param[out] mac_length On success, the number of bytes
1512 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001513 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001514 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001515 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001516 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001517 *
1518 * \retval #PSA_SUCCESS
1519 * Success.
1520 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001521 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001522 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001523 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001524 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1525 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1526 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1527 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001528 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001529 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001530 * \retval #PSA_ERROR_BAD_STATE
1531 * The library has not been previously initialized by psa_crypto_init().
1532 * It is implementation-dependent whether a failure to initialize
1533 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001534 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001535psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1536 uint8_t *mac,
1537 size_t mac_size,
1538 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001539
Gilles Peskinedcd14942018-07-12 00:30:52 +02001540/** Finish the calculation of the MAC of a message and compare it with
1541 * an expected value.
1542 *
1543 * The application must call psa_mac_verify_setup() before calling this function.
1544 * This function calculates the MAC of the message formed by concatenating
1545 * the inputs passed to preceding calls to psa_mac_update(). It then
1546 * compares the calculated MAC with the expected MAC passed as a
1547 * parameter to this function.
1548 *
1549 * When this function returns, the operation becomes inactive.
1550 *
1551 * \note Implementations shall make the best effort to ensure that the
1552 * comparison between the actual MAC and the expected MAC is performed
1553 * in constant time.
1554 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001555 * \param[in,out] operation Active MAC operation.
1556 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001557 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001558 *
1559 * \retval #PSA_SUCCESS
1560 * The expected MAC is identical to the actual MAC of the message.
1561 * \retval #PSA_ERROR_INVALID_SIGNATURE
1562 * The MAC of the message was calculated successfully, but it
1563 * differs from the expected MAC.
1564 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001565 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001566 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1567 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1568 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001569 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001570 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001571 * \retval #PSA_ERROR_BAD_STATE
1572 * The library has not been previously initialized by psa_crypto_init().
1573 * It is implementation-dependent whether a failure to initialize
1574 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001575 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001576psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1577 const uint8_t *mac,
1578 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001579
Gilles Peskinedcd14942018-07-12 00:30:52 +02001580/** Abort a MAC operation.
1581 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001582 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001583 * \p operation structure itself. Once aborted, the operation object
1584 * can be reused for another operation by calling
1585 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001586 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001587 * You may call this function any time after the operation object has
1588 * been initialized by any of the following methods:
1589 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1590 * it succeeds or not.
1591 * - Initializing the \c struct to all-bits-zero.
1592 * - Initializing the \c struct to logical zeros, e.g.
1593 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001594 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001595 * In particular, calling psa_mac_abort() after the operation has been
1596 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1597 * psa_mac_verify_finish() is safe and has no effect.
1598 *
1599 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001600 *
1601 * \retval #PSA_SUCCESS
1602 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001603 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001604 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1605 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001606 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001607 * \retval #PSA_ERROR_BAD_STATE
1608 * The library has not been previously initialized by psa_crypto_init().
1609 * It is implementation-dependent whether a failure to initialize
1610 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001611 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001612psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1613
1614/**@}*/
1615
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001616/** \defgroup cipher Symmetric ciphers
1617 * @{
1618 */
1619
Gilles Peskine69647a42019-01-14 20:18:12 +01001620/** Encrypt a message using a symmetric cipher.
1621 *
1622 * This function encrypts a message with a random IV (initialization
1623 * vector).
1624 *
1625 * \param handle Handle to the key to use for the operation.
1626 * It must remain valid until the operation
1627 * terminates.
1628 * \param alg The cipher algorithm to compute
1629 * (\c PSA_ALG_XXX value such that
1630 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1631 * \param[in] input Buffer containing the message to encrypt.
1632 * \param input_length Size of the \p input buffer in bytes.
1633 * \param[out] output Buffer where the output is to be written.
1634 * The output contains the IV followed by
1635 * the ciphertext proper.
1636 * \param output_size Size of the \p output buffer in bytes.
1637 * \param[out] output_length On success, the number of bytes
1638 * that make up the output.
1639 *
1640 * \retval #PSA_SUCCESS
1641 * Success.
1642 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001643 * \retval #PSA_ERROR_NOT_PERMITTED
1644 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001645 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001646 * \retval #PSA_ERROR_NOT_SUPPORTED
1647 * \p alg is not supported or is not a cipher algorithm.
1648 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1649 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1650 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1651 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001652 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001653 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001654 * \retval #PSA_ERROR_BAD_STATE
1655 * The library has not been previously initialized by psa_crypto_init().
1656 * It is implementation-dependent whether a failure to initialize
1657 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001658 */
1659psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1660 psa_algorithm_t alg,
1661 const uint8_t *input,
1662 size_t input_length,
1663 uint8_t *output,
1664 size_t output_size,
1665 size_t *output_length);
1666
1667/** Decrypt a message using a symmetric cipher.
1668 *
1669 * This function decrypts a message encrypted with a symmetric cipher.
1670 *
1671 * \param handle Handle to the key to use for the operation.
1672 * It must remain valid until the operation
1673 * terminates.
1674 * \param alg The cipher algorithm to compute
1675 * (\c PSA_ALG_XXX value such that
1676 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1677 * \param[in] input Buffer containing the message to decrypt.
1678 * This consists of the IV followed by the
1679 * ciphertext proper.
1680 * \param input_length Size of the \p input buffer in bytes.
1681 * \param[out] output Buffer where the plaintext is to be written.
1682 * \param output_size Size of the \p output buffer in bytes.
1683 * \param[out] output_length On success, the number of bytes
1684 * that make up the output.
1685 *
1686 * \retval #PSA_SUCCESS
1687 * Success.
1688 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001689 * \retval #PSA_ERROR_NOT_PERMITTED
1690 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001691 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001692 * \retval #PSA_ERROR_NOT_SUPPORTED
1693 * \p alg is not supported or is not a cipher algorithm.
1694 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1695 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1696 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1697 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001698 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001699 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001700 * \retval #PSA_ERROR_BAD_STATE
1701 * The library has not been previously initialized by psa_crypto_init().
1702 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001703 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001704 */
1705psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1706 psa_algorithm_t alg,
1707 const uint8_t *input,
1708 size_t input_length,
1709 uint8_t *output,
1710 size_t output_size,
1711 size_t *output_length);
1712
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001713/** The type of the state data structure for multipart cipher operations.
1714 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001715 * Before calling any function on a cipher operation object, the application
1716 * must initialize it by any of the following means:
1717 * - Set the structure to all-bits-zero, for example:
1718 * \code
1719 * psa_cipher_operation_t operation;
1720 * memset(&operation, 0, sizeof(operation));
1721 * \endcode
1722 * - Initialize the structure to logical zero values, for example:
1723 * \code
1724 * psa_cipher_operation_t operation = {0};
1725 * \endcode
1726 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1727 * for example:
1728 * \code
1729 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1730 * \endcode
1731 * - Assign the result of the function psa_cipher_operation_init()
1732 * to the structure, for example:
1733 * \code
1734 * psa_cipher_operation_t operation;
1735 * operation = psa_cipher_operation_init();
1736 * \endcode
1737 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001738 * This is an implementation-defined \c struct. Applications should not
1739 * make any assumptions about the content of this structure except
1740 * as directed by the documentation of a specific implementation. */
1741typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1742
Jaeden Amero5bae2272019-01-04 11:48:27 +00001743/** \def PSA_CIPHER_OPERATION_INIT
1744 *
1745 * This macro returns a suitable initializer for a cipher operation object of
1746 * type #psa_cipher_operation_t.
1747 */
1748#ifdef __DOXYGEN_ONLY__
1749/* This is an example definition for documentation purposes.
1750 * Implementations should define a suitable value in `crypto_struct.h`.
1751 */
1752#define PSA_CIPHER_OPERATION_INIT {0}
1753#endif
1754
1755/** Return an initial value for a cipher operation object.
1756 */
1757static psa_cipher_operation_t psa_cipher_operation_init(void);
1758
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001759/** Set the key for a multipart symmetric encryption operation.
1760 *
1761 * The sequence of operations to encrypt a message with a symmetric cipher
1762 * is as follows:
1763 * -# Allocate an operation object which will be passed to all the functions
1764 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001765 * -# Initialize the operation object with one of the methods described in the
1766 * documentation for #psa_cipher_operation_t, e.g.
1767 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001768 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001769 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001770 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001771 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001772 * requires a specific IV value.
1773 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1774 * of the message each time.
1775 * -# Call psa_cipher_finish().
1776 *
1777 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001778 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001779 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001780 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001781 * eventually terminate the operation. The following events terminate an
1782 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001783 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001784 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001785 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001786 * \param[in,out] operation The operation object to set up. It must have
1787 * been initialized as per the documentation for
1788 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001789 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001790 * It must remain valid until the operation
1791 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001792 * \param alg The cipher algorithm to compute
1793 * (\c PSA_ALG_XXX value such that
1794 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001795 *
Gilles Peskine28538492018-07-11 17:34:00 +02001796 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001797 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001798 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001799 * \retval #PSA_ERROR_NOT_PERMITTED
1800 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001801 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001802 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001803 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001804 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1805 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1806 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001807 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001808 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001809 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001810 * The operation state is not valid (already set up and not
1811 * subsequently completed).
1812 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001813 * The library has not been previously initialized by psa_crypto_init().
1814 * It is implementation-dependent whether a failure to initialize
1815 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001816 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001817psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001818 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001819 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001820
1821/** Set the key for a multipart symmetric decryption operation.
1822 *
1823 * The sequence of operations to decrypt a message with a symmetric cipher
1824 * is as follows:
1825 * -# Allocate an operation object which will be passed to all the functions
1826 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001827 * -# Initialize the operation object with one of the methods described in the
1828 * documentation for #psa_cipher_operation_t, e.g.
1829 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001830 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001831 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001832 * decryption. If the IV is prepended to the ciphertext, you can call
1833 * psa_cipher_update() on a buffer containing the IV followed by the
1834 * beginning of the message.
1835 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1836 * of the message each time.
1837 * -# Call psa_cipher_finish().
1838 *
1839 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001840 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001841 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001842 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001843 * eventually terminate the operation. The following events terminate an
1844 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001845 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001846 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001847 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001848 * \param[in,out] operation The operation object to set up. It must have
1849 * been initialized as per the documentation for
1850 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001851 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001852 * It must remain valid until the operation
1853 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001854 * \param alg The cipher algorithm to compute
1855 * (\c PSA_ALG_XXX value such that
1856 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001857 *
Gilles Peskine28538492018-07-11 17:34:00 +02001858 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001859 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001860 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001861 * \retval #PSA_ERROR_NOT_PERMITTED
1862 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001863 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001864 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001865 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001866 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1867 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1868 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001869 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001870 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001871 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001872 * The operation state is not valid (already set up and not
1873 * subsequently completed).
1874 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001875 * The library has not been previously initialized by psa_crypto_init().
1876 * It is implementation-dependent whether a failure to initialize
1877 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001878 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001879psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001880 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001881 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001882
Gilles Peskinedcd14942018-07-12 00:30:52 +02001883/** Generate an IV for a symmetric encryption operation.
1884 *
1885 * This function generates a random IV (initialization vector), nonce
1886 * or initial counter value for the encryption operation as appropriate
1887 * for the chosen algorithm, key type and key size.
1888 *
1889 * The application must call psa_cipher_encrypt_setup() before
1890 * calling this function.
1891 *
1892 * If this function returns an error status, the operation becomes inactive.
1893 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001894 * \param[in,out] operation Active cipher operation.
1895 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001896 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001897 * \param[out] iv_length On success, the number of bytes of the
1898 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001899 *
1900 * \retval #PSA_SUCCESS
1901 * Success.
1902 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001903 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001904 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001905 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001906 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1907 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1908 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001909 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001910 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001911 * \retval #PSA_ERROR_BAD_STATE
1912 * The library has not been previously initialized by psa_crypto_init().
1913 * It is implementation-dependent whether a failure to initialize
1914 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001915 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001916psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001917 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001918 size_t iv_size,
1919 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001920
Gilles Peskinedcd14942018-07-12 00:30:52 +02001921/** Set the IV for a symmetric encryption or decryption operation.
1922 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001923 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001924 * or initial counter value for the encryption or decryption operation.
1925 *
1926 * The application must call psa_cipher_encrypt_setup() before
1927 * calling this function.
1928 *
1929 * If this function returns an error status, the operation becomes inactive.
1930 *
1931 * \note When encrypting, applications should use psa_cipher_generate_iv()
1932 * instead of this function, unless implementing a protocol that requires
1933 * a non-random IV.
1934 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001935 * \param[in,out] operation Active cipher operation.
1936 * \param[in] iv Buffer containing the IV to use.
1937 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001938 *
1939 * \retval #PSA_SUCCESS
1940 * Success.
1941 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001942 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001943 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001944 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001945 * or the chosen algorithm does not use an IV.
1946 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1947 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1948 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001949 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001950 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001951 * \retval #PSA_ERROR_BAD_STATE
1952 * The library has not been previously initialized by psa_crypto_init().
1953 * It is implementation-dependent whether a failure to initialize
1954 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001955 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001956psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001957 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001958 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001959
Gilles Peskinedcd14942018-07-12 00:30:52 +02001960/** Encrypt or decrypt a message fragment in an active cipher operation.
1961 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001962 * Before calling this function, you must:
1963 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1964 * The choice of setup function determines whether this function
1965 * encrypts or decrypts its input.
1966 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1967 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001968 *
1969 * If this function returns an error status, the operation becomes inactive.
1970 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001971 * \param[in,out] operation Active cipher operation.
1972 * \param[in] input Buffer containing the message fragment to
1973 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001974 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001975 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001976 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001977 * \param[out] output_length On success, the number of bytes
1978 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001979 *
1980 * \retval #PSA_SUCCESS
1981 * Success.
1982 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001983 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001984 * not set, or already completed).
1985 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1986 * The size of the \p output buffer is too small.
1987 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1988 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1989 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001990 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001991 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001992 * \retval #PSA_ERROR_BAD_STATE
1993 * The library has not been previously initialized by psa_crypto_init().
1994 * It is implementation-dependent whether a failure to initialize
1995 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001996 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001997psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1998 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001999 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00002000 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02002001 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002002 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002003
Gilles Peskinedcd14942018-07-12 00:30:52 +02002004/** Finish encrypting or decrypting a message in a cipher operation.
2005 *
2006 * The application must call psa_cipher_encrypt_setup() or
2007 * psa_cipher_decrypt_setup() before calling this function. The choice
2008 * of setup function determines whether this function encrypts or
2009 * decrypts its input.
2010 *
2011 * This function finishes the encryption or decryption of the message
2012 * formed by concatenating the inputs passed to preceding calls to
2013 * psa_cipher_update().
2014 *
2015 * When this function returns, the operation becomes inactive.
2016 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002017 * \param[in,out] operation Active cipher operation.
2018 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002019 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002020 * \param[out] output_length On success, the number of bytes
2021 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002022 *
2023 * \retval #PSA_SUCCESS
2024 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02002025 * \retval #PSA_ERROR_INVALID_ARGUMENT
2026 * The total input size passed to this operation is not valid for
2027 * this particular algorithm. For example, the algorithm is a based
2028 * on block cipher and requires a whole number of blocks, but the
2029 * total input size is not a multiple of the block size.
2030 * \retval #PSA_ERROR_INVALID_PADDING
2031 * This is a decryption operation for an algorithm that includes
2032 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002033 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01002034 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02002035 * not set, or already completed).
2036 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2037 * The size of the \p output buffer is too small.
2038 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2039 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2040 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002041 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002042 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002043 * \retval #PSA_ERROR_BAD_STATE
2044 * The library has not been previously initialized by psa_crypto_init().
2045 * It is implementation-dependent whether a failure to initialize
2046 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002047 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002048psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002049 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002050 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002051 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002052
Gilles Peskinedcd14942018-07-12 00:30:52 +02002053/** Abort a cipher operation.
2054 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002055 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002056 * \p operation structure itself. Once aborted, the operation object
2057 * can be reused for another operation by calling
2058 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002059 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002060 * You may call this function any time after the operation object has
2061 * been initialized by any of the following methods:
2062 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2063 * whether it succeeds or not.
2064 * - Initializing the \c struct to all-bits-zero.
2065 * - Initializing the \c struct to logical zeros, e.g.
2066 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002067 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002068 * In particular, calling psa_cipher_abort() after the operation has been
2069 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2070 * is safe and has no effect.
2071 *
2072 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002073 *
2074 * \retval #PSA_SUCCESS
2075 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002076 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002077 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2078 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002079 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002080 * \retval #PSA_ERROR_BAD_STATE
2081 * The library has not been previously initialized by psa_crypto_init().
2082 * It is implementation-dependent whether a failure to initialize
2083 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002084 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002085psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2086
2087/**@}*/
2088
Gilles Peskine3b555712018-03-03 21:27:57 +01002089/** \defgroup aead Authenticated encryption with associated data (AEAD)
2090 * @{
2091 */
2092
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002093/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002094 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002095 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002096 * \param alg The AEAD algorithm to compute
2097 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002098 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002099 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002100 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002101 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002102 * but not encrypted.
2103 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002104 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002105 * encrypted.
2106 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002107 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002108 * encrypted data. The additional data is not
2109 * part of this output. For algorithms where the
2110 * encrypted data and the authentication tag
2111 * are defined as separate outputs, the
2112 * authentication tag is appended to the
2113 * encrypted data.
2114 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2115 * This must be at least
2116 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2117 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002118 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002119 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002120 *
Gilles Peskine28538492018-07-11 17:34:00 +02002121 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002122 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002123 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002124 * \retval #PSA_ERROR_NOT_PERMITTED
2125 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002126 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002127 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002128 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002129 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002130 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2131 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002132 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2133 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002134 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002135 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002136 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002137 * The library has not been previously initialized by psa_crypto_init().
2138 * It is implementation-dependent whether a failure to initialize
2139 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002140 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002141psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002142 psa_algorithm_t alg,
2143 const uint8_t *nonce,
2144 size_t nonce_length,
2145 const uint8_t *additional_data,
2146 size_t additional_data_length,
2147 const uint8_t *plaintext,
2148 size_t plaintext_length,
2149 uint8_t *ciphertext,
2150 size_t ciphertext_size,
2151 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002152
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002153/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002154 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002155 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002156 * \param alg The AEAD algorithm to compute
2157 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002158 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002159 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002160 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002161 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002162 * but not encrypted.
2163 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002164 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002165 * encrypted. For algorithms where the
2166 * encrypted data and the authentication tag
2167 * are defined as separate inputs, the buffer
2168 * must contain the encrypted data followed
2169 * by the authentication tag.
2170 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002171 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002172 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2173 * This must be at least
2174 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2175 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002176 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002177 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002178 *
Gilles Peskine28538492018-07-11 17:34:00 +02002179 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002180 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002181 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002182 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002183 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002184 * \retval #PSA_ERROR_NOT_PERMITTED
2185 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002186 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002187 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002188 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002189 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002190 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2191 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002192 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2193 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002194 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002195 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002196 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002197 * The library has not been previously initialized by psa_crypto_init().
2198 * It is implementation-dependent whether a failure to initialize
2199 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002200 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002201psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002202 psa_algorithm_t alg,
2203 const uint8_t *nonce,
2204 size_t nonce_length,
2205 const uint8_t *additional_data,
2206 size_t additional_data_length,
2207 const uint8_t *ciphertext,
2208 size_t ciphertext_length,
2209 uint8_t *plaintext,
2210 size_t plaintext_size,
2211 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002212
Gilles Peskine30a9e412019-01-14 18:36:12 +01002213/** The type of the state data structure for multipart AEAD operations.
2214 *
2215 * Before calling any function on an AEAD operation object, the application
2216 * must initialize it by any of the following means:
2217 * - Set the structure to all-bits-zero, for example:
2218 * \code
2219 * psa_aead_operation_t operation;
2220 * memset(&operation, 0, sizeof(operation));
2221 * \endcode
2222 * - Initialize the structure to logical zero values, for example:
2223 * \code
2224 * psa_aead_operation_t operation = {0};
2225 * \endcode
2226 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2227 * for example:
2228 * \code
2229 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2230 * \endcode
2231 * - Assign the result of the function psa_aead_operation_init()
2232 * to the structure, for example:
2233 * \code
2234 * psa_aead_operation_t operation;
2235 * operation = psa_aead_operation_init();
2236 * \endcode
2237 *
2238 * This is an implementation-defined \c struct. Applications should not
2239 * make any assumptions about the content of this structure except
2240 * as directed by the documentation of a specific implementation. */
2241typedef struct psa_aead_operation_s psa_aead_operation_t;
2242
2243/** \def PSA_AEAD_OPERATION_INIT
2244 *
2245 * This macro returns a suitable initializer for an AEAD operation object of
2246 * type #psa_aead_operation_t.
2247 */
2248#ifdef __DOXYGEN_ONLY__
2249/* This is an example definition for documentation purposes.
2250 * Implementations should define a suitable value in `crypto_struct.h`.
2251 */
2252#define PSA_AEAD_OPERATION_INIT {0}
2253#endif
2254
2255/** Return an initial value for an AEAD operation object.
2256 */
2257static psa_aead_operation_t psa_aead_operation_init(void);
2258
2259/** Set the key for a multipart authenticated encryption operation.
2260 *
2261 * The sequence of operations to encrypt a message with authentication
2262 * is as follows:
2263 * -# Allocate an operation object which will be passed to all the functions
2264 * listed here.
2265 * -# Initialize the operation object with one of the methods described in the
2266 * documentation for #psa_aead_operation_t, e.g.
2267 * PSA_AEAD_OPERATION_INIT.
2268 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002269 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2270 * inputs to the subsequent calls to psa_aead_update_ad() and
2271 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2272 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002273 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2274 * generate or set the nonce. You should use
2275 * psa_aead_generate_nonce() unless the protocol you are implementing
2276 * requires a specific nonce value.
2277 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2278 * of the non-encrypted additional authenticated data each time.
2279 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002280 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002281 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002282 *
2283 * The application may call psa_aead_abort() at any time after the operation
2284 * has been initialized.
2285 *
2286 * After a successful call to psa_aead_encrypt_setup(), the application must
2287 * eventually terminate the operation. The following events terminate an
2288 * operation:
2289 * - A failed call to any of the \c psa_aead_xxx functions.
2290 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2291 *
2292 * \param[in,out] operation The operation object to set up. It must have
2293 * been initialized as per the documentation for
2294 * #psa_aead_operation_t and not yet in use.
2295 * \param handle Handle to the key to use for the operation.
2296 * It must remain valid until the operation
2297 * terminates.
2298 * \param alg The AEAD algorithm to compute
2299 * (\c PSA_ALG_XXX value such that
2300 * #PSA_ALG_IS_AEAD(\p alg) is true).
2301 *
2302 * \retval #PSA_SUCCESS
2303 * Success.
2304 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002305 * \retval #PSA_ERROR_NOT_PERMITTED
2306 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002307 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002308 * \retval #PSA_ERROR_NOT_SUPPORTED
2309 * \p alg is not supported or is not an AEAD algorithm.
2310 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2311 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2312 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002313 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002314 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002315 * \retval #PSA_ERROR_BAD_STATE
2316 * The library has not been previously initialized by psa_crypto_init().
2317 * It is implementation-dependent whether a failure to initialize
2318 * results in this error code.
2319 */
2320psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2321 psa_key_handle_t handle,
2322 psa_algorithm_t alg);
2323
2324/** Set the key for a multipart authenticated decryption operation.
2325 *
2326 * The sequence of operations to decrypt a message with authentication
2327 * is as follows:
2328 * -# Allocate an operation object which will be passed to all the functions
2329 * listed here.
2330 * -# Initialize the operation object with one of the methods described in the
2331 * documentation for #psa_aead_operation_t, e.g.
2332 * PSA_AEAD_OPERATION_INIT.
2333 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002334 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2335 * inputs to the subsequent calls to psa_aead_update_ad() and
2336 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2337 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002338 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2339 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2340 * of the non-encrypted additional authenticated data each time.
2341 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002342 * of the ciphertext to decrypt each time.
2343 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002344 *
2345 * The application may call psa_aead_abort() at any time after the operation
2346 * has been initialized.
2347 *
2348 * After a successful call to psa_aead_decrypt_setup(), the application must
2349 * eventually terminate the operation. The following events terminate an
2350 * operation:
2351 * - A failed call to any of the \c psa_aead_xxx functions.
2352 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2353 *
2354 * \param[in,out] operation The operation object to set up. It must have
2355 * been initialized as per the documentation for
2356 * #psa_aead_operation_t and not yet in use.
2357 * \param handle Handle to the key to use for the operation.
2358 * It must remain valid until the operation
2359 * terminates.
2360 * \param alg The AEAD algorithm to compute
2361 * (\c PSA_ALG_XXX value such that
2362 * #PSA_ALG_IS_AEAD(\p alg) is true).
2363 *
2364 * \retval #PSA_SUCCESS
2365 * Success.
2366 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002367 * \retval #PSA_ERROR_NOT_PERMITTED
2368 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002369 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002370 * \retval #PSA_ERROR_NOT_SUPPORTED
2371 * \p alg is not supported or is not an AEAD 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. Shaw3e412492019-08-08 15:10:33 +01002376 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002377 * \retval #PSA_ERROR_BAD_STATE
2378 * The library has not been previously initialized by psa_crypto_init().
2379 * It is implementation-dependent whether a failure to initialize
2380 * results in this error code.
2381 */
2382psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2383 psa_key_handle_t handle,
2384 psa_algorithm_t alg);
2385
2386/** Generate a random nonce for an authenticated encryption operation.
2387 *
2388 * This function generates a random nonce for the authenticated encryption
2389 * operation with an appropriate size for the chosen algorithm, key type
2390 * and key size.
2391 *
2392 * The application must call psa_aead_encrypt_setup() before
2393 * calling this function.
2394 *
2395 * If this function returns an error status, the operation becomes inactive.
2396 *
2397 * \param[in,out] operation Active AEAD operation.
2398 * \param[out] nonce Buffer where the generated nonce is to be
2399 * written.
2400 * \param nonce_size Size of the \p nonce buffer in bytes.
2401 * \param[out] nonce_length On success, the number of bytes of the
2402 * generated nonce.
2403 *
2404 * \retval #PSA_SUCCESS
2405 * Success.
2406 * \retval #PSA_ERROR_BAD_STATE
2407 * The operation state is not valid (not set up, or nonce already set).
2408 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2409 * The size of the \p nonce buffer is too small.
2410 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2411 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2412 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002413 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002414 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002415 * \retval #PSA_ERROR_BAD_STATE
2416 * The library has not been previously initialized by psa_crypto_init().
2417 * It is implementation-dependent whether a failure to initialize
2418 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002419 */
2420psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002421 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002422 size_t nonce_size,
2423 size_t *nonce_length);
2424
2425/** Set the nonce for an authenticated encryption or decryption operation.
2426 *
2427 * This function sets the nonce for the authenticated
2428 * encryption or decryption operation.
2429 *
2430 * The application must call psa_aead_encrypt_setup() before
2431 * calling this function.
2432 *
2433 * If this function returns an error status, the operation becomes inactive.
2434 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002435 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002436 * instead of this function, unless implementing a protocol that requires
2437 * a non-random IV.
2438 *
2439 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002440 * \param[in] nonce Buffer containing the nonce to use.
2441 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002442 *
2443 * \retval #PSA_SUCCESS
2444 * Success.
2445 * \retval #PSA_ERROR_BAD_STATE
2446 * The operation state is not valid (not set up, or nonce already set).
2447 * \retval #PSA_ERROR_INVALID_ARGUMENT
2448 * The size of \p nonce is not acceptable for the chosen algorithm.
2449 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2450 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2451 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002452 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002453 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002454 * \retval #PSA_ERROR_BAD_STATE
2455 * The library has not been previously initialized by psa_crypto_init().
2456 * It is implementation-dependent whether a failure to initialize
2457 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002458 */
2459psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002460 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002461 size_t nonce_length);
2462
Gilles Peskinebc59c852019-01-17 15:26:08 +01002463/** Declare the lengths of the message and additional data for AEAD.
2464 *
2465 * The application must call this function before calling
2466 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2467 * the operation requires it. If the algorithm does not require it,
2468 * calling this function is optional, but if this function is called
2469 * then the implementation must enforce the lengths.
2470 *
2471 * You may call this function before or after setting the nonce with
2472 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2473 *
2474 * - For #PSA_ALG_CCM, calling this function is required.
2475 * - For the other AEAD algorithms defined in this specification, calling
2476 * this function is not required.
2477 * - For vendor-defined algorithm, refer to the vendor documentation.
2478 *
2479 * \param[in,out] operation Active AEAD operation.
2480 * \param ad_length Size of the non-encrypted additional
2481 * authenticated data in bytes.
2482 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2483 *
2484 * \retval #PSA_SUCCESS
2485 * Success.
2486 * \retval #PSA_ERROR_BAD_STATE
2487 * The operation state is not valid (not set up, already completed,
2488 * or psa_aead_update_ad() or psa_aead_update() already called).
2489 * \retval #PSA_ERROR_INVALID_ARGUMENT
2490 * At least one of the lengths is not acceptable for the chosen
2491 * algorithm.
2492 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2493 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2494 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002495 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002496 * \retval #PSA_ERROR_BAD_STATE
2497 * The library has not been previously initialized by psa_crypto_init().
2498 * It is implementation-dependent whether a failure to initialize
2499 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002500 */
2501psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2502 size_t ad_length,
2503 size_t plaintext_length);
2504
Gilles Peskine30a9e412019-01-14 18:36:12 +01002505/** Pass additional data to an active AEAD operation.
2506 *
2507 * Additional data is authenticated, but not encrypted.
2508 *
2509 * You may call this function multiple times to pass successive fragments
2510 * of the additional data. You may not call this function after passing
2511 * data to encrypt or decrypt with psa_aead_update().
2512 *
2513 * Before calling this function, you must:
2514 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2515 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2516 *
2517 * If this function returns an error status, the operation becomes inactive.
2518 *
2519 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2520 * there is no guarantee that the input is valid. Therefore, until
2521 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2522 * treat the input as untrusted and prepare to undo any action that
2523 * depends on the input if psa_aead_verify() returns an error status.
2524 *
2525 * \param[in,out] operation Active AEAD operation.
2526 * \param[in] input Buffer containing the fragment of
2527 * additional data.
2528 * \param input_length Size of the \p input buffer in bytes.
2529 *
2530 * \retval #PSA_SUCCESS
2531 * Success.
2532 * \retval #PSA_ERROR_BAD_STATE
2533 * The operation state is not valid (not set up, nonce not set,
2534 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002535 * \retval #PSA_ERROR_INVALID_ARGUMENT
2536 * The total input length overflows the additional data length that
2537 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002538 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2539 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2540 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002541 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002542 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002543 * \retval #PSA_ERROR_BAD_STATE
2544 * The library has not been previously initialized by psa_crypto_init().
2545 * It is implementation-dependent whether a failure to initialize
2546 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002547 */
2548psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2549 const uint8_t *input,
2550 size_t input_length);
2551
2552/** Encrypt or decrypt a message fragment in an active AEAD operation.
2553 *
2554 * Before calling this function, you must:
2555 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2556 * The choice of setup function determines whether this function
2557 * encrypts or decrypts its input.
2558 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2559 * 3. Call psa_aead_update_ad() to pass all the additional data.
2560 *
2561 * If this function returns an error status, the operation becomes inactive.
2562 *
2563 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2564 * there is no guarantee that the input is valid. Therefore, until
2565 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2566 * - Do not use the output in any way other than storing it in a
2567 * confidential location. If you take any action that depends
2568 * on the tentative decrypted data, this action will need to be
2569 * undone if the input turns out not to be valid. Furthermore,
2570 * if an adversary can observe that this action took place
2571 * (for example through timing), they may be able to use this
2572 * fact as an oracle to decrypt any message encrypted with the
2573 * same key.
2574 * - In particular, do not copy the output anywhere but to a
2575 * memory or storage space that you have exclusive access to.
2576 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002577 * This function does not require the input to be aligned to any
2578 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002579 * a whole block at a time, it must consume all the input provided, but
2580 * it may delay the end of the corresponding output until a subsequent
2581 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2582 * provides sufficient input. The amount of data that can be delayed
2583 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002584 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002585 * \param[in,out] operation Active AEAD operation.
2586 * \param[in] input Buffer containing the message fragment to
2587 * encrypt or decrypt.
2588 * \param input_length Size of the \p input buffer in bytes.
2589 * \param[out] output Buffer where the output is to be written.
2590 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002591 * This must be at least
2592 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2593 * \p input_length) where \c alg is the
2594 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002595 * \param[out] output_length On success, the number of bytes
2596 * that make up the returned output.
2597 *
2598 * \retval #PSA_SUCCESS
2599 * Success.
2600 * \retval #PSA_ERROR_BAD_STATE
2601 * The operation state is not valid (not set up, nonce not set
2602 * or already completed).
2603 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2604 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002605 * You can determine a sufficient buffer size by calling
2606 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2607 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002608 * \retval #PSA_ERROR_INVALID_ARGUMENT
2609 * The total length of input to psa_aead_update_ad() so far is
2610 * less than the additional data length that was previously
2611 * specified with psa_aead_set_lengths().
2612 * \retval #PSA_ERROR_INVALID_ARGUMENT
2613 * The total input length overflows the plaintext length that
2614 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002615 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2616 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2617 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002618 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002619 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002620 * \retval #PSA_ERROR_BAD_STATE
2621 * The library has not been previously initialized by psa_crypto_init().
2622 * It is implementation-dependent whether a failure to initialize
2623 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002624 */
2625psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2626 const uint8_t *input,
2627 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002628 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002629 size_t output_size,
2630 size_t *output_length);
2631
2632/** Finish encrypting a message in an AEAD operation.
2633 *
2634 * The operation must have been set up with psa_aead_encrypt_setup().
2635 *
2636 * This function finishes the authentication of the additional data
2637 * formed by concatenating the inputs passed to preceding calls to
2638 * psa_aead_update_ad() with the plaintext formed by concatenating the
2639 * inputs passed to preceding calls to psa_aead_update().
2640 *
2641 * This function has two output buffers:
2642 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002643 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002644 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002645 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002646 * that the operation performs.
2647 *
2648 * When this function returns, the operation becomes inactive.
2649 *
2650 * \param[in,out] operation Active AEAD operation.
2651 * \param[out] ciphertext Buffer where the last part of the ciphertext
2652 * is to be written.
2653 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002654 * This must be at least
2655 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2656 * \c alg is the algorithm that is being
2657 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002658 * \param[out] ciphertext_length On success, the number of bytes of
2659 * returned ciphertext.
2660 * \param[out] tag Buffer where the authentication tag is
2661 * to be written.
2662 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002663 * This must be at least
2664 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2665 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002666 * \param[out] tag_length On success, the number of bytes
2667 * that make up the returned tag.
2668 *
2669 * \retval #PSA_SUCCESS
2670 * Success.
2671 * \retval #PSA_ERROR_BAD_STATE
2672 * The operation state is not valid (not set up, nonce not set,
2673 * decryption, or already completed).
2674 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002675 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002676 * You can determine a sufficient buffer size for \p ciphertext by
2677 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2678 * where \c alg is the algorithm that is being calculated.
2679 * You can determine a sufficient buffer size for \p tag by
2680 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002681 * \retval #PSA_ERROR_INVALID_ARGUMENT
2682 * The total length of input to psa_aead_update_ad() so far is
2683 * less than the additional data length that was previously
2684 * specified with psa_aead_set_lengths().
2685 * \retval #PSA_ERROR_INVALID_ARGUMENT
2686 * The total length of input to psa_aead_update() so far is
2687 * less than the plaintext length that was previously
2688 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002689 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2690 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2691 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002692 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002693 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002694 * \retval #PSA_ERROR_BAD_STATE
2695 * The library has not been previously initialized by psa_crypto_init().
2696 * It is implementation-dependent whether a failure to initialize
2697 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002698 */
2699psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002700 uint8_t *ciphertext,
2701 size_t ciphertext_size,
2702 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002703 uint8_t *tag,
2704 size_t tag_size,
2705 size_t *tag_length);
2706
2707/** Finish authenticating and decrypting a message in an AEAD operation.
2708 *
2709 * The operation must have been set up with psa_aead_decrypt_setup().
2710 *
2711 * This function finishes the authentication of the additional data
2712 * formed by concatenating the inputs passed to preceding calls to
2713 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2714 * inputs passed to preceding calls to psa_aead_update().
2715 *
2716 * When this function returns, the operation becomes inactive.
2717 *
2718 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002719 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002720 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002721 * from previous calls to psa_aead_update()
2722 * that could not be processed until the end
2723 * of the input.
2724 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002725 * This must be at least
2726 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2727 * \c alg is the algorithm that is being
2728 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002729 * \param[out] plaintext_length On success, the number of bytes of
2730 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002731 * \param[in] tag Buffer containing the authentication tag.
2732 * \param tag_length Size of the \p tag buffer in bytes.
2733 *
2734 * \retval #PSA_SUCCESS
2735 * Success.
2736 * \retval #PSA_ERROR_BAD_STATE
2737 * The operation state is not valid (not set up, nonce not set,
2738 * encryption, or already completed).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002739 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2740 * The size of the \p plaintext buffer is too small.
2741 * You can determine a sufficient buffer size for \p plaintext by
2742 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2743 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002744 * \retval #PSA_ERROR_INVALID_ARGUMENT
2745 * The total length of input to psa_aead_update_ad() so far is
2746 * less than the additional data length that was previously
2747 * specified with psa_aead_set_lengths().
2748 * \retval #PSA_ERROR_INVALID_ARGUMENT
2749 * The total length of input to psa_aead_update() so far is
2750 * less than the plaintext length that was previously
2751 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002752 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2753 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2754 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002755 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002756 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002757 * \retval #PSA_ERROR_BAD_STATE
2758 * The library has not been previously initialized by psa_crypto_init().
2759 * It is implementation-dependent whether a failure to initialize
2760 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002761 */
2762psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002763 uint8_t *plaintext,
2764 size_t plaintext_size,
2765 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002766 const uint8_t *tag,
2767 size_t tag_length);
2768
2769/** Abort an AEAD operation.
2770 *
2771 * Aborting an operation frees all associated resources except for the
2772 * \p operation structure itself. Once aborted, the operation object
2773 * can be reused for another operation by calling
2774 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2775 *
2776 * You may call this function any time after the operation object has
2777 * been initialized by any of the following methods:
2778 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2779 * whether it succeeds or not.
2780 * - Initializing the \c struct to all-bits-zero.
2781 * - Initializing the \c struct to logical zeros, e.g.
2782 * `psa_aead_operation_t operation = {0}`.
2783 *
2784 * In particular, calling psa_aead_abort() after the operation has been
2785 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2786 * is safe and has no effect.
2787 *
2788 * \param[in,out] operation Initialized AEAD operation.
2789 *
2790 * \retval #PSA_SUCCESS
2791 * \retval #PSA_ERROR_BAD_STATE
2792 * \p operation is not an active AEAD operation.
2793 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2794 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002795 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002796 * \retval #PSA_ERROR_BAD_STATE
2797 * The library has not been previously initialized by psa_crypto_init().
2798 * It is implementation-dependent whether a failure to initialize
2799 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002800 */
2801psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2802
Gilles Peskine3b555712018-03-03 21:27:57 +01002803/**@}*/
2804
Gilles Peskine20035e32018-02-03 22:44:14 +01002805/** \defgroup asymmetric Asymmetric cryptography
2806 * @{
2807 */
2808
2809/**
2810 * \brief Sign a hash or short message with a private key.
2811 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002812 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002813 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002814 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2815 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2816 * to determine the hash algorithm to use.
2817 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002818 * \param handle Handle to the key to use for the operation.
2819 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002820 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002821 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002822 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002823 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002824 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002825 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002826 * \param[out] signature_length On success, the number of bytes
2827 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002828 *
Gilles Peskine28538492018-07-11 17:34:00 +02002829 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002830 * \retval #PSA_ERROR_INVALID_HANDLE
2831 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002832 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002833 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002834 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002835 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002836 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002837 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002838 * \retval #PSA_ERROR_NOT_SUPPORTED
2839 * \retval #PSA_ERROR_INVALID_ARGUMENT
2840 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2841 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2842 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002843 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002844 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002845 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002846 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002847 * The library has not been previously initialized by psa_crypto_init().
2848 * It is implementation-dependent whether a failure to initialize
2849 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002850 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002851psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002852 psa_algorithm_t alg,
2853 const uint8_t *hash,
2854 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002855 uint8_t *signature,
2856 size_t signature_size,
2857 size_t *signature_length);
2858
2859/**
2860 * \brief Verify the signature a hash or short message using a public key.
2861 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002862 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002863 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002864 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2865 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2866 * to determine the hash algorithm to use.
2867 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002868 * \param handle Handle to the key to use for the operation.
2869 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002870 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002871 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002872 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002873 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002874 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002875 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002876 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002877 *
Gilles Peskine28538492018-07-11 17:34:00 +02002878 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002879 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002880 * \retval #PSA_ERROR_INVALID_HANDLE
2881 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002882 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002883 * The calculation was perfomed successfully, but the passed
2884 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002885 * \retval #PSA_ERROR_NOT_SUPPORTED
2886 * \retval #PSA_ERROR_INVALID_ARGUMENT
2887 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2888 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2889 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002890 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002891 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002892 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002893 * The library has not been previously initialized by psa_crypto_init().
2894 * It is implementation-dependent whether a failure to initialize
2895 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002896 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002897psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002898 psa_algorithm_t alg,
2899 const uint8_t *hash,
2900 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002901 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002902 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002903
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002904/**
2905 * \brief Encrypt a short message with a public key.
2906 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002907 * \param handle Handle to the key to use for the operation.
2908 * It must be a public key or an asymmetric
2909 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002910 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002911 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002912 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002913 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002914 * \param[in] salt A salt or label, if supported by the
2915 * encryption algorithm.
2916 * If the algorithm does not support a
2917 * salt, pass \c NULL.
2918 * If the algorithm supports an optional
2919 * salt and you do not want to pass a salt,
2920 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002921 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002922 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2923 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002924 * \param salt_length Size of the \p salt buffer in bytes.
2925 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002926 * \param[out] output Buffer where the encrypted message is to
2927 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002928 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002929 * \param[out] output_length On success, the number of bytes
2930 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002931 *
Gilles Peskine28538492018-07-11 17:34:00 +02002932 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002933 * \retval #PSA_ERROR_INVALID_HANDLE
2934 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002935 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002936 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002937 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002938 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002939 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002940 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002941 * \retval #PSA_ERROR_NOT_SUPPORTED
2942 * \retval #PSA_ERROR_INVALID_ARGUMENT
2943 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2944 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2945 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002946 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002947 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002948 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002949 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002950 * The library has not been previously initialized by psa_crypto_init().
2951 * It is implementation-dependent whether a failure to initialize
2952 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002953 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002954psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002955 psa_algorithm_t alg,
2956 const uint8_t *input,
2957 size_t input_length,
2958 const uint8_t *salt,
2959 size_t salt_length,
2960 uint8_t *output,
2961 size_t output_size,
2962 size_t *output_length);
2963
2964/**
2965 * \brief Decrypt a short message with a private key.
2966 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002967 * \param handle Handle to the key to use for the operation.
2968 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002969 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002970 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002971 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002972 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002973 * \param[in] salt A salt or label, if supported by the
2974 * encryption algorithm.
2975 * If the algorithm does not support a
2976 * salt, pass \c NULL.
2977 * If the algorithm supports an optional
2978 * salt and you do not want to pass a salt,
2979 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002980 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002981 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2982 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002983 * \param salt_length Size of the \p salt buffer in bytes.
2984 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002985 * \param[out] output Buffer where the decrypted message is to
2986 * be written.
2987 * \param output_size Size of the \c output buffer in bytes.
2988 * \param[out] output_length On success, the number of bytes
2989 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002990 *
Gilles Peskine28538492018-07-11 17:34:00 +02002991 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002992 * \retval #PSA_ERROR_INVALID_HANDLE
2993 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002994 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002995 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002996 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002997 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002998 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002999 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02003000 * \retval #PSA_ERROR_NOT_SUPPORTED
3001 * \retval #PSA_ERROR_INVALID_ARGUMENT
3002 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3003 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3004 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003005 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003006 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003007 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3008 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003009 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003010 * The library has not been previously initialized by psa_crypto_init().
3011 * It is implementation-dependent whether a failure to initialize
3012 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003013 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003014psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003015 psa_algorithm_t alg,
3016 const uint8_t *input,
3017 size_t input_length,
3018 const uint8_t *salt,
3019 size_t salt_length,
3020 uint8_t *output,
3021 size_t output_size,
3022 size_t *output_length);
3023
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003024/**@}*/
3025
Gilles Peskine35675b62019-05-16 17:26:11 +02003026/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003027 * @{
3028 */
3029
Gilles Peskine35675b62019-05-16 17:26:11 +02003030/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003031 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003032 * Before calling any function on a key derivation operation object, the
3033 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003034 * - Set the structure to all-bits-zero, for example:
3035 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003036 * psa_key_derivation_operation_t operation;
3037 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003038 * \endcode
3039 * - Initialize the structure to logical zero values, for example:
3040 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003041 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003042 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003043 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003044 * for example:
3045 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003046 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003047 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003048 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003049 * to the structure, for example:
3050 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003051 * psa_key_derivation_operation_t operation;
3052 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003053 * \endcode
3054 *
3055 * This is an implementation-defined \c struct. Applications should not
3056 * make any assumptions about the content of this structure except
3057 * as directed by the documentation of a specific implementation.
3058 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003059typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003060
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003061/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003062 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003063 * This macro returns a suitable initializer for a key derivation operation
3064 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003065 */
3066#ifdef __DOXYGEN_ONLY__
3067/* This is an example definition for documentation purposes.
3068 * Implementations should define a suitable value in `crypto_struct.h`.
3069 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003070#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003071#endif
3072
Gilles Peskine35675b62019-05-16 17:26:11 +02003073/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003074 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003075static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003076
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003077/** Set up a key derivation operation.
3078 *
3079 * A key derivation algorithm takes some inputs and uses them to generate
3080 * a byte stream in a deterministic way.
3081 * This byte stream can be used to produce keys and other
3082 * cryptographic material.
3083 *
3084 * To derive a key:
3085 * - Start with an initialized object of type #psa_key_derivation_operation_t.
3086 * - Call psa_key_derivation_setup() to select the algorithm.
3087 * - Provide the inputs for the key derivation by calling
3088 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3089 * as appropriate. Which inputs are needed, in what order, and whether
3090 * they may be keys and if so of what type depends on the algorithm.
3091 * - Optionally set the operation's maximum capacity with
3092 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3093 * of or after providing inputs. For some algorithms, this step is mandatory
3094 * because the output depends on the maximum capacity.
3095 * - To derive a key, call psa_key_derivation_output_key().
3096 * To derive a byte string for a different purpose, call
3097 * - psa_key_derivation_output_bytes().
3098 * Successive calls to these functions use successive output bytes
3099 * calculated by the key derivation algorithm.
3100 * - Clean up the key derivation operation object with
3101 * psa_key_derivation_abort().
3102 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003103 * Implementations must reject an attempt to derive a key of size 0.
3104 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003105 * \param[in,out] operation The key derivation operation object
3106 * to set up. It must
3107 * have been initialized but not set up yet.
3108 * \param alg The key derivation algorithm to compute
3109 * (\c PSA_ALG_XXX value such that
3110 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3111 *
3112 * \retval #PSA_SUCCESS
3113 * Success.
3114 * \retval #PSA_ERROR_INVALID_ARGUMENT
3115 * \c alg is not a key derivation algorithm.
3116 * \retval #PSA_ERROR_NOT_SUPPORTED
3117 * \c alg is not supported or is not a key derivation algorithm.
3118 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3119 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3120 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003121 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003122 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003123 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shaw320659b2019-08-08 14:49:01 +01003124 * The operation state is either not initialized or has already been setup.
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003125 * \retval #PSA_ERROR_BAD_STATE
3126 * The library has not been previously initialized by psa_crypto_init().
3127 * It is implementation-dependent whether a failure to initialize
3128 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003129 */
3130psa_status_t psa_key_derivation_setup(
3131 psa_key_derivation_operation_t *operation,
3132 psa_algorithm_t alg);
3133
Gilles Peskine35675b62019-05-16 17:26:11 +02003134/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003135 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003136 * The capacity of a key derivation is the maximum number of bytes that it can
3137 * return. When you get *N* bytes of output from a key derivation operation,
3138 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003139 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003140 * \param[in] operation The operation to query.
3141 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003142 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003143 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003144 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003145 * \retval #PSA_ERROR_BAD_STATE
3146 * The operation state is not valid.
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003147 * \retval #PSA_ERROR_HARDWARE_FAILURE
3148 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003149 * \retval #PSA_ERROR_BAD_STATE
3150 * The library has not been previously initialized by psa_crypto_init().
3151 * It is implementation-dependent whether a failure to initialize
3152 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003153 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003154psa_status_t psa_key_derivation_get_capacity(
3155 const psa_key_derivation_operation_t *operation,
3156 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003157
Gilles Peskine35675b62019-05-16 17:26:11 +02003158/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003159 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003160 * The capacity of a key derivation operation is the maximum number of bytes
3161 * that the key derivation operation can return from this point onwards.
3162 *
3163 * \param[in,out] operation The key derivation operation object to modify.
3164 * \param capacity The new capacity of the operation.
3165 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003166 * current capacity.
3167 *
3168 * \retval #PSA_SUCCESS
3169 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003170 * \p capacity is larger than the operation's current capacity.
3171 * In this case, the operation object remains valid and its capacity
3172 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003173 * \retval #PSA_ERROR_BAD_STATE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003174 * The operation state is not valid.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003175 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003176 * \retval #PSA_ERROR_HARDWARE_FAILURE
3177 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003178 * \retval #PSA_ERROR_BAD_STATE
3179 * The library has not been previously initialized by psa_crypto_init().
3180 * It is implementation-dependent whether a failure to initialize
3181 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003182 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003183psa_status_t psa_key_derivation_set_capacity(
3184 psa_key_derivation_operation_t *operation,
3185 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003186
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003187/** Use the maximum possible capacity for a key derivation operation.
3188 *
3189 * Use this value as the capacity argument when setting up a key derivation
3190 * to indicate that the operation should have the maximum possible capacity.
3191 * The value of the maximum possible capacity depends on the key derivation
3192 * algorithm.
3193 */
3194#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3195
3196/** Provide an input for key derivation or key agreement.
3197 *
3198 * Which inputs are required and in what order depends on the algorithm.
3199 * Refer to the documentation of each key derivation or key agreement
3200 * algorithm for information.
3201 *
3202 * This function passes direct inputs. Some inputs must be passed as keys
3203 * using psa_key_derivation_input_key() instead of this function. Refer to
3204 * the documentation of individual step types for information.
3205 *
3206 * \param[in,out] operation The key derivation operation object to use.
3207 * It must have been set up with
3208 * psa_key_derivation_setup() and must not
3209 * have produced any output yet.
3210 * \param step Which step the input data is for.
3211 * \param[in] data Input data to use.
3212 * \param data_length Size of the \p data buffer in bytes.
3213 *
3214 * \retval #PSA_SUCCESS
3215 * Success.
3216 * \retval #PSA_ERROR_INVALID_ARGUMENT
3217 * \c step is not compatible with the operation's algorithm.
3218 * \retval #PSA_ERROR_INVALID_ARGUMENT
3219 * \c step does not allow direct inputs.
3220 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3221 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3222 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003223 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003224 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003225 * \retval #PSA_ERROR_BAD_STATE
3226 * The value of \p step is not valid given the state of \p operation.
3227 * \retval #PSA_ERROR_BAD_STATE
3228 * The library has not been previously initialized by psa_crypto_init().
3229 * It is implementation-dependent whether a failure to initialize
3230 * results in this error code.
3231 */
3232psa_status_t psa_key_derivation_input_bytes(
3233 psa_key_derivation_operation_t *operation,
3234 psa_key_derivation_step_t step,
3235 const uint8_t *data,
3236 size_t data_length);
3237
3238/** Provide an input for key derivation in the form of a key.
3239 *
3240 * Which inputs are required and in what order depends on the algorithm.
3241 * Refer to the documentation of each key derivation or key agreement
3242 * algorithm for information.
3243 *
3244 * This function passes key inputs. Some inputs must be passed as keys
3245 * of the appropriate type using this function, while others must be
3246 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3247 * the documentation of individual step types for information.
3248 *
3249 * \param[in,out] operation The key derivation operation object to use.
3250 * It must have been set up with
3251 * psa_key_derivation_setup() and must not
3252 * have produced any output yet.
3253 * \param step Which step the input data is for.
3254 * \param handle Handle to the key. It must have an
3255 * appropriate type for \p step and must
3256 * allow the usage #PSA_KEY_USAGE_DERIVE.
3257 *
3258 * \retval #PSA_SUCCESS
3259 * Success.
3260 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003261 * \retval #PSA_ERROR_NOT_PERMITTED
3262 * \retval #PSA_ERROR_INVALID_ARGUMENT
3263 * \c step is not compatible with the operation's algorithm.
3264 * \retval #PSA_ERROR_INVALID_ARGUMENT
3265 * \c step does not allow key inputs.
3266 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3267 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3268 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003269 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003270 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003271 * \retval #PSA_ERROR_BAD_STATE
3272 * The value of \p step is not valid given the state of \p operation.
3273 * \retval #PSA_ERROR_BAD_STATE
3274 * The library has not been previously initialized by psa_crypto_init().
3275 * It is implementation-dependent whether a failure to initialize
3276 * results in this error code.
3277 */
3278psa_status_t psa_key_derivation_input_key(
3279 psa_key_derivation_operation_t *operation,
3280 psa_key_derivation_step_t step,
3281 psa_key_handle_t handle);
3282
3283/** Perform a key agreement and use the shared secret as input to a key
3284 * derivation.
3285 *
3286 * A key agreement algorithm takes two inputs: a private key \p private_key
3287 * a public key \p peer_key.
3288 * The result of this function is passed as input to a key derivation.
3289 * The output of this key derivation can be extracted by reading from the
3290 * resulting operation to produce keys and other cryptographic material.
3291 *
3292 * \param[in,out] operation The key derivation operation object to use.
3293 * It must have been set up with
3294 * psa_key_derivation_setup() with a
3295 * key agreement and derivation algorithm
3296 * \c alg (\c PSA_ALG_XXX value such that
3297 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3298 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3299 * is false).
3300 * The operation must be ready for an
3301 * input of the type given by \p step.
3302 * \param step Which step the input data is for.
3303 * \param private_key Handle to the private key to use.
3304 * \param[in] peer_key Public key of the peer. The peer key must be in the
3305 * same format that psa_import_key() accepts for the
3306 * public key type corresponding to the type of
3307 * private_key. That is, this function performs the
3308 * equivalent of
3309 * #psa_import_key(...,
3310 * `peer_key`, `peer_key_length`) where
3311 * with key attributes indicating the public key
3312 * type corresponding to the type of `private_key`.
3313 * For example, for EC keys, this means that peer_key
3314 * is interpreted as a point on the curve that the
3315 * private key is on. The standard formats for public
3316 * keys are documented in the documentation of
3317 * psa_export_public_key().
3318 * \param peer_key_length Size of \p peer_key in bytes.
3319 *
3320 * \retval #PSA_SUCCESS
3321 * Success.
3322 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003323 * \retval #PSA_ERROR_NOT_PERMITTED
3324 * \retval #PSA_ERROR_INVALID_ARGUMENT
3325 * \c private_key is not compatible with \c alg,
3326 * or \p peer_key is not valid for \c alg or not compatible with
3327 * \c private_key.
3328 * \retval #PSA_ERROR_NOT_SUPPORTED
3329 * \c alg is not supported or is not a key derivation algorithm.
3330 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3331 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3332 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003333 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003334 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003335 * \retval #PSA_ERROR_BAD_STATE
3336 * The library has not been previously initialized by psa_crypto_init().
3337 * It is implementation-dependent whether a failure to initialize
3338 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003339 */
3340psa_status_t psa_key_derivation_key_agreement(
3341 psa_key_derivation_operation_t *operation,
3342 psa_key_derivation_step_t step,
3343 psa_key_handle_t private_key,
3344 const uint8_t *peer_key,
3345 size_t peer_key_length);
3346
Gilles Peskine35675b62019-05-16 17:26:11 +02003347/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003348 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003349 * This function calculates output bytes from a key derivation algorithm and
3350 * return those bytes.
3351 * If you view the key derivation's output as a stream of bytes, this
3352 * function destructively reads the requested number of bytes from the
3353 * stream.
3354 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003355 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003356 * \param[in,out] operation The key derivation operation object to read from.
3357 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003358 * \param output_length Number of bytes to output.
3359 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003360 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003361 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003362 * The operation's capacity was less than
3363 * \p output_length bytes. Note that in this case,
3364 * no output is written to the output buffer.
3365 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003366 * subsequent calls to this function will not
3367 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003368 * \retval #PSA_ERROR_BAD_STATE
3369 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3370 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3371 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003372 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003373 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003374 * \retval #PSA_ERROR_BAD_STATE
3375 * The library has not been previously initialized by psa_crypto_init().
3376 * It is implementation-dependent whether a failure to initialize
3377 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003378 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003379psa_status_t psa_key_derivation_output_bytes(
3380 psa_key_derivation_operation_t *operation,
3381 uint8_t *output,
3382 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003383
Gilles Peskine35675b62019-05-16 17:26:11 +02003384/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003385 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003386 * This function calculates output bytes from a key derivation algorithm
3387 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003388 * The key's location, usage policy, type and size are taken from
3389 * \p attributes.
3390 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003391 * If you view the key derivation's output as a stream of bytes, this
3392 * function destructively reads as many bytes as required from the
3393 * stream.
3394 * The operation's capacity decreases by the number of bytes read.
3395 *
3396 * How much output is produced and consumed from the operation, and how
3397 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003398 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003399 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003400 * of a given size, this function is functionally equivalent to
3401 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003402 * and passing the resulting output to #psa_import_key.
3403 * However, this function has a security benefit:
3404 * if the implementation provides an isolation boundary then
3405 * the key material is not exposed outside the isolation boundary.
3406 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003407 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003408 * The following key types defined in this specification follow this scheme:
3409 *
3410 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003411 * - #PSA_KEY_TYPE_ARC4;
3412 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003413 * - #PSA_KEY_TYPE_DERIVE;
3414 * - #PSA_KEY_TYPE_HMAC.
3415 *
3416 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003417 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003418 * Montgomery curve), this function always draws a byte string whose
3419 * length is determined by the curve, and sets the mandatory bits
3420 * accordingly. That is:
3421 *
3422 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3423 * and process it as specified in RFC 7748 &sect;5.
3424 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3425 * and process it as specified in RFC 7748 &sect;5.
3426 *
3427 * - For key types for which the key is represented by a single sequence of
3428 * \p bits bits with constraints as to which bit sequences are acceptable,
3429 * this function draws a byte string of length (\p bits / 8) bytes rounded
3430 * up to the nearest whole number of bytes. If the resulting byte string
3431 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3432 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003433 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003434 * for the output produced by psa_export_key().
3435 * The following key types defined in this specification follow this scheme:
3436 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003437 * - #PSA_KEY_TYPE_DES.
3438 * Force-set the parity bits, but discard forbidden weak keys.
3439 * For 2-key and 3-key triple-DES, the three keys are generated
3440 * successively (for example, for 3-key triple-DES,
3441 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3442 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003443 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003444 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003445 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003446 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003447 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003448 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003449 * Weierstrass curve).
3450 * For these key types, interpret the byte string as integer
3451 * in big-endian order. Discard it if it is not in the range
3452 * [0, *N* - 2] where *N* is the boundary of the private key domain
3453 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003454 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003455 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003456 * This method allows compliance to NIST standards, specifically
3457 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003458 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3459 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3460 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3461 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003462 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003463 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003464 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003465 * implementation-defined.
3466 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003467 * In all cases, the data that is read is discarded from the operation.
3468 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003469 *
Gilles Peskine20628592019-04-19 19:29:50 +02003470 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003471 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003472 * \param[out] handle On success, a handle to the newly created key.
3473 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003474 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003475 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003476 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003477 * If the key is persistent, the key material and the key's metadata
3478 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003479 * \retval #PSA_ERROR_ALREADY_EXISTS
3480 * This is an attempt to create a persistent key, and there is
3481 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003482 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003483 * There was not enough data to create the desired key.
3484 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003485 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003486 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003487 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003488 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003489 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003490 * \retval #PSA_ERROR_INVALID_ARGUMENT
3491 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003492 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003493 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3494 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3495 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3496 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003497 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003498 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003499 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003500 * The library has not been previously initialized by psa_crypto_init().
3501 * It is implementation-dependent whether a failure to initialize
3502 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003503 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003504psa_status_t psa_key_derivation_output_key(
3505 const psa_key_attributes_t *attributes,
3506 psa_key_derivation_operation_t *operation,
3507 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003508
Gilles Peskine35675b62019-05-16 17:26:11 +02003509/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003510 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003511 * Once a key derivation operation has been aborted, its capacity is zero.
3512 * Aborting an operation frees all associated resources except for the
3513 * \c operation structure itself.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003514 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003515 * This function may be called at any time as long as the operation
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003516 * object has been initialized to #PSA_KEY_DERIVATION_OPERATION_INIT, to
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003517 * psa_key_derivation_operation_init() or a zero value. In particular,
3518 * it is valid to call psa_key_derivation_abort() twice, or to call
3519 * psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003520 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003521 * Once aborted, the key derivation operation object may be called.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003522 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003523 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003524 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003525 * \retval #PSA_SUCCESS
3526 * \retval #PSA_ERROR_BAD_STATE
3527 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3528 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003529 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003530 * \retval #PSA_ERROR_BAD_STATE
3531 * The library has not been previously initialized by psa_crypto_init().
3532 * It is implementation-dependent whether a failure to initialize
3533 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003534 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003535psa_status_t psa_key_derivation_abort(
3536 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003537
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003538/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003539 *
3540 * \warning The raw result of a key agreement algorithm such as finite-field
3541 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3542 * not be used directly as key material. It should instead be passed as
3543 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003544 * a key derivation, use psa_key_derivation_key_agreement() and other
3545 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003546 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003547 * \param alg The key agreement algorithm to compute
3548 * (\c PSA_ALG_XXX value such that
3549 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3550 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003551 * \param private_key Handle to the private key to use.
3552 * \param[in] peer_key Public key of the peer. It must be
3553 * in the same format that psa_import_key()
3554 * accepts. The standard formats for public
3555 * keys are documented in the documentation
3556 * of psa_export_public_key().
3557 * \param peer_key_length Size of \p peer_key in bytes.
3558 * \param[out] output Buffer where the decrypted message is to
3559 * be written.
3560 * \param output_size Size of the \c output buffer in bytes.
3561 * \param[out] output_length On success, the number of bytes
3562 * that make up the returned output.
3563 *
3564 * \retval #PSA_SUCCESS
3565 * Success.
3566 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003567 * \retval #PSA_ERROR_NOT_PERMITTED
3568 * \retval #PSA_ERROR_INVALID_ARGUMENT
3569 * \p alg is not a key agreement algorithm
3570 * \retval #PSA_ERROR_INVALID_ARGUMENT
3571 * \p private_key is not compatible with \p alg,
3572 * or \p peer_key is not valid for \p alg or not compatible with
3573 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003574 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3575 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003576 * \retval #PSA_ERROR_NOT_SUPPORTED
3577 * \p alg is not a supported key agreement algorithm.
3578 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3579 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3580 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003581 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003582 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003583 * \retval #PSA_ERROR_BAD_STATE
3584 * The library has not been previously initialized by psa_crypto_init().
3585 * It is implementation-dependent whether a failure to initialize
3586 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003587 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003588psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3589 psa_key_handle_t private_key,
3590 const uint8_t *peer_key,
3591 size_t peer_key_length,
3592 uint8_t *output,
3593 size_t output_size,
3594 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003595
Gilles Peskineea0fb492018-07-12 17:17:20 +02003596/**@}*/
3597
Gilles Peskineedd76872018-07-20 17:42:05 +02003598/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003599 * @{
3600 */
3601
3602/**
3603 * \brief Generate random bytes.
3604 *
3605 * \warning This function **can** fail! Callers MUST check the return status
3606 * and MUST NOT use the content of the output buffer if the return
3607 * status is not #PSA_SUCCESS.
3608 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003609 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003610 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003611 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003612 * \param output_size Number of bytes to generate and output.
3613 *
Gilles Peskine28538492018-07-11 17:34:00 +02003614 * \retval #PSA_SUCCESS
3615 * \retval #PSA_ERROR_NOT_SUPPORTED
3616 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003617 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003618 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3619 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003620 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003621 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003622 * The library has not been previously initialized by psa_crypto_init().
3623 * It is implementation-dependent whether a failure to initialize
3624 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003625 */
3626psa_status_t psa_generate_random(uint8_t *output,
3627 size_t output_size);
3628
3629/**
3630 * \brief Generate a key or key pair.
3631 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003632 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003633 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003634 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003635 * Implementations must reject an attempt to generate a key of size 0.
3636 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003637 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003638 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003639 * the public exponent is 65537.
3640 * The modulus is a product of two probabilistic primes
3641 * between 2^{n-1} and 2^n where n is the bit size specified in the
3642 * attributes.
3643 *
Gilles Peskine20628592019-04-19 19:29:50 +02003644 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003645 * \param[out] handle On success, a handle to the newly created key.
3646 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003647 *
Gilles Peskine28538492018-07-11 17:34:00 +02003648 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003649 * Success.
3650 * If the key is persistent, the key material and the key's metadata
3651 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003652 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003653 * This is an attempt to create a persistent key, and there is
3654 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003655 * \retval #PSA_ERROR_NOT_SUPPORTED
3656 * \retval #PSA_ERROR_INVALID_ARGUMENT
3657 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3658 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3659 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3660 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003661 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003662 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3663 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003664 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003665 * The library has not been previously initialized by psa_crypto_init().
3666 * It is implementation-dependent whether a failure to initialize
3667 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003668 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003669psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003670 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003671
3672/**@}*/
3673
Gilles Peskinee59236f2018-01-27 23:32:46 +01003674#ifdef __cplusplus
3675}
3676#endif
3677
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003678/* The file "crypto_sizes.h" contains definitions for size calculation
3679 * macros whose definitions are implementation-specific. */
3680#include "crypto_sizes.h"
3681
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003682/* The file "crypto_struct.h" contains definitions for
3683 * implementation-specific structs that are declared above. */
3684#include "crypto_struct.h"
3685
3686/* The file "crypto_extra.h" contains vendor-specific definitions. This
3687 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003688#include "crypto_extra.h"
3689
3690#endif /* PSA_CRYPTO_H */