blob: 2620af5ba32019c9c683988d86373f4c74e322e4 [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/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02006 * Copyright The Mbed TLS Contributors
Jaeden Amerocab54942018-07-25 13:26:13 +01007 * 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
Andrew Thoelke02b372b2019-10-02 09:32:21 +010060/** \defgroup version API version
Adrian L. Shawd89338a2019-09-19 13:32:57 +010061 * @{
62 */
63
64/**
65 * The major version of this implementation of the PSA Crypto API
66 */
67#define PSA_CRYPTO_API_VERSION_MAJOR 1
68
69/**
70 * The minor version of this implementation of the PSA Crypto API
71 */
72#define PSA_CRYPTO_API_VERSION_MINOR 0
73
74/**@}*/
75
Gilles Peskinef3b731e2018-12-12 13:38:31 +010076/* The file "crypto_values.h" declares macros to build and analyze values
77 * of integral types defined in "crypto_types.h". */
78#include "crypto_values.h"
79
80/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010081 * @{
82 */
83
84/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010085 * \brief Library initialization.
86 *
87 * Applications must call this function before calling any other
88 * function in this module.
89 *
90 * Applications may call this function more than once. Once a call
91 * succeeds, subsequent calls are guaranteed to succeed.
92 *
itayzafrir18617092018-09-16 12:22:41 +030093 * If the application calls other functions before calling psa_crypto_init(),
94 * the behavior is undefined. Implementations are encouraged to either perform
95 * the operation as if the library had been initialized or to return
96 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
97 * implementations should not return a success status if the lack of
98 * initialization may have security implications, for example due to improper
99 * seeding of the random number generator.
100 *
Gilles Peskine28538492018-07-11 17:34:00 +0200101 * \retval #PSA_SUCCESS
102 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
103 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
104 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200105 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Gilles Peskine28538492018-07-11 17:34:00 +0200106 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100107 */
108psa_status_t psa_crypto_init(void);
109
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100110/**@}*/
111
Gilles Peskine105f67f2019-07-23 18:16:05 +0200112/** \addtogroup attributes
Gilles Peskine87a5e562019-04-17 12:28:25 +0200113 * @{
114 */
115
Gilles Peskinea0c06552019-05-21 15:54:54 +0200116/** \def PSA_KEY_ATTRIBUTES_INIT
117 *
118 * This macro returns a suitable initializer for a key attribute structure
119 * of type #psa_key_attributes_t.
120 */
121#ifdef __DOXYGEN_ONLY__
122/* This is an example definition for documentation purposes.
123 * Implementations should define a suitable value in `crypto_struct.h`.
124 */
125#define PSA_KEY_ATTRIBUTES_INIT {0}
126#endif
127
128/** Return an initial value for a key attributes structure.
129 */
130static psa_key_attributes_t psa_key_attributes_init(void);
131
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200132/** Declare a key as persistent and set its key identifier.
Gilles Peskine20628592019-04-19 19:29:50 +0200133 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200134 * If the attribute structure currently declares the key as volatile (which
135 * is the default content of an attribute structure), this function sets
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200136 * the lifetime attribute to #PSA_KEY_LIFETIME_PERSISTENT.
Gilles Peskine20628592019-04-19 19:29:50 +0200137 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200138 * This function does not access storage, it merely stores the given
139 * value in the structure.
140 * The persistent key will be written to storage when the attribute
141 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200142 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200143 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskine20628592019-04-19 19:29:50 +0200144 *
Gilles Peskine20628592019-04-19 19:29:50 +0200145 * This function may be declared as `static` (i.e. without external
146 * linkage). This function may be provided as a function-like macro,
147 * but in this case it must evaluate each of its arguments exactly once.
148 *
Ronald Cron27238fc2020-07-23 12:30:41 +0200149 * \param[out] attributes The attribute structure to write to.
150 * \param key The persistent identifier for the key.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200151 */
Ronald Cron71016a92020-08-28 19:01:50 +0200152static void psa_set_key_id( psa_key_attributes_t *attributes,
153 mbedtls_svc_key_id_t key );
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200154
155/** Set the location of a persistent key.
156 *
157 * To make a key persistent, you must give it a persistent key identifier
Gilles Peskinef1b76942019-05-16 16:10:59 +0200158 * with psa_set_key_id(). By default, a key that has a persistent identifier
159 * is stored in the default storage area identifier by
160 * #PSA_KEY_LIFETIME_PERSISTENT. Call this function to choose a storage
161 * area, or to explicitly declare the key as volatile.
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200162 *
Gilles Peskinef1b76942019-05-16 16:10:59 +0200163 * This function does not access storage, it merely stores the given
164 * value in the structure.
165 * The persistent key will be written to storage when the attribute
166 * structure is passed to a key creation function such as
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200167 * psa_import_key(), psa_generate_key(),
Gilles Peskinea99d3fb2019-05-16 15:28:51 +0200168 * psa_key_derivation_output_key() or psa_copy_key().
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200169 *
170 * This function may be declared as `static` (i.e. without external
171 * linkage). This function may be provided as a function-like macro,
172 * but in this case it must evaluate each of its arguments exactly once.
173 *
174 * \param[out] attributes The attribute structure to write to.
Gilles Peskine20628592019-04-19 19:29:50 +0200175 * \param lifetime The lifetime for the key.
176 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200177 * key will be volatile, and the key identifier
178 * attribute is reset to 0.
Gilles Peskine20628592019-04-19 19:29:50 +0200179 */
Gilles Peskinedc8219a2019-05-15 16:11:15 +0200180static void psa_set_key_lifetime(psa_key_attributes_t *attributes,
181 psa_key_lifetime_t lifetime);
Gilles Peskine4747d192019-04-17 15:05:45 +0200182
Gilles Peskine20628592019-04-19 19:29:50 +0200183/** Retrieve the key identifier from key attributes.
184 *
185 * This function may be declared as `static` (i.e. without external
186 * linkage). This function may be provided as a function-like macro,
187 * but in this case it must evaluate its argument exactly once.
188 *
189 * \param[in] attributes The key attribute structure to query.
190 *
191 * \return The persistent identifier stored in the attribute structure.
192 * This value is unspecified if the attribute structure declares
193 * the key as volatile.
194 */
Ronald Cron71016a92020-08-28 19:01:50 +0200195static mbedtls_svc_key_id_t psa_get_key_id(
196 const psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200197
Gilles Peskine20628592019-04-19 19:29:50 +0200198/** Retrieve the lifetime from key attributes.
199 *
200 * This function may be declared as `static` (i.e. without external
201 * linkage). This function may be provided as a function-like macro,
202 * but in this case it must evaluate its argument exactly once.
203 *
204 * \param[in] attributes The key attribute structure to query.
205 *
206 * \return The lifetime value stored in the attribute structure.
207 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200208static psa_key_lifetime_t psa_get_key_lifetime(
209 const psa_key_attributes_t *attributes);
210
Gilles Peskine20628592019-04-19 19:29:50 +0200211/** Declare usage flags for a key.
212 *
213 * Usage flags are part of a key's usage policy. They encode what
214 * kind of operations are permitted on the key. For more details,
215 * refer to the documentation of the type #psa_key_usage_t.
216 *
217 * This function overwrites any usage flags
218 * previously set in \p attributes.
219 *
220 * This function may be declared as `static` (i.e. without external
221 * linkage). This function may be provided as a function-like macro,
222 * but in this case it must evaluate each of its arguments exactly once.
223 *
224 * \param[out] attributes The attribute structure to write to.
225 * \param usage_flags The usage flags to write.
226 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200227static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
228 psa_key_usage_t usage_flags);
229
Gilles Peskine20628592019-04-19 19:29:50 +0200230/** Retrieve the usage flags from key attributes.
231 *
232 * This function may be declared as `static` (i.e. without external
233 * linkage). This function may be provided as a function-like macro,
234 * but in this case it must evaluate its argument exactly once.
235 *
236 * \param[in] attributes The key attribute structure to query.
237 *
238 * \return The usage flags stored in the attribute structure.
239 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200240static psa_key_usage_t psa_get_key_usage_flags(
241 const psa_key_attributes_t *attributes);
242
Gilles Peskine20628592019-04-19 19:29:50 +0200243/** Declare the permitted algorithm policy for a key.
244 *
245 * The permitted algorithm policy of a key encodes which algorithm or
Gilles Peskinea170d922019-09-12 16:59:37 +0200246 * algorithms are permitted to be used with this key. The following
247 * algorithm policies are supported:
248 * - 0 does not allow any cryptographic operation with the key. The key
249 * may be used for non-cryptographic actions such as exporting (if
250 * permitted by the usage flags).
251 * - An algorithm value permits this particular algorithm.
252 * - An algorithm wildcard built from #PSA_ALG_ANY_HASH allows the specified
253 * signature scheme with any hash algorithm.
Gilles Peskine20628592019-04-19 19:29:50 +0200254 *
255 * This function overwrites any algorithm policy
256 * previously set in \p attributes.
257 *
258 * This function may be declared as `static` (i.e. without external
259 * linkage). This function may be provided as a function-like macro,
260 * but in this case it must evaluate each of its arguments exactly once.
261 *
262 * \param[out] attributes The attribute structure to write to.
263 * \param alg The permitted algorithm policy to write.
264 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200265static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
266 psa_algorithm_t alg);
267
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100268
Gilles Peskine20628592019-04-19 19:29:50 +0200269/** Retrieve the algorithm policy from key attributes.
270 *
271 * This function may be declared as `static` (i.e. without external
272 * linkage). This function may be provided as a function-like macro,
273 * but in this case it must evaluate its argument exactly once.
274 *
275 * \param[in] attributes The key attribute structure to query.
276 *
277 * \return The algorithm stored in the attribute structure.
278 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200279static psa_algorithm_t psa_get_key_algorithm(
280 const psa_key_attributes_t *attributes);
281
Gilles Peskine20628592019-04-19 19:29:50 +0200282/** Declare the type of a key.
283 *
Gilles Peskine24f10f82019-05-16 12:18:32 +0200284 * This function overwrites any key type
Gilles Peskine20628592019-04-19 19:29:50 +0200285 * 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 type The key type to write.
Gilles Peskinea170d922019-09-12 16:59:37 +0200293 * If this is 0, the key type in \p attributes
294 * becomes unspecified.
Gilles Peskine20628592019-04-19 19:29:50 +0200295 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200296static void psa_set_key_type(psa_key_attributes_t *attributes,
297 psa_key_type_t type);
298
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +0100299
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200300/** Declare the size of a key.
301 *
302 * This function overwrites any key size previously set in \p attributes.
303 *
304 * This function may be declared as `static` (i.e. without external
305 * linkage). This function may be provided as a function-like macro,
306 * but in this case it must evaluate each of its arguments exactly once.
307 *
308 * \param[out] attributes The attribute structure to write to.
309 * \param bits The key size in bits.
Gilles Peskinea170d922019-09-12 16:59:37 +0200310 * If this is 0, the key size in \p attributes
Gilles Peskine05c900b2019-09-12 18:29:43 +0200311 * becomes unspecified. Keys of size 0 are
312 * not supported.
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200313 */
314static void psa_set_key_bits(psa_key_attributes_t *attributes,
315 size_t bits);
316
Gilles Peskine20628592019-04-19 19:29:50 +0200317/** Retrieve the key type from key attributes.
318 *
319 * This function may be declared as `static` (i.e. without external
320 * linkage). This function may be provided as a function-like macro,
321 * but in this case it must evaluate its argument exactly once.
322 *
323 * \param[in] attributes The key attribute structure to query.
324 *
325 * \return The key type stored in the attribute structure.
326 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200327static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
328
Gilles Peskine20628592019-04-19 19:29:50 +0200329/** Retrieve the key size from key attributes.
330 *
331 * This function may be declared as `static` (i.e. without external
332 * linkage). This function may be provided as a function-like macro,
333 * but in this case it must evaluate its argument exactly once.
334 *
335 * \param[in] attributes The key attribute structure to query.
336 *
337 * \return The key size stored in the attribute structure, in bits.
338 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200339static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
340
Gilles Peskine20628592019-04-19 19:29:50 +0200341/** Retrieve the attributes of a key.
342 *
343 * This function first resets the attribute structure as with
Gilles Peskine9c640f92019-04-28 11:36:21 +0200344 * psa_reset_key_attributes(). It then copies the attributes of
345 * the given key into the given attribute structure.
Gilles Peskine20628592019-04-19 19:29:50 +0200346 *
Gilles Peskine9c640f92019-04-28 11:36:21 +0200347 * \note This function may allocate memory or other resources.
348 * Once you have called this function on an attribute structure,
349 * you must call psa_reset_key_attributes() to free these resources.
Gilles Peskine20628592019-04-19 19:29:50 +0200350 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200351 * \param[in] key Identifier of the key to query.
Gilles Peskine20628592019-04-19 19:29:50 +0200352 * \param[in,out] attributes On success, the attributes of the key.
353 * On failure, equivalent to a
354 * freshly-initialized structure.
355 *
356 * \retval #PSA_SUCCESS
357 * \retval #PSA_ERROR_INVALID_HANDLE
358 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
359 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw29b64072019-08-06 16:02:12 +0100360 * \retval #PSA_ERROR_CORRUPTION_DETECTED
361 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100362 * \retval #PSA_ERROR_BAD_STATE
363 * The library has not been previously initialized by psa_crypto_init().
364 * It is implementation-dependent whether a failure to initialize
365 * results in this error code.
Gilles Peskine20628592019-04-19 19:29:50 +0200366 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200367psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
Gilles Peskine4747d192019-04-17 15:05:45 +0200368 psa_key_attributes_t *attributes);
369
Gilles Peskine20628592019-04-19 19:29:50 +0200370/** Reset a key attribute structure to a freshly initialized state.
371 *
372 * You must initialize the attribute structure as described in the
373 * documentation of the type #psa_key_attributes_t before calling this
374 * function. Once the structure has been initialized, you may call this
375 * function at any time.
376 *
377 * This function frees any auxiliary resources that the structure
378 * may contain.
379 *
380 * \param[in,out] attributes The attribute structure to reset.
381 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200382void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200383
Gilles Peskine87a5e562019-04-17 12:28:25 +0200384/**@}*/
385
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100386/** \defgroup key_management Key management
387 * @{
388 */
389
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100390/** Make a copy of a key.
391 *
392 * Copy key material from one location to another.
393 *
394 * This function is primarily useful to copy a key from one location
395 * to another, since it populates a key using the material from
396 * another key which may have a different lifetime.
397 *
398 * This function may be used to share a key with a different party,
399 * subject to implementation-defined restrictions on key sharing.
400 *
401 * The policy on the source key must have the usage flag
402 * #PSA_KEY_USAGE_COPY set.
403 * This flag is sufficient to permit the copy if the key has the lifetime
404 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
405 * Some secure elements do not provide a way to copy a key without
406 * making it extractable from the secure element. If a key is located
407 * in such a secure element, then the key must have both usage flags
408 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
409 * a copy of the key outside the secure element.
410 *
411 * The resulting key may only be used in a way that conforms to
412 * both the policy of the original key and the policy specified in
413 * the \p attributes parameter:
414 * - The usage flags on the resulting key are the bitwise-and of the
415 * usage flags on the source policy and the usage flags in \p attributes.
416 * - If both allow the same algorithm or wildcard-based
417 * algorithm policy, the resulting key has the same algorithm policy.
418 * - If either of the policies allows an algorithm and the other policy
419 * allows a wildcard-based algorithm policy that includes this algorithm,
420 * the resulting key allows the same algorithm.
421 * - If the policies do not allow any algorithm in common, this function
422 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
423 *
424 * The effect of this function on implementation-defined attributes is
425 * implementation-defined.
426 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200427 * \param source_key The key to copy. It must allow the usage
428 * PSA_KEY_USAGE_COPY. If a private or secret key is
429 * being copied outside of a secure element it must
430 * also allow PSA_KEY_USAGE_EXPORT.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100431 * \param[in] attributes The attributes for the new key.
432 * They are used as follows:
433 * - The key type and size may be 0. If either is
434 * nonzero, it must match the corresponding
435 * attribute of the source key.
436 * - The key location (the lifetime and, for
437 * persistent keys, the key identifier) is
438 * used directly.
439 * - The policy constraints (usage flags and
440 * algorithm policy) are combined from
441 * the source key and \p attributes so that
442 * both sets of restrictions apply, as
443 * described in the documentation of this function.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200444 * \param[out] target_key On success, an identifier for the newly created
445 * key. \c 0 on failure.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100446 *
447 * \retval #PSA_SUCCESS
448 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200449 * \p source_key is invalid.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100450 * \retval #PSA_ERROR_ALREADY_EXISTS
451 * This is an attempt to create a persistent key, and there is
452 * already a persistent key with the given identifier.
453 * \retval #PSA_ERROR_INVALID_ARGUMENT
454 * The lifetime or identifier in \p attributes are invalid.
455 * \retval #PSA_ERROR_INVALID_ARGUMENT
456 * The policy constraints on the source and specified in
457 * \p attributes are incompatible.
458 * \retval #PSA_ERROR_INVALID_ARGUMENT
459 * \p attributes specifies a key type or key size
460 * which does not match the attributes of the source key.
461 * \retval #PSA_ERROR_NOT_PERMITTED
462 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
463 * \retval #PSA_ERROR_NOT_PERMITTED
464 * The source key is not exportable and its lifetime does not
465 * allow copying it to the target's lifetime.
466 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
467 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
468 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
469 * \retval #PSA_ERROR_HARDWARE_FAILURE
470 * \retval #PSA_ERROR_STORAGE_FAILURE
471 * \retval #PSA_ERROR_CORRUPTION_DETECTED
472 * \retval #PSA_ERROR_BAD_STATE
473 * The library has not been previously initialized by psa_crypto_init().
474 * It is implementation-dependent whether a failure to initialize
475 * results in this error code.
476 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200477psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100478 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200479 mbedtls_svc_key_id_t *target_key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100480
481
482/**
483 * \brief Destroy a key.
484 *
485 * This function destroys a key from both volatile
486 * memory and, if applicable, non-volatile storage. Implementations shall
487 * make a best effort to ensure that that the key material cannot be recovered.
488 *
489 * This function also erases any metadata such as policies and frees
Ronald Croncf56a0a2020-08-04 09:51:30 +0200490 * resources associated with the key.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100491 *
492 * If a key is currently in use in a multipart operation, then destroying the
493 * key will cause the multipart operation to fail.
494 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200495 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
496 * return PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100497 *
498 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +0200499 * \p key was a valid identifier and the key material that it
500 * referred to has been erased. Alternatively, \p key is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100501 * \retval #PSA_ERROR_NOT_PERMITTED
502 * The key cannot be erased because it is
503 * read-only, either due to a policy or due to physical restrictions.
504 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200505 * \p key is not a valid identifier nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100506 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
507 * There was an failure in communication with the cryptoprocessor.
508 * The key material may still be present in the cryptoprocessor.
509 * \retval #PSA_ERROR_STORAGE_FAILURE
510 * The storage is corrupted. Implementations shall make a best effort
511 * to erase key material even in this stage, however applications
512 * should be aware that it may be impossible to guarantee that the
513 * key material is not recoverable in such cases.
514 * \retval #PSA_ERROR_CORRUPTION_DETECTED
515 * An unexpected condition which is not a storage corruption or
516 * a communication failure occurred. The cryptoprocessor may have
517 * been compromised.
518 * \retval #PSA_ERROR_BAD_STATE
519 * The library has not been previously initialized by psa_crypto_init().
520 * It is implementation-dependent whether a failure to initialize
521 * results in this error code.
522 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200523psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100524
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100525/**@}*/
526
527/** \defgroup import_export Key import and export
528 * @{
529 */
530
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100531/**
532 * \brief Import a key in binary format.
533 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100534 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100535 * documentation of psa_export_public_key() for the format of public keys
536 * and to the documentation of psa_export_key() for the format for
537 * other key types.
538 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200539 * The key data determines the key size. The attributes may optionally
540 * specify a key size; in this case it must match the size determined
541 * from the key data. A key size of 0 in \p attributes indicates that
542 * the key size is solely determined by the key data.
543 *
544 * Implementations must reject an attempt to import a key of size 0.
545 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100546 * This specification supports a single format for each key type.
547 * Implementations may support other formats as long as the standard
548 * format is supported. Implementations that support other formats
549 * should ensure that the formats are clearly unambiguous so as to
550 * minimize the risk that an invalid input is accidentally interpreted
551 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552 *
Gilles Peskine20628592019-04-19 19:29:50 +0200553 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200554 * The key size is always determined from the
555 * \p data buffer.
556 * If the key size in \p attributes is nonzero,
557 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200558 * \param[out] key On success, an identifier to the newly created key.
Gilles Peskine20628592019-04-19 19:29:50 +0200559 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100560 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200561 * buffer is interpreted according to the type declared
562 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200563 * All implementations must support at least the format
564 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100565 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200566 * the chosen type. Implementations may allow other
567 * formats, but should be conservative: implementations
568 * should err on the side of rejecting content if it
569 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200570 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100571 *
Gilles Peskine28538492018-07-11 17:34:00 +0200572 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100573 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100574 * If the key is persistent, the key material and the key's metadata
575 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200576 * \retval #PSA_ERROR_ALREADY_EXISTS
577 * This is an attempt to create a persistent key, and there is
578 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200579 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200580 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200581 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200582 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200583 * The key attributes, as a whole, are invalid.
584 * \retval #PSA_ERROR_INVALID_ARGUMENT
585 * The key data is not correctly formatted.
586 * \retval #PSA_ERROR_INVALID_ARGUMENT
587 * The size in \p attributes is nonzero and does not match the size
588 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200589 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
590 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
591 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100592 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200593 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200594 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300595 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300596 * The library has not been previously initialized by psa_crypto_init().
597 * It is implementation-dependent whether a failure to initialize
598 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100599 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200600psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200602 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200603 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100604
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100605
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100606
607/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100608 * \brief Export a key in binary format.
609 *
610 * The output of this function can be passed to psa_import_key() to
611 * create an equivalent object.
612 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100613 * If the implementation of psa_import_key() supports other formats
614 * beyond the format specified here, the output from psa_export_key()
615 * must use the representation specified here, not the original
616 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100617 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100618 * For standard key types, the output format is as follows:
619 *
620 * - For symmetric keys (including MAC keys), the format is the
621 * raw bytes of the key.
622 * - For DES, the key data consists of 8 bytes. The parity bits must be
623 * correct.
624 * - For Triple-DES, the format is the concatenation of the
625 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200626 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200627 * is the non-encrypted DER encoding of the representation defined by
628 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
629 * ```
630 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200631 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200632 * modulus INTEGER, -- n
633 * publicExponent INTEGER, -- e
634 * privateExponent INTEGER, -- d
635 * prime1 INTEGER, -- p
636 * prime2 INTEGER, -- q
637 * exponent1 INTEGER, -- d mod (p-1)
638 * exponent2 INTEGER, -- d mod (q-1)
639 * coefficient INTEGER, -- (inverse of q) mod p
640 * }
641 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200642 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200643 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100644 * a representation of the private value as a `ceiling(m/8)`-byte string
645 * where `m` is the bit size associated with the curve, i.e. the bit size
646 * of the order of the curve's coordinate field. This byte string is
647 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100648 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
649 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
650 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200651 * For Weierstrass curves, this is the content of the `privateKey` field of
652 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
653 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200654 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200655 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000656 * format is the representation of the private key `x` as a big-endian byte
657 * string. The length of the byte string is the private key size in bytes
658 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200659 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
660 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100661 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200662 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
663 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200664 * \param key Identifier of the key to export. It must allow the
665 * usage PSA_KEY_USAGE_EXPORT, unless it is a public
666 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200667 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200668 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200669 * \param[out] data_length On success, the number of bytes
670 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100671 *
Gilles Peskine28538492018-07-11 17:34:00 +0200672 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100673 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200674 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200675 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100676 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200677 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
678 * The size of the \p data buffer is too small. You can determine a
679 * sufficient buffer size by calling
680 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
681 * where \c type is the key type
682 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200683 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
684 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200685 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100686 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100687 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300688 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300689 * The library has not been previously initialized by psa_crypto_init().
690 * It is implementation-dependent whether a failure to initialize
691 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100692 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200693psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100694 uint8_t *data,
695 size_t data_size,
696 size_t *data_length);
697
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100698/**
699 * \brief Export a public key or the public part of a key pair in binary format.
700 *
701 * The output of this function can be passed to psa_import_key() to
702 * create an object that is equivalent to the public key.
703 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000704 * This specification supports a single format for each key type.
705 * Implementations may support other formats as long as the standard
706 * format is supported. Implementations that support other formats
707 * should ensure that the formats are clearly unambiguous so as to
708 * minimize the risk that an invalid input is accidentally interpreted
709 * according to a different format.
710 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000711 * For standard key types, the output format is as follows:
712 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
713 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
714 * ```
715 * RSAPublicKey ::= SEQUENCE {
716 * modulus INTEGER, -- n
717 * publicExponent INTEGER } -- e
718 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000719 * - For elliptic curve public keys (key types for which
720 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
721 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
722 * Let `m` be the bit size associated with the curve, i.e. the bit size of
723 * `q` for a curve over `F_q`. The representation consists of:
724 * - The byte 0x04;
725 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
726 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200727 * - For Diffie-Hellman key exchange public keys (key types for which
728 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000729 * the format is the representation of the public key `y = g^x mod p` as a
730 * big-endian byte string. The length of the byte string is the length of the
731 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100732 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200733 * Exporting a public key object or the public part of a key pair is
734 * always permitted, regardless of the key's usage flags.
735 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200736 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200737 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200738 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200739 * \param[out] data_length On success, the number of bytes
740 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100741 *
Gilles Peskine28538492018-07-11 17:34:00 +0200742 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100743 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200744 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200745 * The key is neither a public key nor a key pair.
746 * \retval #PSA_ERROR_NOT_SUPPORTED
747 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
748 * The size of the \p data buffer is too small. You can determine a
749 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200750 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200751 * where \c type is the key type
752 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200753 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
754 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200755 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100756 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100757 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300758 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300759 * The library has not been previously initialized by psa_crypto_init().
760 * It is implementation-dependent whether a failure to initialize
761 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100762 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200763psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100764 uint8_t *data,
765 size_t data_size,
766 size_t *data_length);
767
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100768
Gilles Peskine20035e32018-02-03 22:44:14 +0100769
770/**@}*/
771
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100772/** \defgroup hash Message digests
773 * @{
774 */
775
Gilles Peskine69647a42019-01-14 20:18:12 +0100776/** Calculate the hash (digest) of a message.
777 *
778 * \note To verify the hash of a message against an
779 * expected value, use psa_hash_compare() instead.
780 *
781 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
782 * such that #PSA_ALG_IS_HASH(\p alg) is true).
783 * \param[in] input Buffer containing the message to hash.
784 * \param input_length Size of the \p input buffer in bytes.
785 * \param[out] hash Buffer where the hash is to be written.
786 * \param hash_size Size of the \p hash buffer in bytes.
787 * \param[out] hash_length On success, the number of bytes
788 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100789 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100790 *
791 * \retval #PSA_SUCCESS
792 * Success.
793 * \retval #PSA_ERROR_NOT_SUPPORTED
794 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100795 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100796 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
797 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100798 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
799 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
800 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200801 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100802 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100803 * \retval #PSA_ERROR_BAD_STATE
804 * The library has not been previously initialized by psa_crypto_init().
805 * It is implementation-dependent whether a failure to initialize
806 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100807 */
808psa_status_t psa_hash_compute(psa_algorithm_t alg,
809 const uint8_t *input,
810 size_t input_length,
811 uint8_t *hash,
812 size_t hash_size,
813 size_t *hash_length);
814
815/** Calculate the hash (digest) of a message and compare it with a
816 * reference value.
817 *
818 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
819 * such that #PSA_ALG_IS_HASH(\p alg) is true).
820 * \param[in] input Buffer containing the message to hash.
821 * \param input_length Size of the \p input buffer in bytes.
822 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100823 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100824 *
825 * \retval #PSA_SUCCESS
826 * The expected hash is identical to the actual hash of the input.
827 * \retval #PSA_ERROR_INVALID_SIGNATURE
828 * The hash of the message was calculated successfully, but it
829 * differs from the expected hash.
830 * \retval #PSA_ERROR_NOT_SUPPORTED
831 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100832 * \retval #PSA_ERROR_INVALID_ARGUMENT
833 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100834 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
835 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
836 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200837 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100838 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100839 * \retval #PSA_ERROR_BAD_STATE
840 * The library has not been previously initialized by psa_crypto_init().
841 * It is implementation-dependent whether a failure to initialize
842 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100843 */
844psa_status_t psa_hash_compare(psa_algorithm_t alg,
845 const uint8_t *input,
846 size_t input_length,
847 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100848 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100849
Gilles Peskine308b91d2018-02-08 09:47:44 +0100850/** The type of the state data structure for multipart hash operations.
851 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000852 * Before calling any function on a hash operation object, the application must
853 * initialize it by any of the following means:
854 * - Set the structure to all-bits-zero, for example:
855 * \code
856 * psa_hash_operation_t operation;
857 * memset(&operation, 0, sizeof(operation));
858 * \endcode
859 * - Initialize the structure to logical zero values, for example:
860 * \code
861 * psa_hash_operation_t operation = {0};
862 * \endcode
863 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
864 * for example:
865 * \code
866 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
867 * \endcode
868 * - Assign the result of the function psa_hash_operation_init()
869 * to the structure, for example:
870 * \code
871 * psa_hash_operation_t operation;
872 * operation = psa_hash_operation_init();
873 * \endcode
874 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100875 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100876 * make any assumptions about the content of this structure except
877 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100878typedef struct psa_hash_operation_s psa_hash_operation_t;
879
Jaeden Amero6a25b412019-01-04 11:47:44 +0000880/** \def PSA_HASH_OPERATION_INIT
881 *
882 * This macro returns a suitable initializer for a hash operation object
883 * of type #psa_hash_operation_t.
884 */
885#ifdef __DOXYGEN_ONLY__
886/* This is an example definition for documentation purposes.
887 * Implementations should define a suitable value in `crypto_struct.h`.
888 */
889#define PSA_HASH_OPERATION_INIT {0}
890#endif
891
892/** Return an initial value for a hash operation object.
893 */
894static psa_hash_operation_t psa_hash_operation_init(void);
895
Gilles Peskinef45adda2019-01-14 18:29:18 +0100896/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100897 *
898 * The sequence of operations to calculate a hash (message digest)
899 * is as follows:
900 * -# Allocate an operation object which will be passed to all the functions
901 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000902 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100903 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200904 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100905 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100906 * of the message each time. The hash that is calculated is the hash
907 * of the concatenation of these messages in order.
908 * -# To calculate the hash, call psa_hash_finish().
909 * To compare the hash with an expected value, call psa_hash_verify().
910 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100911 * If an error occurs at any step after a call to psa_hash_setup(), the
912 * operation will need to be reset by a call to psa_hash_abort(). The
913 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000914 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100915 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200916 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100917 * eventually terminate the operation. The following events terminate an
918 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100919 * - A successful call to psa_hash_finish() or psa_hash_verify().
920 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100921 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000922 * \param[in,out] operation The operation object to set up. It must have
923 * been initialized as per the documentation for
924 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200925 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
926 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100927 *
Gilles Peskine28538492018-07-11 17:34:00 +0200928 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100929 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200930 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100931 * \p alg is not a supported hash algorithm.
932 * \retval #PSA_ERROR_INVALID_ARGUMENT
933 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100934 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100935 * The operation state is not valid (it must be inactive).
Gilles Peskine28538492018-07-11 17:34:00 +0200936 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
937 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
938 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200939 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100940 * \retval #PSA_ERROR_BAD_STATE
941 * The library has not been previously initialized by psa_crypto_init().
942 * It is implementation-dependent whether a failure to initialize
943 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100944 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200945psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100946 psa_algorithm_t alg);
947
Gilles Peskine308b91d2018-02-08 09:47:44 +0100948/** Add a message fragment to a multipart hash operation.
949 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200950 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100951 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100952 * If this function returns an error status, the operation enters an error
953 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100954 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200955 * \param[in,out] operation Active hash operation.
956 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200957 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100958 *
Gilles Peskine28538492018-07-11 17:34:00 +0200959 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100960 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200961 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100962 * The operation state is not valid (it muct be active).
Gilles Peskine28538492018-07-11 17:34:00 +0200963 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
964 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
965 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200966 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100967 * \retval #PSA_ERROR_BAD_STATE
968 * The library has not been previously initialized by psa_crypto_init().
969 * It is implementation-dependent whether a failure to initialize
970 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100971 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100972psa_status_t psa_hash_update(psa_hash_operation_t *operation,
973 const uint8_t *input,
974 size_t input_length);
975
Gilles Peskine308b91d2018-02-08 09:47:44 +0100976/** Finish the calculation of the hash of a message.
977 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200978 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100979 * This function calculates the hash of the message formed by concatenating
980 * the inputs passed to preceding calls to psa_hash_update().
981 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100982 * When this function returns successfuly, the operation becomes inactive.
983 * If this function returns an error status, the operation enters an error
984 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100985 *
986 * \warning Applications should not call this function if they expect
987 * a specific value for the hash. Call psa_hash_verify() instead.
988 * Beware that comparing integrity or authenticity data such as
989 * hash values with a function such as \c memcmp is risky
990 * because the time taken by the comparison may leak information
991 * about the hashed data which could allow an attacker to guess
992 * a valid hash and thereby bypass security controls.
993 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200994 * \param[in,out] operation Active hash operation.
995 * \param[out] hash Buffer where the hash is to be written.
996 * \param hash_size Size of the \p hash buffer in bytes.
997 * \param[out] hash_length On success, the number of bytes
998 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200999 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001000 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001001 *
Gilles Peskine28538492018-07-11 17:34:00 +02001002 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001003 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001004 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001005 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001006 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001007 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001008 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001009 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001010 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1011 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1012 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001013 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001014 * \retval #PSA_ERROR_BAD_STATE
1015 * The library has not been previously initialized by psa_crypto_init().
1016 * It is implementation-dependent whether a failure to initialize
1017 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001018 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001019psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1020 uint8_t *hash,
1021 size_t hash_size,
1022 size_t *hash_length);
1023
Gilles Peskine308b91d2018-02-08 09:47:44 +01001024/** Finish the calculation of the hash of a message and compare it with
1025 * an expected value.
1026 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001027 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001028 * This function calculates the hash of the message formed by concatenating
1029 * the inputs passed to preceding calls to psa_hash_update(). It then
1030 * compares the calculated hash with the expected hash passed as a
1031 * parameter to this function.
1032 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001033 * When this function returns successfuly, the operation becomes inactive.
1034 * If this function returns an error status, the operation enters an error
1035 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001036 *
Gilles Peskine19067982018-03-20 17:54:53 +01001037 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001038 * comparison between the actual hash and the expected hash is performed
1039 * in constant time.
1040 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001041 * \param[in,out] operation Active hash operation.
1042 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001043 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001044 *
Gilles Peskine28538492018-07-11 17:34:00 +02001045 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001046 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001047 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001048 * The hash of the message was calculated successfully, but it
1049 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001050 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001051 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001052 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1053 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1054 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001055 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001056 * \retval #PSA_ERROR_BAD_STATE
1057 * The library has not been previously initialized by psa_crypto_init().
1058 * It is implementation-dependent whether a failure to initialize
1059 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001060 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001061psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1062 const uint8_t *hash,
1063 size_t hash_length);
1064
Gilles Peskine308b91d2018-02-08 09:47:44 +01001065/** Abort a hash operation.
1066 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001067 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001068 * \p operation structure itself. Once aborted, the operation object
1069 * can be reused for another operation by calling
1070 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001071 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001072 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001073 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001075 * In particular, calling psa_hash_abort() after the operation has been
1076 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1077 * psa_hash_verify() is safe and has no effect.
1078 *
1079 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001080 *
Gilles Peskine28538492018-07-11 17:34:00 +02001081 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001082 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1083 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001084 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001085 * \retval #PSA_ERROR_BAD_STATE
1086 * The library has not been previously initialized by psa_crypto_init().
1087 * It is implementation-dependent whether a failure to initialize
1088 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001089 */
1090psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001091
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001092/** Clone a hash operation.
1093 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001094 * This function copies the state of an ongoing hash operation to
1095 * a new operation object. In other words, this function is equivalent
1096 * to calling psa_hash_setup() on \p target_operation with the same
1097 * algorithm that \p source_operation was set up for, then
1098 * psa_hash_update() on \p target_operation with the same input that
1099 * that was passed to \p source_operation. After this function returns, the
1100 * two objects are independent, i.e. subsequent calls involving one of
1101 * the objects do not affect the other object.
1102 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001103 * \param[in] source_operation The active hash operation to clone.
1104 * \param[in,out] target_operation The operation object to set up.
1105 * It must be initialized but not active.
1106 *
1107 * \retval #PSA_SUCCESS
1108 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001109 * The \p source_operation state is not valid (it must be active).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001110 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001111 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001112 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1113 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001114 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001115 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001116 * \retval #PSA_ERROR_BAD_STATE
1117 * The library has not been previously initialized by psa_crypto_init().
1118 * It is implementation-dependent whether a failure to initialize
1119 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001120 */
1121psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1122 psa_hash_operation_t *target_operation);
1123
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001124/**@}*/
1125
Gilles Peskine8c9def32018-02-08 10:02:12 +01001126/** \defgroup MAC Message authentication codes
1127 * @{
1128 */
1129
Gilles Peskine69647a42019-01-14 20:18:12 +01001130/** Calculate the MAC (message authentication code) of a message.
1131 *
1132 * \note To verify the MAC of a message against an
1133 * expected value, use psa_mac_verify() instead.
1134 * Beware that comparing integrity or authenticity data such as
1135 * MAC values with a function such as \c memcmp is risky
1136 * because the time taken by the comparison may leak information
1137 * about the MAC value which could allow an attacker to guess
1138 * a valid MAC and thereby bypass security controls.
1139 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001140 * \param key Identifier of the key to use for the operation. It
1141 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001142 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001143 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001144 * \param[in] input Buffer containing the input message.
1145 * \param input_length Size of the \p input buffer in bytes.
1146 * \param[out] mac Buffer where the MAC value is to be written.
1147 * \param mac_size Size of the \p mac buffer in bytes.
1148 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001149 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001150 *
1151 * \retval #PSA_SUCCESS
1152 * Success.
1153 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001154 * \retval #PSA_ERROR_NOT_PERMITTED
1155 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001156 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001157 * \retval #PSA_ERROR_NOT_SUPPORTED
1158 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001159 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1160 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001161 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1162 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1163 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001164 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001165 * \retval #PSA_ERROR_STORAGE_FAILURE
1166 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001167 * \retval #PSA_ERROR_BAD_STATE
1168 * The library has not been previously initialized by psa_crypto_init().
1169 * It is implementation-dependent whether a failure to initialize
1170 * results in this error code.
1171 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001172psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001173 psa_algorithm_t alg,
1174 const uint8_t *input,
1175 size_t input_length,
1176 uint8_t *mac,
1177 size_t mac_size,
1178 size_t *mac_length);
1179
1180/** Calculate the MAC of a message and compare it with a reference value.
1181 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001182 * \param key Identifier of the key to use for the operation. It
1183 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001184 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001185 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001186 * \param[in] input Buffer containing the input message.
1187 * \param input_length Size of the \p input buffer in bytes.
1188 * \param[out] mac Buffer containing the expected MAC value.
1189 * \param mac_length Size of the \p mac buffer in bytes.
1190 *
1191 * \retval #PSA_SUCCESS
1192 * The expected MAC is identical to the actual MAC of the input.
1193 * \retval #PSA_ERROR_INVALID_SIGNATURE
1194 * The MAC of the message was calculated successfully, but it
1195 * differs from the expected value.
1196 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001197 * \retval #PSA_ERROR_NOT_PERMITTED
1198 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001199 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001200 * \retval #PSA_ERROR_NOT_SUPPORTED
1201 * \p alg is not supported or is not a MAC algorithm.
1202 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1203 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1204 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001205 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001206 * \retval #PSA_ERROR_STORAGE_FAILURE
1207 * The key could not be retrieved from storage.
1208 * \retval #PSA_ERROR_BAD_STATE
1209 * The library has not been previously initialized by psa_crypto_init().
1210 * It is implementation-dependent whether a failure to initialize
1211 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001212 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001213psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001214 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001215 const uint8_t *input,
1216 size_t input_length,
1217 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001218 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001219
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001220/** The type of the state data structure for multipart MAC operations.
1221 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001222 * Before calling any function on a MAC operation object, the application must
1223 * initialize it by any of the following means:
1224 * - Set the structure to all-bits-zero, for example:
1225 * \code
1226 * psa_mac_operation_t operation;
1227 * memset(&operation, 0, sizeof(operation));
1228 * \endcode
1229 * - Initialize the structure to logical zero values, for example:
1230 * \code
1231 * psa_mac_operation_t operation = {0};
1232 * \endcode
1233 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1234 * for example:
1235 * \code
1236 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1237 * \endcode
1238 * - Assign the result of the function psa_mac_operation_init()
1239 * to the structure, for example:
1240 * \code
1241 * psa_mac_operation_t operation;
1242 * operation = psa_mac_operation_init();
1243 * \endcode
1244 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001245 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001246 * make any assumptions about the content of this structure except
1247 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001248typedef struct psa_mac_operation_s psa_mac_operation_t;
1249
Jaeden Amero769ce272019-01-04 11:48:03 +00001250/** \def PSA_MAC_OPERATION_INIT
1251 *
1252 * This macro returns a suitable initializer for a MAC operation object of type
1253 * #psa_mac_operation_t.
1254 */
1255#ifdef __DOXYGEN_ONLY__
1256/* This is an example definition for documentation purposes.
1257 * Implementations should define a suitable value in `crypto_struct.h`.
1258 */
1259#define PSA_MAC_OPERATION_INIT {0}
1260#endif
1261
1262/** Return an initial value for a MAC operation object.
1263 */
1264static psa_mac_operation_t psa_mac_operation_init(void);
1265
Gilles Peskinef45adda2019-01-14 18:29:18 +01001266/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001267 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001268 * This function sets up the calculation of the MAC
1269 * (message authentication code) of a byte string.
1270 * To verify the MAC of a message against an
1271 * expected value, use psa_mac_verify_setup() instead.
1272 *
1273 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001274 * -# Allocate an operation object which will be passed to all the functions
1275 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001276 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001277 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001278 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001279 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1280 * of the message each time. The MAC that is calculated is the MAC
1281 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001282 * -# At the end of the message, call psa_mac_sign_finish() to finish
1283 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001284 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001285 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1286 * operation will need to be reset by a call to psa_mac_abort(). The
1287 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001288 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001289 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001290 * After a successful call to psa_mac_sign_setup(), the application must
1291 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001292 * - A successful call to psa_mac_sign_finish().
1293 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001294 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001295 * \param[in,out] operation The operation object to set up. It must have
1296 * been initialized as per the documentation for
1297 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001298 * \param key Identifier of the key to use for the operation. It
1299 * must remain valid until the operation terminates.
1300 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001301 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001302 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001303 *
Gilles Peskine28538492018-07-11 17:34:00 +02001304 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001305 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001306 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001307 * \retval #PSA_ERROR_NOT_PERMITTED
1308 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001309 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001310 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001311 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001312 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1313 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1314 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001315 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001316 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001317 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001318 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001319 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001320 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001321 * The library has not been previously initialized by psa_crypto_init().
1322 * It is implementation-dependent whether a failure to initialize
1323 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001324 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001325psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001326 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001327 psa_algorithm_t alg);
1328
Gilles Peskinef45adda2019-01-14 18:29:18 +01001329/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001330 *
1331 * This function sets up the verification of the MAC
1332 * (message authentication code) of a byte string against an expected value.
1333 *
1334 * The sequence of operations to verify a MAC is as follows:
1335 * -# Allocate an operation object which will be passed to all the functions
1336 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001337 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001338 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001339 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001340 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1341 * of the message each time. The MAC that is calculated is the MAC
1342 * of the concatenation of these messages in order.
1343 * -# At the end of the message, call psa_mac_verify_finish() to finish
1344 * calculating the actual MAC of the message and verify it against
1345 * the expected value.
1346 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001347 * If an error occurs at any step after a call to psa_mac_verify_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 Peskine89167cb2018-07-08 20:12:23 +02001351 *
1352 * After a successful call to psa_mac_verify_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_verify_finish().
1355 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001356 *
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.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001360 * \param key Identifier of the key to use for the operation. It
1361 * must remain valid until the operation terminates.
1362 * It must allow the usage
1363 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001364 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1365 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001366 *
Gilles Peskine28538492018-07-11 17:34:00 +02001367 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001368 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001369 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001370 * \retval #PSA_ERROR_NOT_PERMITTED
1371 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001372 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001373 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001374 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001375 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1376 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1377 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001378 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001379 * \retval #PSA_ERROR_STORAGE_FAILURE
1380 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001381 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001382 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001383 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001384 * The library has not been previously initialized by psa_crypto_init().
1385 * It is implementation-dependent whether a failure to initialize
1386 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001387 */
1388psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001389 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001390 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001391
Gilles Peskinedcd14942018-07-12 00:30:52 +02001392/** Add a message fragment to a multipart MAC operation.
1393 *
1394 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1395 * before calling this function.
1396 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001397 * If this function returns an error status, the operation enters an error
1398 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001399 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001400 * \param[in,out] operation Active MAC operation.
1401 * \param[in] input Buffer containing the message fragment to add to
1402 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001403 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001404 *
1405 * \retval #PSA_SUCCESS
1406 * Success.
1407 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001408 * The operation state is not valid (it must be active).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001409 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1410 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1411 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001412 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001413 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001414 * \retval #PSA_ERROR_BAD_STATE
1415 * The library has not been previously initialized by psa_crypto_init().
1416 * It is implementation-dependent whether a failure to initialize
1417 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001418 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001419psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1420 const uint8_t *input,
1421 size_t input_length);
1422
Gilles Peskinedcd14942018-07-12 00:30:52 +02001423/** Finish the calculation of the MAC of a message.
1424 *
1425 * The application must call psa_mac_sign_setup() before calling this function.
1426 * This function calculates the MAC of the message formed by concatenating
1427 * the inputs passed to preceding calls to psa_mac_update().
1428 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001429 * When this function returns successfuly, the operation becomes inactive.
1430 * If this function returns an error status, the operation enters an error
1431 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001432 *
1433 * \warning Applications should not call this function if they expect
1434 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1435 * Beware that comparing integrity or authenticity data such as
1436 * MAC values with a function such as \c memcmp is risky
1437 * because the time taken by the comparison may leak information
1438 * about the MAC value which could allow an attacker to guess
1439 * a valid MAC and thereby bypass security controls.
1440 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001441 * \param[in,out] operation Active MAC operation.
1442 * \param[out] mac Buffer where the MAC value is to be written.
1443 * \param mac_size Size of the \p mac buffer in bytes.
1444 * \param[out] mac_length On success, the number of bytes
1445 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001446 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001447 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001448 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001449 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001450 *
1451 * \retval #PSA_SUCCESS
1452 * Success.
1453 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001454 * The operation state is not valid (it must be an active mac sign
1455 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001456 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001457 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001458 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1459 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1460 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1461 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001462 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001463 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001464 * \retval #PSA_ERROR_BAD_STATE
1465 * The library has not been previously initialized by psa_crypto_init().
1466 * It is implementation-dependent whether a failure to initialize
1467 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001468 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001469psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1470 uint8_t *mac,
1471 size_t mac_size,
1472 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001473
Gilles Peskinedcd14942018-07-12 00:30:52 +02001474/** Finish the calculation of the MAC of a message and compare it with
1475 * an expected value.
1476 *
1477 * The application must call psa_mac_verify_setup() before calling this function.
1478 * This function calculates the MAC of the message formed by concatenating
1479 * the inputs passed to preceding calls to psa_mac_update(). It then
1480 * compares the calculated MAC with the expected MAC passed as a
1481 * parameter to this function.
1482 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001483 * When this function returns successfuly, the operation becomes inactive.
1484 * If this function returns an error status, the operation enters an error
1485 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001486 *
1487 * \note Implementations shall make the best effort to ensure that the
1488 * comparison between the actual MAC and the expected MAC is performed
1489 * in constant time.
1490 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001491 * \param[in,out] operation Active MAC operation.
1492 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001493 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001494 *
1495 * \retval #PSA_SUCCESS
1496 * The expected MAC is identical to the actual MAC of the message.
1497 * \retval #PSA_ERROR_INVALID_SIGNATURE
1498 * The MAC of the message was calculated successfully, but it
1499 * differs from the expected MAC.
1500 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001501 * The operation state is not valid (it must be an active mac verify
1502 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001503 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1504 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1505 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001506 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001507 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001508 * \retval #PSA_ERROR_BAD_STATE
1509 * The library has not been previously initialized by psa_crypto_init().
1510 * It is implementation-dependent whether a failure to initialize
1511 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001512 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001513psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1514 const uint8_t *mac,
1515 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001516
Gilles Peskinedcd14942018-07-12 00:30:52 +02001517/** Abort a MAC operation.
1518 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001519 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001520 * \p operation structure itself. Once aborted, the operation object
1521 * can be reused for another operation by calling
1522 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001523 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001524 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001525 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001526 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001527 * In particular, calling psa_mac_abort() after the operation has been
1528 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1529 * psa_mac_verify_finish() is safe and has no effect.
1530 *
1531 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001532 *
1533 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001534 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1535 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001536 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001537 * \retval #PSA_ERROR_BAD_STATE
1538 * The library has not been previously initialized by psa_crypto_init().
1539 * It is implementation-dependent whether a failure to initialize
1540 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001541 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001542psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1543
1544/**@}*/
1545
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001546/** \defgroup cipher Symmetric ciphers
1547 * @{
1548 */
1549
Gilles Peskine69647a42019-01-14 20:18:12 +01001550/** Encrypt a message using a symmetric cipher.
1551 *
1552 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001553 * vector). Use the multipart operation interface with a
1554 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001555 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001556 * \param key Identifier of the key to use for the operation.
1557 * It must allow the usage PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001558 * \param alg The cipher algorithm to compute
1559 * (\c PSA_ALG_XXX value such that
1560 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1561 * \param[in] input Buffer containing the message to encrypt.
1562 * \param input_length Size of the \p input buffer in bytes.
1563 * \param[out] output Buffer where the output is to be written.
1564 * The output contains the IV followed by
1565 * the ciphertext proper.
1566 * \param output_size Size of the \p output buffer in bytes.
1567 * \param[out] output_length On success, the number of bytes
1568 * that make up the output.
1569 *
1570 * \retval #PSA_SUCCESS
1571 * Success.
1572 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001573 * \retval #PSA_ERROR_NOT_PERMITTED
1574 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001575 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001576 * \retval #PSA_ERROR_NOT_SUPPORTED
1577 * \p alg is not supported or is not a cipher algorithm.
1578 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1579 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1580 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1581 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001582 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001583 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001584 * \retval #PSA_ERROR_BAD_STATE
1585 * The library has not been previously initialized by psa_crypto_init().
1586 * It is implementation-dependent whether a failure to initialize
1587 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001588 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001589psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001590 psa_algorithm_t alg,
1591 const uint8_t *input,
1592 size_t input_length,
1593 uint8_t *output,
1594 size_t output_size,
1595 size_t *output_length);
1596
1597/** Decrypt a message using a symmetric cipher.
1598 *
1599 * This function decrypts a message encrypted with a symmetric cipher.
1600 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001601 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001602 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001603 * terminates. It must allow the usage
1604 * PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001605 * \param alg The cipher algorithm to compute
1606 * (\c PSA_ALG_XXX value such that
1607 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1608 * \param[in] input Buffer containing the message to decrypt.
1609 * This consists of the IV followed by the
1610 * ciphertext proper.
1611 * \param input_length Size of the \p input buffer in bytes.
1612 * \param[out] output Buffer where the plaintext is to be written.
1613 * \param output_size Size of the \p output buffer in bytes.
1614 * \param[out] output_length On success, the number of bytes
1615 * that make up the output.
1616 *
1617 * \retval #PSA_SUCCESS
1618 * Success.
1619 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001620 * \retval #PSA_ERROR_NOT_PERMITTED
1621 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001622 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001623 * \retval #PSA_ERROR_NOT_SUPPORTED
1624 * \p alg is not supported or is not a cipher algorithm.
1625 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1626 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1627 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1628 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001629 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001630 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001631 * \retval #PSA_ERROR_BAD_STATE
1632 * The library has not been previously initialized by psa_crypto_init().
1633 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001634 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001635 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001636psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001637 psa_algorithm_t alg,
1638 const uint8_t *input,
1639 size_t input_length,
1640 uint8_t *output,
1641 size_t output_size,
1642 size_t *output_length);
1643
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001644/** The type of the state data structure for multipart cipher operations.
1645 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001646 * Before calling any function on a cipher operation object, the application
1647 * must initialize it by any of the following means:
1648 * - Set the structure to all-bits-zero, for example:
1649 * \code
1650 * psa_cipher_operation_t operation;
1651 * memset(&operation, 0, sizeof(operation));
1652 * \endcode
1653 * - Initialize the structure to logical zero values, for example:
1654 * \code
1655 * psa_cipher_operation_t operation = {0};
1656 * \endcode
1657 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1658 * for example:
1659 * \code
1660 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1661 * \endcode
1662 * - Assign the result of the function psa_cipher_operation_init()
1663 * to the structure, for example:
1664 * \code
1665 * psa_cipher_operation_t operation;
1666 * operation = psa_cipher_operation_init();
1667 * \endcode
1668 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001669 * This is an implementation-defined \c struct. Applications should not
1670 * make any assumptions about the content of this structure except
1671 * as directed by the documentation of a specific implementation. */
1672typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1673
Jaeden Amero5bae2272019-01-04 11:48:27 +00001674/** \def PSA_CIPHER_OPERATION_INIT
1675 *
1676 * This macro returns a suitable initializer for a cipher operation object of
1677 * type #psa_cipher_operation_t.
1678 */
1679#ifdef __DOXYGEN_ONLY__
1680/* This is an example definition for documentation purposes.
1681 * Implementations should define a suitable value in `crypto_struct.h`.
1682 */
1683#define PSA_CIPHER_OPERATION_INIT {0}
1684#endif
1685
1686/** Return an initial value for a cipher operation object.
1687 */
1688static psa_cipher_operation_t psa_cipher_operation_init(void);
1689
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001690/** Set the key for a multipart symmetric encryption operation.
1691 *
1692 * The sequence of operations to encrypt a message with a symmetric cipher
1693 * is as follows:
1694 * -# Allocate an operation object which will be passed to all the functions
1695 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001696 * -# Initialize the operation object with one of the methods described in the
1697 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001698 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001699 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001700 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001701 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001702 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001703 * requires a specific IV value.
1704 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1705 * of the message each time.
1706 * -# Call psa_cipher_finish().
1707 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001708 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1709 * the operation will need to be reset by a call to psa_cipher_abort(). The
1710 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001711 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001712 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001713 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001714 * eventually terminate the operation. The following events terminate an
1715 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001716 * - A successful call to psa_cipher_finish().
1717 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001718 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001719 * \param[in,out] operation The operation object to set up. It must have
1720 * been initialized as per the documentation for
1721 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001722 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001723 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001724 * terminates. It must allow the usage
1725 * PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001726 * \param alg The cipher algorithm to compute
1727 * (\c PSA_ALG_XXX value such that
1728 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001729 *
Gilles Peskine28538492018-07-11 17:34:00 +02001730 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001731 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001732 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001733 * \retval #PSA_ERROR_NOT_PERMITTED
1734 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001735 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001736 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001737 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001738 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1739 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1740 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001741 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001742 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001743 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001744 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001745 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001746 * The library has not been previously initialized by psa_crypto_init().
1747 * It is implementation-dependent whether a failure to initialize
1748 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001749 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001750psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001751 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001752 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001753
1754/** Set the key for a multipart symmetric decryption operation.
1755 *
1756 * The sequence of operations to decrypt a message with a symmetric cipher
1757 * is as follows:
1758 * -# Allocate an operation object which will be passed to all the functions
1759 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001760 * -# Initialize the operation object with one of the methods described in the
1761 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001762 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001763 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001764 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001765 * decryption. If the IV is prepended to the ciphertext, you can call
1766 * psa_cipher_update() on a buffer containing the IV followed by the
1767 * beginning of the message.
1768 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1769 * of the message each time.
1770 * -# Call psa_cipher_finish().
1771 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001772 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1773 * the operation will need to be reset by a call to psa_cipher_abort(). The
1774 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001775 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001776 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001777 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001778 * eventually terminate the operation. The following events terminate an
1779 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001780 * - A successful call to psa_cipher_finish().
1781 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001782 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001783 * \param[in,out] operation The operation object to set up. It must have
1784 * been initialized as per the documentation for
1785 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001786 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001787 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001788 * terminates. It must allow the usage
1789 * PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001790 * \param alg The cipher algorithm to compute
1791 * (\c PSA_ALG_XXX value such that
1792 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001793 *
Gilles Peskine28538492018-07-11 17:34:00 +02001794 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001795 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001796 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001797 * \retval #PSA_ERROR_NOT_PERMITTED
1798 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001799 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001800 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001801 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001802 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1803 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1804 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001805 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001806 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001807 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001808 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001809 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001810 * The library has not been previously initialized by psa_crypto_init().
1811 * It is implementation-dependent whether a failure to initialize
1812 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001813 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001814psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001815 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001816 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001817
Gilles Peskinedcd14942018-07-12 00:30:52 +02001818/** Generate an IV for a symmetric encryption operation.
1819 *
1820 * This function generates a random IV (initialization vector), nonce
1821 * or initial counter value for the encryption operation as appropriate
1822 * for the chosen algorithm, key type and key size.
1823 *
1824 * The application must call psa_cipher_encrypt_setup() before
1825 * calling this function.
1826 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001827 * If this function returns an error status, the operation enters an error
1828 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001829 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001830 * \param[in,out] operation Active cipher operation.
1831 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001832 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001833 * \param[out] iv_length On success, the number of bytes of the
1834 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001835 *
1836 * \retval #PSA_SUCCESS
1837 * Success.
1838 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001839 * The operation state is not valid (it must be active, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001840 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001841 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001842 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1843 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1844 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001845 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001846 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001847 * \retval #PSA_ERROR_BAD_STATE
1848 * The library has not been previously initialized by psa_crypto_init().
1849 * It is implementation-dependent whether a failure to initialize
1850 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001851 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001852psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001853 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001854 size_t iv_size,
1855 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001856
Gilles Peskinedcd14942018-07-12 00:30:52 +02001857/** Set the IV for a symmetric encryption or decryption operation.
1858 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001859 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001860 * or initial counter value for the encryption or decryption operation.
1861 *
1862 * The application must call psa_cipher_encrypt_setup() before
1863 * calling this function.
1864 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001865 * If this function returns an error status, the operation enters an error
1866 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001867 *
1868 * \note When encrypting, applications should use psa_cipher_generate_iv()
1869 * instead of this function, unless implementing a protocol that requires
1870 * a non-random IV.
1871 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001872 * \param[in,out] operation Active cipher operation.
1873 * \param[in] iv Buffer containing the IV to use.
1874 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001875 *
1876 * \retval #PSA_SUCCESS
1877 * Success.
1878 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001879 * The operation state is not valid (it must be an active cipher
1880 * encrypt operation, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001881 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001882 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001883 * or the chosen algorithm does not use an IV.
1884 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1885 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1886 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001887 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001888 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001889 * \retval #PSA_ERROR_BAD_STATE
1890 * The library has not been previously initialized by psa_crypto_init().
1891 * It is implementation-dependent whether a failure to initialize
1892 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001893 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001894psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001895 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001896 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001897
Gilles Peskinedcd14942018-07-12 00:30:52 +02001898/** Encrypt or decrypt a message fragment in an active cipher operation.
1899 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001900 * Before calling this function, you must:
1901 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1902 * The choice of setup function determines whether this function
1903 * encrypts or decrypts its input.
1904 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1905 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001906 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001907 * If this function returns an error status, the operation enters an error
1908 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001909 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001910 * \param[in,out] operation Active cipher operation.
1911 * \param[in] input Buffer containing the message fragment to
1912 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001913 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001914 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001915 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001916 * \param[out] output_length On success, the number of bytes
1917 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001918 *
1919 * \retval #PSA_SUCCESS
1920 * Success.
1921 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001922 * The operation state is not valid (it must be active, with an IV set
1923 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001924 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1925 * The size of the \p output buffer is too small.
1926 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1927 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1928 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001929 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001930 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001931 * \retval #PSA_ERROR_BAD_STATE
1932 * The library has not been previously initialized by psa_crypto_init().
1933 * It is implementation-dependent whether a failure to initialize
1934 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001935 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001936psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1937 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001938 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001939 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001940 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001941 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001942
Gilles Peskinedcd14942018-07-12 00:30:52 +02001943/** Finish encrypting or decrypting a message in a cipher operation.
1944 *
1945 * The application must call psa_cipher_encrypt_setup() or
1946 * psa_cipher_decrypt_setup() before calling this function. The choice
1947 * of setup function determines whether this function encrypts or
1948 * decrypts its input.
1949 *
1950 * This function finishes the encryption or decryption of the message
1951 * formed by concatenating the inputs passed to preceding calls to
1952 * psa_cipher_update().
1953 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001954 * When this function returns successfuly, the operation becomes inactive.
1955 * If this function returns an error status, the operation enters an error
1956 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001957 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001958 * \param[in,out] operation Active cipher operation.
1959 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001960 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001961 * \param[out] output_length On success, the number of bytes
1962 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001963 *
1964 * \retval #PSA_SUCCESS
1965 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02001966 * \retval #PSA_ERROR_INVALID_ARGUMENT
1967 * The total input size passed to this operation is not valid for
1968 * this particular algorithm. For example, the algorithm is a based
1969 * on block cipher and requires a whole number of blocks, but the
1970 * total input size is not a multiple of the block size.
1971 * \retval #PSA_ERROR_INVALID_PADDING
1972 * This is a decryption operation for an algorithm that includes
1973 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001974 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001975 * The operation state is not valid (it must be active, with an IV set
1976 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001977 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1978 * The size of the \p output buffer is too small.
1979 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1980 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1981 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001982 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01001983 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001984 * \retval #PSA_ERROR_BAD_STATE
1985 * The library has not been previously initialized by psa_crypto_init().
1986 * It is implementation-dependent whether a failure to initialize
1987 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001988 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001989psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001990 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001991 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001992 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001993
Gilles Peskinedcd14942018-07-12 00:30:52 +02001994/** Abort a cipher operation.
1995 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001996 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001997 * \p operation structure itself. Once aborted, the operation object
1998 * can be reused for another operation by calling
1999 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002000 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002001 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002002 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002003 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002004 * In particular, calling psa_cipher_abort() after the operation has been
2005 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2006 * is safe and has no effect.
2007 *
2008 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002009 *
2010 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02002011 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2012 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002013 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002014 * \retval #PSA_ERROR_BAD_STATE
2015 * The library has not been previously initialized by psa_crypto_init().
2016 * It is implementation-dependent whether a failure to initialize
2017 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002018 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002019psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2020
2021/**@}*/
2022
Gilles Peskine3b555712018-03-03 21:27:57 +01002023/** \defgroup aead Authenticated encryption with associated data (AEAD)
2024 * @{
2025 */
2026
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002027/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002028 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002029 * \param key Identifier of the key to use for the
2030 * operation. It must allow the usage
2031 * PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002032 * \param alg The AEAD algorithm to compute
2033 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002034 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002035 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002036 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002037 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002038 * but not encrypted.
2039 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002040 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002041 * encrypted.
2042 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002043 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002044 * encrypted data. The additional data is not
2045 * part of this output. For algorithms where the
2046 * encrypted data and the authentication tag
2047 * are defined as separate outputs, the
2048 * authentication tag is appended to the
2049 * encrypted data.
2050 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2051 * This must be at least
2052 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2053 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002054 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002055 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002056 *
Gilles Peskine28538492018-07-11 17:34:00 +02002057 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002058 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002059 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002060 * \retval #PSA_ERROR_NOT_PERMITTED
2061 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002062 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002063 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002064 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002065 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002066 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2067 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002068 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2069 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002070 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002071 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002072 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002073 * The library has not been previously initialized by psa_crypto_init().
2074 * It is implementation-dependent whether a failure to initialize
2075 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002076 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002077psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002078 psa_algorithm_t alg,
2079 const uint8_t *nonce,
2080 size_t nonce_length,
2081 const uint8_t *additional_data,
2082 size_t additional_data_length,
2083 const uint8_t *plaintext,
2084 size_t plaintext_length,
2085 uint8_t *ciphertext,
2086 size_t ciphertext_size,
2087 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002088
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002089/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002090 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002091 * \param key Identifier of the key to use for the
2092 * operation. It must allow the usage
2093 * PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002094 * \param alg The AEAD algorithm to compute
2095 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002096 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002097 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002098 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002099 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002100 * but not encrypted.
2101 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002102 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002103 * encrypted. For algorithms where the
2104 * encrypted data and the authentication tag
2105 * are defined as separate inputs, the buffer
2106 * must contain the encrypted data followed
2107 * by the authentication tag.
2108 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002109 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002110 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2111 * This must be at least
2112 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2113 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002114 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002115 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002116 *
Gilles Peskine28538492018-07-11 17:34:00 +02002117 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002118 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002119 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002120 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002121 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002122 * \retval #PSA_ERROR_NOT_PERMITTED
2123 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002124 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002125 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002126 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002127 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002128 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2129 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002130 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2131 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002132 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002133 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002134 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002135 * The library has not been previously initialized by psa_crypto_init().
2136 * It is implementation-dependent whether a failure to initialize
2137 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002138 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002139psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002140 psa_algorithm_t alg,
2141 const uint8_t *nonce,
2142 size_t nonce_length,
2143 const uint8_t *additional_data,
2144 size_t additional_data_length,
2145 const uint8_t *ciphertext,
2146 size_t ciphertext_length,
2147 uint8_t *plaintext,
2148 size_t plaintext_size,
2149 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002150
Gilles Peskine30a9e412019-01-14 18:36:12 +01002151/** The type of the state data structure for multipart AEAD operations.
2152 *
2153 * Before calling any function on an AEAD operation object, the application
2154 * must initialize it by any of the following means:
2155 * - Set the structure to all-bits-zero, for example:
2156 * \code
2157 * psa_aead_operation_t operation;
2158 * memset(&operation, 0, sizeof(operation));
2159 * \endcode
2160 * - Initialize the structure to logical zero values, for example:
2161 * \code
2162 * psa_aead_operation_t operation = {0};
2163 * \endcode
2164 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2165 * for example:
2166 * \code
2167 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2168 * \endcode
2169 * - Assign the result of the function psa_aead_operation_init()
2170 * to the structure, for example:
2171 * \code
2172 * psa_aead_operation_t operation;
2173 * operation = psa_aead_operation_init();
2174 * \endcode
2175 *
2176 * This is an implementation-defined \c struct. Applications should not
2177 * make any assumptions about the content of this structure except
2178 * as directed by the documentation of a specific implementation. */
2179typedef struct psa_aead_operation_s psa_aead_operation_t;
2180
2181/** \def PSA_AEAD_OPERATION_INIT
2182 *
2183 * This macro returns a suitable initializer for an AEAD operation object of
2184 * type #psa_aead_operation_t.
2185 */
2186#ifdef __DOXYGEN_ONLY__
2187/* This is an example definition for documentation purposes.
2188 * Implementations should define a suitable value in `crypto_struct.h`.
2189 */
2190#define PSA_AEAD_OPERATION_INIT {0}
2191#endif
2192
2193/** Return an initial value for an AEAD operation object.
2194 */
2195static psa_aead_operation_t psa_aead_operation_init(void);
2196
2197/** Set the key for a multipart authenticated encryption operation.
2198 *
2199 * The sequence of operations to encrypt a message with authentication
2200 * is as follows:
2201 * -# Allocate an operation object which will be passed to all the functions
2202 * listed here.
2203 * -# Initialize the operation object with one of the methods described in the
2204 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002205 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002206 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002207 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2208 * inputs to the subsequent calls to psa_aead_update_ad() and
2209 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2210 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002211 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2212 * generate or set the nonce. You should use
2213 * psa_aead_generate_nonce() unless the protocol you are implementing
2214 * requires a specific nonce value.
2215 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2216 * of the non-encrypted additional authenticated data each time.
2217 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002218 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002219 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002220 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002221 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2222 * the operation will need to be reset by a call to psa_aead_abort(). The
2223 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002224 * has been initialized.
2225 *
2226 * After a successful call to psa_aead_encrypt_setup(), the application must
2227 * eventually terminate the operation. The following events terminate an
2228 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002229 * - A successful call to psa_aead_finish().
2230 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002231 *
2232 * \param[in,out] operation The operation object to set up. It must have
2233 * been initialized as per the documentation for
2234 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002235 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002236 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002237 * terminates. It must allow the usage
2238 * PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002239 * \param alg The AEAD algorithm to compute
2240 * (\c PSA_ALG_XXX value such that
2241 * #PSA_ALG_IS_AEAD(\p alg) is true).
2242 *
2243 * \retval #PSA_SUCCESS
2244 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002245 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002246 * The operation state is not valid (it must be inactive).
Andrew Thoelke340984b2019-09-11 21:33:41 +01002247 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002248 * \retval #PSA_ERROR_NOT_PERMITTED
2249 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002250 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002251 * \retval #PSA_ERROR_NOT_SUPPORTED
2252 * \p alg is not supported or is not an AEAD algorithm.
2253 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2254 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2255 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002256 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002257 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002258 * \retval #PSA_ERROR_BAD_STATE
2259 * The library has not been previously initialized by psa_crypto_init().
2260 * It is implementation-dependent whether a failure to initialize
2261 * results in this error code.
2262 */
2263psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002264 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002265 psa_algorithm_t alg);
2266
2267/** Set the key for a multipart authenticated decryption operation.
2268 *
2269 * The sequence of operations to decrypt a message with authentication
2270 * is as follows:
2271 * -# Allocate an operation object which will be passed to all the functions
2272 * listed here.
2273 * -# Initialize the operation object with one of the methods described in the
2274 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002275 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002276 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002277 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2278 * inputs to the subsequent calls to psa_aead_update_ad() and
2279 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2280 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002281 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2282 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2283 * of the non-encrypted additional authenticated data each time.
2284 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002285 * of the ciphertext to decrypt each time.
2286 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002287 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002288 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2289 * the operation will need to be reset by a call to psa_aead_abort(). The
2290 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002291 * has been initialized.
2292 *
2293 * After a successful call to psa_aead_decrypt_setup(), the application must
2294 * eventually terminate the operation. The following events terminate an
2295 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002296 * - A successful call to psa_aead_verify().
2297 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002298 *
2299 * \param[in,out] operation The operation object to set up. It must have
2300 * been initialized as per the documentation for
2301 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002302 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002303 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002304 * terminates. It must allow the usage
2305 * PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002306 * \param alg The AEAD algorithm to compute
2307 * (\c PSA_ALG_XXX value such that
2308 * #PSA_ALG_IS_AEAD(\p alg) is true).
2309 *
2310 * \retval #PSA_SUCCESS
2311 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002312 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002313 * The operation state is not valid (it must be inactive).
Andrew Thoelke340984b2019-09-11 21:33:41 +01002314 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002315 * \retval #PSA_ERROR_NOT_PERMITTED
2316 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002317 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002318 * \retval #PSA_ERROR_NOT_SUPPORTED
2319 * \p alg is not supported or is not an AEAD algorithm.
2320 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2321 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2322 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002323 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002324 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002325 * \retval #PSA_ERROR_BAD_STATE
2326 * The library has not been previously initialized by psa_crypto_init().
2327 * It is implementation-dependent whether a failure to initialize
2328 * results in this error code.
2329 */
2330psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002331 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002332 psa_algorithm_t alg);
2333
2334/** Generate a random nonce for an authenticated encryption operation.
2335 *
2336 * This function generates a random nonce for the authenticated encryption
2337 * operation with an appropriate size for the chosen algorithm, key type
2338 * and key size.
2339 *
2340 * The application must call psa_aead_encrypt_setup() before
2341 * calling this function.
2342 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002343 * If this function returns an error status, the operation enters an error
2344 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002345 *
2346 * \param[in,out] operation Active AEAD operation.
2347 * \param[out] nonce Buffer where the generated nonce is to be
2348 * written.
2349 * \param nonce_size Size of the \p nonce buffer in bytes.
2350 * \param[out] nonce_length On success, the number of bytes of the
2351 * generated nonce.
2352 *
2353 * \retval #PSA_SUCCESS
2354 * Success.
2355 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002356 * The operation state is not valid (it must be an active aead encrypt
2357 operation, with no nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002358 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2359 * The size of the \p nonce buffer is too small.
2360 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2361 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2362 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002363 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002364 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002365 * \retval #PSA_ERROR_BAD_STATE
2366 * The library has not been previously initialized by psa_crypto_init().
2367 * It is implementation-dependent whether a failure to initialize
2368 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002369 */
2370psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002371 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002372 size_t nonce_size,
2373 size_t *nonce_length);
2374
2375/** Set the nonce for an authenticated encryption or decryption operation.
2376 *
2377 * This function sets the nonce for the authenticated
2378 * encryption or decryption operation.
2379 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002380 * The application must call psa_aead_encrypt_setup() or
2381 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002382 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002383 * If this function returns an error status, the operation enters an error
2384 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002385 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002386 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002387 * instead of this function, unless implementing a protocol that requires
2388 * a non-random IV.
2389 *
2390 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002391 * \param[in] nonce Buffer containing the nonce to use.
2392 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002393 *
2394 * \retval #PSA_SUCCESS
2395 * Success.
2396 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002397 * The operation state is not valid (it must be active, with no nonce
2398 * set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002399 * \retval #PSA_ERROR_INVALID_ARGUMENT
2400 * The size of \p nonce is not acceptable for the chosen algorithm.
2401 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2402 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2403 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002404 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002405 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002406 * \retval #PSA_ERROR_BAD_STATE
2407 * The library has not been previously initialized by psa_crypto_init().
2408 * It is implementation-dependent whether a failure to initialize
2409 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002410 */
2411psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002412 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002413 size_t nonce_length);
2414
Gilles Peskinebc59c852019-01-17 15:26:08 +01002415/** Declare the lengths of the message and additional data for AEAD.
2416 *
2417 * The application must call this function before calling
2418 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2419 * the operation requires it. If the algorithm does not require it,
2420 * calling this function is optional, but if this function is called
2421 * then the implementation must enforce the lengths.
2422 *
2423 * You may call this function before or after setting the nonce with
2424 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2425 *
2426 * - For #PSA_ALG_CCM, calling this function is required.
2427 * - For the other AEAD algorithms defined in this specification, calling
2428 * this function is not required.
2429 * - For vendor-defined algorithm, refer to the vendor documentation.
2430 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002431 * If this function returns an error status, the operation enters an error
2432 * state and must be aborted by calling psa_aead_abort().
2433 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002434 * \param[in,out] operation Active AEAD operation.
2435 * \param ad_length Size of the non-encrypted additional
2436 * authenticated data in bytes.
2437 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2438 *
2439 * \retval #PSA_SUCCESS
2440 * Success.
2441 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002442 * The operation state is not valid (it must be active, and
2443 * psa_aead_update_ad() and psa_aead_update() must not have been
2444 * called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002445 * \retval #PSA_ERROR_INVALID_ARGUMENT
2446 * At least one of the lengths is not acceptable for the chosen
2447 * algorithm.
2448 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2449 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2450 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002451 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002452 * \retval #PSA_ERROR_BAD_STATE
2453 * The library has not been previously initialized by psa_crypto_init().
2454 * It is implementation-dependent whether a failure to initialize
2455 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002456 */
2457psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2458 size_t ad_length,
2459 size_t plaintext_length);
2460
Gilles Peskine30a9e412019-01-14 18:36:12 +01002461/** Pass additional data to an active AEAD operation.
2462 *
2463 * Additional data is authenticated, but not encrypted.
2464 *
2465 * You may call this function multiple times to pass successive fragments
2466 * of the additional data. You may not call this function after passing
2467 * data to encrypt or decrypt with psa_aead_update().
2468 *
2469 * Before calling this function, you must:
2470 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2471 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2472 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002473 * If this function returns an error status, the operation enters an error
2474 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002475 *
2476 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2477 * there is no guarantee that the input is valid. Therefore, until
2478 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2479 * treat the input as untrusted and prepare to undo any action that
2480 * depends on the input if psa_aead_verify() returns an error status.
2481 *
2482 * \param[in,out] operation Active AEAD operation.
2483 * \param[in] input Buffer containing the fragment of
2484 * additional data.
2485 * \param input_length Size of the \p input buffer in bytes.
2486 *
2487 * \retval #PSA_SUCCESS
2488 * Success.
2489 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002490 * The operation state is not valid (it must be active, have a nonce
2491 * set, have lengths set if required by the algorithm, and
2492 * psa_aead_update() must not have been called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002493 * \retval #PSA_ERROR_INVALID_ARGUMENT
2494 * The total input length overflows the additional data length that
2495 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002496 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2497 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2498 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002499 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002500 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002501 * \retval #PSA_ERROR_BAD_STATE
2502 * The library has not been previously initialized by psa_crypto_init().
2503 * It is implementation-dependent whether a failure to initialize
2504 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002505 */
2506psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2507 const uint8_t *input,
2508 size_t input_length);
2509
2510/** Encrypt or decrypt a message fragment in an active AEAD operation.
2511 *
2512 * Before calling this function, you must:
2513 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2514 * The choice of setup function determines whether this function
2515 * encrypts or decrypts its input.
2516 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2517 * 3. Call psa_aead_update_ad() to pass all the additional data.
2518 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002519 * If this function returns an error status, the operation enters an error
2520 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002521 *
2522 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2523 * there is no guarantee that the input is valid. Therefore, until
2524 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2525 * - Do not use the output in any way other than storing it in a
2526 * confidential location. If you take any action that depends
2527 * on the tentative decrypted data, this action will need to be
2528 * undone if the input turns out not to be valid. Furthermore,
2529 * if an adversary can observe that this action took place
2530 * (for example through timing), they may be able to use this
2531 * fact as an oracle to decrypt any message encrypted with the
2532 * same key.
2533 * - In particular, do not copy the output anywhere but to a
2534 * memory or storage space that you have exclusive access to.
2535 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002536 * This function does not require the input to be aligned to any
2537 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002538 * a whole block at a time, it must consume all the input provided, but
2539 * it may delay the end of the corresponding output until a subsequent
2540 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2541 * provides sufficient input. The amount of data that can be delayed
2542 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002543 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002544 * \param[in,out] operation Active AEAD operation.
2545 * \param[in] input Buffer containing the message fragment to
2546 * encrypt or decrypt.
2547 * \param input_length Size of the \p input buffer in bytes.
2548 * \param[out] output Buffer where the output is to be written.
2549 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002550 * This must be at least
2551 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2552 * \p input_length) where \c alg is the
2553 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002554 * \param[out] output_length On success, the number of bytes
2555 * that make up the returned output.
2556 *
2557 * \retval #PSA_SUCCESS
2558 * Success.
2559 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002560 * The operation state is not valid (it must be active, have a nonce
2561 * set, and have lengths set if required by the algorithm).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002562 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2563 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002564 * You can determine a sufficient buffer size by calling
2565 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2566 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002567 * \retval #PSA_ERROR_INVALID_ARGUMENT
2568 * The total length of input to psa_aead_update_ad() so far is
2569 * less than the additional data length that was previously
2570 * specified with psa_aead_set_lengths().
2571 * \retval #PSA_ERROR_INVALID_ARGUMENT
2572 * The total input length overflows the plaintext length that
2573 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002574 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2575 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2576 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002577 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002578 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002579 * \retval #PSA_ERROR_BAD_STATE
2580 * The library has not been previously initialized by psa_crypto_init().
2581 * It is implementation-dependent whether a failure to initialize
2582 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002583 */
2584psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2585 const uint8_t *input,
2586 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002587 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002588 size_t output_size,
2589 size_t *output_length);
2590
2591/** Finish encrypting a message in an AEAD operation.
2592 *
2593 * The operation must have been set up with psa_aead_encrypt_setup().
2594 *
2595 * This function finishes the authentication of the additional data
2596 * formed by concatenating the inputs passed to preceding calls to
2597 * psa_aead_update_ad() with the plaintext formed by concatenating the
2598 * inputs passed to preceding calls to psa_aead_update().
2599 *
2600 * This function has two output buffers:
2601 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002602 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002603 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002604 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002605 * that the operation performs.
2606 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002607 * When this function returns successfuly, the operation becomes inactive.
2608 * If this function returns an error status, the operation enters an error
2609 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002610 *
2611 * \param[in,out] operation Active AEAD operation.
2612 * \param[out] ciphertext Buffer where the last part of the ciphertext
2613 * is to be written.
2614 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002615 * This must be at least
2616 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2617 * \c alg is the algorithm that is being
2618 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002619 * \param[out] ciphertext_length On success, the number of bytes of
2620 * returned ciphertext.
2621 * \param[out] tag Buffer where the authentication tag is
2622 * to be written.
2623 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002624 * This must be at least
2625 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2626 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002627 * \param[out] tag_length On success, the number of bytes
2628 * that make up the returned tag.
2629 *
2630 * \retval #PSA_SUCCESS
2631 * Success.
2632 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002633 * The operation state is not valid (it must be an active encryption
2634 * operation with a nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002635 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002636 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002637 * You can determine a sufficient buffer size for \p ciphertext by
2638 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2639 * where \c alg is the algorithm that is being calculated.
2640 * You can determine a sufficient buffer size for \p tag by
2641 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002642 * \retval #PSA_ERROR_INVALID_ARGUMENT
2643 * The total length of input to psa_aead_update_ad() so far is
2644 * less than the additional data length that was previously
2645 * specified with psa_aead_set_lengths().
2646 * \retval #PSA_ERROR_INVALID_ARGUMENT
2647 * The total length of input to psa_aead_update() so far is
2648 * less than the plaintext length that was previously
2649 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002650 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2651 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2652 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002653 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002654 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002655 * \retval #PSA_ERROR_BAD_STATE
2656 * The library has not been previously initialized by psa_crypto_init().
2657 * It is implementation-dependent whether a failure to initialize
2658 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002659 */
2660psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002661 uint8_t *ciphertext,
2662 size_t ciphertext_size,
2663 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002664 uint8_t *tag,
2665 size_t tag_size,
2666 size_t *tag_length);
2667
2668/** Finish authenticating and decrypting a message in an AEAD operation.
2669 *
2670 * The operation must have been set up with psa_aead_decrypt_setup().
2671 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002672 * This function finishes the authenticated decryption of the message
2673 * components:
2674 *
2675 * - The additional data consisting of the concatenation of the inputs
2676 * passed to preceding calls to psa_aead_update_ad().
2677 * - The ciphertext consisting of the concatenation of the inputs passed to
2678 * preceding calls to psa_aead_update().
2679 * - The tag passed to this function call.
2680 *
2681 * If the authentication tag is correct, this function outputs any remaining
2682 * plaintext and reports success. If the authentication tag is not correct,
2683 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002684 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002685 * When this function returns successfuly, the operation becomes inactive.
2686 * If this function returns an error status, the operation enters an error
2687 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002688 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002689 * \note Implementations shall make the best effort to ensure that the
2690 * comparison between the actual tag and the expected tag is performed
2691 * in constant time.
2692 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002693 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002694 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002695 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002696 * from previous calls to psa_aead_update()
2697 * that could not be processed until the end
2698 * of the input.
2699 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002700 * This must be at least
2701 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2702 * \c alg is the algorithm that is being
2703 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002704 * \param[out] plaintext_length On success, the number of bytes of
2705 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002706 * \param[in] tag Buffer containing the authentication tag.
2707 * \param tag_length Size of the \p tag buffer in bytes.
2708 *
2709 * \retval #PSA_SUCCESS
2710 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002711 * \retval #PSA_ERROR_INVALID_SIGNATURE
2712 * The calculations were successful, but the authentication tag is
2713 * not correct.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002714 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002715 * The operation state is not valid (it must be an active decryption
2716 * operation with a nonce set).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002717 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2718 * The size of the \p plaintext buffer is too small.
2719 * You can determine a sufficient buffer size for \p plaintext by
2720 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2721 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002722 * \retval #PSA_ERROR_INVALID_ARGUMENT
2723 * The total length of input to psa_aead_update_ad() so far is
2724 * less than the additional data length that was previously
2725 * specified with psa_aead_set_lengths().
2726 * \retval #PSA_ERROR_INVALID_ARGUMENT
2727 * The total length of input to psa_aead_update() so far is
2728 * less than the plaintext length that was previously
2729 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002730 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2731 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2732 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002733 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002734 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002735 * \retval #PSA_ERROR_BAD_STATE
2736 * The library has not been previously initialized by psa_crypto_init().
2737 * It is implementation-dependent whether a failure to initialize
2738 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002739 */
2740psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002741 uint8_t *plaintext,
2742 size_t plaintext_size,
2743 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002744 const uint8_t *tag,
2745 size_t tag_length);
2746
2747/** Abort an AEAD operation.
2748 *
2749 * Aborting an operation frees all associated resources except for the
2750 * \p operation structure itself. Once aborted, the operation object
2751 * can be reused for another operation by calling
2752 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2753 *
2754 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002755 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002756 *
2757 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002758 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2759 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002760 *
2761 * \param[in,out] operation Initialized AEAD operation.
2762 *
2763 * \retval #PSA_SUCCESS
Gilles Peskine30a9e412019-01-14 18:36:12 +01002764 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2765 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002766 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002767 * \retval #PSA_ERROR_BAD_STATE
2768 * The library has not been previously initialized by psa_crypto_init().
2769 * It is implementation-dependent whether a failure to initialize
2770 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002771 */
2772psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2773
Gilles Peskine3b555712018-03-03 21:27:57 +01002774/**@}*/
2775
Gilles Peskine20035e32018-02-03 22:44:14 +01002776/** \defgroup asymmetric Asymmetric cryptography
2777 * @{
2778 */
2779
2780/**
2781 * \brief Sign a hash or short message with a private key.
2782 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002783 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002784 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002785 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2786 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2787 * to determine the hash algorithm to use.
2788 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002789 * \param key Identifier of the key to use for the operation.
2790 * It must be an asymmetric key pair. The key must
2791 * allow the usage PSA_KEY_USAGE_SIGN_HASH.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002792 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002793 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002794 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002795 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002796 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002797 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002798 * \param[out] signature_length On success, the number of bytes
2799 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002800 *
Gilles Peskine28538492018-07-11 17:34:00 +02002801 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002802 * \retval #PSA_ERROR_INVALID_HANDLE
2803 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002804 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002805 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002806 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002807 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002808 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002809 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002810 * \retval #PSA_ERROR_NOT_SUPPORTED
2811 * \retval #PSA_ERROR_INVALID_ARGUMENT
2812 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2813 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2814 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002815 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002816 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002817 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002818 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002819 * The library has not been previously initialized by psa_crypto_init().
2820 * It is implementation-dependent whether a failure to initialize
2821 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002822 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002823psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002824 psa_algorithm_t alg,
2825 const uint8_t *hash,
2826 size_t hash_length,
2827 uint8_t *signature,
2828 size_t signature_size,
2829 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002830
2831/**
2832 * \brief Verify the signature a hash or short message using a public key.
2833 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002834 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002835 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002836 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2837 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2838 * to determine the hash algorithm to use.
2839 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002840 * \param key Identifier of the key to use for the operation. It
2841 * must be a public key or an asymmetric key pair. The
2842 * key must allow the usage PSA_KEY_USAGE_VERIFY_HASH.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002843 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002844 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002845 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002846 * verified.
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[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002849 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002850 *
Gilles Peskine28538492018-07-11 17:34:00 +02002851 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002852 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002853 * \retval #PSA_ERROR_INVALID_HANDLE
2854 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002855 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002856 * The calculation was perfomed successfully, but the passed
2857 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002858 * \retval #PSA_ERROR_NOT_SUPPORTED
2859 * \retval #PSA_ERROR_INVALID_ARGUMENT
2860 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2861 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2862 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002863 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002864 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002865 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002866 * The library has not been previously initialized by psa_crypto_init().
2867 * It is implementation-dependent whether a failure to initialize
2868 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002869 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002870psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002871 psa_algorithm_t alg,
2872 const uint8_t *hash,
2873 size_t hash_length,
2874 const uint8_t *signature,
2875 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002876
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002877/**
2878 * \brief Encrypt a short message with a public key.
2879 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002880 * \param key Identifer of the key to use for the operation.
2881 * It must be a public key or an asymmetric key
2882 * pair. It must allow the usage
2883 * PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002884 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02002885 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002886 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002887 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002888 * \param[in] salt A salt or label, if supported by the
2889 * encryption algorithm.
2890 * If the algorithm does not support a
2891 * salt, pass \c NULL.
2892 * If the algorithm supports an optional
2893 * salt and you do not want to pass a salt,
2894 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002895 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002896 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2897 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002898 * \param salt_length Size of the \p salt buffer in bytes.
2899 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002900 * \param[out] output Buffer where the encrypted message is to
2901 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002902 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002903 * \param[out] output_length On success, the number of bytes
2904 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002905 *
Gilles Peskine28538492018-07-11 17:34:00 +02002906 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002907 * \retval #PSA_ERROR_INVALID_HANDLE
2908 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002909 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002910 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002911 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002912 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002913 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002914 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002915 * \retval #PSA_ERROR_NOT_SUPPORTED
2916 * \retval #PSA_ERROR_INVALID_ARGUMENT
2917 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2918 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2919 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002920 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002921 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002922 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002923 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002924 * The library has not been previously initialized by psa_crypto_init().
2925 * It is implementation-dependent whether a failure to initialize
2926 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002927 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002928psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002929 psa_algorithm_t alg,
2930 const uint8_t *input,
2931 size_t input_length,
2932 const uint8_t *salt,
2933 size_t salt_length,
2934 uint8_t *output,
2935 size_t output_size,
2936 size_t *output_length);
2937
2938/**
2939 * \brief Decrypt a short message with a private key.
2940 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002941 * \param key Identifier of the key to use for the operation.
2942 * It must be an asymmetric key pair. It must
2943 * allow the usage PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002944 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02002945 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002946 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002947 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002948 * \param[in] salt A salt or label, if supported by the
2949 * encryption algorithm.
2950 * If the algorithm does not support a
2951 * salt, pass \c NULL.
2952 * If the algorithm supports an optional
2953 * salt and you do not want to pass a salt,
2954 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002955 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002956 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2957 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002958 * \param salt_length Size of the \p salt buffer in bytes.
2959 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002960 * \param[out] output Buffer where the decrypted message is to
2961 * be written.
2962 * \param output_size Size of the \c output buffer in bytes.
2963 * \param[out] output_length On success, the number of bytes
2964 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002965 *
Gilles Peskine28538492018-07-11 17:34:00 +02002966 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002967 * \retval #PSA_ERROR_INVALID_HANDLE
2968 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002969 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002970 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002971 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002972 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002973 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002974 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002975 * \retval #PSA_ERROR_NOT_SUPPORTED
2976 * \retval #PSA_ERROR_INVALID_ARGUMENT
2977 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2978 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2979 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002980 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002981 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002982 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2983 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002984 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002985 * The library has not been previously initialized by psa_crypto_init().
2986 * It is implementation-dependent whether a failure to initialize
2987 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002988 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002989psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002990 psa_algorithm_t alg,
2991 const uint8_t *input,
2992 size_t input_length,
2993 const uint8_t *salt,
2994 size_t salt_length,
2995 uint8_t *output,
2996 size_t output_size,
2997 size_t *output_length);
2998
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002999/**@}*/
3000
Gilles Peskine35675b62019-05-16 17:26:11 +02003001/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003002 * @{
3003 */
3004
Gilles Peskine35675b62019-05-16 17:26:11 +02003005/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003006 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003007 * Before calling any function on a key derivation operation object, the
3008 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003009 * - Set the structure to all-bits-zero, for example:
3010 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003011 * psa_key_derivation_operation_t operation;
3012 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003013 * \endcode
3014 * - Initialize the structure to logical zero values, for example:
3015 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003016 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003017 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003018 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003019 * for example:
3020 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003021 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003022 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003023 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003024 * to the structure, for example:
3025 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003026 * psa_key_derivation_operation_t operation;
3027 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003028 * \endcode
3029 *
3030 * This is an implementation-defined \c struct. Applications should not
3031 * make any assumptions about the content of this structure except
3032 * as directed by the documentation of a specific implementation.
3033 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003034typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003035
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003036/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003037 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003038 * This macro returns a suitable initializer for a key derivation operation
3039 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003040 */
3041#ifdef __DOXYGEN_ONLY__
3042/* This is an example definition for documentation purposes.
3043 * Implementations should define a suitable value in `crypto_struct.h`.
3044 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003045#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003046#endif
3047
Gilles Peskine35675b62019-05-16 17:26:11 +02003048/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003049 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003050static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003051
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003052/** Set up a key derivation operation.
3053 *
3054 * A key derivation algorithm takes some inputs and uses them to generate
3055 * a byte stream in a deterministic way.
3056 * This byte stream can be used to produce keys and other
3057 * cryptographic material.
3058 *
3059 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003060 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3061 * -# Call psa_key_derivation_setup() to select the algorithm.
3062 * -# Provide the inputs for the key derivation by calling
3063 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3064 * as appropriate. Which inputs are needed, in what order, and whether
3065 * they may be keys and if so of what type depends on the algorithm.
3066 * -# Optionally set the operation's maximum capacity with
3067 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3068 * of or after providing inputs. For some algorithms, this step is mandatory
3069 * because the output depends on the maximum capacity.
3070 * -# To derive a key, call psa_key_derivation_output_key().
3071 * To derive a byte string for a different purpose, call
3072 * psa_key_derivation_output_bytes().
3073 * Successive calls to these functions use successive output bytes
3074 * calculated by the key derivation algorithm.
3075 * -# Clean up the key derivation operation object with
3076 * psa_key_derivation_abort().
3077 *
3078 * If this function returns an error, the key derivation operation object is
3079 * not changed.
3080 *
3081 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3082 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003083 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003084 * Implementations must reject an attempt to derive a key of size 0.
3085 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003086 * \param[in,out] operation The key derivation operation object
3087 * to set up. It must
3088 * have been initialized but not set up yet.
3089 * \param alg The key derivation algorithm to compute
3090 * (\c PSA_ALG_XXX value such that
3091 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3092 *
3093 * \retval #PSA_SUCCESS
3094 * Success.
3095 * \retval #PSA_ERROR_INVALID_ARGUMENT
3096 * \c alg is not a key derivation algorithm.
3097 * \retval #PSA_ERROR_NOT_SUPPORTED
3098 * \c alg is not supported or is not a key derivation algorithm.
3099 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3100 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3101 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003102 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003103 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003104 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003105 * The operation state is not valid (it must be inactive).
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003106 * \retval #PSA_ERROR_BAD_STATE
3107 * The library has not been previously initialized by psa_crypto_init().
3108 * It is implementation-dependent whether a failure to initialize
3109 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003110 */
3111psa_status_t psa_key_derivation_setup(
3112 psa_key_derivation_operation_t *operation,
3113 psa_algorithm_t alg);
3114
Gilles Peskine35675b62019-05-16 17:26:11 +02003115/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003116 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003117 * The capacity of a key derivation is the maximum number of bytes that it can
3118 * return. When you get *N* bytes of output from a key derivation operation,
3119 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003120 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003121 * \param[in] operation The operation to query.
3122 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003123 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003124 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003125 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003126 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003127 * The operation state is not valid (it must be active).
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003128 * \retval #PSA_ERROR_HARDWARE_FAILURE
3129 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003130 * \retval #PSA_ERROR_BAD_STATE
3131 * The library has not been previously initialized by psa_crypto_init().
3132 * It is implementation-dependent whether a failure to initialize
3133 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003134 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003135psa_status_t psa_key_derivation_get_capacity(
3136 const psa_key_derivation_operation_t *operation,
3137 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003138
Gilles Peskine35675b62019-05-16 17:26:11 +02003139/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003140 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003141 * The capacity of a key derivation operation is the maximum number of bytes
3142 * that the key derivation operation can return from this point onwards.
3143 *
3144 * \param[in,out] operation The key derivation operation object to modify.
3145 * \param capacity The new capacity of the operation.
3146 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003147 * current capacity.
3148 *
3149 * \retval #PSA_SUCCESS
3150 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003151 * \p capacity is larger than the operation's current capacity.
3152 * In this case, the operation object remains valid and its capacity
3153 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003154 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003155 * The operation state is not valid (it must be active).
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003156 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003157 * \retval #PSA_ERROR_HARDWARE_FAILURE
3158 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003159 * \retval #PSA_ERROR_BAD_STATE
3160 * The library has not been previously initialized by psa_crypto_init().
3161 * It is implementation-dependent whether a failure to initialize
3162 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003163 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003164psa_status_t psa_key_derivation_set_capacity(
3165 psa_key_derivation_operation_t *operation,
3166 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003167
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003168/** Use the maximum possible capacity for a key derivation operation.
3169 *
3170 * Use this value as the capacity argument when setting up a key derivation
3171 * to indicate that the operation should have the maximum possible capacity.
3172 * The value of the maximum possible capacity depends on the key derivation
3173 * algorithm.
3174 */
3175#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3176
3177/** Provide an input for key derivation or key agreement.
3178 *
3179 * Which inputs are required and in what order depends on the algorithm.
3180 * Refer to the documentation of each key derivation or key agreement
3181 * algorithm for information.
3182 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003183 * This function passes direct inputs, which is usually correct for
3184 * non-secret inputs. To pass a secret input, which should be in a key
3185 * object, call psa_key_derivation_input_key() instead of this function.
3186 * Refer to the documentation of individual step types
3187 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3188 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003189 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003190 * If this function returns an error status, the operation enters an error
3191 * state and must be aborted by calling psa_key_derivation_abort().
3192 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003193 * \param[in,out] operation The key derivation operation object to use.
3194 * It must have been set up with
3195 * psa_key_derivation_setup() and must not
3196 * have produced any output yet.
3197 * \param step Which step the input data is for.
3198 * \param[in] data Input data to use.
3199 * \param data_length Size of the \p data buffer in bytes.
3200 *
3201 * \retval #PSA_SUCCESS
3202 * Success.
3203 * \retval #PSA_ERROR_INVALID_ARGUMENT
3204 * \c step is not compatible with the operation's algorithm.
3205 * \retval #PSA_ERROR_INVALID_ARGUMENT
3206 * \c step does not allow direct inputs.
3207 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3208 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3209 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003210 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003211 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003212 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003213 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003214 * \retval #PSA_ERROR_BAD_STATE
3215 * The library has not been previously initialized by psa_crypto_init().
3216 * It is implementation-dependent whether a failure to initialize
3217 * results in this error code.
3218 */
3219psa_status_t psa_key_derivation_input_bytes(
3220 psa_key_derivation_operation_t *operation,
3221 psa_key_derivation_step_t step,
3222 const uint8_t *data,
3223 size_t data_length);
3224
3225/** Provide an input for key derivation in the form of a key.
3226 *
3227 * Which inputs are required and in what order depends on the algorithm.
3228 * Refer to the documentation of each key derivation or key agreement
3229 * algorithm for information.
3230 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003231 * This function obtains input from a key object, which is usually correct for
3232 * secret inputs or for non-secret personalization strings kept in the key
3233 * store. To pass a non-secret parameter which is not in the key store,
3234 * call psa_key_derivation_input_bytes() instead of this function.
3235 * Refer to the documentation of individual step types
3236 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3237 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003238 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003239 * If this function returns an error status, the operation enters an error
3240 * state and must be aborted by calling psa_key_derivation_abort().
3241 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003242 * \param[in,out] operation The key derivation operation object to use.
3243 * It must have been set up with
3244 * psa_key_derivation_setup() and must not
3245 * have produced any output yet.
3246 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003247 * \param key Identifier of the key. It must have an
3248 * appropriate type for step and must allow the
3249 * usage PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003250 *
3251 * \retval #PSA_SUCCESS
3252 * Success.
3253 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003254 * \retval #PSA_ERROR_NOT_PERMITTED
3255 * \retval #PSA_ERROR_INVALID_ARGUMENT
3256 * \c step is not compatible with the operation's algorithm.
3257 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine224b0d62019-09-23 18:13:17 +02003258 * \c step does not allow key inputs of the given type
3259 * or does not allow key inputs at all.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003260 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3261 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3262 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003263 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003264 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003265 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003266 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003267 * \retval #PSA_ERROR_BAD_STATE
3268 * The library has not been previously initialized by psa_crypto_init().
3269 * It is implementation-dependent whether a failure to initialize
3270 * results in this error code.
3271 */
3272psa_status_t psa_key_derivation_input_key(
3273 psa_key_derivation_operation_t *operation,
3274 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003275 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003276
3277/** Perform a key agreement and use the shared secret as input to a key
3278 * derivation.
3279 *
3280 * A key agreement algorithm takes two inputs: a private key \p private_key
3281 * a public key \p peer_key.
3282 * The result of this function is passed as input to a key derivation.
3283 * The output of this key derivation can be extracted by reading from the
3284 * resulting operation to produce keys and other cryptographic material.
3285 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003286 * If this function returns an error status, the operation enters an error
3287 * state and must be aborted by calling psa_key_derivation_abort().
3288 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003289 * \param[in,out] operation The key derivation operation object to use.
3290 * It must have been set up with
3291 * psa_key_derivation_setup() with a
3292 * key agreement and derivation algorithm
3293 * \c alg (\c PSA_ALG_XXX value such that
3294 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3295 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3296 * is false).
3297 * The operation must be ready for an
3298 * input of the type given by \p step.
3299 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003300 * \param private_key Identifier of the private key to use. It must
3301 * allow the usage PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003302 * \param[in] peer_key Public key of the peer. The peer key must be in the
3303 * same format that psa_import_key() accepts for the
3304 * public key type corresponding to the type of
3305 * private_key. That is, this function performs the
3306 * equivalent of
3307 * #psa_import_key(...,
3308 * `peer_key`, `peer_key_length`) where
3309 * with key attributes indicating the public key
3310 * type corresponding to the type of `private_key`.
3311 * For example, for EC keys, this means that peer_key
3312 * is interpreted as a point on the curve that the
3313 * private key is on. The standard formats for public
3314 * keys are documented in the documentation of
3315 * psa_export_public_key().
3316 * \param peer_key_length Size of \p peer_key in bytes.
3317 *
3318 * \retval #PSA_SUCCESS
3319 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003320 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003321 * The operation state is not valid for this key agreement \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003322 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003323 * \retval #PSA_ERROR_NOT_PERMITTED
3324 * \retval #PSA_ERROR_INVALID_ARGUMENT
3325 * \c private_key is not compatible with \c alg,
3326 * or \p peer_key is not valid for \c alg or not compatible with
3327 * \c private_key.
3328 * \retval #PSA_ERROR_NOT_SUPPORTED
3329 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskine224b0d62019-09-23 18:13:17 +02003330 * \retval #PSA_ERROR_INVALID_ARGUMENT
3331 * \c step does not allow an input resulting from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003332 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3333 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3334 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003335 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003336 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003337 * \retval #PSA_ERROR_BAD_STATE
3338 * The library has not been previously initialized by psa_crypto_init().
3339 * It is implementation-dependent whether a failure to initialize
3340 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003341 */
3342psa_status_t psa_key_derivation_key_agreement(
3343 psa_key_derivation_operation_t *operation,
3344 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003345 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003346 const uint8_t *peer_key,
3347 size_t peer_key_length);
3348
Gilles Peskine35675b62019-05-16 17:26:11 +02003349/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003350 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003351 * This function calculates output bytes from a key derivation algorithm and
3352 * return those bytes.
3353 * If you view the key derivation's output as a stream of bytes, this
3354 * function destructively reads the requested number of bytes from the
3355 * stream.
3356 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003357 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003358 * If this function returns an error status other than
3359 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003360 * state and must be aborted by calling psa_key_derivation_abort().
3361 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003362 * \param[in,out] operation The key derivation operation object to read from.
3363 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003364 * \param output_length Number of bytes to output.
3365 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003366 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003367 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003368 * The operation's capacity was less than
3369 * \p output_length bytes. Note that in this case,
3370 * no output is written to the output buffer.
3371 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003372 * subsequent calls to this function will not
3373 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003374 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003375 * The operation state is not valid (it must be active and completed
3376 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003377 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3378 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3379 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003380 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003381 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003382 * \retval #PSA_ERROR_BAD_STATE
3383 * The library has not been previously initialized by psa_crypto_init().
3384 * It is implementation-dependent whether a failure to initialize
3385 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003386 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003387psa_status_t psa_key_derivation_output_bytes(
3388 psa_key_derivation_operation_t *operation,
3389 uint8_t *output,
3390 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003391
Gilles Peskine35675b62019-05-16 17:26:11 +02003392/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003393 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003394 * This function calculates output bytes from a key derivation algorithm
3395 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003396 * The key's location, usage policy, type and size are taken from
3397 * \p attributes.
3398 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003399 * If you view the key derivation's output as a stream of bytes, this
3400 * function destructively reads as many bytes as required from the
3401 * stream.
3402 * The operation's capacity decreases by the number of bytes read.
3403 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003404 * If this function returns an error status other than
3405 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003406 * state and must be aborted by calling psa_key_derivation_abort().
3407 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003408 * How much output is produced and consumed from the operation, and how
3409 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003410 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003411 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003412 * of a given size, this function is functionally equivalent to
3413 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003414 * and passing the resulting output to #psa_import_key.
3415 * However, this function has a security benefit:
3416 * if the implementation provides an isolation boundary then
3417 * the key material is not exposed outside the isolation boundary.
3418 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003419 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003420 * The following key types defined in this specification follow this scheme:
3421 *
3422 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003423 * - #PSA_KEY_TYPE_ARC4;
3424 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003425 * - #PSA_KEY_TYPE_DERIVE;
3426 * - #PSA_KEY_TYPE_HMAC.
3427 *
3428 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003429 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003430 * Montgomery curve), this function always draws a byte string whose
3431 * length is determined by the curve, and sets the mandatory bits
3432 * accordingly. That is:
3433 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003434 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003435 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003436 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003437 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003438 *
3439 * - For key types for which the key is represented by a single sequence of
3440 * \p bits bits with constraints as to which bit sequences are acceptable,
3441 * this function draws a byte string of length (\p bits / 8) bytes rounded
3442 * up to the nearest whole number of bytes. If the resulting byte string
3443 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3444 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003445 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003446 * for the output produced by psa_export_key().
3447 * The following key types defined in this specification follow this scheme:
3448 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003449 * - #PSA_KEY_TYPE_DES.
3450 * Force-set the parity bits, but discard forbidden weak keys.
3451 * For 2-key and 3-key triple-DES, the three keys are generated
3452 * successively (for example, for 3-key triple-DES,
3453 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3454 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003455 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003456 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003457 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003458 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003459 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003460 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003461 * Weierstrass curve).
3462 * For these key types, interpret the byte string as integer
3463 * in big-endian order. Discard it if it is not in the range
3464 * [0, *N* - 2] where *N* is the boundary of the private key domain
3465 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003466 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003467 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003468 * This method allows compliance to NIST standards, specifically
3469 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003470 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3471 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3472 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3473 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003474 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003475 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003476 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003477 * implementation-defined.
3478 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003479 * In all cases, the data that is read is discarded from the operation.
3480 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003481 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003482 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3483 * the input to that step must be provided with psa_key_derivation_input_key().
3484 * Future versions of this specification may include additional restrictions
3485 * on the derived key based on the attributes and strength of the secret key.
3486 *
Gilles Peskine20628592019-04-19 19:29:50 +02003487 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003488 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003489 * \param[out] key On success, an identifier for the newly created
3490 * key. \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003491 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003492 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003493 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003494 * If the key is persistent, the key material and the key's metadata
3495 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003496 * \retval #PSA_ERROR_ALREADY_EXISTS
3497 * This is an attempt to create a persistent key, and there is
3498 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003499 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003500 * There was not enough data to create the desired key.
3501 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003502 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003503 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003504 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003505 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003506 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003507 * \retval #PSA_ERROR_INVALID_ARGUMENT
3508 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003509 * \retval #PSA_ERROR_NOT_PERMITTED
3510 * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
3511 * a key.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003512 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003513 * The operation state is not valid (it must be active and completed
3514 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003515 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3516 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3517 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3518 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003519 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003520 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003521 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003522 * The library has not been previously initialized by psa_crypto_init().
3523 * It is implementation-dependent whether a failure to initialize
3524 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003525 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003526psa_status_t psa_key_derivation_output_key(
3527 const psa_key_attributes_t *attributes,
3528 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003529 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003530
Gilles Peskine35675b62019-05-16 17:26:11 +02003531/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003532 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003533 * Aborting an operation frees all associated resources except for the \c
3534 * operation structure itself. Once aborted, the operation object can be reused
3535 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003536 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003537 * This function may be called at any time after the operation
3538 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003539 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003540 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3541 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003542 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003543 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003544 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003545 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003546 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3547 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003548 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003549 * \retval #PSA_ERROR_BAD_STATE
3550 * The library has not been previously initialized by psa_crypto_init().
3551 * It is implementation-dependent whether a failure to initialize
3552 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003553 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003554psa_status_t psa_key_derivation_abort(
3555 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003556
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003557/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003558 *
3559 * \warning The raw result of a key agreement algorithm such as finite-field
3560 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3561 * not be used directly as key material. It should instead be passed as
3562 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003563 * a key derivation, use psa_key_derivation_key_agreement() and other
3564 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003565 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003566 * \param alg The key agreement algorithm to compute
3567 * (\c PSA_ALG_XXX value such that
3568 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3569 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02003570 * \param private_key Identifier of the private key to use. It must
3571 * allow the usage PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003572 * \param[in] peer_key Public key of the peer. It must be
3573 * in the same format that psa_import_key()
3574 * accepts. The standard formats for public
3575 * keys are documented in the documentation
3576 * of psa_export_public_key().
3577 * \param peer_key_length Size of \p peer_key in bytes.
3578 * \param[out] output Buffer where the decrypted message is to
3579 * be written.
3580 * \param output_size Size of the \c output buffer in bytes.
3581 * \param[out] output_length On success, the number of bytes
3582 * that make up the returned output.
3583 *
3584 * \retval #PSA_SUCCESS
3585 * Success.
3586 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003587 * \retval #PSA_ERROR_NOT_PERMITTED
3588 * \retval #PSA_ERROR_INVALID_ARGUMENT
3589 * \p alg is not a key agreement algorithm
3590 * \retval #PSA_ERROR_INVALID_ARGUMENT
3591 * \p private_key is not compatible with \p alg,
3592 * or \p peer_key is not valid for \p alg or not compatible with
3593 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003594 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3595 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003596 * \retval #PSA_ERROR_NOT_SUPPORTED
3597 * \p alg is not a supported key agreement algorithm.
3598 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3599 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3600 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003601 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003602 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003603 * \retval #PSA_ERROR_BAD_STATE
3604 * The library has not been previously initialized by psa_crypto_init().
3605 * It is implementation-dependent whether a failure to initialize
3606 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003607 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003608psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003609 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02003610 const uint8_t *peer_key,
3611 size_t peer_key_length,
3612 uint8_t *output,
3613 size_t output_size,
3614 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003615
Gilles Peskineea0fb492018-07-12 17:17:20 +02003616/**@}*/
3617
Gilles Peskineedd76872018-07-20 17:42:05 +02003618/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003619 * @{
3620 */
3621
3622/**
3623 * \brief Generate random bytes.
3624 *
3625 * \warning This function **can** fail! Callers MUST check the return status
3626 * and MUST NOT use the content of the output buffer if the return
3627 * status is not #PSA_SUCCESS.
3628 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003629 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003630 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003631 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003632 * \param output_size Number of bytes to generate and output.
3633 *
Gilles Peskine28538492018-07-11 17:34:00 +02003634 * \retval #PSA_SUCCESS
3635 * \retval #PSA_ERROR_NOT_SUPPORTED
3636 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003637 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003638 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3639 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003640 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003641 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003642 * The library has not been previously initialized by psa_crypto_init().
3643 * It is implementation-dependent whether a failure to initialize
3644 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003645 */
3646psa_status_t psa_generate_random(uint8_t *output,
3647 size_t output_size);
3648
3649/**
3650 * \brief Generate a key or key pair.
3651 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003652 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003653 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003654 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003655 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003656 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003657 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003658 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003659 * the public exponent is 65537.
3660 * The modulus is a product of two probabilistic primes
3661 * between 2^{n-1} and 2^n where n is the bit size specified in the
3662 * attributes.
3663 *
Gilles Peskine20628592019-04-19 19:29:50 +02003664 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003665 * \param[out] key On success, an identifier for the newly created
3666 * key. \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003667 *
Gilles Peskine28538492018-07-11 17:34:00 +02003668 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003669 * Success.
3670 * If the key is persistent, the key material and the key's metadata
3671 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003672 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003673 * This is an attempt to create a persistent key, and there is
3674 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003675 * \retval #PSA_ERROR_NOT_SUPPORTED
3676 * \retval #PSA_ERROR_INVALID_ARGUMENT
3677 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3678 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3679 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3680 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003681 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003682 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3683 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003684 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003685 * The library has not been previously initialized by psa_crypto_init().
3686 * It is implementation-dependent whether a failure to initialize
3687 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003688 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003689psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003690 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003691
3692/**@}*/
3693
Gilles Peskinee59236f2018-01-27 23:32:46 +01003694#ifdef __cplusplus
3695}
3696#endif
3697
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003698/* The file "crypto_sizes.h" contains definitions for size calculation
3699 * macros whose definitions are implementation-specific. */
3700#include "crypto_sizes.h"
3701
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003702/* The file "crypto_struct.h" contains definitions for
3703 * implementation-specific structs that are declared above. */
3704#include "crypto_struct.h"
3705
3706/* The file "crypto_extra.h" contains vendor-specific definitions. This
3707 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003708#include "crypto_extra.h"
3709
3710#endif /* PSA_CRYPTO_H */