blob: ddc86cd58ee2ebda0db0e1160f693a92bfb5cd90 [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
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100458/** Make a copy of a key.
459 *
460 * Copy key material from one location to another.
461 *
462 * This function is primarily useful to copy a key from one location
463 * to another, since it populates a key using the material from
464 * another key which may have a different lifetime.
465 *
466 * This function may be used to share a key with a different party,
467 * subject to implementation-defined restrictions on key sharing.
468 *
469 * The policy on the source key must have the usage flag
470 * #PSA_KEY_USAGE_COPY set.
471 * This flag is sufficient to permit the copy if the key has the lifetime
472 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
473 * Some secure elements do not provide a way to copy a key without
474 * making it extractable from the secure element. If a key is located
475 * in such a secure element, then the key must have both usage flags
476 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
477 * a copy of the key outside the secure element.
478 *
479 * The resulting key may only be used in a way that conforms to
480 * both the policy of the original key and the policy specified in
481 * the \p attributes parameter:
482 * - The usage flags on the resulting key are the bitwise-and of the
483 * usage flags on the source policy and the usage flags in \p attributes.
484 * - If both allow the same algorithm or wildcard-based
485 * algorithm policy, the resulting key has the same algorithm policy.
486 * - If either of the policies allows an algorithm and the other policy
487 * allows a wildcard-based algorithm policy that includes this algorithm,
488 * the resulting key allows the same algorithm.
489 * - If the policies do not allow any algorithm in common, this function
490 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
491 *
492 * The effect of this function on implementation-defined attributes is
493 * implementation-defined.
494 *
495 * \param source_handle The key to copy. It must be a valid key handle.
496 * \param[in] attributes The attributes for the new key.
497 * They are used as follows:
498 * - The key type and size may be 0. If either is
499 * nonzero, it must match the corresponding
500 * attribute of the source key.
501 * - The key location (the lifetime and, for
502 * persistent keys, the key identifier) is
503 * used directly.
504 * - The policy constraints (usage flags and
505 * algorithm policy) are combined from
506 * the source key and \p attributes so that
507 * both sets of restrictions apply, as
508 * described in the documentation of this function.
509 * \param[out] target_handle On success, a handle to the newly created key.
510 * \c 0 on failure.
511 *
512 * \retval #PSA_SUCCESS
513 * \retval #PSA_ERROR_INVALID_HANDLE
514 * \p source_handle is invalid.
515 * \retval #PSA_ERROR_ALREADY_EXISTS
516 * This is an attempt to create a persistent key, and there is
517 * already a persistent key with the given identifier.
518 * \retval #PSA_ERROR_INVALID_ARGUMENT
519 * The lifetime or identifier in \p attributes are invalid.
520 * \retval #PSA_ERROR_INVALID_ARGUMENT
521 * The policy constraints on the source and specified in
522 * \p attributes are incompatible.
523 * \retval #PSA_ERROR_INVALID_ARGUMENT
524 * \p attributes specifies a key type or key size
525 * which does not match the attributes of the source key.
526 * \retval #PSA_ERROR_NOT_PERMITTED
527 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
528 * \retval #PSA_ERROR_NOT_PERMITTED
529 * The source key is not exportable and its lifetime does not
530 * allow copying it to the target's lifetime.
531 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
532 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
533 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
534 * \retval #PSA_ERROR_HARDWARE_FAILURE
535 * \retval #PSA_ERROR_STORAGE_FAILURE
536 * \retval #PSA_ERROR_CORRUPTION_DETECTED
537 * \retval #PSA_ERROR_BAD_STATE
538 * The library has not been previously initialized by psa_crypto_init().
539 * It is implementation-dependent whether a failure to initialize
540 * results in this error code.
541 */
542psa_status_t psa_copy_key(psa_key_handle_t source_handle,
543 const psa_key_attributes_t *attributes,
544 psa_key_handle_t *target_handle);
545
546
547/**
548 * \brief Destroy a key.
549 *
550 * This function destroys a key from both volatile
551 * memory and, if applicable, non-volatile storage. Implementations shall
552 * make a best effort to ensure that that the key material cannot be recovered.
553 *
554 * This function also erases any metadata such as policies and frees
555 * resources associated with the key. To free all resources associated with
556 * the key, all handles to the key must be closed or destroyed.
557 *
558 * Destroying the key makes the handle invalid, and the key handle
559 * must not be used again by the application. Using other open handles to the
560 * destroyed key in a cryptographic operation will result in an error.
561 *
562 * If a key is currently in use in a multipart operation, then destroying the
563 * key will cause the multipart operation to fail.
564 *
565 * \param handle Handle to the key to erase.
566 *
567 * \retval #PSA_SUCCESS
568 * The key material has been erased.
569 * \retval #PSA_ERROR_NOT_PERMITTED
570 * The key cannot be erased because it is
571 * read-only, either due to a policy or due to physical restrictions.
572 * \retval #PSA_ERROR_INVALID_HANDLE
573 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
574 * There was an failure in communication with the cryptoprocessor.
575 * The key material may still be present in the cryptoprocessor.
576 * \retval #PSA_ERROR_STORAGE_FAILURE
577 * The storage is corrupted. Implementations shall make a best effort
578 * to erase key material even in this stage, however applications
579 * should be aware that it may be impossible to guarantee that the
580 * key material is not recoverable in such cases.
581 * \retval #PSA_ERROR_CORRUPTION_DETECTED
582 * An unexpected condition which is not a storage corruption or
583 * a communication failure occurred. The cryptoprocessor may have
584 * been compromised.
585 * \retval #PSA_ERROR_BAD_STATE
586 * The library has not been previously initialized by psa_crypto_init().
587 * It is implementation-dependent whether a failure to initialize
588 * results in this error code.
589 */
590psa_status_t psa_destroy_key(psa_key_handle_t handle);
591
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100592/**@}*/
593
594/** \defgroup import_export Key import and export
595 * @{
596 */
597
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100598/**
599 * \brief Import a key in binary format.
600 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100601 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100602 * documentation of psa_export_public_key() for the format of public keys
603 * and to the documentation of psa_export_key() for the format for
604 * other key types.
605 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200606 * The key data determines the key size. The attributes may optionally
607 * specify a key size; in this case it must match the size determined
608 * from the key data. A key size of 0 in \p attributes indicates that
609 * the key size is solely determined by the key data.
610 *
611 * Implementations must reject an attempt to import a key of size 0.
612 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100613 * This specification supports a single format for each key type.
614 * Implementations may support other formats as long as the standard
615 * format is supported. Implementations that support other formats
616 * should ensure that the formats are clearly unambiguous so as to
617 * minimize the risk that an invalid input is accidentally interpreted
618 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619 *
Gilles Peskine20628592019-04-19 19:29:50 +0200620 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200621 * The key size is always determined from the
622 * \p data buffer.
623 * If the key size in \p attributes is nonzero,
624 * it must be equal to the size from \p data.
Gilles Peskine20628592019-04-19 19:29:50 +0200625 * \param[out] handle On success, a handle to the newly created key.
626 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100627 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200628 * buffer is interpreted according to the type declared
629 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200630 * All implementations must support at least the format
631 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100632 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200633 * the chosen type. Implementations may allow other
634 * formats, but should be conservative: implementations
635 * should err on the side of rejecting content if it
636 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200637 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100638 *
Gilles Peskine28538492018-07-11 17:34:00 +0200639 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100640 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100641 * If the key is persistent, the key material and the key's metadata
642 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200643 * \retval #PSA_ERROR_ALREADY_EXISTS
644 * This is an attempt to create a persistent key, and there is
645 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200646 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200647 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200648 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200649 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200650 * The key attributes, as a whole, are invalid.
651 * \retval #PSA_ERROR_INVALID_ARGUMENT
652 * The key data is not correctly formatted.
653 * \retval #PSA_ERROR_INVALID_ARGUMENT
654 * The size in \p attributes is nonzero and does not match the size
655 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200656 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
657 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
658 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100659 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200660 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200661 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300662 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300663 * The library has not been previously initialized by psa_crypto_init().
664 * It is implementation-dependent whether a failure to initialize
665 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100666 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200667psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100668 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200669 size_t data_length,
670 psa_key_handle_t *handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100671
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100672
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673
674/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675 * \brief Export a key in binary format.
676 *
677 * The output of this function can be passed to psa_import_key() to
678 * create an equivalent object.
679 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100680 * If the implementation of psa_import_key() supports other formats
681 * beyond the format specified here, the output from psa_export_key()
682 * must use the representation specified here, not the original
683 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100685 * For standard key types, the output format is as follows:
686 *
687 * - For symmetric keys (including MAC keys), the format is the
688 * raw bytes of the key.
689 * - For DES, the key data consists of 8 bytes. The parity bits must be
690 * correct.
691 * - For Triple-DES, the format is the concatenation of the
692 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200693 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200694 * is the non-encrypted DER encoding of the representation defined by
695 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
696 * ```
697 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200698 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200699 * modulus INTEGER, -- n
700 * publicExponent INTEGER, -- e
701 * privateExponent INTEGER, -- d
702 * prime1 INTEGER, -- p
703 * prime2 INTEGER, -- q
704 * exponent1 INTEGER, -- d mod (p-1)
705 * exponent2 INTEGER, -- d mod (q-1)
706 * coefficient INTEGER, -- (inverse of q) mod p
707 * }
708 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200709 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200710 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100711 * a representation of the private value as a `ceiling(m/8)`-byte string
712 * where `m` is the bit size associated with the curve, i.e. the bit size
713 * of the order of the curve's coordinate field. This byte string is
714 * in little-endian order for Montgomery curves (curve types
715 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
716 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
717 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100718 * This is the content of the `privateKey` field of the `ECPrivateKey`
719 * format defined by RFC 5915.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200720 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200721 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000722 * format is the representation of the private key `x` as a big-endian byte
723 * string. The length of the byte string is the private key size in bytes
724 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200725 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
726 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100727 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200728 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
729 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100730 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200731 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200732 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200733 * \param[out] data_length On success, the number of bytes
734 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100735 *
Gilles Peskine28538492018-07-11 17:34:00 +0200736 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100737 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200738 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200739 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100740 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200741 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
742 * The size of the \p data buffer is too small. You can determine a
743 * sufficient buffer size by calling
744 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
745 * where \c type is the key type
746 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200747 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
748 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200749 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100750 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100751 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300752 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300753 * The library has not been previously initialized by psa_crypto_init().
754 * It is implementation-dependent whether a failure to initialize
755 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100756 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100757psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100758 uint8_t *data,
759 size_t data_size,
760 size_t *data_length);
761
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100762/**
763 * \brief Export a public key or the public part of a key pair in binary format.
764 *
765 * The output of this function can be passed to psa_import_key() to
766 * create an object that is equivalent to the public key.
767 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000768 * This specification supports a single format for each key type.
769 * Implementations may support other formats as long as the standard
770 * format is supported. Implementations that support other formats
771 * should ensure that the formats are clearly unambiguous so as to
772 * minimize the risk that an invalid input is accidentally interpreted
773 * according to a different format.
774 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000775 * For standard key types, the output format is as follows:
776 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
777 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
778 * ```
779 * RSAPublicKey ::= SEQUENCE {
780 * modulus INTEGER, -- n
781 * publicExponent INTEGER } -- e
782 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000783 * - For elliptic curve public keys (key types for which
784 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
785 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
786 * Let `m` be the bit size associated with the curve, i.e. the bit size of
787 * `q` for a curve over `F_q`. The representation consists of:
788 * - The byte 0x04;
789 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
790 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200791 * - For Diffie-Hellman key exchange public keys (key types for which
792 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000793 * the format is the representation of the public key `y = g^x mod p` as a
794 * big-endian byte string. The length of the byte string is the length of the
795 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100796 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200797 * Exporting a public key object or the public part of a key pair is
798 * always permitted, regardless of the key's usage flags.
799 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100800 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200801 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200802 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200803 * \param[out] data_length On success, the number of bytes
804 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100805 *
Gilles Peskine28538492018-07-11 17:34:00 +0200806 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100807 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200808 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200809 * The key is neither a public key nor a key pair.
810 * \retval #PSA_ERROR_NOT_SUPPORTED
811 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
812 * The size of the \p data buffer is too small. You can determine a
813 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200814 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200815 * where \c type is the key type
816 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200817 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
818 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200819 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100820 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100821 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300822 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300823 * 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 Peskineae32aac2018-11-30 14:39:32 +0100827psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100828 uint8_t *data,
829 size_t data_size,
830 size_t *data_length);
831
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100832
Gilles Peskine20035e32018-02-03 22:44:14 +0100833
834/**@}*/
835
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100836/** \defgroup hash Message digests
837 * @{
838 */
839
Gilles Peskine69647a42019-01-14 20:18:12 +0100840/** Calculate the hash (digest) of a message.
841 *
842 * \note To verify the hash of a message against an
843 * expected value, use psa_hash_compare() instead.
844 *
845 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
846 * such that #PSA_ALG_IS_HASH(\p alg) is true).
847 * \param[in] input Buffer containing the message to hash.
848 * \param input_length Size of the \p input buffer in bytes.
849 * \param[out] hash Buffer where the hash is to be written.
850 * \param hash_size Size of the \p hash buffer in bytes.
851 * \param[out] hash_length On success, the number of bytes
852 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100853 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100854 *
855 * \retval #PSA_SUCCESS
856 * Success.
857 * \retval #PSA_ERROR_NOT_SUPPORTED
858 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100859 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100860 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
861 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100862 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
863 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
864 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200865 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100866 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100867 * \retval #PSA_ERROR_BAD_STATE
868 * The library has not been previously initialized by psa_crypto_init().
869 * It is implementation-dependent whether a failure to initialize
870 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100871 */
872psa_status_t psa_hash_compute(psa_algorithm_t alg,
873 const uint8_t *input,
874 size_t input_length,
875 uint8_t *hash,
876 size_t hash_size,
877 size_t *hash_length);
878
879/** Calculate the hash (digest) of a message and compare it with a
880 * reference value.
881 *
882 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
883 * such that #PSA_ALG_IS_HASH(\p alg) is true).
884 * \param[in] input Buffer containing the message to hash.
885 * \param input_length Size of the \p input buffer in bytes.
886 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100887 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100888 *
889 * \retval #PSA_SUCCESS
890 * The expected hash is identical to the actual hash of the input.
891 * \retval #PSA_ERROR_INVALID_SIGNATURE
892 * The hash of the message was calculated successfully, but it
893 * differs from the expected hash.
894 * \retval #PSA_ERROR_NOT_SUPPORTED
895 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100896 * \retval #PSA_ERROR_INVALID_ARGUMENT
897 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100898 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
899 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
900 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200901 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100902 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100903 * \retval #PSA_ERROR_BAD_STATE
904 * The library has not been previously initialized by psa_crypto_init().
905 * It is implementation-dependent whether a failure to initialize
906 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100907 */
908psa_status_t psa_hash_compare(psa_algorithm_t alg,
909 const uint8_t *input,
910 size_t input_length,
911 const uint8_t *hash,
912 const size_t hash_length);
913
Gilles Peskine308b91d2018-02-08 09:47:44 +0100914/** The type of the state data structure for multipart hash operations.
915 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000916 * Before calling any function on a hash operation object, the application must
917 * initialize it by any of the following means:
918 * - Set the structure to all-bits-zero, for example:
919 * \code
920 * psa_hash_operation_t operation;
921 * memset(&operation, 0, sizeof(operation));
922 * \endcode
923 * - Initialize the structure to logical zero values, for example:
924 * \code
925 * psa_hash_operation_t operation = {0};
926 * \endcode
927 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
928 * for example:
929 * \code
930 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
931 * \endcode
932 * - Assign the result of the function psa_hash_operation_init()
933 * to the structure, for example:
934 * \code
935 * psa_hash_operation_t operation;
936 * operation = psa_hash_operation_init();
937 * \endcode
938 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100939 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100940 * make any assumptions about the content of this structure except
941 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100942typedef struct psa_hash_operation_s psa_hash_operation_t;
943
Jaeden Amero6a25b412019-01-04 11:47:44 +0000944/** \def PSA_HASH_OPERATION_INIT
945 *
946 * This macro returns a suitable initializer for a hash operation object
947 * of type #psa_hash_operation_t.
948 */
949#ifdef __DOXYGEN_ONLY__
950/* This is an example definition for documentation purposes.
951 * Implementations should define a suitable value in `crypto_struct.h`.
952 */
953#define PSA_HASH_OPERATION_INIT {0}
954#endif
955
956/** Return an initial value for a hash operation object.
957 */
958static psa_hash_operation_t psa_hash_operation_init(void);
959
Gilles Peskinef45adda2019-01-14 18:29:18 +0100960/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100961 *
962 * The sequence of operations to calculate a hash (message digest)
963 * is as follows:
964 * -# Allocate an operation object which will be passed to all the functions
965 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000966 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100967 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200968 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100969 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100970 * of the message each time. The hash that is calculated is the hash
971 * of the concatenation of these messages in order.
972 * -# To calculate the hash, call psa_hash_finish().
973 * To compare the hash with an expected value, call psa_hash_verify().
974 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100975 * If an error occurs at any step after a call to psa_hash_setup(), the
976 * operation will need to be reset by a call to psa_hash_abort(). The
977 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000978 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100979 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200980 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100981 * eventually terminate the operation. The following events terminate an
982 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100983 * - A successful call to psa_hash_finish() or psa_hash_verify().
984 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100985 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000986 * \param[in,out] operation The operation object to set up. It must have
987 * been initialized as per the documentation for
988 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200989 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
990 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100991 *
Gilles Peskine28538492018-07-11 17:34:00 +0200992 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100993 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200994 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100995 * \p alg is not a supported hash algorithm.
996 * \retval #PSA_ERROR_INVALID_ARGUMENT
997 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100998 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100999 * The operation state is not valid (it must be inactive).
Gilles Peskine28538492018-07-11 17:34:00 +02001000 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1001 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1002 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001003 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001004 * \retval #PSA_ERROR_BAD_STATE
1005 * The library has not been previously initialized by psa_crypto_init().
1006 * It is implementation-dependent whether a failure to initialize
1007 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001008 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001009psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001010 psa_algorithm_t alg);
1011
Gilles Peskine308b91d2018-02-08 09:47:44 +01001012/** Add a message fragment to a multipart hash operation.
1013 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001014 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001015 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001016 * If this function returns an error status, the operation enters an error
1017 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001018 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001019 * \param[in,out] operation Active hash operation.
1020 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001021 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001022 *
Gilles Peskine28538492018-07-11 17:34:00 +02001023 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001024 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001025 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001026 * The operation state is not valid (it muct be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001027 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1028 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1029 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001030 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001031 * \retval #PSA_ERROR_BAD_STATE
1032 * The library has not been previously initialized by psa_crypto_init().
1033 * It is implementation-dependent whether a failure to initialize
1034 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001035 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001036psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1037 const uint8_t *input,
1038 size_t input_length);
1039
Gilles Peskine308b91d2018-02-08 09:47:44 +01001040/** Finish the calculation of the hash of a message.
1041 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001042 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001043 * This function calculates the hash of the message formed by concatenating
1044 * the inputs passed to preceding calls to psa_hash_update().
1045 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001046 * When this function returns successfuly, the operation becomes inactive.
1047 * If this function returns an error status, the operation enters an error
1048 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001049 *
1050 * \warning Applications should not call this function if they expect
1051 * a specific value for the hash. Call psa_hash_verify() instead.
1052 * Beware that comparing integrity or authenticity data such as
1053 * hash values with a function such as \c memcmp is risky
1054 * because the time taken by the comparison may leak information
1055 * about the hashed data which could allow an attacker to guess
1056 * a valid hash and thereby bypass security controls.
1057 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001058 * \param[in,out] operation Active hash operation.
1059 * \param[out] hash Buffer where the hash is to be written.
1060 * \param hash_size Size of the \p hash buffer in bytes.
1061 * \param[out] hash_length On success, the number of bytes
1062 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001063 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001064 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001065 *
Gilles Peskine28538492018-07-11 17:34:00 +02001066 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001067 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001068 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001069 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001070 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001071 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001072 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001073 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001074 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1075 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1076 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001077 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001078 * \retval #PSA_ERROR_BAD_STATE
1079 * The library has not been previously initialized by psa_crypto_init().
1080 * It is implementation-dependent whether a failure to initialize
1081 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001083psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1084 uint8_t *hash,
1085 size_t hash_size,
1086 size_t *hash_length);
1087
Gilles Peskine308b91d2018-02-08 09:47:44 +01001088/** Finish the calculation of the hash of a message and compare it with
1089 * an expected value.
1090 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001091 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001092 * This function calculates the hash of the message formed by concatenating
1093 * the inputs passed to preceding calls to psa_hash_update(). It then
1094 * compares the calculated hash with the expected hash passed as a
1095 * parameter to this function.
1096 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001097 * When this function returns successfuly, the operation becomes inactive.
1098 * If this function returns an error status, the operation enters an error
1099 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001100 *
Gilles Peskine19067982018-03-20 17:54:53 +01001101 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001102 * comparison between the actual hash and the expected hash is performed
1103 * in constant time.
1104 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001105 * \param[in,out] operation Active hash operation.
1106 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001107 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001108 *
Gilles Peskine28538492018-07-11 17:34:00 +02001109 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001110 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001111 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001112 * The hash of the message was calculated successfully, but it
1113 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001114 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001115 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001116 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1117 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1118 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001119 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001120 * \retval #PSA_ERROR_BAD_STATE
1121 * The library has not been previously initialized by psa_crypto_init().
1122 * It is implementation-dependent whether a failure to initialize
1123 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001124 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001125psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1126 const uint8_t *hash,
1127 size_t hash_length);
1128
Gilles Peskine308b91d2018-02-08 09:47:44 +01001129/** Abort a hash operation.
1130 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001131 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001132 * \p operation structure itself. Once aborted, the operation object
1133 * can be reused for another operation by calling
1134 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001135 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001136 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001137 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001138 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001139 * In particular, calling psa_hash_abort() after the operation has been
1140 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1141 * psa_hash_verify() is safe and has no effect.
1142 *
1143 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001144 *
Gilles Peskine28538492018-07-11 17:34:00 +02001145 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001146 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1147 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001148 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001149 * \retval #PSA_ERROR_BAD_STATE
1150 * The library has not been previously initialized by psa_crypto_init().
1151 * It is implementation-dependent whether a failure to initialize
1152 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001153 */
1154psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001155
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001156/** Clone a hash operation.
1157 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001158 * This function copies the state of an ongoing hash operation to
1159 * a new operation object. In other words, this function is equivalent
1160 * to calling psa_hash_setup() on \p target_operation with the same
1161 * algorithm that \p source_operation was set up for, then
1162 * psa_hash_update() on \p target_operation with the same input that
1163 * that was passed to \p source_operation. After this function returns, the
1164 * two objects are independent, i.e. subsequent calls involving one of
1165 * the objects do not affect the other object.
1166 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001167 * \param[in] source_operation The active hash operation to clone.
1168 * \param[in,out] target_operation The operation object to set up.
1169 * It must be initialized but not active.
1170 *
1171 * \retval #PSA_SUCCESS
1172 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001173 * The \p source_operation state is not valid (it must be active).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001174 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001175 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001176 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1177 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001178 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001179 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001180 * \retval #PSA_ERROR_BAD_STATE
1181 * The library has not been previously initialized by psa_crypto_init().
1182 * It is implementation-dependent whether a failure to initialize
1183 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001184 */
1185psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1186 psa_hash_operation_t *target_operation);
1187
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001188/**@}*/
1189
Gilles Peskine8c9def32018-02-08 10:02:12 +01001190/** \defgroup MAC Message authentication codes
1191 * @{
1192 */
1193
Gilles Peskine69647a42019-01-14 20:18:12 +01001194/** Calculate the MAC (message authentication code) of a message.
1195 *
1196 * \note To verify the MAC of a message against an
1197 * expected value, use psa_mac_verify() instead.
1198 * Beware that comparing integrity or authenticity data such as
1199 * MAC values with a function such as \c memcmp is risky
1200 * because the time taken by the comparison may leak information
1201 * about the MAC value which could allow an attacker to guess
1202 * a valid MAC and thereby bypass security controls.
1203 *
1204 * \param handle Handle to the key to use for the operation.
1205 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001206 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001207 * \param[in] input Buffer containing the input message.
1208 * \param input_length Size of the \p input buffer in bytes.
1209 * \param[out] mac Buffer where the MAC value is to be written.
1210 * \param mac_size Size of the \p mac buffer in bytes.
1211 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001212 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001213 *
1214 * \retval #PSA_SUCCESS
1215 * Success.
1216 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001217 * \retval #PSA_ERROR_NOT_PERMITTED
1218 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001219 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001220 * \retval #PSA_ERROR_NOT_SUPPORTED
1221 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001222 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1223 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001224 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1225 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1226 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001227 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001228 * \retval #PSA_ERROR_STORAGE_FAILURE
1229 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001230 * \retval #PSA_ERROR_BAD_STATE
1231 * The library has not been previously initialized by psa_crypto_init().
1232 * It is implementation-dependent whether a failure to initialize
1233 * results in this error code.
1234 */
1235psa_status_t psa_mac_compute(psa_key_handle_t handle,
1236 psa_algorithm_t alg,
1237 const uint8_t *input,
1238 size_t input_length,
1239 uint8_t *mac,
1240 size_t mac_size,
1241 size_t *mac_length);
1242
1243/** Calculate the MAC of a message and compare it with a reference value.
1244 *
1245 * \param handle Handle to the key to use for the operation.
1246 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001247 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001248 * \param[in] input Buffer containing the input message.
1249 * \param input_length Size of the \p input buffer in bytes.
1250 * \param[out] mac Buffer containing the expected MAC value.
1251 * \param mac_length Size of the \p mac buffer in bytes.
1252 *
1253 * \retval #PSA_SUCCESS
1254 * The expected MAC is identical to the actual MAC of the input.
1255 * \retval #PSA_ERROR_INVALID_SIGNATURE
1256 * The MAC of the message was calculated successfully, but it
1257 * differs from the expected value.
1258 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001259 * \retval #PSA_ERROR_NOT_PERMITTED
1260 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001261 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001262 * \retval #PSA_ERROR_NOT_SUPPORTED
1263 * \p alg is not supported or is not a MAC algorithm.
1264 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1265 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1266 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001267 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001268 * \retval #PSA_ERROR_STORAGE_FAILURE
1269 * The key could not be retrieved from storage.
1270 * \retval #PSA_ERROR_BAD_STATE
1271 * The library has not been previously initialized by psa_crypto_init().
1272 * It is implementation-dependent whether a failure to initialize
1273 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001274 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001275psa_status_t psa_mac_verify(psa_key_handle_t handle,
1276 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001277 const uint8_t *input,
1278 size_t input_length,
1279 const uint8_t *mac,
1280 const size_t mac_length);
1281
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001282/** The type of the state data structure for multipart MAC operations.
1283 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001284 * Before calling any function on a MAC operation object, the application must
1285 * initialize it by any of the following means:
1286 * - Set the structure to all-bits-zero, for example:
1287 * \code
1288 * psa_mac_operation_t operation;
1289 * memset(&operation, 0, sizeof(operation));
1290 * \endcode
1291 * - Initialize the structure to logical zero values, for example:
1292 * \code
1293 * psa_mac_operation_t operation = {0};
1294 * \endcode
1295 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1296 * for example:
1297 * \code
1298 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1299 * \endcode
1300 * - Assign the result of the function psa_mac_operation_init()
1301 * to the structure, for example:
1302 * \code
1303 * psa_mac_operation_t operation;
1304 * operation = psa_mac_operation_init();
1305 * \endcode
1306 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001307 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001308 * make any assumptions about the content of this structure except
1309 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001310typedef struct psa_mac_operation_s psa_mac_operation_t;
1311
Jaeden Amero769ce272019-01-04 11:48:03 +00001312/** \def PSA_MAC_OPERATION_INIT
1313 *
1314 * This macro returns a suitable initializer for a MAC operation object of type
1315 * #psa_mac_operation_t.
1316 */
1317#ifdef __DOXYGEN_ONLY__
1318/* This is an example definition for documentation purposes.
1319 * Implementations should define a suitable value in `crypto_struct.h`.
1320 */
1321#define PSA_MAC_OPERATION_INIT {0}
1322#endif
1323
1324/** Return an initial value for a MAC operation object.
1325 */
1326static psa_mac_operation_t psa_mac_operation_init(void);
1327
Gilles Peskinef45adda2019-01-14 18:29:18 +01001328/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001329 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001330 * This function sets up the calculation of the MAC
1331 * (message authentication code) of a byte string.
1332 * To verify the MAC of a message against an
1333 * expected value, use psa_mac_verify_setup() instead.
1334 *
1335 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001336 * -# Allocate an operation object which will be passed to all the functions
1337 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001338 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001339 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001340 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001341 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1342 * of the message each time. The MAC that is calculated is the MAC
1343 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001344 * -# At the end of the message, call psa_mac_sign_finish() to finish
1345 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001346 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001347 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1348 * operation will need to be reset by a call to psa_mac_abort(). The
1349 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001350 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001351 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001352 * After a successful call to psa_mac_sign_setup(), the application must
1353 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001354 * - A successful call to psa_mac_sign_finish().
1355 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001356 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001357 * \param[in,out] operation The operation object to set up. It must have
1358 * been initialized as per the documentation for
1359 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001360 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001361 * It must remain valid until the operation
1362 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001363 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001364 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001365 *
Gilles Peskine28538492018-07-11 17:34:00 +02001366 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001367 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001368 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001369 * \retval #PSA_ERROR_NOT_PERMITTED
1370 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001371 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001372 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001373 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001374 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1375 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1376 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001377 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001378 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001379 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001380 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001381 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001382 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001383 * The library has not been previously initialized by psa_crypto_init().
1384 * It is implementation-dependent whether a failure to initialize
1385 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001386 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001387psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001388 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001389 psa_algorithm_t alg);
1390
Gilles Peskinef45adda2019-01-14 18:29:18 +01001391/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001392 *
1393 * This function sets up the verification of the MAC
1394 * (message authentication code) of a byte string against an expected value.
1395 *
1396 * The sequence of operations to verify a MAC is as follows:
1397 * -# Allocate an operation object which will be passed to all the functions
1398 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001399 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001400 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001401 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001402 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1403 * of the message each time. The MAC that is calculated is the MAC
1404 * of the concatenation of these messages in order.
1405 * -# At the end of the message, call psa_mac_verify_finish() to finish
1406 * calculating the actual MAC of the message and verify it against
1407 * the expected value.
1408 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001409 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1410 * operation will need to be reset by a call to psa_mac_abort(). The
1411 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001412 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001413 *
1414 * After a successful call to psa_mac_verify_setup(), the application must
1415 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001416 * - A successful call to psa_mac_verify_finish().
1417 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001418 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001419 * \param[in,out] operation The operation object to set up. It must have
1420 * been initialized as per the documentation for
1421 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001422 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001423 * It must remain valid until the operation
1424 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001425 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1426 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001427 *
Gilles Peskine28538492018-07-11 17:34:00 +02001428 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001429 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001430 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001431 * \retval #PSA_ERROR_NOT_PERMITTED
1432 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001433 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001434 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001435 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001436 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1437 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1438 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001439 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001440 * \retval #PSA_ERROR_STORAGE_FAILURE
1441 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001442 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001443 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001444 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001445 * The library has not been previously initialized by psa_crypto_init().
1446 * It is implementation-dependent whether a failure to initialize
1447 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001448 */
1449psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001450 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001451 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001452
Gilles Peskinedcd14942018-07-12 00:30:52 +02001453/** Add a message fragment to a multipart MAC operation.
1454 *
1455 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1456 * before calling this function.
1457 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001458 * If this function returns an error status, the operation enters an error
1459 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001460 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001461 * \param[in,out] operation Active MAC operation.
1462 * \param[in] input Buffer containing the message fragment to add to
1463 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001464 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001465 *
1466 * \retval #PSA_SUCCESS
1467 * Success.
1468 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001469 * The operation state is not valid (it must be active).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001470 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1471 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1472 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001473 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001474 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001475 * \retval #PSA_ERROR_BAD_STATE
1476 * The library has not been previously initialized by psa_crypto_init().
1477 * It is implementation-dependent whether a failure to initialize
1478 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001479 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001480psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1481 const uint8_t *input,
1482 size_t input_length);
1483
Gilles Peskinedcd14942018-07-12 00:30:52 +02001484/** Finish the calculation of the MAC of a message.
1485 *
1486 * The application must call psa_mac_sign_setup() before calling this function.
1487 * This function calculates the MAC of the message formed by concatenating
1488 * the inputs passed to preceding calls to psa_mac_update().
1489 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001490 * When this function returns successfuly, the operation becomes inactive.
1491 * If this function returns an error status, the operation enters an error
1492 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001493 *
1494 * \warning Applications should not call this function if they expect
1495 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1496 * Beware that comparing integrity or authenticity data such as
1497 * MAC values with a function such as \c memcmp is risky
1498 * because the time taken by the comparison may leak information
1499 * about the MAC value which could allow an attacker to guess
1500 * a valid MAC and thereby bypass security controls.
1501 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001502 * \param[in,out] operation Active MAC operation.
1503 * \param[out] mac Buffer where the MAC value is to be written.
1504 * \param mac_size Size of the \p mac buffer in bytes.
1505 * \param[out] mac_length On success, the number of bytes
1506 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001507 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001508 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001509 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001510 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001511 *
1512 * \retval #PSA_SUCCESS
1513 * Success.
1514 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001515 * The operation state is not valid (it must be an active mac sign
1516 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001517 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001518 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001519 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1520 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1521 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1522 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001523 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001524 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001525 * \retval #PSA_ERROR_BAD_STATE
1526 * The library has not been previously initialized by psa_crypto_init().
1527 * It is implementation-dependent whether a failure to initialize
1528 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001529 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001530psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1531 uint8_t *mac,
1532 size_t mac_size,
1533 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001534
Gilles Peskinedcd14942018-07-12 00:30:52 +02001535/** Finish the calculation of the MAC of a message and compare it with
1536 * an expected value.
1537 *
1538 * The application must call psa_mac_verify_setup() before calling this function.
1539 * This function calculates the MAC of the message formed by concatenating
1540 * the inputs passed to preceding calls to psa_mac_update(). It then
1541 * compares the calculated MAC with the expected MAC passed as a
1542 * parameter to this function.
1543 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001544 * When this function returns successfuly, the operation becomes inactive.
1545 * If this function returns an error status, the operation enters an error
1546 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001547 *
1548 * \note Implementations shall make the best effort to ensure that the
1549 * comparison between the actual MAC and the expected MAC is performed
1550 * in constant time.
1551 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001552 * \param[in,out] operation Active MAC operation.
1553 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001554 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001555 *
1556 * \retval #PSA_SUCCESS
1557 * The expected MAC is identical to the actual MAC of the message.
1558 * \retval #PSA_ERROR_INVALID_SIGNATURE
1559 * The MAC of the message was calculated successfully, but it
1560 * differs from the expected MAC.
1561 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001562 * The operation state is not valid (it must be an active mac verify
1563 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001564 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1565 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1566 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001567 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001568 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001569 * \retval #PSA_ERROR_BAD_STATE
1570 * The library has not been previously initialized by psa_crypto_init().
1571 * It is implementation-dependent whether a failure to initialize
1572 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001573 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001574psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1575 const uint8_t *mac,
1576 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001577
Gilles Peskinedcd14942018-07-12 00:30:52 +02001578/** Abort a MAC operation.
1579 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001580 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001581 * \p operation structure itself. Once aborted, the operation object
1582 * can be reused for another operation by calling
1583 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001584 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001585 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001586 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001587 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001588 * In particular, calling psa_mac_abort() after the operation has been
1589 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1590 * psa_mac_verify_finish() is safe and has no effect.
1591 *
1592 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001593 *
1594 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001595 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1596 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001597 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001598 * \retval #PSA_ERROR_BAD_STATE
1599 * The library has not been previously initialized by psa_crypto_init().
1600 * It is implementation-dependent whether a failure to initialize
1601 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001602 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001603psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1604
1605/**@}*/
1606
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001607/** \defgroup cipher Symmetric ciphers
1608 * @{
1609 */
1610
Gilles Peskine69647a42019-01-14 20:18:12 +01001611/** Encrypt a message using a symmetric cipher.
1612 *
1613 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001614 * vector). Use the multipart operation interface with a
1615 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001616 *
1617 * \param handle Handle to the key to use for the operation.
1618 * It must remain valid until the operation
1619 * terminates.
1620 * \param alg The cipher algorithm to compute
1621 * (\c PSA_ALG_XXX value such that
1622 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1623 * \param[in] input Buffer containing the message to encrypt.
1624 * \param input_length Size of the \p input buffer in bytes.
1625 * \param[out] output Buffer where the output is to be written.
1626 * The output contains the IV followed by
1627 * the ciphertext proper.
1628 * \param output_size Size of the \p output buffer in bytes.
1629 * \param[out] output_length On success, the number of bytes
1630 * that make up the output.
1631 *
1632 * \retval #PSA_SUCCESS
1633 * Success.
1634 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001635 * \retval #PSA_ERROR_NOT_PERMITTED
1636 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001637 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001638 * \retval #PSA_ERROR_NOT_SUPPORTED
1639 * \p alg is not supported or is not a cipher algorithm.
1640 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1641 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1642 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1643 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001644 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001645 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001646 * \retval #PSA_ERROR_BAD_STATE
1647 * The library has not been previously initialized by psa_crypto_init().
1648 * It is implementation-dependent whether a failure to initialize
1649 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001650 */
1651psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1652 psa_algorithm_t alg,
1653 const uint8_t *input,
1654 size_t input_length,
1655 uint8_t *output,
1656 size_t output_size,
1657 size_t *output_length);
1658
1659/** Decrypt a message using a symmetric cipher.
1660 *
1661 * This function decrypts a message encrypted with a symmetric cipher.
1662 *
1663 * \param handle Handle to the key to use for the operation.
1664 * It must remain valid until the operation
1665 * terminates.
1666 * \param alg The cipher algorithm to compute
1667 * (\c PSA_ALG_XXX value such that
1668 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1669 * \param[in] input Buffer containing the message to decrypt.
1670 * This consists of the IV followed by the
1671 * ciphertext proper.
1672 * \param input_length Size of the \p input buffer in bytes.
1673 * \param[out] output Buffer where the plaintext is to be written.
1674 * \param output_size Size of the \p output buffer in bytes.
1675 * \param[out] output_length On success, the number of bytes
1676 * that make up the output.
1677 *
1678 * \retval #PSA_SUCCESS
1679 * Success.
1680 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001681 * \retval #PSA_ERROR_NOT_PERMITTED
1682 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001683 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001684 * \retval #PSA_ERROR_NOT_SUPPORTED
1685 * \p alg is not supported or is not a cipher algorithm.
1686 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1687 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1688 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1689 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001690 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001691 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001692 * \retval #PSA_ERROR_BAD_STATE
1693 * The library has not been previously initialized by psa_crypto_init().
1694 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001695 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001696 */
1697psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1698 psa_algorithm_t alg,
1699 const uint8_t *input,
1700 size_t input_length,
1701 uint8_t *output,
1702 size_t output_size,
1703 size_t *output_length);
1704
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001705/** The type of the state data structure for multipart cipher operations.
1706 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001707 * Before calling any function on a cipher operation object, the application
1708 * must initialize it by any of the following means:
1709 * - Set the structure to all-bits-zero, for example:
1710 * \code
1711 * psa_cipher_operation_t operation;
1712 * memset(&operation, 0, sizeof(operation));
1713 * \endcode
1714 * - Initialize the structure to logical zero values, for example:
1715 * \code
1716 * psa_cipher_operation_t operation = {0};
1717 * \endcode
1718 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1719 * for example:
1720 * \code
1721 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1722 * \endcode
1723 * - Assign the result of the function psa_cipher_operation_init()
1724 * to the structure, for example:
1725 * \code
1726 * psa_cipher_operation_t operation;
1727 * operation = psa_cipher_operation_init();
1728 * \endcode
1729 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001730 * This is an implementation-defined \c struct. Applications should not
1731 * make any assumptions about the content of this structure except
1732 * as directed by the documentation of a specific implementation. */
1733typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1734
Jaeden Amero5bae2272019-01-04 11:48:27 +00001735/** \def PSA_CIPHER_OPERATION_INIT
1736 *
1737 * This macro returns a suitable initializer for a cipher operation object of
1738 * type #psa_cipher_operation_t.
1739 */
1740#ifdef __DOXYGEN_ONLY__
1741/* This is an example definition for documentation purposes.
1742 * Implementations should define a suitable value in `crypto_struct.h`.
1743 */
1744#define PSA_CIPHER_OPERATION_INIT {0}
1745#endif
1746
1747/** Return an initial value for a cipher operation object.
1748 */
1749static psa_cipher_operation_t psa_cipher_operation_init(void);
1750
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001751/** Set the key for a multipart symmetric encryption operation.
1752 *
1753 * The sequence of operations to encrypt a message with a symmetric cipher
1754 * is as follows:
1755 * -# Allocate an operation object which will be passed to all the functions
1756 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001757 * -# Initialize the operation object with one of the methods described in the
1758 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001759 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001760 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001761 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001762 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001763 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001764 * requires a specific IV value.
1765 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1766 * of the message each time.
1767 * -# Call psa_cipher_finish().
1768 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001769 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1770 * the operation will need to be reset by a call to psa_cipher_abort(). The
1771 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001772 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001773 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001774 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001775 * eventually terminate the operation. The following events terminate an
1776 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001777 * - A successful call to psa_cipher_finish().
1778 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001779 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001780 * \param[in,out] operation The operation object to set up. It must have
1781 * been initialized as per the documentation for
1782 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001783 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001784 * It must remain valid until the operation
1785 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001786 * \param alg The cipher algorithm to compute
1787 * (\c PSA_ALG_XXX value such that
1788 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001789 *
Gilles Peskine28538492018-07-11 17:34:00 +02001790 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001791 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001792 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001793 * \retval #PSA_ERROR_NOT_PERMITTED
1794 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001795 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001796 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001797 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001798 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1799 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1800 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001801 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001802 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001803 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001804 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001805 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001806 * The library has not been previously initialized by psa_crypto_init().
1807 * It is implementation-dependent whether a failure to initialize
1808 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001809 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001810psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001811 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001812 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001813
1814/** Set the key for a multipart symmetric decryption operation.
1815 *
1816 * The sequence of operations to decrypt a message with a symmetric cipher
1817 * is as follows:
1818 * -# Allocate an operation object which will be passed to all the functions
1819 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001820 * -# Initialize the operation object with one of the methods described in the
1821 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001822 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001823 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001824 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001825 * decryption. If the IV is prepended to the ciphertext, you can call
1826 * psa_cipher_update() on a buffer containing the IV followed by the
1827 * beginning of the message.
1828 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1829 * of the message each time.
1830 * -# Call psa_cipher_finish().
1831 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001832 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1833 * the operation will need to be reset by a call to psa_cipher_abort(). The
1834 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001835 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001836 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001837 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001838 * eventually terminate the operation. The following events terminate an
1839 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001840 * - A successful call to psa_cipher_finish().
1841 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001842 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001843 * \param[in,out] operation The operation object to set up. It must have
1844 * been initialized as per the documentation for
1845 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001846 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001847 * It must remain valid until the operation
1848 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001849 * \param alg The cipher algorithm to compute
1850 * (\c PSA_ALG_XXX value such that
1851 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001852 *
Gilles Peskine28538492018-07-11 17:34:00 +02001853 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001854 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001855 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001856 * \retval #PSA_ERROR_NOT_PERMITTED
1857 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001858 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001859 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001860 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001861 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1862 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1863 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001864 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001865 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001866 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001867 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001868 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001869 * The library has not been previously initialized by psa_crypto_init().
1870 * It is implementation-dependent whether a failure to initialize
1871 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001872 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001873psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001874 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001875 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001876
Gilles Peskinedcd14942018-07-12 00:30:52 +02001877/** Generate an IV for a symmetric encryption operation.
1878 *
1879 * This function generates a random IV (initialization vector), nonce
1880 * or initial counter value for the encryption operation as appropriate
1881 * for the chosen algorithm, key type and key size.
1882 *
1883 * The application must call psa_cipher_encrypt_setup() before
1884 * calling this function.
1885 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001886 * If this function returns an error status, the operation enters an error
1887 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001888 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001889 * \param[in,out] operation Active cipher operation.
1890 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001891 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001892 * \param[out] iv_length On success, the number of bytes of the
1893 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001894 *
1895 * \retval #PSA_SUCCESS
1896 * Success.
1897 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001898 * The operation state is not valid (it must be active, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001899 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001900 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001901 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1902 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1903 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001904 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001905 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001906 * \retval #PSA_ERROR_BAD_STATE
1907 * The library has not been previously initialized by psa_crypto_init().
1908 * It is implementation-dependent whether a failure to initialize
1909 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001910 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001911psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001912 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001913 size_t iv_size,
1914 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001915
Gilles Peskinedcd14942018-07-12 00:30:52 +02001916/** Set the IV for a symmetric encryption or decryption operation.
1917 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001918 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001919 * or initial counter value for the encryption or decryption operation.
1920 *
1921 * The application must call psa_cipher_encrypt_setup() before
1922 * calling this function.
1923 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001924 * If this function returns an error status, the operation enters an error
1925 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001926 *
1927 * \note When encrypting, applications should use psa_cipher_generate_iv()
1928 * instead of this function, unless implementing a protocol that requires
1929 * a non-random IV.
1930 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001931 * \param[in,out] operation Active cipher operation.
1932 * \param[in] iv Buffer containing the IV to use.
1933 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001934 *
1935 * \retval #PSA_SUCCESS
1936 * Success.
1937 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001938 * The operation state is not valid (it must be an active cipher
1939 * encrypt operation, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001940 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001941 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001942 * or the chosen algorithm does not use an IV.
1943 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1944 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1945 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001946 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001947 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001948 * \retval #PSA_ERROR_BAD_STATE
1949 * The library has not been previously initialized by psa_crypto_init().
1950 * It is implementation-dependent whether a failure to initialize
1951 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001952 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001953psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001954 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001955 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001956
Gilles Peskinedcd14942018-07-12 00:30:52 +02001957/** Encrypt or decrypt a message fragment in an active cipher operation.
1958 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001959 * Before calling this function, you must:
1960 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1961 * The choice of setup function determines whether this function
1962 * encrypts or decrypts its input.
1963 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1964 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001965 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001966 * If this function returns an error status, the operation enters an error
1967 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001968 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001969 * \param[in,out] operation Active cipher operation.
1970 * \param[in] input Buffer containing the message fragment to
1971 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001972 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001973 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001974 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001975 * \param[out] output_length On success, the number of bytes
1976 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001977 *
1978 * \retval #PSA_SUCCESS
1979 * Success.
1980 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001981 * The operation state is not valid (it must be active, with an IV set
1982 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001983 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1984 * The size of the \p output buffer is too small.
1985 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1986 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1987 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001988 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001989 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001990 * \retval #PSA_ERROR_BAD_STATE
1991 * The library has not been previously initialized by psa_crypto_init().
1992 * It is implementation-dependent whether a failure to initialize
1993 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001994 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001995psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1996 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001997 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001998 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001999 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002000 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002001
Gilles Peskinedcd14942018-07-12 00:30:52 +02002002/** Finish encrypting or decrypting a message in a cipher operation.
2003 *
2004 * The application must call psa_cipher_encrypt_setup() or
2005 * psa_cipher_decrypt_setup() before calling this function. The choice
2006 * of setup function determines whether this function encrypts or
2007 * decrypts its input.
2008 *
2009 * This function finishes the encryption or decryption of the message
2010 * formed by concatenating the inputs passed to preceding calls to
2011 * psa_cipher_update().
2012 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002013 * When this function returns successfuly, the operation becomes inactive.
2014 * If this function returns an error status, the operation enters an error
2015 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002016 *
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
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002034 * The operation state is not valid (it must be active, with an IV set
2035 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002036 * \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
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002061 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002062 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002063 * In particular, calling psa_cipher_abort() after the operation has been
2064 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2065 * is safe and has no effect.
2066 *
2067 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002068 *
2069 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02002070 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2071 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002072 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002073 * \retval #PSA_ERROR_BAD_STATE
2074 * The library has not been previously initialized by psa_crypto_init().
2075 * It is implementation-dependent whether a failure to initialize
2076 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002077 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002078psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2079
2080/**@}*/
2081
Gilles Peskine3b555712018-03-03 21:27:57 +01002082/** \defgroup aead Authenticated encryption with associated data (AEAD)
2083 * @{
2084 */
2085
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002086/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002087 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002088 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002089 * \param alg The AEAD algorithm to compute
2090 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002091 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002092 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002093 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002094 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002095 * but not encrypted.
2096 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002097 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002098 * encrypted.
2099 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002100 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002101 * encrypted data. The additional data is not
2102 * part of this output. For algorithms where the
2103 * encrypted data and the authentication tag
2104 * are defined as separate outputs, the
2105 * authentication tag is appended to the
2106 * encrypted data.
2107 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2108 * This must be at least
2109 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2110 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002111 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002112 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002113 *
Gilles Peskine28538492018-07-11 17:34:00 +02002114 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002115 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002116 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002117 * \retval #PSA_ERROR_NOT_PERMITTED
2118 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002119 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002120 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002121 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002122 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002123 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2124 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002125 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2126 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002127 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002128 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002129 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002130 * The library has not been previously initialized by psa_crypto_init().
2131 * It is implementation-dependent whether a failure to initialize
2132 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002133 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002134psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002135 psa_algorithm_t alg,
2136 const uint8_t *nonce,
2137 size_t nonce_length,
2138 const uint8_t *additional_data,
2139 size_t additional_data_length,
2140 const uint8_t *plaintext,
2141 size_t plaintext_length,
2142 uint8_t *ciphertext,
2143 size_t ciphertext_size,
2144 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002145
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002146/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002147 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002148 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002149 * \param alg The AEAD algorithm to compute
2150 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002151 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002152 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002153 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002154 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002155 * but not encrypted.
2156 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002157 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002158 * encrypted. For algorithms where the
2159 * encrypted data and the authentication tag
2160 * are defined as separate inputs, the buffer
2161 * must contain the encrypted data followed
2162 * by the authentication tag.
2163 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002164 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002165 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2166 * This must be at least
2167 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2168 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002169 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002170 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002171 *
Gilles Peskine28538492018-07-11 17:34:00 +02002172 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002173 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002174 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002175 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002176 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002177 * \retval #PSA_ERROR_NOT_PERMITTED
2178 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002179 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002180 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002181 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002182 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002183 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2184 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002185 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2186 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002187 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002188 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002189 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002190 * The library has not been previously initialized by psa_crypto_init().
2191 * It is implementation-dependent whether a failure to initialize
2192 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002193 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002194psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002195 psa_algorithm_t alg,
2196 const uint8_t *nonce,
2197 size_t nonce_length,
2198 const uint8_t *additional_data,
2199 size_t additional_data_length,
2200 const uint8_t *ciphertext,
2201 size_t ciphertext_length,
2202 uint8_t *plaintext,
2203 size_t plaintext_size,
2204 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002205
Gilles Peskine30a9e412019-01-14 18:36:12 +01002206/** The type of the state data structure for multipart AEAD operations.
2207 *
2208 * Before calling any function on an AEAD operation object, the application
2209 * must initialize it by any of the following means:
2210 * - Set the structure to all-bits-zero, for example:
2211 * \code
2212 * psa_aead_operation_t operation;
2213 * memset(&operation, 0, sizeof(operation));
2214 * \endcode
2215 * - Initialize the structure to logical zero values, for example:
2216 * \code
2217 * psa_aead_operation_t operation = {0};
2218 * \endcode
2219 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2220 * for example:
2221 * \code
2222 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2223 * \endcode
2224 * - Assign the result of the function psa_aead_operation_init()
2225 * to the structure, for example:
2226 * \code
2227 * psa_aead_operation_t operation;
2228 * operation = psa_aead_operation_init();
2229 * \endcode
2230 *
2231 * This is an implementation-defined \c struct. Applications should not
2232 * make any assumptions about the content of this structure except
2233 * as directed by the documentation of a specific implementation. */
2234typedef struct psa_aead_operation_s psa_aead_operation_t;
2235
2236/** \def PSA_AEAD_OPERATION_INIT
2237 *
2238 * This macro returns a suitable initializer for an AEAD operation object of
2239 * type #psa_aead_operation_t.
2240 */
2241#ifdef __DOXYGEN_ONLY__
2242/* This is an example definition for documentation purposes.
2243 * Implementations should define a suitable value in `crypto_struct.h`.
2244 */
2245#define PSA_AEAD_OPERATION_INIT {0}
2246#endif
2247
2248/** Return an initial value for an AEAD operation object.
2249 */
2250static psa_aead_operation_t psa_aead_operation_init(void);
2251
2252/** Set the key for a multipart authenticated encryption operation.
2253 *
2254 * The sequence of operations to encrypt a message with authentication
2255 * is as follows:
2256 * -# Allocate an operation object which will be passed to all the functions
2257 * listed here.
2258 * -# Initialize the operation object with one of the methods described in the
2259 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002260 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002261 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002262 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2263 * inputs to the subsequent calls to psa_aead_update_ad() and
2264 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2265 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002266 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2267 * generate or set the nonce. You should use
2268 * psa_aead_generate_nonce() unless the protocol you are implementing
2269 * requires a specific nonce value.
2270 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2271 * of the non-encrypted additional authenticated data each time.
2272 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002273 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002274 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002275 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002276 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2277 * the operation will need to be reset by a call to psa_aead_abort(). The
2278 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002279 * has been initialized.
2280 *
2281 * After a successful call to psa_aead_encrypt_setup(), the application must
2282 * eventually terminate the operation. The following events terminate an
2283 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002284 * - A successful call to psa_aead_finish().
2285 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002286 *
2287 * \param[in,out] operation The operation object to set up. It must have
2288 * been initialized as per the documentation for
2289 * #psa_aead_operation_t and not yet in use.
2290 * \param handle Handle to the key to use for the operation.
2291 * It must remain valid until the operation
2292 * terminates.
2293 * \param alg The AEAD algorithm to compute
2294 * (\c PSA_ALG_XXX value such that
2295 * #PSA_ALG_IS_AEAD(\p alg) is true).
2296 *
2297 * \retval #PSA_SUCCESS
2298 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002299 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002300 * The operation state is not valid (it must be inactive).
Andrew Thoelke340984b2019-09-11 21:33:41 +01002301 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002302 * \retval #PSA_ERROR_NOT_PERMITTED
2303 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002304 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002305 * \retval #PSA_ERROR_NOT_SUPPORTED
2306 * \p alg is not supported or is not an AEAD algorithm.
2307 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2308 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2309 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002310 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002311 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002312 * \retval #PSA_ERROR_BAD_STATE
2313 * The library has not been previously initialized by psa_crypto_init().
2314 * It is implementation-dependent whether a failure to initialize
2315 * results in this error code.
2316 */
2317psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2318 psa_key_handle_t handle,
2319 psa_algorithm_t alg);
2320
2321/** Set the key for a multipart authenticated decryption operation.
2322 *
2323 * The sequence of operations to decrypt a message with authentication
2324 * is as follows:
2325 * -# Allocate an operation object which will be passed to all the functions
2326 * listed here.
2327 * -# Initialize the operation object with one of the methods described in the
2328 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002329 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002330 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002331 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2332 * inputs to the subsequent calls to psa_aead_update_ad() and
2333 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2334 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002335 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2336 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2337 * of the non-encrypted additional authenticated data each time.
2338 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002339 * of the ciphertext to decrypt each time.
2340 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002341 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002342 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2343 * the operation will need to be reset by a call to psa_aead_abort(). The
2344 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002345 * has been initialized.
2346 *
2347 * After a successful call to psa_aead_decrypt_setup(), the application must
2348 * eventually terminate the operation. The following events terminate an
2349 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002350 * - A successful call to psa_aead_verify().
2351 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002352 *
2353 * \param[in,out] operation The operation object to set up. It must have
2354 * been initialized as per the documentation for
2355 * #psa_aead_operation_t and not yet in use.
2356 * \param handle Handle to the key to use for the operation.
2357 * It must remain valid until the operation
2358 * terminates.
2359 * \param alg The AEAD algorithm to compute
2360 * (\c PSA_ALG_XXX value such that
2361 * #PSA_ALG_IS_AEAD(\p alg) is true).
2362 *
2363 * \retval #PSA_SUCCESS
2364 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002365 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002366 * The operation state is not valid (it must be inactive).
Andrew Thoelke340984b2019-09-11 21:33:41 +01002367 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002368 * \retval #PSA_ERROR_NOT_PERMITTED
2369 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002370 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002371 * \retval #PSA_ERROR_NOT_SUPPORTED
2372 * \p alg is not supported or is not an AEAD algorithm.
2373 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2374 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2375 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002376 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002377 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002378 * \retval #PSA_ERROR_BAD_STATE
2379 * The library has not been previously initialized by psa_crypto_init().
2380 * It is implementation-dependent whether a failure to initialize
2381 * results in this error code.
2382 */
2383psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2384 psa_key_handle_t handle,
2385 psa_algorithm_t alg);
2386
2387/** Generate a random nonce for an authenticated encryption operation.
2388 *
2389 * This function generates a random nonce for the authenticated encryption
2390 * operation with an appropriate size for the chosen algorithm, key type
2391 * and key size.
2392 *
2393 * The application must call psa_aead_encrypt_setup() before
2394 * calling this function.
2395 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002396 * If this function returns an error status, the operation enters an error
2397 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002398 *
2399 * \param[in,out] operation Active AEAD operation.
2400 * \param[out] nonce Buffer where the generated nonce is to be
2401 * written.
2402 * \param nonce_size Size of the \p nonce buffer in bytes.
2403 * \param[out] nonce_length On success, the number of bytes of the
2404 * generated nonce.
2405 *
2406 * \retval #PSA_SUCCESS
2407 * Success.
2408 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002409 * The operation state is not valid (it must be an active aead encrypt
2410 operation, with no nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002411 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2412 * The size of the \p nonce buffer is too small.
2413 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2414 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2415 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002416 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002417 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002418 * \retval #PSA_ERROR_BAD_STATE
2419 * The library has not been previously initialized by psa_crypto_init().
2420 * It is implementation-dependent whether a failure to initialize
2421 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002422 */
2423psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002424 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002425 size_t nonce_size,
2426 size_t *nonce_length);
2427
2428/** Set the nonce for an authenticated encryption or decryption operation.
2429 *
2430 * This function sets the nonce for the authenticated
2431 * encryption or decryption operation.
2432 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002433 * The application must call psa_aead_encrypt_setup() or
2434 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002435 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002436 * If this function returns an error status, the operation enters an error
2437 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002438 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002439 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002440 * instead of this function, unless implementing a protocol that requires
2441 * a non-random IV.
2442 *
2443 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002444 * \param[in] nonce Buffer containing the nonce to use.
2445 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002446 *
2447 * \retval #PSA_SUCCESS
2448 * Success.
2449 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002450 * The operation state is not valid (it must be active, with no nonce
2451 * set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002452 * \retval #PSA_ERROR_INVALID_ARGUMENT
2453 * The size of \p nonce is not acceptable for the chosen algorithm.
2454 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2455 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2456 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002457 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002458 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002459 * \retval #PSA_ERROR_BAD_STATE
2460 * The library has not been previously initialized by psa_crypto_init().
2461 * It is implementation-dependent whether a failure to initialize
2462 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002463 */
2464psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002465 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002466 size_t nonce_length);
2467
Gilles Peskinebc59c852019-01-17 15:26:08 +01002468/** Declare the lengths of the message and additional data for AEAD.
2469 *
2470 * The application must call this function before calling
2471 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2472 * the operation requires it. If the algorithm does not require it,
2473 * calling this function is optional, but if this function is called
2474 * then the implementation must enforce the lengths.
2475 *
2476 * You may call this function before or after setting the nonce with
2477 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2478 *
2479 * - For #PSA_ALG_CCM, calling this function is required.
2480 * - For the other AEAD algorithms defined in this specification, calling
2481 * this function is not required.
2482 * - For vendor-defined algorithm, refer to the vendor documentation.
2483 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002484 * If this function returns an error status, the operation enters an error
2485 * state and must be aborted by calling psa_aead_abort().
2486 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002487 * \param[in,out] operation Active AEAD operation.
2488 * \param ad_length Size of the non-encrypted additional
2489 * authenticated data in bytes.
2490 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2491 *
2492 * \retval #PSA_SUCCESS
2493 * Success.
2494 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002495 * The operation state is not valid (it must be active, and
2496 * psa_aead_update_ad() and psa_aead_update() must not have been
2497 * called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002498 * \retval #PSA_ERROR_INVALID_ARGUMENT
2499 * At least one of the lengths is not acceptable for the chosen
2500 * algorithm.
2501 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2502 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2503 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002504 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002505 * \retval #PSA_ERROR_BAD_STATE
2506 * The library has not been previously initialized by psa_crypto_init().
2507 * It is implementation-dependent whether a failure to initialize
2508 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002509 */
2510psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2511 size_t ad_length,
2512 size_t plaintext_length);
2513
Gilles Peskine30a9e412019-01-14 18:36:12 +01002514/** Pass additional data to an active AEAD operation.
2515 *
2516 * Additional data is authenticated, but not encrypted.
2517 *
2518 * You may call this function multiple times to pass successive fragments
2519 * of the additional data. You may not call this function after passing
2520 * data to encrypt or decrypt with psa_aead_update().
2521 *
2522 * Before calling this function, you must:
2523 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2524 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2525 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002526 * If this function returns an error status, the operation enters an error
2527 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002528 *
2529 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2530 * there is no guarantee that the input is valid. Therefore, until
2531 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2532 * treat the input as untrusted and prepare to undo any action that
2533 * depends on the input if psa_aead_verify() returns an error status.
2534 *
2535 * \param[in,out] operation Active AEAD operation.
2536 * \param[in] input Buffer containing the fragment of
2537 * additional data.
2538 * \param input_length Size of the \p input buffer in bytes.
2539 *
2540 * \retval #PSA_SUCCESS
2541 * Success.
2542 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002543 * The operation state is not valid (it must be active, have a nonce
2544 * set, have lengths set if required by the algorithm, and
2545 * psa_aead_update() must not have been called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002546 * \retval #PSA_ERROR_INVALID_ARGUMENT
2547 * The total input length overflows the additional data length that
2548 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002549 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2550 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2551 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002552 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002553 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002554 * \retval #PSA_ERROR_BAD_STATE
2555 * The library has not been previously initialized by psa_crypto_init().
2556 * It is implementation-dependent whether a failure to initialize
2557 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002558 */
2559psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2560 const uint8_t *input,
2561 size_t input_length);
2562
2563/** Encrypt or decrypt a message fragment in an active AEAD operation.
2564 *
2565 * Before calling this function, you must:
2566 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2567 * The choice of setup function determines whether this function
2568 * encrypts or decrypts its input.
2569 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2570 * 3. Call psa_aead_update_ad() to pass all the additional data.
2571 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002572 * If this function returns an error status, the operation enters an error
2573 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002574 *
2575 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2576 * there is no guarantee that the input is valid. Therefore, until
2577 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2578 * - Do not use the output in any way other than storing it in a
2579 * confidential location. If you take any action that depends
2580 * on the tentative decrypted data, this action will need to be
2581 * undone if the input turns out not to be valid. Furthermore,
2582 * if an adversary can observe that this action took place
2583 * (for example through timing), they may be able to use this
2584 * fact as an oracle to decrypt any message encrypted with the
2585 * same key.
2586 * - In particular, do not copy the output anywhere but to a
2587 * memory or storage space that you have exclusive access to.
2588 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002589 * This function does not require the input to be aligned to any
2590 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002591 * a whole block at a time, it must consume all the input provided, but
2592 * it may delay the end of the corresponding output until a subsequent
2593 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2594 * provides sufficient input. The amount of data that can be delayed
2595 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002596 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002597 * \param[in,out] operation Active AEAD operation.
2598 * \param[in] input Buffer containing the message fragment to
2599 * encrypt or decrypt.
2600 * \param input_length Size of the \p input buffer in bytes.
2601 * \param[out] output Buffer where the output is to be written.
2602 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002603 * This must be at least
2604 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2605 * \p input_length) where \c alg is the
2606 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002607 * \param[out] output_length On success, the number of bytes
2608 * that make up the returned output.
2609 *
2610 * \retval #PSA_SUCCESS
2611 * Success.
2612 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002613 * The operation state is not valid (it must be active, have a nonce
2614 * set, and have lengths set if required by the algorithm).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002615 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2616 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002617 * You can determine a sufficient buffer size by calling
2618 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2619 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002620 * \retval #PSA_ERROR_INVALID_ARGUMENT
2621 * The total length of input to psa_aead_update_ad() so far is
2622 * less than the additional data length that was previously
2623 * specified with psa_aead_set_lengths().
2624 * \retval #PSA_ERROR_INVALID_ARGUMENT
2625 * The total input length overflows the plaintext length that
2626 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002627 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2628 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2629 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002630 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002631 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002632 * \retval #PSA_ERROR_BAD_STATE
2633 * The library has not been previously initialized by psa_crypto_init().
2634 * It is implementation-dependent whether a failure to initialize
2635 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002636 */
2637psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2638 const uint8_t *input,
2639 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002640 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002641 size_t output_size,
2642 size_t *output_length);
2643
2644/** Finish encrypting a message in an AEAD operation.
2645 *
2646 * The operation must have been set up with psa_aead_encrypt_setup().
2647 *
2648 * This function finishes the authentication of the additional data
2649 * formed by concatenating the inputs passed to preceding calls to
2650 * psa_aead_update_ad() with the plaintext formed by concatenating the
2651 * inputs passed to preceding calls to psa_aead_update().
2652 *
2653 * This function has two output buffers:
2654 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002655 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002656 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002657 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002658 * that the operation performs.
2659 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002660 * When this function returns successfuly, the operation becomes inactive.
2661 * If this function returns an error status, the operation enters an error
2662 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002663 *
2664 * \param[in,out] operation Active AEAD operation.
2665 * \param[out] ciphertext Buffer where the last part of the ciphertext
2666 * is to be written.
2667 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002668 * This must be at least
2669 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2670 * \c alg is the algorithm that is being
2671 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002672 * \param[out] ciphertext_length On success, the number of bytes of
2673 * returned ciphertext.
2674 * \param[out] tag Buffer where the authentication tag is
2675 * to be written.
2676 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002677 * This must be at least
2678 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2679 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002680 * \param[out] tag_length On success, the number of bytes
2681 * that make up the returned tag.
2682 *
2683 * \retval #PSA_SUCCESS
2684 * Success.
2685 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002686 * The operation state is not valid (it must be an active encryption
2687 * operation with a nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002688 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002689 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002690 * You can determine a sufficient buffer size for \p ciphertext by
2691 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2692 * where \c alg is the algorithm that is being calculated.
2693 * You can determine a sufficient buffer size for \p tag by
2694 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002695 * \retval #PSA_ERROR_INVALID_ARGUMENT
2696 * The total length of input to psa_aead_update_ad() so far is
2697 * less than the additional data length that was previously
2698 * specified with psa_aead_set_lengths().
2699 * \retval #PSA_ERROR_INVALID_ARGUMENT
2700 * The total length of input to psa_aead_update() so far is
2701 * less than the plaintext length that was previously
2702 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002703 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2704 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2705 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002706 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002707 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002708 * \retval #PSA_ERROR_BAD_STATE
2709 * The library has not been previously initialized by psa_crypto_init().
2710 * It is implementation-dependent whether a failure to initialize
2711 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002712 */
2713psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002714 uint8_t *ciphertext,
2715 size_t ciphertext_size,
2716 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002717 uint8_t *tag,
2718 size_t tag_size,
2719 size_t *tag_length);
2720
2721/** Finish authenticating and decrypting a message in an AEAD operation.
2722 *
2723 * The operation must have been set up with psa_aead_decrypt_setup().
2724 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002725 * This function finishes the authenticated decryption of the message
2726 * components:
2727 *
2728 * - The additional data consisting of the concatenation of the inputs
2729 * passed to preceding calls to psa_aead_update_ad().
2730 * - The ciphertext consisting of the concatenation of the inputs passed to
2731 * preceding calls to psa_aead_update().
2732 * - The tag passed to this function call.
2733 *
2734 * If the authentication tag is correct, this function outputs any remaining
2735 * plaintext and reports success. If the authentication tag is not correct,
2736 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002737 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002738 * When this function returns successfuly, the operation becomes inactive.
2739 * If this function returns an error status, the operation enters an error
2740 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002741 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002742 * \note Implementations shall make the best effort to ensure that the
2743 * comparison between the actual tag and the expected tag is performed
2744 * in constant time.
2745 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002746 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002747 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002748 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002749 * from previous calls to psa_aead_update()
2750 * that could not be processed until the end
2751 * of the input.
2752 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002753 * This must be at least
2754 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2755 * \c alg is the algorithm that is being
2756 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002757 * \param[out] plaintext_length On success, the number of bytes of
2758 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002759 * \param[in] tag Buffer containing the authentication tag.
2760 * \param tag_length Size of the \p tag buffer in bytes.
2761 *
2762 * \retval #PSA_SUCCESS
2763 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002764 * \retval #PSA_ERROR_INVALID_SIGNATURE
2765 * The calculations were successful, but the authentication tag is
2766 * not correct.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002767 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002768 * The operation state is not valid (it must be an active decryption
2769 * operation with a nonce set).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002770 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2771 * The size of the \p plaintext buffer is too small.
2772 * You can determine a sufficient buffer size for \p plaintext by
2773 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2774 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002775 * \retval #PSA_ERROR_INVALID_ARGUMENT
2776 * The total length of input to psa_aead_update_ad() so far is
2777 * less than the additional data length that was previously
2778 * specified with psa_aead_set_lengths().
2779 * \retval #PSA_ERROR_INVALID_ARGUMENT
2780 * The total length of input to psa_aead_update() so far is
2781 * less than the plaintext length that was previously
2782 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002783 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2784 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2785 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002786 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002787 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002788 * \retval #PSA_ERROR_BAD_STATE
2789 * The library has not been previously initialized by psa_crypto_init().
2790 * It is implementation-dependent whether a failure to initialize
2791 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002792 */
2793psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002794 uint8_t *plaintext,
2795 size_t plaintext_size,
2796 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002797 const uint8_t *tag,
2798 size_t tag_length);
2799
2800/** Abort an AEAD operation.
2801 *
2802 * Aborting an operation frees all associated resources except for the
2803 * \p operation structure itself. Once aborted, the operation object
2804 * can be reused for another operation by calling
2805 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2806 *
2807 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002808 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002809 *
2810 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002811 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2812 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002813 *
2814 * \param[in,out] operation Initialized AEAD operation.
2815 *
2816 * \retval #PSA_SUCCESS
Gilles Peskine30a9e412019-01-14 18:36:12 +01002817 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2818 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002819 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002820 * \retval #PSA_ERROR_BAD_STATE
2821 * The library has not been previously initialized by psa_crypto_init().
2822 * It is implementation-dependent whether a failure to initialize
2823 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002824 */
2825psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2826
Gilles Peskine3b555712018-03-03 21:27:57 +01002827/**@}*/
2828
Gilles Peskine20035e32018-02-03 22:44:14 +01002829/** \defgroup asymmetric Asymmetric cryptography
2830 * @{
2831 */
2832
2833/**
2834 * \brief Sign a hash or short message with a private key.
2835 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002836 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002837 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002838 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2839 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2840 * to determine the hash algorithm to use.
2841 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002842 * \param handle Handle to the key to use for the operation.
2843 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002844 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002845 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002846 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002847 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002848 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002849 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002850 * \param[out] signature_length On success, the number of bytes
2851 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002852 *
Gilles Peskine28538492018-07-11 17:34:00 +02002853 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002854 * \retval #PSA_ERROR_INVALID_HANDLE
2855 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002856 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002857 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002858 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002859 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002860 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002861 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002862 * \retval #PSA_ERROR_NOT_SUPPORTED
2863 * \retval #PSA_ERROR_INVALID_ARGUMENT
2864 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2865 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2866 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002867 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002868 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002869 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002870 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002871 * The library has not been previously initialized by psa_crypto_init().
2872 * It is implementation-dependent whether a failure to initialize
2873 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002874 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002875psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002876 psa_algorithm_t alg,
2877 const uint8_t *hash,
2878 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002879 uint8_t *signature,
2880 size_t signature_size,
2881 size_t *signature_length);
2882
2883/**
2884 * \brief Verify the signature a hash or short message using a public key.
2885 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002886 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002887 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002888 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2889 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2890 * to determine the hash algorithm to use.
2891 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002892 * \param handle Handle to the key to use for the operation.
2893 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002894 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002895 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002896 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002897 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002898 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002899 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002900 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002901 *
Gilles Peskine28538492018-07-11 17:34:00 +02002902 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002903 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002904 * \retval #PSA_ERROR_INVALID_HANDLE
2905 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002906 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002907 * The calculation was perfomed successfully, but the passed
2908 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002909 * \retval #PSA_ERROR_NOT_SUPPORTED
2910 * \retval #PSA_ERROR_INVALID_ARGUMENT
2911 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2912 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2913 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002914 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002915 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002916 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002917 * The library has not been previously initialized by psa_crypto_init().
2918 * It is implementation-dependent whether a failure to initialize
2919 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002920 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002921psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002922 psa_algorithm_t alg,
2923 const uint8_t *hash,
2924 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002925 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002926 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002927
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002928/**
2929 * \brief Encrypt a short message with a public key.
2930 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002931 * \param handle Handle to the key to use for the operation.
2932 * It must be a public key or an asymmetric
2933 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002934 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002935 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002936 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002937 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002938 * \param[in] salt A salt or label, if supported by the
2939 * encryption algorithm.
2940 * If the algorithm does not support a
2941 * salt, pass \c NULL.
2942 * If the algorithm supports an optional
2943 * salt and you do not want to pass a salt,
2944 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002945 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002946 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2947 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002948 * \param salt_length Size of the \p salt buffer in bytes.
2949 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002950 * \param[out] output Buffer where the encrypted message is to
2951 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002952 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002953 * \param[out] output_length On success, the number of bytes
2954 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002955 *
Gilles Peskine28538492018-07-11 17:34:00 +02002956 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002957 * \retval #PSA_ERROR_INVALID_HANDLE
2958 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002959 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002960 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002961 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002962 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002963 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002964 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002965 * \retval #PSA_ERROR_NOT_SUPPORTED
2966 * \retval #PSA_ERROR_INVALID_ARGUMENT
2967 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2968 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2969 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002970 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002971 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002972 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002973 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002974 * The library has not been previously initialized by psa_crypto_init().
2975 * It is implementation-dependent whether a failure to initialize
2976 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002977 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002978psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002979 psa_algorithm_t alg,
2980 const uint8_t *input,
2981 size_t input_length,
2982 const uint8_t *salt,
2983 size_t salt_length,
2984 uint8_t *output,
2985 size_t output_size,
2986 size_t *output_length);
2987
2988/**
2989 * \brief Decrypt a short message with a private key.
2990 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002991 * \param handle Handle to the key to use for the operation.
2992 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002993 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002994 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002995 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002996 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002997 * \param[in] salt A salt or label, if supported by the
2998 * encryption algorithm.
2999 * If the algorithm does not support a
3000 * salt, pass \c NULL.
3001 * If the algorithm supports an optional
3002 * salt and you do not want to pass a salt,
3003 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003004 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003005 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3006 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003007 * \param salt_length Size of the \p salt buffer in bytes.
3008 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003009 * \param[out] output Buffer where the decrypted message is to
3010 * be written.
3011 * \param output_size Size of the \c output buffer in bytes.
3012 * \param[out] output_length On success, the number of bytes
3013 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003014 *
Gilles Peskine28538492018-07-11 17:34:00 +02003015 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003016 * \retval #PSA_ERROR_INVALID_HANDLE
3017 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02003018 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003019 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003020 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003021 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003022 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003023 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02003024 * \retval #PSA_ERROR_NOT_SUPPORTED
3025 * \retval #PSA_ERROR_INVALID_ARGUMENT
3026 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3027 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3028 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003029 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003030 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003031 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3032 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003033 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003034 * The library has not been previously initialized by psa_crypto_init().
3035 * It is implementation-dependent whether a failure to initialize
3036 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003037 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003038psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003039 psa_algorithm_t alg,
3040 const uint8_t *input,
3041 size_t input_length,
3042 const uint8_t *salt,
3043 size_t salt_length,
3044 uint8_t *output,
3045 size_t output_size,
3046 size_t *output_length);
3047
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003048/**@}*/
3049
Gilles Peskine35675b62019-05-16 17:26:11 +02003050/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003051 * @{
3052 */
3053
Gilles Peskine35675b62019-05-16 17:26:11 +02003054/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003055 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003056 * Before calling any function on a key derivation operation object, the
3057 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003058 * - Set the structure to all-bits-zero, for example:
3059 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003060 * psa_key_derivation_operation_t operation;
3061 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003062 * \endcode
3063 * - Initialize the structure to logical zero values, for example:
3064 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003065 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003066 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003067 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003068 * for example:
3069 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003070 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003071 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003072 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003073 * to the structure, for example:
3074 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003075 * psa_key_derivation_operation_t operation;
3076 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003077 * \endcode
3078 *
3079 * This is an implementation-defined \c struct. Applications should not
3080 * make any assumptions about the content of this structure except
3081 * as directed by the documentation of a specific implementation.
3082 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003083typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003084
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003085/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003086 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003087 * This macro returns a suitable initializer for a key derivation operation
3088 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003089 */
3090#ifdef __DOXYGEN_ONLY__
3091/* This is an example definition for documentation purposes.
3092 * Implementations should define a suitable value in `crypto_struct.h`.
3093 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003094#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003095#endif
3096
Gilles Peskine35675b62019-05-16 17:26:11 +02003097/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003098 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003099static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003100
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003101/** Set up a key derivation operation.
3102 *
3103 * A key derivation algorithm takes some inputs and uses them to generate
3104 * a byte stream in a deterministic way.
3105 * This byte stream can be used to produce keys and other
3106 * cryptographic material.
3107 *
3108 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003109 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3110 * -# Call psa_key_derivation_setup() to select the algorithm.
3111 * -# Provide the inputs for the key derivation by calling
3112 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3113 * as appropriate. Which inputs are needed, in what order, and whether
3114 * they may be keys and if so of what type depends on the algorithm.
3115 * -# Optionally set the operation's maximum capacity with
3116 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3117 * of or after providing inputs. For some algorithms, this step is mandatory
3118 * because the output depends on the maximum capacity.
3119 * -# To derive a key, call psa_key_derivation_output_key().
3120 * To derive a byte string for a different purpose, call
3121 * psa_key_derivation_output_bytes().
3122 * Successive calls to these functions use successive output bytes
3123 * calculated by the key derivation algorithm.
3124 * -# Clean up the key derivation operation object with
3125 * psa_key_derivation_abort().
3126 *
3127 * If this function returns an error, the key derivation operation object is
3128 * not changed.
3129 *
3130 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3131 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003132 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003133 * Implementations must reject an attempt to derive a key of size 0.
3134 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003135 * \param[in,out] operation The key derivation operation object
3136 * to set up. It must
3137 * have been initialized but not set up yet.
3138 * \param alg The key derivation algorithm to compute
3139 * (\c PSA_ALG_XXX value such that
3140 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3141 *
3142 * \retval #PSA_SUCCESS
3143 * Success.
3144 * \retval #PSA_ERROR_INVALID_ARGUMENT
3145 * \c alg is not a key derivation algorithm.
3146 * \retval #PSA_ERROR_NOT_SUPPORTED
3147 * \c alg is not supported or is not a key derivation algorithm.
3148 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3149 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3150 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003151 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003152 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003153 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003154 * The operation state is not valid (it must be inactive).
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003155 * \retval #PSA_ERROR_BAD_STATE
3156 * The library has not been previously initialized by psa_crypto_init().
3157 * It is implementation-dependent whether a failure to initialize
3158 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003159 */
3160psa_status_t psa_key_derivation_setup(
3161 psa_key_derivation_operation_t *operation,
3162 psa_algorithm_t alg);
3163
Gilles Peskine35675b62019-05-16 17:26:11 +02003164/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003165 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003166 * The capacity of a key derivation is the maximum number of bytes that it can
3167 * return. When you get *N* bytes of output from a key derivation operation,
3168 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003169 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003170 * \param[in] operation The operation to query.
3171 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003172 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003173 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003174 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003175 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003176 * The operation state is not valid (it must be active).
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003177 * \retval #PSA_ERROR_HARDWARE_FAILURE
3178 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003179 * \retval #PSA_ERROR_BAD_STATE
3180 * The library has not been previously initialized by psa_crypto_init().
3181 * It is implementation-dependent whether a failure to initialize
3182 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003183 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003184psa_status_t psa_key_derivation_get_capacity(
3185 const psa_key_derivation_operation_t *operation,
3186 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003187
Gilles Peskine35675b62019-05-16 17:26:11 +02003188/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003189 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003190 * The capacity of a key derivation operation is the maximum number of bytes
3191 * that the key derivation operation can return from this point onwards.
3192 *
3193 * \param[in,out] operation The key derivation operation object to modify.
3194 * \param capacity The new capacity of the operation.
3195 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003196 * current capacity.
3197 *
3198 * \retval #PSA_SUCCESS
3199 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003200 * \p capacity is larger than the operation's current capacity.
3201 * In this case, the operation object remains valid and its capacity
3202 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003203 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003204 * The operation state is not valid (it must be active).
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003205 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003206 * \retval #PSA_ERROR_HARDWARE_FAILURE
3207 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003208 * \retval #PSA_ERROR_BAD_STATE
3209 * The library has not been previously initialized by psa_crypto_init().
3210 * It is implementation-dependent whether a failure to initialize
3211 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003212 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003213psa_status_t psa_key_derivation_set_capacity(
3214 psa_key_derivation_operation_t *operation,
3215 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003216
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003217/** Use the maximum possible capacity for a key derivation operation.
3218 *
3219 * Use this value as the capacity argument when setting up a key derivation
3220 * to indicate that the operation should have the maximum possible capacity.
3221 * The value of the maximum possible capacity depends on the key derivation
3222 * algorithm.
3223 */
3224#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3225
3226/** Provide an input for key derivation or key agreement.
3227 *
3228 * Which inputs are required and in what order depends on the algorithm.
3229 * Refer to the documentation of each key derivation or key agreement
3230 * algorithm for information.
3231 *
3232 * This function passes direct inputs. Some inputs must be passed as keys
3233 * using psa_key_derivation_input_key() instead of this function. Refer to
3234 * the documentation of individual step types for information.
3235 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003236 * If this function returns an error status, the operation enters an error
3237 * state and must be aborted by calling psa_key_derivation_abort().
3238 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003239 * \param[in,out] operation The key derivation operation object to use.
3240 * It must have been set up with
3241 * psa_key_derivation_setup() and must not
3242 * have produced any output yet.
3243 * \param step Which step the input data is for.
3244 * \param[in] data Input data to use.
3245 * \param data_length Size of the \p data buffer in bytes.
3246 *
3247 * \retval #PSA_SUCCESS
3248 * Success.
3249 * \retval #PSA_ERROR_INVALID_ARGUMENT
3250 * \c step is not compatible with the operation's algorithm.
3251 * \retval #PSA_ERROR_INVALID_ARGUMENT
3252 * \c step does not allow direct inputs.
3253 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3254 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3255 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003256 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003257 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003258 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003259 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003260 * \retval #PSA_ERROR_BAD_STATE
3261 * The library has not been previously initialized by psa_crypto_init().
3262 * It is implementation-dependent whether a failure to initialize
3263 * results in this error code.
3264 */
3265psa_status_t psa_key_derivation_input_bytes(
3266 psa_key_derivation_operation_t *operation,
3267 psa_key_derivation_step_t step,
3268 const uint8_t *data,
3269 size_t data_length);
3270
3271/** Provide an input for key derivation in the form of a key.
3272 *
3273 * Which inputs are required and in what order depends on the algorithm.
3274 * Refer to the documentation of each key derivation or key agreement
3275 * algorithm for information.
3276 *
3277 * This function passes key inputs. Some inputs must be passed as keys
3278 * of the appropriate type using this function, while others must be
3279 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3280 * the documentation of individual step types for information.
3281 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003282 * If this function returns an error status, the operation enters an error
3283 * state and must be aborted by calling psa_key_derivation_abort().
3284 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003285 * \param[in,out] operation The key derivation operation object to use.
3286 * It must have been set up with
3287 * psa_key_derivation_setup() and must not
3288 * have produced any output yet.
3289 * \param step Which step the input data is for.
3290 * \param handle Handle to the key. It must have an
3291 * appropriate type for \p step and must
3292 * allow the usage #PSA_KEY_USAGE_DERIVE.
3293 *
3294 * \retval #PSA_SUCCESS
3295 * Success.
3296 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003297 * \retval #PSA_ERROR_NOT_PERMITTED
3298 * \retval #PSA_ERROR_INVALID_ARGUMENT
3299 * \c step is not compatible with the operation's algorithm.
3300 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine224b0d62019-09-23 18:13:17 +02003301 * \c step does not allow key inputs of the given type
3302 * or does not allow key inputs at all.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003303 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3304 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3305 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003306 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003307 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003308 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003309 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003310 * \retval #PSA_ERROR_BAD_STATE
3311 * The library has not been previously initialized by psa_crypto_init().
3312 * It is implementation-dependent whether a failure to initialize
3313 * results in this error code.
3314 */
3315psa_status_t psa_key_derivation_input_key(
3316 psa_key_derivation_operation_t *operation,
3317 psa_key_derivation_step_t step,
3318 psa_key_handle_t handle);
3319
3320/** Perform a key agreement and use the shared secret as input to a key
3321 * derivation.
3322 *
3323 * A key agreement algorithm takes two inputs: a private key \p private_key
3324 * a public key \p peer_key.
3325 * The result of this function is passed as input to a key derivation.
3326 * The output of this key derivation can be extracted by reading from the
3327 * resulting operation to produce keys and other cryptographic material.
3328 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003329 * If this function returns an error status, the operation enters an error
3330 * state and must be aborted by calling psa_key_derivation_abort().
3331 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003332 * \param[in,out] operation The key derivation operation object to use.
3333 * It must have been set up with
3334 * psa_key_derivation_setup() with a
3335 * key agreement and derivation algorithm
3336 * \c alg (\c PSA_ALG_XXX value such that
3337 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3338 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3339 * is false).
3340 * The operation must be ready for an
3341 * input of the type given by \p step.
3342 * \param step Which step the input data is for.
3343 * \param private_key Handle to the private key to use.
3344 * \param[in] peer_key Public key of the peer. The peer key must be in the
3345 * same format that psa_import_key() accepts for the
3346 * public key type corresponding to the type of
3347 * private_key. That is, this function performs the
3348 * equivalent of
3349 * #psa_import_key(...,
3350 * `peer_key`, `peer_key_length`) where
3351 * with key attributes indicating the public key
3352 * type corresponding to the type of `private_key`.
3353 * For example, for EC keys, this means that peer_key
3354 * is interpreted as a point on the curve that the
3355 * private key is on. The standard formats for public
3356 * keys are documented in the documentation of
3357 * psa_export_public_key().
3358 * \param peer_key_length Size of \p peer_key in bytes.
3359 *
3360 * \retval #PSA_SUCCESS
3361 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003362 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003363 * The operation state is not valid for this key agreement \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003364 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003365 * \retval #PSA_ERROR_NOT_PERMITTED
3366 * \retval #PSA_ERROR_INVALID_ARGUMENT
3367 * \c private_key is not compatible with \c alg,
3368 * or \p peer_key is not valid for \c alg or not compatible with
3369 * \c private_key.
3370 * \retval #PSA_ERROR_NOT_SUPPORTED
3371 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskine224b0d62019-09-23 18:13:17 +02003372 * \retval #PSA_ERROR_INVALID_ARGUMENT
3373 * \c step does not allow an input resulting from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003374 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3375 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3376 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003377 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003378 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003379 * \retval #PSA_ERROR_BAD_STATE
3380 * The library has not been previously initialized by psa_crypto_init().
3381 * It is implementation-dependent whether a failure to initialize
3382 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003383 */
3384psa_status_t psa_key_derivation_key_agreement(
3385 psa_key_derivation_operation_t *operation,
3386 psa_key_derivation_step_t step,
3387 psa_key_handle_t private_key,
3388 const uint8_t *peer_key,
3389 size_t peer_key_length);
3390
Gilles Peskine35675b62019-05-16 17:26:11 +02003391/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003392 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003393 * This function calculates output bytes from a key derivation algorithm and
3394 * return those bytes.
3395 * If you view the key derivation's output as a stream of bytes, this
3396 * function destructively reads the requested number of bytes from the
3397 * stream.
3398 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003399 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003400 * If this function returns an error status other than
3401 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003402 * state and must be aborted by calling psa_key_derivation_abort().
3403 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003404 * \param[in,out] operation The key derivation operation object to read from.
3405 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003406 * \param output_length Number of bytes to output.
3407 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003408 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003409 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003410 * The operation's capacity was less than
3411 * \p output_length bytes. Note that in this case,
3412 * no output is written to the output buffer.
3413 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003414 * subsequent calls to this function will not
3415 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003416 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003417 * The operation state is not valid (it must be active and completed
3418 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003419 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3420 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3421 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003422 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003423 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003424 * \retval #PSA_ERROR_BAD_STATE
3425 * The library has not been previously initialized by psa_crypto_init().
3426 * It is implementation-dependent whether a failure to initialize
3427 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003428 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003429psa_status_t psa_key_derivation_output_bytes(
3430 psa_key_derivation_operation_t *operation,
3431 uint8_t *output,
3432 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003433
Gilles Peskine35675b62019-05-16 17:26:11 +02003434/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003435 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003436 * This function calculates output bytes from a key derivation algorithm
3437 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003438 * The key's location, usage policy, type and size are taken from
3439 * \p attributes.
3440 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003441 * If you view the key derivation's output as a stream of bytes, this
3442 * function destructively reads as many bytes as required from the
3443 * stream.
3444 * The operation's capacity decreases by the number of bytes read.
3445 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003446 * If this function returns an error status other than
3447 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003448 * state and must be aborted by calling psa_key_derivation_abort().
3449 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003450 * How much output is produced and consumed from the operation, and how
3451 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003452 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003453 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003454 * of a given size, this function is functionally equivalent to
3455 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003456 * and passing the resulting output to #psa_import_key.
3457 * However, this function has a security benefit:
3458 * if the implementation provides an isolation boundary then
3459 * the key material is not exposed outside the isolation boundary.
3460 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003461 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003462 * The following key types defined in this specification follow this scheme:
3463 *
3464 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003465 * - #PSA_KEY_TYPE_ARC4;
3466 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003467 * - #PSA_KEY_TYPE_DERIVE;
3468 * - #PSA_KEY_TYPE_HMAC.
3469 *
3470 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003471 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003472 * Montgomery curve), this function always draws a byte string whose
3473 * length is determined by the curve, and sets the mandatory bits
3474 * accordingly. That is:
3475 *
3476 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
3477 * and process it as specified in RFC 7748 &sect;5.
3478 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
3479 * and process it as specified in RFC 7748 &sect;5.
3480 *
3481 * - For key types for which the key is represented by a single sequence of
3482 * \p bits bits with constraints as to which bit sequences are acceptable,
3483 * this function draws a byte string of length (\p bits / 8) bytes rounded
3484 * up to the nearest whole number of bytes. If the resulting byte string
3485 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3486 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003487 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003488 * for the output produced by psa_export_key().
3489 * The following key types defined in this specification follow this scheme:
3490 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003491 * - #PSA_KEY_TYPE_DES.
3492 * Force-set the parity bits, but discard forbidden weak keys.
3493 * For 2-key and 3-key triple-DES, the three keys are generated
3494 * successively (for example, for 3-key triple-DES,
3495 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3496 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003497 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003498 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003499 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003500 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003501 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003502 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003503 * Weierstrass curve).
3504 * For these key types, interpret the byte string as integer
3505 * in big-endian order. Discard it if it is not in the range
3506 * [0, *N* - 2] where *N* is the boundary of the private key domain
3507 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003508 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003509 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003510 * This method allows compliance to NIST standards, specifically
3511 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003512 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3513 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3514 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3515 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003516 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003517 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003518 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003519 * implementation-defined.
3520 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003521 * In all cases, the data that is read is discarded from the operation.
3522 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003523 *
Gilles Peskine20628592019-04-19 19:29:50 +02003524 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003525 * \param[in,out] operation The key derivation operation object to read from.
Gilles Peskine20628592019-04-19 19:29:50 +02003526 * \param[out] handle On success, a handle to the newly created key.
3527 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003528 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003529 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003530 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003531 * If the key is persistent, the key material and the key's metadata
3532 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003533 * \retval #PSA_ERROR_ALREADY_EXISTS
3534 * This is an attempt to create a persistent key, and there is
3535 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003536 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003537 * There was not enough data to create the desired key.
3538 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003539 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003540 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003541 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003542 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003543 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003544 * \retval #PSA_ERROR_INVALID_ARGUMENT
3545 * The provided key attributes are not valid for the operation.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003546 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003547 * The operation state is not valid (it must be active and completed
3548 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003549 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3550 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3551 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3552 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003553 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003554 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003555 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003556 * The library has not been previously initialized by psa_crypto_init().
3557 * It is implementation-dependent whether a failure to initialize
3558 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003559 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003560psa_status_t psa_key_derivation_output_key(
3561 const psa_key_attributes_t *attributes,
3562 psa_key_derivation_operation_t *operation,
3563 psa_key_handle_t *handle);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003564
Gilles Peskine35675b62019-05-16 17:26:11 +02003565/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003566 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003567 * Aborting an operation frees all associated resources except for the \c
3568 * operation structure itself. Once aborted, the operation object can be reused
3569 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003570 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003571 * This function may be called at any time after the operation
3572 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003573 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003574 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3575 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003576 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003577 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003578 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003579 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003580 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3581 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003582 * \retval #PSA_ERROR_CORRUPTION_DETECTED
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 Peskineeab56e42018-07-12 17:12:33 +02003587 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003588psa_status_t psa_key_derivation_abort(
3589 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003590
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003591/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003592 *
3593 * \warning The raw result of a key agreement algorithm such as finite-field
3594 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3595 * not be used directly as key material. It should instead be passed as
3596 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003597 * a key derivation, use psa_key_derivation_key_agreement() and other
3598 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003599 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003600 * \param alg The key agreement algorithm to compute
3601 * (\c PSA_ALG_XXX value such that
3602 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3603 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003604 * \param private_key Handle to the private key to use.
3605 * \param[in] peer_key Public key of the peer. It must be
3606 * in the same format that psa_import_key()
3607 * accepts. The standard formats for public
3608 * keys are documented in the documentation
3609 * of psa_export_public_key().
3610 * \param peer_key_length Size of \p peer_key in bytes.
3611 * \param[out] output Buffer where the decrypted message is to
3612 * be written.
3613 * \param output_size Size of the \c output buffer in bytes.
3614 * \param[out] output_length On success, the number of bytes
3615 * that make up the returned output.
3616 *
3617 * \retval #PSA_SUCCESS
3618 * Success.
3619 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003620 * \retval #PSA_ERROR_NOT_PERMITTED
3621 * \retval #PSA_ERROR_INVALID_ARGUMENT
3622 * \p alg is not a key agreement algorithm
3623 * \retval #PSA_ERROR_INVALID_ARGUMENT
3624 * \p private_key is not compatible with \p alg,
3625 * or \p peer_key is not valid for \p alg or not compatible with
3626 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003627 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3628 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003629 * \retval #PSA_ERROR_NOT_SUPPORTED
3630 * \p alg is not a supported key agreement algorithm.
3631 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3632 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3633 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003634 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003635 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003636 * \retval #PSA_ERROR_BAD_STATE
3637 * The library has not been previously initialized by psa_crypto_init().
3638 * It is implementation-dependent whether a failure to initialize
3639 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003640 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003641psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
3642 psa_key_handle_t private_key,
3643 const uint8_t *peer_key,
3644 size_t peer_key_length,
3645 uint8_t *output,
3646 size_t output_size,
3647 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003648
Gilles Peskineea0fb492018-07-12 17:17:20 +02003649/**@}*/
3650
Gilles Peskineedd76872018-07-20 17:42:05 +02003651/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003652 * @{
3653 */
3654
3655/**
3656 * \brief Generate random bytes.
3657 *
3658 * \warning This function **can** fail! Callers MUST check the return status
3659 * and MUST NOT use the content of the output buffer if the return
3660 * status is not #PSA_SUCCESS.
3661 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003662 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003663 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003664 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003665 * \param output_size Number of bytes to generate and output.
3666 *
Gilles Peskine28538492018-07-11 17:34:00 +02003667 * \retval #PSA_SUCCESS
3668 * \retval #PSA_ERROR_NOT_SUPPORTED
3669 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003670 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003671 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3672 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003673 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003674 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003675 * The library has not been previously initialized by psa_crypto_init().
3676 * It is implementation-dependent whether a failure to initialize
3677 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003678 */
3679psa_status_t psa_generate_random(uint8_t *output,
3680 size_t output_size);
3681
3682/**
3683 * \brief Generate a key or key pair.
3684 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003685 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003686 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003687 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003688 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003689 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003690 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003691 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003692 * the public exponent is 65537.
3693 * The modulus is a product of two probabilistic primes
3694 * between 2^{n-1} and 2^n where n is the bit size specified in the
3695 * attributes.
3696 *
Gilles Peskine20628592019-04-19 19:29:50 +02003697 * \param[in] attributes The attributes for the new key.
Gilles Peskine20628592019-04-19 19:29:50 +02003698 * \param[out] handle On success, a handle to the newly created key.
3699 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003700 *
Gilles Peskine28538492018-07-11 17:34:00 +02003701 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003702 * Success.
3703 * If the key is persistent, the key material and the key's metadata
3704 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003705 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003706 * This is an attempt to create a persistent key, and there is
3707 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003708 * \retval #PSA_ERROR_NOT_SUPPORTED
3709 * \retval #PSA_ERROR_INVALID_ARGUMENT
3710 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3711 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3712 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3713 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003714 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003715 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3716 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003717 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003718 * The library has not been previously initialized by psa_crypto_init().
3719 * It is implementation-dependent whether a failure to initialize
3720 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003721 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003722psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02003723 psa_key_handle_t *handle);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003724
3725/**@}*/
3726
Gilles Peskinee59236f2018-01-27 23:32:46 +01003727#ifdef __cplusplus
3728}
3729#endif
3730
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003731/* The file "crypto_sizes.h" contains definitions for size calculation
3732 * macros whose definitions are implementation-specific. */
3733#include "crypto_sizes.h"
3734
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003735/* The file "crypto_struct.h" contains definitions for
3736 * implementation-specific structs that are declared above. */
3737#include "crypto_struct.h"
3738
3739/* The file "crypto_extra.h" contains vendor-specific definitions. This
3740 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003741#include "crypto_extra.h"
3742
3743#endif /* PSA_CRYPTO_H */