blob: 15ffe2271f41fe0fd63a200149df211671a1afc1 [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
Ronald Cron277a85f2020-08-04 15:49:48 +0200390/** Remove non-essential copies of key material from memory.
391 *
392 * If the key identifier designates a volatile key, this functions does not do
393 * anything and returns successfully.
394 *
395 * If the key identifier designates a persistent key, then this function will
396 * free all resources associated with the key in volatile memory. The key
397 * data in persistent storage is not affected and the key can still be used.
398 *
399 * \param key Identifier of the key to purge.
400 *
401 * \retval #PSA_SUCCESS
402 * The key material will have been removed from memory if it is not
403 * currently required.
404 * \retval #PSA_ERROR_INVALID_ARGUMENT
405 * \p key is not a valid key identifier.
406 * \retval #PSA_ERROR_BAD_STATE
407 * The library has not been previously initialized by psa_crypto_init().
408 * It is implementation-dependent whether a failure to initialize
409 * results in this error code.
410 */
411psa_status_t psa_purge_key(mbedtls_svc_key_id_t key);
412
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100413/** Make a copy of a key.
414 *
415 * Copy key material from one location to another.
416 *
417 * This function is primarily useful to copy a key from one location
418 * to another, since it populates a key using the material from
419 * another key which may have a different lifetime.
420 *
421 * This function may be used to share a key with a different party,
422 * subject to implementation-defined restrictions on key sharing.
423 *
424 * The policy on the source key must have the usage flag
425 * #PSA_KEY_USAGE_COPY set.
426 * This flag is sufficient to permit the copy if the key has the lifetime
427 * #PSA_KEY_LIFETIME_VOLATILE or #PSA_KEY_LIFETIME_PERSISTENT.
428 * Some secure elements do not provide a way to copy a key without
429 * making it extractable from the secure element. If a key is located
430 * in such a secure element, then the key must have both usage flags
431 * #PSA_KEY_USAGE_COPY and #PSA_KEY_USAGE_EXPORT in order to make
432 * a copy of the key outside the secure element.
433 *
434 * The resulting key may only be used in a way that conforms to
435 * both the policy of the original key and the policy specified in
436 * the \p attributes parameter:
437 * - The usage flags on the resulting key are the bitwise-and of the
438 * usage flags on the source policy and the usage flags in \p attributes.
439 * - If both allow the same algorithm or wildcard-based
440 * algorithm policy, the resulting key has the same algorithm policy.
441 * - If either of the policies allows an algorithm and the other policy
442 * allows a wildcard-based algorithm policy that includes this algorithm,
443 * the resulting key allows the same algorithm.
444 * - If the policies do not allow any algorithm in common, this function
445 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
446 *
447 * The effect of this function on implementation-defined attributes is
448 * implementation-defined.
449 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200450 * \param source_key The key to copy. It must allow the usage
451 * PSA_KEY_USAGE_COPY. If a private or secret key is
452 * being copied outside of a secure element it must
453 * also allow PSA_KEY_USAGE_EXPORT.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100454 * \param[in] attributes The attributes for the new key.
455 * They are used as follows:
456 * - The key type and size may be 0. If either is
457 * nonzero, it must match the corresponding
458 * attribute of the source key.
459 * - The key location (the lifetime and, for
460 * persistent keys, the key identifier) is
461 * used directly.
462 * - The policy constraints (usage flags and
463 * algorithm policy) are combined from
464 * the source key and \p attributes so that
465 * both sets of restrictions apply, as
466 * described in the documentation of this function.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200467 * \param[out] target_key On success, an identifier for the newly created
468 * key. \c 0 on failure.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100469 *
470 * \retval #PSA_SUCCESS
471 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200472 * \p source_key is invalid.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100473 * \retval #PSA_ERROR_ALREADY_EXISTS
474 * This is an attempt to create a persistent key, and there is
475 * already a persistent key with the given identifier.
476 * \retval #PSA_ERROR_INVALID_ARGUMENT
477 * The lifetime or identifier in \p attributes are invalid.
478 * \retval #PSA_ERROR_INVALID_ARGUMENT
479 * The policy constraints on the source and specified in
480 * \p attributes are incompatible.
481 * \retval #PSA_ERROR_INVALID_ARGUMENT
482 * \p attributes specifies a key type or key size
483 * which does not match the attributes of the source key.
484 * \retval #PSA_ERROR_NOT_PERMITTED
485 * The source key does not have the #PSA_KEY_USAGE_COPY usage flag.
486 * \retval #PSA_ERROR_NOT_PERMITTED
487 * The source key is not exportable and its lifetime does not
488 * allow copying it to the target's lifetime.
489 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
490 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
491 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
492 * \retval #PSA_ERROR_HARDWARE_FAILURE
493 * \retval #PSA_ERROR_STORAGE_FAILURE
494 * \retval #PSA_ERROR_CORRUPTION_DETECTED
495 * \retval #PSA_ERROR_BAD_STATE
496 * The library has not been previously initialized by psa_crypto_init().
497 * It is implementation-dependent whether a failure to initialize
498 * results in this error code.
499 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200500psa_status_t psa_copy_key(mbedtls_svc_key_id_t source_key,
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100501 const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200502 mbedtls_svc_key_id_t *target_key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100503
504
505/**
506 * \brief Destroy a key.
507 *
508 * This function destroys a key from both volatile
509 * memory and, if applicable, non-volatile storage. Implementations shall
510 * make a best effort to ensure that that the key material cannot be recovered.
511 *
512 * This function also erases any metadata such as policies and frees
Ronald Croncf56a0a2020-08-04 09:51:30 +0200513 * resources associated with the key.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100514 *
515 * If a key is currently in use in a multipart operation, then destroying the
516 * key will cause the multipart operation to fail.
517 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200518 * \param key Identifier of the key to erase. If this is \c 0, do nothing and
519 * return PSA_SUCCESS.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100520 *
521 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +0200522 * \p key was a valid identifier and the key material that it
523 * referred to has been erased. Alternatively, \p key is \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100524 * \retval #PSA_ERROR_NOT_PERMITTED
525 * The key cannot be erased because it is
526 * read-only, either due to a policy or due to physical restrictions.
527 * \retval #PSA_ERROR_INVALID_HANDLE
Ronald Croncf56a0a2020-08-04 09:51:30 +0200528 * \p key is not a valid identifier nor \c 0.
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100529 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
530 * There was an failure in communication with the cryptoprocessor.
531 * The key material may still be present in the cryptoprocessor.
532 * \retval #PSA_ERROR_STORAGE_FAILURE
533 * The storage is corrupted. Implementations shall make a best effort
534 * to erase key material even in this stage, however applications
535 * should be aware that it may be impossible to guarantee that the
536 * key material is not recoverable in such cases.
537 * \retval #PSA_ERROR_CORRUPTION_DETECTED
538 * An unexpected condition which is not a storage corruption or
539 * a communication failure occurred. The cryptoprocessor may have
540 * been compromised.
541 * \retval #PSA_ERROR_BAD_STATE
542 * The library has not been previously initialized by psa_crypto_init().
543 * It is implementation-dependent whether a failure to initialize
544 * results in this error code.
545 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200546psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100547
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100548/**@}*/
549
550/** \defgroup import_export Key import and export
551 * @{
552 */
553
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554/**
555 * \brief Import a key in binary format.
556 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100557 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100558 * documentation of psa_export_public_key() for the format of public keys
559 * and to the documentation of psa_export_key() for the format for
560 * other key types.
561 *
Gilles Peskine05c900b2019-09-12 18:29:43 +0200562 * The key data determines the key size. The attributes may optionally
563 * specify a key size; in this case it must match the size determined
564 * from the key data. A key size of 0 in \p attributes indicates that
565 * the key size is solely determined by the key data.
566 *
567 * Implementations must reject an attempt to import a key of size 0.
568 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100569 * This specification supports a single format for each key type.
570 * Implementations may support other formats as long as the standard
571 * format is supported. Implementations that support other formats
572 * should ensure that the formats are clearly unambiguous so as to
573 * minimize the risk that an invalid input is accidentally interpreted
574 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100575 *
Gilles Peskine20628592019-04-19 19:29:50 +0200576 * \param[in] attributes The attributes for the new key.
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200577 * The key size is always determined from the
578 * \p data buffer.
579 * If the key size in \p attributes is nonzero,
580 * it must be equal to the size from \p data.
Ronald Croncf56a0a2020-08-04 09:51:30 +0200581 * \param[out] key On success, an identifier to the newly created key.
Gilles Peskine20628592019-04-19 19:29:50 +0200582 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100583 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine24f10f82019-05-16 12:18:32 +0200584 * buffer is interpreted according to the type declared
585 * in \p attributes.
Gilles Peskine20628592019-04-19 19:29:50 +0200586 * All implementations must support at least the format
587 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100588 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200589 * the chosen type. Implementations may allow other
590 * formats, but should be conservative: implementations
591 * should err on the side of rejecting content if it
592 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200593 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100594 *
Gilles Peskine28538492018-07-11 17:34:00 +0200595 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100596 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100597 * If the key is persistent, the key material and the key's metadata
598 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200599 * \retval #PSA_ERROR_ALREADY_EXISTS
600 * This is an attempt to create a persistent key, and there is
601 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200602 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200603 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200604 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200605 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +0200606 * The key attributes, as a whole, are invalid.
607 * \retval #PSA_ERROR_INVALID_ARGUMENT
608 * The key data is not correctly formatted.
609 * \retval #PSA_ERROR_INVALID_ARGUMENT
610 * The size in \p attributes is nonzero and does not match the size
611 * of the key data.
Gilles Peskine28538492018-07-11 17:34:00 +0200612 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
613 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
614 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100615 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200616 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200617 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300618 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300619 * The library has not been previously initialized by psa_crypto_init().
620 * It is implementation-dependent whether a failure to initialize
621 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100622 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200623psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +0200625 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +0200626 mbedtls_svc_key_id_t *key);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100627
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100628
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100629
630/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100631 * \brief Export a key in binary format.
632 *
633 * The output of this function can be passed to psa_import_key() to
634 * create an equivalent object.
635 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100636 * If the implementation of psa_import_key() supports other formats
637 * beyond the format specified here, the output from psa_export_key()
638 * must use the representation specified here, not the original
639 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100640 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100641 * For standard key types, the output format is as follows:
642 *
643 * - For symmetric keys (including MAC keys), the format is the
644 * raw bytes of the key.
645 * - For DES, the key data consists of 8 bytes. The parity bits must be
646 * correct.
647 * - For Triple-DES, the format is the concatenation of the
648 * two or three DES keys.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200649 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEY_PAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200650 * is the non-encrypted DER encoding of the representation defined by
651 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
652 * ```
653 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200654 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200655 * modulus INTEGER, -- n
656 * publicExponent INTEGER, -- e
657 * privateExponent INTEGER, -- d
658 * prime1 INTEGER, -- p
659 * prime2 INTEGER, -- q
660 * exponent1 INTEGER, -- d mod (p-1)
661 * exponent2 INTEGER, -- d mod (q-1)
662 * coefficient INTEGER, -- (inverse of q) mod p
663 * }
664 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200665 * - For elliptic curve key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200666 * #PSA_KEY_TYPE_IS_ECC_KEY_PAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100667 * a representation of the private value as a `ceiling(m/8)`-byte string
668 * where `m` is the bit size associated with the curve, i.e. the bit size
669 * of the order of the curve's coordinate field. This byte string is
670 * in little-endian order for Montgomery curves (curve types
Paul Elliott8ff510a2020-06-02 17:19:28 +0100671 * `PSA_ECC_FAMILY_CURVEXXX`), and in big-endian order for Weierstrass
672 * curves (curve types `PSA_ECC_FAMILY_SECTXXX`, `PSA_ECC_FAMILY_SECPXXX`
673 * and `PSA_ECC_FAMILY_BRAINPOOL_PXXX`).
Steven Cooreman6f5cc712020-06-11 16:40:41 +0200674 * For Weierstrass curves, this is the content of the `privateKey` field of
675 * the `ECPrivateKey` format defined by RFC 5915. For Montgomery curves,
676 * the format is defined by RFC 7748, and output is masked according to §5.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200677 * - For Diffie-Hellman key exchange key pairs (key types for which
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200678 * #PSA_KEY_TYPE_IS_DH_KEY_PAIR is true), the
Jaeden Amero8851c402019-01-11 14:20:03 +0000679 * format is the representation of the private key `x` as a big-endian byte
680 * string. The length of the byte string is the private key size in bytes
681 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200682 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
683 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100684 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200685 * The policy on the key must have the usage flag #PSA_KEY_USAGE_EXPORT set.
686 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200687 * \param key Identifier of the key to export. It must allow the
688 * usage PSA_KEY_USAGE_EXPORT, unless it is a public
689 * key.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200690 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200691 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200692 * \param[out] data_length On success, the number of bytes
693 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100694 *
Gilles Peskine28538492018-07-11 17:34:00 +0200695 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100696 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200697 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200698 * The key does not have the #PSA_KEY_USAGE_EXPORT flag.
Darryl Green9e2d7a02018-07-24 16:33:30 +0100699 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200700 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
701 * The size of the \p data buffer is too small. You can determine a
702 * sufficient buffer size by calling
703 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
704 * where \c type is the key type
705 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200706 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
707 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200708 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw89b71522019-08-06 16:21:00 +0100709 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw0542d592019-08-06 16:34:44 +0100710 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300711 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300712 * The library has not been previously initialized by psa_crypto_init().
713 * It is implementation-dependent whether a failure to initialize
714 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100715 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200716psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100717 uint8_t *data,
718 size_t data_size,
719 size_t *data_length);
720
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100721/**
722 * \brief Export a public key or the public part of a key pair in binary format.
723 *
724 * The output of this function can be passed to psa_import_key() to
725 * create an object that is equivalent to the public key.
726 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000727 * This specification supports a single format for each key type.
728 * Implementations may support other formats as long as the standard
729 * format is supported. Implementations that support other formats
730 * should ensure that the formats are clearly unambiguous so as to
731 * minimize the risk that an invalid input is accidentally interpreted
732 * according to a different format.
733 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000734 * For standard key types, the output format is as follows:
735 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
736 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
737 * ```
738 * RSAPublicKey ::= SEQUENCE {
739 * modulus INTEGER, -- n
740 * publicExponent INTEGER } -- e
741 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000742 * - For elliptic curve public keys (key types for which
743 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
744 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
745 * Let `m` be the bit size associated with the curve, i.e. the bit size of
746 * `q` for a curve over `F_q`. The representation consists of:
747 * - The byte 0x04;
748 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
749 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200750 * - For Diffie-Hellman key exchange public keys (key types for which
751 * #PSA_KEY_TYPE_IS_DH_PUBLIC_KEY is true),
Jaeden Amero8851c402019-01-11 14:20:03 +0000752 * the format is the representation of the public key `y = g^x mod p` as a
753 * big-endian byte string. The length of the byte string is the length of the
754 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100755 *
Gilles Peskine4318dfc2019-05-14 14:23:32 +0200756 * Exporting a public key object or the public part of a key pair is
757 * always permitted, regardless of the key's usage flags.
758 *
Ronald Croncf56a0a2020-08-04 09:51:30 +0200759 * \param key Identifier of the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200760 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200761 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200762 * \param[out] data_length On success, the number of bytes
763 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100764 *
Gilles Peskine28538492018-07-11 17:34:00 +0200765 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100766 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200767 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200768 * The key is neither a public key nor a key pair.
769 * \retval #PSA_ERROR_NOT_SUPPORTED
770 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
771 * The size of the \p data buffer is too small. You can determine a
772 * sufficient buffer size by calling
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200773 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\c type), \c bits)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200774 * where \c type is the key type
775 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200776 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
777 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200778 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw398b3c22019-08-06 17:22:41 +0100779 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw88c51ad2019-08-06 17:09:33 +0100780 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
itayzafrir90d8c7a2018-09-12 11:44:52 +0300781 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300782 * The library has not been previously initialized by psa_crypto_init().
783 * It is implementation-dependent whether a failure to initialize
784 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100785 */
Ronald Croncf56a0a2020-08-04 09:51:30 +0200786psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100787 uint8_t *data,
788 size_t data_size,
789 size_t *data_length);
790
Adrian L. Shaw4c61c1a2019-09-11 14:40:51 +0100791
Gilles Peskine20035e32018-02-03 22:44:14 +0100792
793/**@}*/
794
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100795/** \defgroup hash Message digests
796 * @{
797 */
798
Gilles Peskine69647a42019-01-14 20:18:12 +0100799/** Calculate the hash (digest) of a message.
800 *
801 * \note To verify the hash of a message against an
802 * expected value, use psa_hash_compare() instead.
803 *
804 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
805 * such that #PSA_ALG_IS_HASH(\p alg) is true).
806 * \param[in] input Buffer containing the message to hash.
807 * \param input_length Size of the \p input buffer in bytes.
808 * \param[out] hash Buffer where the hash is to be written.
809 * \param hash_size Size of the \p hash buffer in bytes.
810 * \param[out] hash_length On success, the number of bytes
811 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100812 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100813 *
814 * \retval #PSA_SUCCESS
815 * Success.
816 * \retval #PSA_ERROR_NOT_SUPPORTED
817 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +0100818 * \retval #PSA_ERROR_INVALID_ARGUMENT
Adrian L. Shawf7d852a2019-08-06 17:50:26 +0100819 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
820 * \p hash_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +0100821 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
822 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
823 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200824 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw7f1863c2019-08-06 16:34:44 +0100825 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100826 * \retval #PSA_ERROR_BAD_STATE
827 * The library has not been previously initialized by psa_crypto_init().
828 * It is implementation-dependent whether a failure to initialize
829 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100830 */
831psa_status_t psa_hash_compute(psa_algorithm_t alg,
832 const uint8_t *input,
833 size_t input_length,
834 uint8_t *hash,
835 size_t hash_size,
836 size_t *hash_length);
837
838/** Calculate the hash (digest) of a message and compare it with a
839 * reference value.
840 *
841 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
842 * such that #PSA_ALG_IS_HASH(\p alg) is true).
843 * \param[in] input Buffer containing the message to hash.
844 * \param input_length Size of the \p input buffer in bytes.
845 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100846 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100847 *
848 * \retval #PSA_SUCCESS
849 * The expected hash is identical to the actual hash of the input.
850 * \retval #PSA_ERROR_INVALID_SIGNATURE
851 * The hash of the message was calculated successfully, but it
852 * differs from the expected hash.
853 * \retval #PSA_ERROR_NOT_SUPPORTED
854 * \p alg is not supported or is not a hash algorithm.
Adrian L. Shaw8d0bcf22019-08-13 11:36:29 +0100855 * \retval #PSA_ERROR_INVALID_ARGUMENT
856 * \p input_length or \p hash_length do not match the hash size for \p alg
Gilles Peskine69647a42019-01-14 20:18:12 +0100857 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
858 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
859 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200860 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw11638b92019-08-06 17:09:33 +0100861 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100862 * \retval #PSA_ERROR_BAD_STATE
863 * The library has not been previously initialized by psa_crypto_init().
864 * It is implementation-dependent whether a failure to initialize
865 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +0100866 */
867psa_status_t psa_hash_compare(psa_algorithm_t alg,
868 const uint8_t *input,
869 size_t input_length,
870 const uint8_t *hash,
Gilles Peskinefa710f52019-12-02 14:31:48 +0100871 size_t hash_length);
Gilles Peskine69647a42019-01-14 20:18:12 +0100872
Gilles Peskine308b91d2018-02-08 09:47:44 +0100873/** The type of the state data structure for multipart hash operations.
874 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000875 * Before calling any function on a hash operation object, the application must
876 * initialize it by any of the following means:
877 * - Set the structure to all-bits-zero, for example:
878 * \code
879 * psa_hash_operation_t operation;
880 * memset(&operation, 0, sizeof(operation));
881 * \endcode
882 * - Initialize the structure to logical zero values, for example:
883 * \code
884 * psa_hash_operation_t operation = {0};
885 * \endcode
886 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
887 * for example:
888 * \code
889 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
890 * \endcode
891 * - Assign the result of the function psa_hash_operation_init()
892 * to the structure, for example:
893 * \code
894 * psa_hash_operation_t operation;
895 * operation = psa_hash_operation_init();
896 * \endcode
897 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100898 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100899 * make any assumptions about the content of this structure except
900 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100901typedef struct psa_hash_operation_s psa_hash_operation_t;
902
Jaeden Amero6a25b412019-01-04 11:47:44 +0000903/** \def PSA_HASH_OPERATION_INIT
904 *
905 * This macro returns a suitable initializer for a hash operation object
906 * of type #psa_hash_operation_t.
907 */
908#ifdef __DOXYGEN_ONLY__
909/* This is an example definition for documentation purposes.
910 * Implementations should define a suitable value in `crypto_struct.h`.
911 */
912#define PSA_HASH_OPERATION_INIT {0}
913#endif
914
915/** Return an initial value for a hash operation object.
916 */
917static psa_hash_operation_t psa_hash_operation_init(void);
918
Gilles Peskinef45adda2019-01-14 18:29:18 +0100919/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100920 *
921 * The sequence of operations to calculate a hash (message digest)
922 * is as follows:
923 * -# Allocate an operation object which will be passed to all the functions
924 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000925 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100926 * documentation for #psa_hash_operation_t, e.g. #PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200927 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100928 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100929 * of the message each time. The hash that is calculated is the hash
930 * of the concatenation of these messages in order.
931 * -# To calculate the hash, call psa_hash_finish().
932 * To compare the hash with an expected value, call psa_hash_verify().
933 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100934 * If an error occurs at any step after a call to psa_hash_setup(), the
935 * operation will need to be reset by a call to psa_hash_abort(). The
936 * application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000937 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100938 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200939 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100940 * eventually terminate the operation. The following events terminate an
941 * operation:
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100942 * - A successful call to psa_hash_finish() or psa_hash_verify().
943 * - A call to psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100944 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000945 * \param[in,out] operation The operation object to set up. It must have
946 * been initialized as per the documentation for
947 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200948 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
949 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100950 *
Gilles Peskine28538492018-07-11 17:34:00 +0200951 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100952 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200953 * \retval #PSA_ERROR_NOT_SUPPORTED
Adrian L. Shawfbf7f122019-08-15 13:34:51 +0100954 * \p alg is not a supported hash algorithm.
955 * \retval #PSA_ERROR_INVALID_ARGUMENT
956 * \p alg is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100957 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100958 * The operation state is not valid (it must be inactive).
Gilles Peskine28538492018-07-11 17:34:00 +0200959 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
960 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
961 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200962 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100963 * \retval #PSA_ERROR_BAD_STATE
964 * The library has not been previously initialized by psa_crypto_init().
965 * It is implementation-dependent whether a failure to initialize
966 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100967 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200968psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100969 psa_algorithm_t alg);
970
Gilles Peskine308b91d2018-02-08 09:47:44 +0100971/** Add a message fragment to a multipart hash operation.
972 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200973 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100974 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100975 * If this function returns an error status, the operation enters an error
976 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100977 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200978 * \param[in,out] operation Active hash operation.
979 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200980 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100981 *
Gilles Peskine28538492018-07-11 17:34:00 +0200982 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100983 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200984 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +0100985 * The operation state is not valid (it muct be active).
Gilles Peskine28538492018-07-11 17:34:00 +0200986 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
987 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
988 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200989 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +0100990 * \retval #PSA_ERROR_BAD_STATE
991 * The library has not been previously initialized by psa_crypto_init().
992 * It is implementation-dependent whether a failure to initialize
993 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100994 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100995psa_status_t psa_hash_update(psa_hash_operation_t *operation,
996 const uint8_t *input,
997 size_t input_length);
998
Gilles Peskine308b91d2018-02-08 09:47:44 +0100999/** Finish the calculation of the hash of a message.
1000 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001001 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001002 * This function calculates the hash of the message formed by concatenating
1003 * the inputs passed to preceding calls to psa_hash_update().
1004 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001005 * When this function returns successfuly, the operation becomes inactive.
1006 * If this function returns an error status, the operation enters an error
1007 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001008 *
1009 * \warning Applications should not call this function if they expect
1010 * a specific value for the hash. Call psa_hash_verify() instead.
1011 * Beware that comparing integrity or authenticity data such as
1012 * hash values with a function such as \c memcmp is risky
1013 * because the time taken by the comparison may leak information
1014 * about the hashed data which could allow an attacker to guess
1015 * a valid hash and thereby bypass security controls.
1016 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001017 * \param[in,out] operation Active hash operation.
1018 * \param[out] hash Buffer where the hash is to be written.
1019 * \param hash_size Size of the \p hash buffer in bytes.
1020 * \param[out] hash_length On success, the number of bytes
1021 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001022 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001023 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001024 *
Gilles Peskine28538492018-07-11 17:34:00 +02001025 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001026 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001027 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001028 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001029 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001030 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001031 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001032 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001033 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1034 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1035 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001036 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001037 * \retval #PSA_ERROR_BAD_STATE
1038 * The library has not been previously initialized by psa_crypto_init().
1039 * It is implementation-dependent whether a failure to initialize
1040 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001041 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001042psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1043 uint8_t *hash,
1044 size_t hash_size,
1045 size_t *hash_length);
1046
Gilles Peskine308b91d2018-02-08 09:47:44 +01001047/** Finish the calculation of the hash of a message and compare it with
1048 * an expected value.
1049 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001050 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001051 * This function calculates the hash of the message formed by concatenating
1052 * the inputs passed to preceding calls to psa_hash_update(). It then
1053 * compares the calculated hash with the expected hash passed as a
1054 * parameter to this function.
1055 *
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001056 * When this function returns successfuly, the operation becomes inactive.
1057 * If this function returns an error status, the operation enters an error
1058 * state and must be aborted by calling psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001059 *
Gilles Peskine19067982018-03-20 17:54:53 +01001060 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001061 * comparison between the actual hash and the expected hash is performed
1062 * in constant time.
1063 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001064 * \param[in,out] operation Active hash operation.
1065 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001066 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001067 *
Gilles Peskine28538492018-07-11 17:34:00 +02001068 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001069 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001070 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001071 * The hash of the message was calculated successfully, but it
1072 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001073 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001074 * The operation state is not valid (it must be active).
Gilles Peskine28538492018-07-11 17:34:00 +02001075 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1076 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1077 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001078 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001079 * \retval #PSA_ERROR_BAD_STATE
1080 * The library has not been previously initialized by psa_crypto_init().
1081 * It is implementation-dependent whether a failure to initialize
1082 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001083 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001084psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1085 const uint8_t *hash,
1086 size_t hash_length);
1087
Gilles Peskine308b91d2018-02-08 09:47:44 +01001088/** Abort a hash operation.
1089 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001090 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001091 * \p operation structure itself. Once aborted, the operation object
1092 * can be reused for another operation by calling
1093 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001095 * You may call this function any time after the operation object has
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001096 * been initialized by one of the methods described in #psa_hash_operation_t.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001097 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001098 * In particular, calling psa_hash_abort() after the operation has been
1099 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1100 * psa_hash_verify() is safe and has no effect.
1101 *
1102 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001103 *
Gilles Peskine28538492018-07-11 17:34:00 +02001104 * \retval #PSA_SUCCESS
Gilles Peskine28538492018-07-11 17:34:00 +02001105 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1106 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001107 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001108 * \retval #PSA_ERROR_BAD_STATE
1109 * The library has not been previously initialized by psa_crypto_init().
1110 * It is implementation-dependent whether a failure to initialize
1111 * results in this error code.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001112 */
1113psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001114
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001115/** Clone a hash operation.
1116 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001117 * This function copies the state of an ongoing hash operation to
1118 * a new operation object. In other words, this function is equivalent
1119 * to calling psa_hash_setup() on \p target_operation with the same
1120 * algorithm that \p source_operation was set up for, then
1121 * psa_hash_update() on \p target_operation with the same input that
1122 * that was passed to \p source_operation. After this function returns, the
1123 * two objects are independent, i.e. subsequent calls involving one of
1124 * the objects do not affect the other object.
1125 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001126 * \param[in] source_operation The active hash operation to clone.
1127 * \param[in,out] target_operation The operation object to set up.
1128 * It must be initialized but not active.
1129 *
1130 * \retval #PSA_SUCCESS
1131 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001132 * The \p source_operation state is not valid (it must be active).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001133 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke272ba1d2019-09-11 22:53:21 +01001134 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001135 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1136 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001137 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001138 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001139 * \retval #PSA_ERROR_BAD_STATE
1140 * The library has not been previously initialized by psa_crypto_init().
1141 * It is implementation-dependent whether a failure to initialize
1142 * results in this error code.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001143 */
1144psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1145 psa_hash_operation_t *target_operation);
1146
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001147/**@}*/
1148
Gilles Peskine8c9def32018-02-08 10:02:12 +01001149/** \defgroup MAC Message authentication codes
1150 * @{
1151 */
1152
Gilles Peskine69647a42019-01-14 20:18:12 +01001153/** Calculate the MAC (message authentication code) of a message.
1154 *
1155 * \note To verify the MAC of a message against an
1156 * expected value, use psa_mac_verify() instead.
1157 * Beware that comparing integrity or authenticity data such as
1158 * MAC values with a function such as \c memcmp is risky
1159 * because the time taken by the comparison may leak information
1160 * about the MAC value which could allow an attacker to guess
1161 * a valid MAC and thereby bypass security controls.
1162 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001163 * \param key Identifier of the key to use for the operation. It
1164 * must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001165 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001166 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001167 * \param[in] input Buffer containing the input message.
1168 * \param input_length Size of the \p input buffer in bytes.
1169 * \param[out] mac Buffer where the MAC value is to be written.
1170 * \param mac_size Size of the \p mac buffer in bytes.
1171 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001172 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001173 *
1174 * \retval #PSA_SUCCESS
1175 * Success.
1176 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001177 * \retval #PSA_ERROR_NOT_PERMITTED
1178 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001179 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001180 * \retval #PSA_ERROR_NOT_SUPPORTED
1181 * \p alg is not supported or is not a MAC algorithm.
Adrian L. Shawd5ae06b2019-08-07 15:59:33 +01001182 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1183 * \p mac_size is too small
Gilles Peskine69647a42019-01-14 20:18:12 +01001184 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1185 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1186 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001187 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa591c42019-08-07 10:47:47 +01001188 * \retval #PSA_ERROR_STORAGE_FAILURE
1189 * The key could not be retrieved from storage.
Gilles Peskine69647a42019-01-14 20:18:12 +01001190 * \retval #PSA_ERROR_BAD_STATE
1191 * The library has not been previously initialized by psa_crypto_init().
1192 * It is implementation-dependent whether a failure to initialize
1193 * results in this error code.
1194 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001195psa_status_t psa_mac_compute(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001196 psa_algorithm_t alg,
1197 const uint8_t *input,
1198 size_t input_length,
1199 uint8_t *mac,
1200 size_t mac_size,
1201 size_t *mac_length);
1202
1203/** Calculate the MAC of a message and compare it with a reference value.
1204 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001205 * \param key Identifier of the key to use for the operation. It
1206 * must allow the usage PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskine69647a42019-01-14 20:18:12 +01001207 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001208 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001209 * \param[in] input Buffer containing the input message.
1210 * \param input_length Size of the \p input buffer in bytes.
1211 * \param[out] mac Buffer containing the expected MAC value.
1212 * \param mac_length Size of the \p mac buffer in bytes.
1213 *
1214 * \retval #PSA_SUCCESS
1215 * The expected MAC is identical to the actual MAC of the input.
1216 * \retval #PSA_ERROR_INVALID_SIGNATURE
1217 * The MAC of the message was calculated successfully, but it
1218 * differs from the expected value.
1219 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001220 * \retval #PSA_ERROR_NOT_PERMITTED
1221 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001222 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001223 * \retval #PSA_ERROR_NOT_SUPPORTED
1224 * \p alg is not supported or is not a MAC algorithm.
1225 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1226 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1227 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001228 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001229 * \retval #PSA_ERROR_STORAGE_FAILURE
1230 * The key could not be retrieved from storage.
1231 * \retval #PSA_ERROR_BAD_STATE
1232 * The library has not been previously initialized by psa_crypto_init().
1233 * It is implementation-dependent whether a failure to initialize
1234 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001235 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001236psa_status_t psa_mac_verify(mbedtls_svc_key_id_t key,
Gilles Peskinea05602d2019-01-17 15:25:52 +01001237 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001238 const uint8_t *input,
1239 size_t input_length,
1240 const uint8_t *mac,
Gilles Peskine13faa2d2020-01-30 16:32:21 +01001241 size_t mac_length);
Gilles Peskine69647a42019-01-14 20:18:12 +01001242
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001243/** The type of the state data structure for multipart MAC operations.
1244 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001245 * Before calling any function on a MAC operation object, the application must
1246 * initialize it by any of the following means:
1247 * - Set the structure to all-bits-zero, for example:
1248 * \code
1249 * psa_mac_operation_t operation;
1250 * memset(&operation, 0, sizeof(operation));
1251 * \endcode
1252 * - Initialize the structure to logical zero values, for example:
1253 * \code
1254 * psa_mac_operation_t operation = {0};
1255 * \endcode
1256 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1257 * for example:
1258 * \code
1259 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1260 * \endcode
1261 * - Assign the result of the function psa_mac_operation_init()
1262 * to the structure, for example:
1263 * \code
1264 * psa_mac_operation_t operation;
1265 * operation = psa_mac_operation_init();
1266 * \endcode
1267 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001268 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001269 * make any assumptions about the content of this structure except
1270 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001271typedef struct psa_mac_operation_s psa_mac_operation_t;
1272
Jaeden Amero769ce272019-01-04 11:48:03 +00001273/** \def PSA_MAC_OPERATION_INIT
1274 *
1275 * This macro returns a suitable initializer for a MAC operation object of type
1276 * #psa_mac_operation_t.
1277 */
1278#ifdef __DOXYGEN_ONLY__
1279/* This is an example definition for documentation purposes.
1280 * Implementations should define a suitable value in `crypto_struct.h`.
1281 */
1282#define PSA_MAC_OPERATION_INIT {0}
1283#endif
1284
1285/** Return an initial value for a MAC operation object.
1286 */
1287static psa_mac_operation_t psa_mac_operation_init(void);
1288
Gilles Peskinef45adda2019-01-14 18:29:18 +01001289/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001290 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001291 * This function sets up the calculation of the MAC
1292 * (message authentication code) of a byte string.
1293 * To verify the MAC of a message against an
1294 * expected value, use psa_mac_verify_setup() instead.
1295 *
1296 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001297 * -# Allocate an operation object which will be passed to all the functions
1298 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001299 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001300 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001301 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001302 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1303 * of the message each time. The MAC that is calculated is the MAC
1304 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001305 * -# At the end of the message, call psa_mac_sign_finish() to finish
1306 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001307 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001308 * If an error occurs at any step after a call to psa_mac_sign_setup(), the
1309 * operation will need to be reset by a call to psa_mac_abort(). The
1310 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001311 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001312 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001313 * After a successful call to psa_mac_sign_setup(), the application must
1314 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001315 * - A successful call to psa_mac_sign_finish().
1316 * - A call to psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001317 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001318 * \param[in,out] operation The operation object to set up. It must have
1319 * been initialized as per the documentation for
1320 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001321 * \param key Identifier of the key to use for the operation. It
1322 * must remain valid until the operation terminates.
1323 * It must allow the usage PSA_KEY_USAGE_SIGN_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001324 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001325 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001326 *
Gilles Peskine28538492018-07-11 17:34:00 +02001327 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001328 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001329 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001330 * \retval #PSA_ERROR_NOT_PERMITTED
1331 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001332 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001333 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001334 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001335 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1336 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1337 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001338 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw2409ba02019-08-07 16:05:06 +01001339 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdf3c7ac2019-08-12 16:43:30 +01001340 * The key could not be retrieved from storage.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001341 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001342 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001343 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001344 * The library has not been previously initialized by psa_crypto_init().
1345 * It is implementation-dependent whether a failure to initialize
1346 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001347 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001348psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001349 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001350 psa_algorithm_t alg);
1351
Gilles Peskinef45adda2019-01-14 18:29:18 +01001352/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001353 *
1354 * This function sets up the verification of the MAC
1355 * (message authentication code) of a byte string against an expected value.
1356 *
1357 * The sequence of operations to verify a MAC is as follows:
1358 * -# Allocate an operation object which will be passed to all the functions
1359 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001360 * -# Initialize the operation object with one of the methods described in the
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001361 * documentation for #psa_mac_operation_t, e.g. #PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001362 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001363 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1364 * of the message each time. The MAC that is calculated is the MAC
1365 * of the concatenation of these messages in order.
1366 * -# At the end of the message, call psa_mac_verify_finish() to finish
1367 * calculating the actual MAC of the message and verify it against
1368 * the expected value.
1369 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001370 * If an error occurs at any step after a call to psa_mac_verify_setup(), the
1371 * operation will need to be reset by a call to psa_mac_abort(). The
1372 * application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001373 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001374 *
1375 * After a successful call to psa_mac_verify_setup(), the application must
1376 * eventually terminate the operation through one of the following methods:
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001377 * - A successful call to psa_mac_verify_finish().
1378 * - A call to psa_mac_abort().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001379 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001380 * \param[in,out] operation The operation object to set up. It must have
1381 * been initialized as per the documentation for
1382 * #psa_mac_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001383 * \param key Identifier of the key to use for the operation. It
1384 * must remain valid until the operation terminates.
1385 * It must allow the usage
1386 * PSA_KEY_USAGE_VERIFY_MESSAGE.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001387 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1388 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001389 *
Gilles Peskine28538492018-07-11 17:34:00 +02001390 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001391 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001392 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001393 * \retval #PSA_ERROR_NOT_PERMITTED
1394 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001395 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001396 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001397 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001398 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1399 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1400 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001401 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw9770d0e2019-08-07 16:18:18 +01001402 * \retval #PSA_ERROR_STORAGE_FAILURE
1403 * The key could not be retrieved from storage
itayzafrir90d8c7a2018-09-12 11:44:52 +03001404 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001405 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001406 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001407 * The library has not been previously initialized by psa_crypto_init().
1408 * It is implementation-dependent whether a failure to initialize
1409 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001410 */
1411psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001412 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001413 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001414
Gilles Peskinedcd14942018-07-12 00:30:52 +02001415/** Add a message fragment to a multipart MAC operation.
1416 *
1417 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1418 * before calling this function.
1419 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001420 * If this function returns an error status, the operation enters an error
1421 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001422 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001423 * \param[in,out] operation Active MAC operation.
1424 * \param[in] input Buffer containing the message fragment to add to
1425 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001426 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001427 *
1428 * \retval #PSA_SUCCESS
1429 * Success.
1430 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001431 * The operation state is not valid (it must be active).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001432 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1433 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1434 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001435 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd789dc12019-08-12 15:06:48 +01001436 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001437 * \retval #PSA_ERROR_BAD_STATE
1438 * The library has not been previously initialized by psa_crypto_init().
1439 * It is implementation-dependent whether a failure to initialize
1440 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001441 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001442psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1443 const uint8_t *input,
1444 size_t input_length);
1445
Gilles Peskinedcd14942018-07-12 00:30:52 +02001446/** Finish the calculation of the MAC of a message.
1447 *
1448 * The application must call psa_mac_sign_setup() before calling this function.
1449 * This function calculates the MAC of the message formed by concatenating
1450 * the inputs passed to preceding calls to psa_mac_update().
1451 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001452 * When this function returns successfuly, the operation becomes inactive.
1453 * If this function returns an error status, the operation enters an error
1454 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001455 *
1456 * \warning Applications should not call this function if they expect
1457 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1458 * Beware that comparing integrity or authenticity data such as
1459 * MAC values with a function such as \c memcmp is risky
1460 * because the time taken by the comparison may leak information
1461 * about the MAC value which could allow an attacker to guess
1462 * a valid MAC and thereby bypass security controls.
1463 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001464 * \param[in,out] operation Active MAC operation.
1465 * \param[out] mac Buffer where the MAC value is to be written.
1466 * \param mac_size Size of the \p mac buffer in bytes.
1467 * \param[out] mac_length On success, the number of bytes
1468 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001469 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001470 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001471 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001472 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001473 *
1474 * \retval #PSA_SUCCESS
1475 * Success.
1476 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001477 * The operation state is not valid (it must be an active mac sign
1478 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001479 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001480 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001481 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1482 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1483 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1484 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001485 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw26322362019-08-13 11:43:40 +01001486 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001487 * \retval #PSA_ERROR_BAD_STATE
1488 * The library has not been previously initialized by psa_crypto_init().
1489 * It is implementation-dependent whether a failure to initialize
1490 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001491 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001492psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1493 uint8_t *mac,
1494 size_t mac_size,
1495 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001496
Gilles Peskinedcd14942018-07-12 00:30:52 +02001497/** Finish the calculation of the MAC of a message and compare it with
1498 * an expected value.
1499 *
1500 * The application must call psa_mac_verify_setup() before calling this function.
1501 * This function calculates the MAC of the message formed by concatenating
1502 * the inputs passed to preceding calls to psa_mac_update(). It then
1503 * compares the calculated MAC with the expected MAC passed as a
1504 * parameter to this function.
1505 *
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001506 * When this function returns successfuly, the operation becomes inactive.
1507 * If this function returns an error status, the operation enters an error
1508 * state and must be aborted by calling psa_mac_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001509 *
1510 * \note Implementations shall make the best effort to ensure that the
1511 * comparison between the actual MAC and the expected MAC is performed
1512 * in constant time.
1513 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001514 * \param[in,out] operation Active MAC operation.
1515 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001516 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001517 *
1518 * \retval #PSA_SUCCESS
1519 * The expected MAC is identical to the actual MAC of the message.
1520 * \retval #PSA_ERROR_INVALID_SIGNATURE
1521 * The MAC of the message was calculated successfully, but it
1522 * differs from the expected MAC.
1523 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001524 * The operation state is not valid (it must be an active mac verify
1525 * operation).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001526 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1527 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1528 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001529 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd9e90242019-08-13 11:44:30 +01001530 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001531 * \retval #PSA_ERROR_BAD_STATE
1532 * The library has not been previously initialized by psa_crypto_init().
1533 * It is implementation-dependent whether a failure to initialize
1534 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001535 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001536psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1537 const uint8_t *mac,
1538 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001539
Gilles Peskinedcd14942018-07-12 00:30:52 +02001540/** Abort a MAC operation.
1541 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001542 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001543 * \p operation structure itself. Once aborted, the operation object
1544 * can be reused for another operation by calling
1545 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001546 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001547 * You may call this function any time after the operation object has
Andrew Thoelke9f208cc2019-09-11 23:04:42 +01001548 * been initialized by one of the methods described in #psa_mac_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001549 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001550 * In particular, calling psa_mac_abort() after the operation has been
1551 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1552 * psa_mac_verify_finish() is safe and has no effect.
1553 *
1554 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001555 *
1556 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02001557 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1558 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001559 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001560 * \retval #PSA_ERROR_BAD_STATE
1561 * The library has not been previously initialized by psa_crypto_init().
1562 * It is implementation-dependent whether a failure to initialize
1563 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001564 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001565psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1566
1567/**@}*/
1568
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001569/** \defgroup cipher Symmetric ciphers
1570 * @{
1571 */
1572
Gilles Peskine69647a42019-01-14 20:18:12 +01001573/** Encrypt a message using a symmetric cipher.
1574 *
1575 * This function encrypts a message with a random IV (initialization
Andrew Thoelke4104afb2019-09-18 17:47:25 +01001576 * vector). Use the multipart operation interface with a
1577 * #psa_cipher_operation_t object to provide other forms of IV.
Gilles Peskine69647a42019-01-14 20:18:12 +01001578 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001579 * \param key Identifier of the key to use for the operation.
1580 * It must allow the usage PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001581 * \param alg The cipher algorithm to compute
1582 * (\c PSA_ALG_XXX value such that
1583 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1584 * \param[in] input Buffer containing the message to encrypt.
1585 * \param input_length Size of the \p input buffer in bytes.
1586 * \param[out] output Buffer where the output is to be written.
1587 * The output contains the IV followed by
1588 * the ciphertext proper.
1589 * \param output_size Size of the \p output buffer in bytes.
1590 * \param[out] output_length On success, the number of bytes
1591 * that make up the output.
1592 *
1593 * \retval #PSA_SUCCESS
1594 * Success.
1595 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001596 * \retval #PSA_ERROR_NOT_PERMITTED
1597 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001598 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001599 * \retval #PSA_ERROR_NOT_SUPPORTED
1600 * \p alg is not supported or is not a cipher algorithm.
1601 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1602 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1603 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1604 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001605 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001606 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001607 * \retval #PSA_ERROR_BAD_STATE
1608 * The library has not been previously initialized by psa_crypto_init().
1609 * It is implementation-dependent whether a failure to initialize
1610 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001611 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001612psa_status_t psa_cipher_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001613 psa_algorithm_t alg,
1614 const uint8_t *input,
1615 size_t input_length,
1616 uint8_t *output,
1617 size_t output_size,
1618 size_t *output_length);
1619
1620/** Decrypt a message using a symmetric cipher.
1621 *
1622 * This function decrypts a message encrypted with a symmetric cipher.
1623 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02001624 * \param key Identifier of the key to use for the operation.
Gilles Peskine69647a42019-01-14 20:18:12 +01001625 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001626 * terminates. It must allow the usage
1627 * PSA_KEY_USAGE_DECRYPT.
Gilles Peskine69647a42019-01-14 20:18:12 +01001628 * \param alg The cipher algorithm to compute
1629 * (\c PSA_ALG_XXX value such that
1630 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1631 * \param[in] input Buffer containing the message to decrypt.
1632 * This consists of the IV followed by the
1633 * ciphertext proper.
1634 * \param input_length Size of the \p input buffer in bytes.
1635 * \param[out] output Buffer where the plaintext is to be written.
1636 * \param output_size Size of the \p output buffer in bytes.
1637 * \param[out] output_length On success, the number of bytes
1638 * that make up the output.
1639 *
1640 * \retval #PSA_SUCCESS
1641 * Success.
1642 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine69647a42019-01-14 20:18:12 +01001643 * \retval #PSA_ERROR_NOT_PERMITTED
1644 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001645 * \p key is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001646 * \retval #PSA_ERROR_NOT_SUPPORTED
1647 * \p alg is not supported or is not a cipher algorithm.
1648 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1649 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1650 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1651 * \retval #PSA_ERROR_HARDWARE_FAILURE
Adrian L. Shawa3f6ba52019-08-08 14:51:49 +01001652 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shaw39797aa2019-08-23 16:17:43 +01001653 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001654 * \retval #PSA_ERROR_BAD_STATE
1655 * The library has not been previously initialized by psa_crypto_init().
1656 * It is implementation-dependent whether a failure to initialize
Adrian L. Shaw23c006f2019-08-06 16:02:12 +01001657 * results in this error code.
Gilles Peskine69647a42019-01-14 20:18:12 +01001658 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001659psa_status_t psa_cipher_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine69647a42019-01-14 20:18:12 +01001660 psa_algorithm_t alg,
1661 const uint8_t *input,
1662 size_t input_length,
1663 uint8_t *output,
1664 size_t output_size,
1665 size_t *output_length);
1666
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001667/** The type of the state data structure for multipart cipher operations.
1668 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001669 * Before calling any function on a cipher operation object, the application
1670 * must initialize it by any of the following means:
1671 * - Set the structure to all-bits-zero, for example:
1672 * \code
1673 * psa_cipher_operation_t operation;
1674 * memset(&operation, 0, sizeof(operation));
1675 * \endcode
1676 * - Initialize the structure to logical zero values, for example:
1677 * \code
1678 * psa_cipher_operation_t operation = {0};
1679 * \endcode
1680 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1681 * for example:
1682 * \code
1683 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1684 * \endcode
1685 * - Assign the result of the function psa_cipher_operation_init()
1686 * to the structure, for example:
1687 * \code
1688 * psa_cipher_operation_t operation;
1689 * operation = psa_cipher_operation_init();
1690 * \endcode
1691 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001692 * This is an implementation-defined \c struct. Applications should not
1693 * make any assumptions about the content of this structure except
1694 * as directed by the documentation of a specific implementation. */
1695typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1696
Jaeden Amero5bae2272019-01-04 11:48:27 +00001697/** \def PSA_CIPHER_OPERATION_INIT
1698 *
1699 * This macro returns a suitable initializer for a cipher operation object of
1700 * type #psa_cipher_operation_t.
1701 */
1702#ifdef __DOXYGEN_ONLY__
1703/* This is an example definition for documentation purposes.
1704 * Implementations should define a suitable value in `crypto_struct.h`.
1705 */
1706#define PSA_CIPHER_OPERATION_INIT {0}
1707#endif
1708
1709/** Return an initial value for a cipher operation object.
1710 */
1711static psa_cipher_operation_t psa_cipher_operation_init(void);
1712
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001713/** Set the key for a multipart symmetric encryption operation.
1714 *
1715 * The sequence of operations to encrypt a message with a symmetric cipher
1716 * is as follows:
1717 * -# Allocate an operation object which will be passed to all the functions
1718 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001719 * -# Initialize the operation object with one of the methods described in the
1720 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001721 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001722 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001723 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001724 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001725 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001726 * requires a specific IV value.
1727 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1728 * of the message each time.
1729 * -# Call psa_cipher_finish().
1730 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001731 * If an error occurs at any step after a call to psa_cipher_encrypt_setup(),
1732 * the operation will need to be reset by a call to psa_cipher_abort(). The
1733 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001734 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001735 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001736 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001737 * eventually terminate the operation. The following events terminate an
1738 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001739 * - A successful call to psa_cipher_finish().
1740 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001741 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001742 * \param[in,out] operation The operation object to set up. It must have
1743 * been initialized as per the documentation for
1744 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001745 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001746 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001747 * terminates. It must allow the usage
1748 * PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001749 * \param alg The cipher algorithm to compute
1750 * (\c PSA_ALG_XXX value such that
1751 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001752 *
Gilles Peskine28538492018-07-11 17:34:00 +02001753 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001754 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001755 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001756 * \retval #PSA_ERROR_NOT_PERMITTED
1757 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001758 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001759 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001760 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001761 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1762 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1763 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001764 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001765 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001766 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001767 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001768 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001769 * The library has not been previously initialized by psa_crypto_init().
1770 * It is implementation-dependent whether a failure to initialize
1771 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001772 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001773psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001774 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001775 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001776
1777/** Set the key for a multipart symmetric decryption operation.
1778 *
1779 * The sequence of operations to decrypt a message with a symmetric cipher
1780 * is as follows:
1781 * -# Allocate an operation object which will be passed to all the functions
1782 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001783 * -# Initialize the operation object with one of the methods described in the
1784 * documentation for #psa_cipher_operation_t, e.g.
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001785 * #PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001786 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001787 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001788 * decryption. If the IV is prepended to the ciphertext, you can call
1789 * psa_cipher_update() on a buffer containing the IV followed by the
1790 * beginning of the message.
1791 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1792 * of the message each time.
1793 * -# Call psa_cipher_finish().
1794 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001795 * If an error occurs at any step after a call to psa_cipher_decrypt_setup(),
1796 * the operation will need to be reset by a call to psa_cipher_abort(). The
1797 * application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001798 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001799 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001800 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001801 * eventually terminate the operation. The following events terminate an
1802 * operation:
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001803 * - A successful call to psa_cipher_finish().
1804 * - A call to psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001805 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001806 * \param[in,out] operation The operation object to set up. It must have
1807 * been initialized as per the documentation for
1808 * #psa_cipher_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02001809 * \param key Identifier of the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001810 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02001811 * terminates. It must allow the usage
1812 * PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001813 * \param alg The cipher algorithm to compute
1814 * (\c PSA_ALG_XXX value such that
1815 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001816 *
Gilles Peskine28538492018-07-11 17:34:00 +02001817 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001818 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001819 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001820 * \retval #PSA_ERROR_NOT_PERMITTED
1821 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02001822 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001823 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001824 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001825 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1826 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1827 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001828 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdc5bf5c2019-08-13 11:46:09 +01001829 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03001830 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001831 * The operation state is not valid (it must be inactive).
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001832 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001833 * The library has not been previously initialized by psa_crypto_init().
1834 * It is implementation-dependent whether a failure to initialize
1835 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001836 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001837psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001838 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02001839 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001840
Gilles Peskinedcd14942018-07-12 00:30:52 +02001841/** Generate an IV for a symmetric encryption operation.
1842 *
1843 * This function generates a random IV (initialization vector), nonce
1844 * or initial counter value for the encryption operation as appropriate
1845 * for the chosen algorithm, key type and key size.
1846 *
1847 * The application must call psa_cipher_encrypt_setup() before
1848 * calling this function.
1849 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001850 * If this function returns an error status, the operation enters an error
1851 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001852 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001853 * \param[in,out] operation Active cipher operation.
1854 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001855 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001856 * \param[out] iv_length On success, the number of bytes of the
1857 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001858 *
1859 * \retval #PSA_SUCCESS
1860 * Success.
1861 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001862 * The operation state is not valid (it must be active, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001863 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001864 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001865 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1866 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1867 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001868 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw66200c42019-08-15 13:30:57 +01001869 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001870 * \retval #PSA_ERROR_BAD_STATE
1871 * The library has not been previously initialized by psa_crypto_init().
1872 * It is implementation-dependent whether a failure to initialize
1873 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001874 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001875psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001876 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001877 size_t iv_size,
1878 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001879
Gilles Peskinedcd14942018-07-12 00:30:52 +02001880/** Set the IV for a symmetric encryption or decryption operation.
1881 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001882 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001883 * or initial counter value for the encryption or decryption operation.
1884 *
1885 * The application must call psa_cipher_encrypt_setup() before
1886 * calling this function.
1887 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001888 * If this function returns an error status, the operation enters an error
1889 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001890 *
1891 * \note When encrypting, applications should use psa_cipher_generate_iv()
1892 * instead of this function, unless implementing a protocol that requires
1893 * a non-random IV.
1894 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001895 * \param[in,out] operation Active cipher operation.
1896 * \param[in] iv Buffer containing the IV to use.
1897 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001898 *
1899 * \retval #PSA_SUCCESS
1900 * Success.
1901 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001902 * The operation state is not valid (it must be an active cipher
1903 * encrypt operation, with no IV set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001904 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001905 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001906 * or the chosen algorithm does not use an IV.
1907 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1908 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1909 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001910 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw56b32b12019-08-13 11:43:40 +01001911 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001912 * \retval #PSA_ERROR_BAD_STATE
1913 * The library has not been previously initialized by psa_crypto_init().
1914 * It is implementation-dependent whether a failure to initialize
1915 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001916 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001917psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001918 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02001919 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001920
Gilles Peskinedcd14942018-07-12 00:30:52 +02001921/** Encrypt or decrypt a message fragment in an active cipher operation.
1922 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001923 * Before calling this function, you must:
1924 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1925 * The choice of setup function determines whether this function
1926 * encrypts or decrypts its input.
1927 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1928 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001929 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001930 * If this function returns an error status, the operation enters an error
1931 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001932 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001933 * \param[in,out] operation Active cipher operation.
1934 * \param[in] input Buffer containing the message fragment to
1935 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001936 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001937 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001938 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001939 * \param[out] output_length On success, the number of bytes
1940 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001941 *
1942 * \retval #PSA_SUCCESS
1943 * Success.
1944 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001945 * The operation state is not valid (it must be active, with an IV set
1946 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001947 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1948 * The size of the \p output buffer is too small.
1949 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1950 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1951 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001952 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01001953 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01001954 * \retval #PSA_ERROR_BAD_STATE
1955 * The library has not been previously initialized by psa_crypto_init().
1956 * It is implementation-dependent whether a failure to initialize
1957 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001958 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001959psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1960 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001961 size_t input_length,
Andrew Thoelke47629d02019-03-22 11:24:17 +00001962 uint8_t *output,
Gilles Peskine2d277862018-06-18 15:41:12 +02001963 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001964 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001965
Gilles Peskinedcd14942018-07-12 00:30:52 +02001966/** Finish encrypting or decrypting a message in a cipher operation.
1967 *
1968 * The application must call psa_cipher_encrypt_setup() or
1969 * psa_cipher_decrypt_setup() before calling this function. The choice
1970 * of setup function determines whether this function encrypts or
1971 * decrypts its input.
1972 *
1973 * This function finishes the encryption or decryption of the message
1974 * formed by concatenating the inputs passed to preceding calls to
1975 * psa_cipher_update().
1976 *
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001977 * When this function returns successfuly, the operation becomes inactive.
1978 * If this function returns an error status, the operation enters an error
1979 * state and must be aborted by calling psa_cipher_abort().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001980 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001981 * \param[in,out] operation Active cipher operation.
1982 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001983 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001984 * \param[out] output_length On success, the number of bytes
1985 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001986 *
1987 * \retval #PSA_SUCCESS
1988 * Success.
Gilles Peskinebe061332019-07-18 13:52:30 +02001989 * \retval #PSA_ERROR_INVALID_ARGUMENT
1990 * The total input size passed to this operation is not valid for
1991 * this particular algorithm. For example, the algorithm is a based
1992 * on block cipher and requires a whole number of blocks, but the
1993 * total input size is not a multiple of the block size.
1994 * \retval #PSA_ERROR_INVALID_PADDING
1995 * This is a decryption operation for an algorithm that includes
1996 * padding, and the ciphertext does not contain valid padding.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001997 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01001998 * The operation state is not valid (it must be active, with an IV set
1999 * if required for the algorithm).
Gilles Peskinedcd14942018-07-12 00:30:52 +02002000 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2001 * The size of the \p output buffer is too small.
2002 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2003 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2004 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002005 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw1f1e1a52019-08-13 11:44:30 +01002006 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002007 * \retval #PSA_ERROR_BAD_STATE
2008 * The library has not been previously initialized by psa_crypto_init().
2009 * It is implementation-dependent whether a failure to initialize
2010 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002011 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002012psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002013 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002014 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002015 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002016
Gilles Peskinedcd14942018-07-12 00:30:52 +02002017/** Abort a cipher operation.
2018 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002019 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002020 * \p operation structure itself. Once aborted, the operation object
2021 * can be reused for another operation by calling
2022 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002023 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002024 * You may call this function any time after the operation object has
Andrew Thoelkedb6f44f2019-09-11 23:33:30 +01002025 * been initialized as described in #psa_cipher_operation_t.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002026 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002027 * In particular, calling psa_cipher_abort() after the operation has been
2028 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2029 * is safe and has no effect.
2030 *
2031 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002032 *
2033 * \retval #PSA_SUCCESS
Gilles Peskinedcd14942018-07-12 00:30:52 +02002034 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2035 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002036 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002037 * \retval #PSA_ERROR_BAD_STATE
2038 * The library has not been previously initialized by psa_crypto_init().
2039 * It is implementation-dependent whether a failure to initialize
2040 * results in this error code.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002041 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002042psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2043
2044/**@}*/
2045
Gilles Peskine3b555712018-03-03 21:27:57 +01002046/** \defgroup aead Authenticated encryption with associated data (AEAD)
2047 * @{
2048 */
2049
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002050/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002051 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002052 * \param key Identifier of the key to use for the
2053 * operation. It must allow the usage
2054 * PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002055 * \param alg The AEAD algorithm to compute
2056 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002057 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002058 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002059 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002060 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002061 * but not encrypted.
2062 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002063 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002064 * encrypted.
2065 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002066 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002067 * encrypted data. The additional data is not
2068 * part of this output. For algorithms where the
2069 * encrypted data and the authentication tag
2070 * are defined as separate outputs, the
2071 * authentication tag is appended to the
2072 * encrypted data.
2073 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2074 * This must be at least
2075 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2076 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002077 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002078 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002079 *
Gilles Peskine28538492018-07-11 17:34:00 +02002080 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002081 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002082 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002083 * \retval #PSA_ERROR_NOT_PERMITTED
2084 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002085 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002086 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002087 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002088 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002089 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2090 * \p ciphertext_size is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002091 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2092 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002093 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw22bc8ff2019-08-08 15:10:33 +01002094 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002095 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002096 * The library has not been previously initialized by psa_crypto_init().
2097 * It is implementation-dependent whether a failure to initialize
2098 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002099 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002100psa_status_t psa_aead_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002101 psa_algorithm_t alg,
2102 const uint8_t *nonce,
2103 size_t nonce_length,
2104 const uint8_t *additional_data,
2105 size_t additional_data_length,
2106 const uint8_t *plaintext,
2107 size_t plaintext_length,
2108 uint8_t *ciphertext,
2109 size_t ciphertext_size,
2110 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002111
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002112/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002113 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002114 * \param key Identifier of the key to use for the
2115 * operation. It must allow the usage
2116 * PSA_KEY_USAGE_DECRYPT.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002117 * \param alg The AEAD algorithm to compute
2118 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002119 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002120 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002121 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002122 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002123 * but not encrypted.
2124 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002125 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002126 * encrypted. For algorithms where the
2127 * encrypted data and the authentication tag
2128 * are defined as separate inputs, the buffer
2129 * must contain the encrypted data followed
2130 * by the authentication tag.
2131 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002132 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002133 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2134 * This must be at least
2135 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2136 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002137 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002138 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002139 *
Gilles Peskine28538492018-07-11 17:34:00 +02002140 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002141 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002142 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002143 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002144 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002145 * \retval #PSA_ERROR_NOT_PERMITTED
2146 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002147 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002148 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002149 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002150 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002151 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2152 * \p plaintext_size or \p nonce_length is too small
Gilles Peskine28538492018-07-11 17:34:00 +02002153 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2154 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002155 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawc207ba32019-08-08 10:55:38 +01002156 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002157 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002158 * The library has not been previously initialized by psa_crypto_init().
2159 * It is implementation-dependent whether a failure to initialize
2160 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002161 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002162psa_status_t psa_aead_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002163 psa_algorithm_t alg,
2164 const uint8_t *nonce,
2165 size_t nonce_length,
2166 const uint8_t *additional_data,
2167 size_t additional_data_length,
2168 const uint8_t *ciphertext,
2169 size_t ciphertext_length,
2170 uint8_t *plaintext,
2171 size_t plaintext_size,
2172 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002173
Gilles Peskine30a9e412019-01-14 18:36:12 +01002174/** The type of the state data structure for multipart AEAD operations.
2175 *
2176 * Before calling any function on an AEAD operation object, the application
2177 * must initialize it by any of the following means:
2178 * - Set the structure to all-bits-zero, for example:
2179 * \code
2180 * psa_aead_operation_t operation;
2181 * memset(&operation, 0, sizeof(operation));
2182 * \endcode
2183 * - Initialize the structure to logical zero values, for example:
2184 * \code
2185 * psa_aead_operation_t operation = {0};
2186 * \endcode
2187 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2188 * for example:
2189 * \code
2190 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2191 * \endcode
2192 * - Assign the result of the function psa_aead_operation_init()
2193 * to the structure, for example:
2194 * \code
2195 * psa_aead_operation_t operation;
2196 * operation = psa_aead_operation_init();
2197 * \endcode
2198 *
2199 * This is an implementation-defined \c struct. Applications should not
2200 * make any assumptions about the content of this structure except
2201 * as directed by the documentation of a specific implementation. */
2202typedef struct psa_aead_operation_s psa_aead_operation_t;
2203
2204/** \def PSA_AEAD_OPERATION_INIT
2205 *
2206 * This macro returns a suitable initializer for an AEAD operation object of
2207 * type #psa_aead_operation_t.
2208 */
2209#ifdef __DOXYGEN_ONLY__
2210/* This is an example definition for documentation purposes.
2211 * Implementations should define a suitable value in `crypto_struct.h`.
2212 */
2213#define PSA_AEAD_OPERATION_INIT {0}
2214#endif
2215
2216/** Return an initial value for an AEAD operation object.
2217 */
2218static psa_aead_operation_t psa_aead_operation_init(void);
2219
2220/** Set the key for a multipart authenticated encryption operation.
2221 *
2222 * The sequence of operations to encrypt a message with authentication
2223 * is as follows:
2224 * -# Allocate an operation object which will be passed to all the functions
2225 * listed here.
2226 * -# Initialize the operation object with one of the methods described in the
2227 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002228 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002229 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002230 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2231 * inputs to the subsequent calls to psa_aead_update_ad() and
2232 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2233 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002234 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2235 * generate or set the nonce. You should use
2236 * psa_aead_generate_nonce() unless the protocol you are implementing
2237 * requires a specific nonce value.
2238 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2239 * of the non-encrypted additional authenticated data each time.
2240 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002241 * of the message to encrypt each time.
Adrian L. Shaw599c7122019-08-15 10:53:47 +01002242 * -# Call psa_aead_finish().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002243 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002244 * If an error occurs at any step after a call to psa_aead_encrypt_setup(),
2245 * the operation will need to be reset by a call to psa_aead_abort(). The
2246 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002247 * has been initialized.
2248 *
2249 * After a successful call to psa_aead_encrypt_setup(), the application must
2250 * eventually terminate the operation. The following events terminate an
2251 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002252 * - A successful call to psa_aead_finish().
2253 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002254 *
2255 * \param[in,out] operation The operation object to set up. It must have
2256 * been initialized as per the documentation for
2257 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002258 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002259 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002260 * terminates. It must allow the usage
2261 * PSA_KEY_USAGE_ENCRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002262 * \param alg The AEAD algorithm to compute
2263 * (\c PSA_ALG_XXX value such that
2264 * #PSA_ALG_IS_AEAD(\p alg) is true).
2265 *
2266 * \retval #PSA_SUCCESS
2267 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002268 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002269 * The operation state is not valid (it must be inactive).
Andrew Thoelke340984b2019-09-11 21:33:41 +01002270 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002271 * \retval #PSA_ERROR_NOT_PERMITTED
2272 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002273 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002274 * \retval #PSA_ERROR_NOT_SUPPORTED
2275 * \p alg is not supported or is not an AEAD algorithm.
2276 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2277 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2278 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002279 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002280 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002281 * \retval #PSA_ERROR_BAD_STATE
2282 * The library has not been previously initialized by psa_crypto_init().
2283 * It is implementation-dependent whether a failure to initialize
2284 * results in this error code.
2285 */
2286psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002287 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002288 psa_algorithm_t alg);
2289
2290/** Set the key for a multipart authenticated decryption operation.
2291 *
2292 * The sequence of operations to decrypt a message with authentication
2293 * is as follows:
2294 * -# Allocate an operation object which will be passed to all the functions
2295 * listed here.
2296 * -# Initialize the operation object with one of the methods described in the
2297 * documentation for #psa_aead_operation_t, e.g.
Andrew Thoelke414415a2019-09-12 00:02:45 +01002298 * #PSA_AEAD_OPERATION_INIT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002299 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002300 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2301 * inputs to the subsequent calls to psa_aead_update_ad() and
2302 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2303 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002304 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2305 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2306 * of the non-encrypted additional authenticated data each time.
2307 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002308 * of the ciphertext to decrypt each time.
2309 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002310 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002311 * If an error occurs at any step after a call to psa_aead_decrypt_setup(),
2312 * the operation will need to be reset by a call to psa_aead_abort(). The
2313 * application may call psa_aead_abort() at any time after the operation
Gilles Peskine30a9e412019-01-14 18:36:12 +01002314 * has been initialized.
2315 *
2316 * After a successful call to psa_aead_decrypt_setup(), the application must
2317 * eventually terminate the operation. The following events terminate an
2318 * operation:
Andrew Thoelke414415a2019-09-12 00:02:45 +01002319 * - A successful call to psa_aead_verify().
2320 * - A call to psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002321 *
2322 * \param[in,out] operation The operation object to set up. It must have
2323 * been initialized as per the documentation for
2324 * #psa_aead_operation_t and not yet in use.
Ronald Croncf56a0a2020-08-04 09:51:30 +02002325 * \param key Identifier of the key to use for the operation.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002326 * It must remain valid until the operation
Ronald Croncf56a0a2020-08-04 09:51:30 +02002327 * terminates. It must allow the usage
2328 * PSA_KEY_USAGE_DECRYPT.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002329 * \param alg The AEAD algorithm to compute
2330 * (\c PSA_ALG_XXX value such that
2331 * #PSA_ALG_IS_AEAD(\p alg) is true).
2332 *
2333 * \retval #PSA_SUCCESS
2334 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01002335 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002336 * The operation state is not valid (it must be inactive).
Andrew Thoelke340984b2019-09-11 21:33:41 +01002337 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002338 * \retval #PSA_ERROR_NOT_PERMITTED
2339 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Croncf56a0a2020-08-04 09:51:30 +02002340 * \p key is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002341 * \retval #PSA_ERROR_NOT_SUPPORTED
2342 * \p alg is not supported or is not an AEAD algorithm.
2343 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2344 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2345 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002346 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw3e412492019-08-08 15:10:33 +01002347 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine30a9e412019-01-14 18:36:12 +01002348 * \retval #PSA_ERROR_BAD_STATE
2349 * The library has not been previously initialized by psa_crypto_init().
2350 * It is implementation-dependent whether a failure to initialize
2351 * results in this error code.
2352 */
2353psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002354 mbedtls_svc_key_id_t key,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002355 psa_algorithm_t alg);
2356
2357/** Generate a random nonce for an authenticated encryption operation.
2358 *
2359 * This function generates a random nonce for the authenticated encryption
2360 * operation with an appropriate size for the chosen algorithm, key type
2361 * and key size.
2362 *
2363 * The application must call psa_aead_encrypt_setup() before
2364 * calling this function.
2365 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002366 * If this function returns an error status, the operation enters an error
2367 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002368 *
2369 * \param[in,out] operation Active AEAD operation.
2370 * \param[out] nonce Buffer where the generated nonce is to be
2371 * written.
2372 * \param nonce_size Size of the \p nonce buffer in bytes.
2373 * \param[out] nonce_length On success, the number of bytes of the
2374 * generated nonce.
2375 *
2376 * \retval #PSA_SUCCESS
2377 * Success.
2378 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002379 * The operation state is not valid (it must be an active aead encrypt
2380 operation, with no nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002381 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2382 * The size of the \p nonce buffer is too small.
2383 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2384 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2385 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002386 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002387 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002388 * \retval #PSA_ERROR_BAD_STATE
2389 * The library has not been previously initialized by psa_crypto_init().
2390 * It is implementation-dependent whether a failure to initialize
2391 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002392 */
2393psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002394 uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002395 size_t nonce_size,
2396 size_t *nonce_length);
2397
2398/** Set the nonce for an authenticated encryption or decryption operation.
2399 *
2400 * This function sets the nonce for the authenticated
2401 * encryption or decryption operation.
2402 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002403 * The application must call psa_aead_encrypt_setup() or
2404 * psa_aead_decrypt_setup() before calling this function.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002405 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002406 * If this function returns an error status, the operation enters an error
2407 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002408 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002409 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002410 * instead of this function, unless implementing a protocol that requires
2411 * a non-random IV.
2412 *
2413 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002414 * \param[in] nonce Buffer containing the nonce to use.
2415 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002416 *
2417 * \retval #PSA_SUCCESS
2418 * Success.
2419 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke414415a2019-09-12 00:02:45 +01002420 * The operation state is not valid (it must be active, with no nonce
2421 * set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002422 * \retval #PSA_ERROR_INVALID_ARGUMENT
2423 * The size of \p nonce is not acceptable for the chosen algorithm.
2424 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2425 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2426 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002427 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002428 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002429 * \retval #PSA_ERROR_BAD_STATE
2430 * The library has not been previously initialized by psa_crypto_init().
2431 * It is implementation-dependent whether a failure to initialize
2432 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002433 */
2434psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002435 const uint8_t *nonce,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002436 size_t nonce_length);
2437
Gilles Peskinebc59c852019-01-17 15:26:08 +01002438/** Declare the lengths of the message and additional data for AEAD.
2439 *
2440 * The application must call this function before calling
2441 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2442 * the operation requires it. If the algorithm does not require it,
2443 * calling this function is optional, but if this function is called
2444 * then the implementation must enforce the lengths.
2445 *
2446 * You may call this function before or after setting the nonce with
2447 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2448 *
2449 * - For #PSA_ALG_CCM, calling this function is required.
2450 * - For the other AEAD algorithms defined in this specification, calling
2451 * this function is not required.
2452 * - For vendor-defined algorithm, refer to the vendor documentation.
2453 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002454 * If this function returns an error status, the operation enters an error
2455 * state and must be aborted by calling psa_aead_abort().
2456 *
Gilles Peskinebc59c852019-01-17 15:26:08 +01002457 * \param[in,out] operation Active AEAD operation.
2458 * \param ad_length Size of the non-encrypted additional
2459 * authenticated data in bytes.
2460 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2461 *
2462 * \retval #PSA_SUCCESS
2463 * Success.
2464 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002465 * The operation state is not valid (it must be active, and
2466 * psa_aead_update_ad() and psa_aead_update() must not have been
2467 * called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002468 * \retval #PSA_ERROR_INVALID_ARGUMENT
2469 * At least one of the lengths is not acceptable for the chosen
2470 * algorithm.
2471 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2472 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2473 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002474 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002475 * \retval #PSA_ERROR_BAD_STATE
2476 * The library has not been previously initialized by psa_crypto_init().
2477 * It is implementation-dependent whether a failure to initialize
2478 * results in this error code.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002479 */
2480psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2481 size_t ad_length,
2482 size_t plaintext_length);
2483
Gilles Peskine30a9e412019-01-14 18:36:12 +01002484/** Pass additional data to an active AEAD operation.
2485 *
2486 * Additional data is authenticated, but not encrypted.
2487 *
2488 * You may call this function multiple times to pass successive fragments
2489 * of the additional data. You may not call this function after passing
2490 * data to encrypt or decrypt with psa_aead_update().
2491 *
2492 * Before calling this function, you must:
2493 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2494 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2495 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002496 * If this function returns an error status, the operation enters an error
2497 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002498 *
2499 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2500 * there is no guarantee that the input is valid. Therefore, until
2501 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2502 * treat the input as untrusted and prepare to undo any action that
2503 * depends on the input if psa_aead_verify() returns an error status.
2504 *
2505 * \param[in,out] operation Active AEAD operation.
2506 * \param[in] input Buffer containing the fragment of
2507 * additional data.
2508 * \param input_length Size of the \p input buffer in bytes.
2509 *
2510 * \retval #PSA_SUCCESS
2511 * Success.
2512 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002513 * The operation state is not valid (it must be active, have a nonce
2514 * set, have lengths set if required by the algorithm, and
2515 * psa_aead_update() must not have been called yet).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002516 * \retval #PSA_ERROR_INVALID_ARGUMENT
2517 * The total input length overflows the additional data length that
2518 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002519 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2520 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2521 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002522 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002523 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002524 * \retval #PSA_ERROR_BAD_STATE
2525 * The library has not been previously initialized by psa_crypto_init().
2526 * It is implementation-dependent whether a failure to initialize
2527 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002528 */
2529psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2530 const uint8_t *input,
2531 size_t input_length);
2532
2533/** Encrypt or decrypt a message fragment in an active AEAD operation.
2534 *
2535 * Before calling this function, you must:
2536 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2537 * The choice of setup function determines whether this function
2538 * encrypts or decrypts its input.
2539 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2540 * 3. Call psa_aead_update_ad() to pass all the additional data.
2541 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002542 * If this function returns an error status, the operation enters an error
2543 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002544 *
2545 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2546 * there is no guarantee that the input is valid. Therefore, until
2547 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2548 * - Do not use the output in any way other than storing it in a
2549 * confidential location. If you take any action that depends
2550 * on the tentative decrypted data, this action will need to be
2551 * undone if the input turns out not to be valid. Furthermore,
2552 * if an adversary can observe that this action took place
2553 * (for example through timing), they may be able to use this
2554 * fact as an oracle to decrypt any message encrypted with the
2555 * same key.
2556 * - In particular, do not copy the output anywhere but to a
2557 * memory or storage space that you have exclusive access to.
2558 *
Gilles Peskinef02aec92019-05-06 15:42:54 +02002559 * This function does not require the input to be aligned to any
2560 * particular block boundary. If the implementation can only process
Gilles Peskineac99e322019-05-14 16:10:53 +02002561 * a whole block at a time, it must consume all the input provided, but
2562 * it may delay the end of the corresponding output until a subsequent
2563 * call to psa_aead_update(), psa_aead_finish() or psa_aead_verify()
2564 * provides sufficient input. The amount of data that can be delayed
2565 * in this way is bounded by #PSA_AEAD_UPDATE_OUTPUT_SIZE.
Gilles Peskinef02aec92019-05-06 15:42:54 +02002566 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002567 * \param[in,out] operation Active AEAD operation.
2568 * \param[in] input Buffer containing the message fragment to
2569 * encrypt or decrypt.
2570 * \param input_length Size of the \p input buffer in bytes.
2571 * \param[out] output Buffer where the output is to be written.
2572 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002573 * This must be at least
2574 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg,
2575 * \p input_length) where \c alg is the
2576 * algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002577 * \param[out] output_length On success, the number of bytes
2578 * that make up the returned output.
2579 *
2580 * \retval #PSA_SUCCESS
2581 * Success.
2582 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002583 * The operation state is not valid (it must be active, have a nonce
2584 * set, and have lengths set if required by the algorithm).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002585 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2586 * The size of the \p output buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002587 * You can determine a sufficient buffer size by calling
2588 * #PSA_AEAD_UPDATE_OUTPUT_SIZE(\c alg, \p input_length)
2589 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002590 * \retval #PSA_ERROR_INVALID_ARGUMENT
2591 * The total length of input to psa_aead_update_ad() so far is
2592 * less than the additional data length that was previously
2593 * specified with psa_aead_set_lengths().
2594 * \retval #PSA_ERROR_INVALID_ARGUMENT
2595 * The total input length overflows the plaintext length that
2596 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002597 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2598 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2599 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002600 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002601 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002602 * \retval #PSA_ERROR_BAD_STATE
2603 * The library has not been previously initialized by psa_crypto_init().
2604 * It is implementation-dependent whether a failure to initialize
2605 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002606 */
2607psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2608 const uint8_t *input,
2609 size_t input_length,
Andrew Thoelked16bdac2019-05-15 12:34:01 +01002610 uint8_t *output,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002611 size_t output_size,
2612 size_t *output_length);
2613
2614/** Finish encrypting a message in an AEAD operation.
2615 *
2616 * The operation must have been set up with psa_aead_encrypt_setup().
2617 *
2618 * This function finishes the authentication of the additional data
2619 * formed by concatenating the inputs passed to preceding calls to
2620 * psa_aead_update_ad() with the plaintext formed by concatenating the
2621 * inputs passed to preceding calls to psa_aead_update().
2622 *
2623 * This function has two output buffers:
2624 * - \p ciphertext contains trailing ciphertext that was buffered from
Gilles Peskinef02aec92019-05-06 15:42:54 +02002625 * preceding calls to psa_aead_update().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002626 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002627 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002628 * that the operation performs.
2629 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002630 * When this function returns successfuly, the operation becomes inactive.
2631 * If this function returns an error status, the operation enters an error
2632 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002633 *
2634 * \param[in,out] operation Active AEAD operation.
2635 * \param[out] ciphertext Buffer where the last part of the ciphertext
2636 * is to be written.
2637 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002638 * This must be at least
2639 * #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg) where
2640 * \c alg is the algorithm that is being
2641 * calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002642 * \param[out] ciphertext_length On success, the number of bytes of
2643 * returned ciphertext.
2644 * \param[out] tag Buffer where the authentication tag is
2645 * to be written.
2646 * \param tag_size Size of the \p tag buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002647 * This must be at least
2648 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is
2649 * the algorithm that is being calculated.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002650 * \param[out] tag_length On success, the number of bytes
2651 * that make up the returned tag.
2652 *
2653 * \retval #PSA_SUCCESS
2654 * Success.
2655 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002656 * The operation state is not valid (it must be an active encryption
2657 * operation with a nonce set).
Gilles Peskine30a9e412019-01-14 18:36:12 +01002658 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002659 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002660 * You can determine a sufficient buffer size for \p ciphertext by
2661 * calling #PSA_AEAD_FINISH_OUTPUT_SIZE(\c alg)
2662 * where \c alg is the algorithm that is being calculated.
2663 * You can determine a sufficient buffer size for \p tag by
2664 * calling #PSA_AEAD_TAG_LENGTH(\c alg).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002665 * \retval #PSA_ERROR_INVALID_ARGUMENT
2666 * The total length of input to psa_aead_update_ad() so far is
2667 * less than the additional data length that was previously
2668 * specified with psa_aead_set_lengths().
2669 * \retval #PSA_ERROR_INVALID_ARGUMENT
2670 * The total length of input to psa_aead_update() so far is
2671 * less than the plaintext length that was previously
2672 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002673 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2674 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2675 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002676 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002677 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002678 * \retval #PSA_ERROR_BAD_STATE
2679 * The library has not been previously initialized by psa_crypto_init().
2680 * It is implementation-dependent whether a failure to initialize
2681 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002682 */
2683psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002684 uint8_t *ciphertext,
2685 size_t ciphertext_size,
2686 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002687 uint8_t *tag,
2688 size_t tag_size,
2689 size_t *tag_length);
2690
2691/** Finish authenticating and decrypting a message in an AEAD operation.
2692 *
2693 * The operation must have been set up with psa_aead_decrypt_setup().
2694 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002695 * This function finishes the authenticated decryption of the message
2696 * components:
2697 *
2698 * - The additional data consisting of the concatenation of the inputs
2699 * passed to preceding calls to psa_aead_update_ad().
2700 * - The ciphertext consisting of the concatenation of the inputs passed to
2701 * preceding calls to psa_aead_update().
2702 * - The tag passed to this function call.
2703 *
2704 * If the authentication tag is correct, this function outputs any remaining
2705 * plaintext and reports success. If the authentication tag is not correct,
2706 * this function returns #PSA_ERROR_INVALID_SIGNATURE.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002707 *
Andrew Thoelke414415a2019-09-12 00:02:45 +01002708 * When this function returns successfuly, the operation becomes inactive.
2709 * If this function returns an error status, the operation enters an error
2710 * state and must be aborted by calling psa_aead_abort().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002711 *
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002712 * \note Implementations shall make the best effort to ensure that the
2713 * comparison between the actual tag and the expected tag is performed
2714 * in constant time.
2715 *
Gilles Peskine30a9e412019-01-14 18:36:12 +01002716 * \param[in,out] operation Active AEAD operation.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002717 * \param[out] plaintext Buffer where the last part of the plaintext
Gilles Peskineac99e322019-05-14 16:10:53 +02002718 * is to be written. This is the remaining data
Gilles Peskine5211efb2019-05-06 15:56:05 +02002719 * from previous calls to psa_aead_update()
2720 * that could not be processed until the end
2721 * of the input.
2722 * \param plaintext_size Size of the \p plaintext buffer in bytes.
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002723 * This must be at least
2724 * #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg) where
2725 * \c alg is the algorithm that is being
2726 * calculated.
Gilles Peskine5211efb2019-05-06 15:56:05 +02002727 * \param[out] plaintext_length On success, the number of bytes of
2728 * returned plaintext.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002729 * \param[in] tag Buffer containing the authentication tag.
2730 * \param tag_length Size of the \p tag buffer in bytes.
2731 *
2732 * \retval #PSA_SUCCESS
2733 * Success.
Andrew Thoelke5ae24ec2019-09-12 09:44:33 +01002734 * \retval #PSA_ERROR_INVALID_SIGNATURE
2735 * The calculations were successful, but the authentication tag is
2736 * not correct.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002737 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelke4104afb2019-09-18 17:47:25 +01002738 * The operation state is not valid (it must be an active decryption
2739 * operation with a nonce set).
Gilles Peskine49dd8d82019-05-06 15:16:19 +02002740 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2741 * The size of the \p plaintext buffer is too small.
2742 * You can determine a sufficient buffer size for \p plaintext by
2743 * calling #PSA_AEAD_VERIFY_OUTPUT_SIZE(\c alg)
2744 * where \c alg is the algorithm that is being calculated.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002745 * \retval #PSA_ERROR_INVALID_ARGUMENT
2746 * The total length of input to psa_aead_update_ad() so far is
2747 * less than the additional data length that was previously
2748 * specified with psa_aead_set_lengths().
2749 * \retval #PSA_ERROR_INVALID_ARGUMENT
2750 * The total length of input to psa_aead_update() so far is
2751 * less than the plaintext length that was previously
2752 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002753 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2754 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2755 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002756 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01002757 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002758 * \retval #PSA_ERROR_BAD_STATE
2759 * The library has not been previously initialized by psa_crypto_init().
2760 * It is implementation-dependent whether a failure to initialize
2761 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002762 */
2763psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
Gilles Peskine5211efb2019-05-06 15:56:05 +02002764 uint8_t *plaintext,
2765 size_t plaintext_size,
2766 size_t *plaintext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002767 const uint8_t *tag,
2768 size_t tag_length);
2769
2770/** Abort an AEAD operation.
2771 *
2772 * Aborting an operation frees all associated resources except for the
2773 * \p operation structure itself. Once aborted, the operation object
2774 * can be reused for another operation by calling
2775 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2776 *
2777 * You may call this function any time after the operation object has
Andrew Thoelke414415a2019-09-12 00:02:45 +01002778 * been initialized as described in #psa_aead_operation_t.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002779 *
2780 * In particular, calling psa_aead_abort() after the operation has been
Andrew Thoelke414415a2019-09-12 00:02:45 +01002781 * terminated by a call to psa_aead_abort(), psa_aead_finish() or
2782 * psa_aead_verify() is safe and has no effect.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002783 *
2784 * \param[in,out] operation Initialized AEAD operation.
2785 *
2786 * \retval #PSA_SUCCESS
Gilles Peskine30a9e412019-01-14 18:36:12 +01002787 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2788 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002789 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01002790 * \retval #PSA_ERROR_BAD_STATE
2791 * The library has not been previously initialized by psa_crypto_init().
2792 * It is implementation-dependent whether a failure to initialize
2793 * results in this error code.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002794 */
2795psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2796
Gilles Peskine3b555712018-03-03 21:27:57 +01002797/**@}*/
2798
Gilles Peskine20035e32018-02-03 22:44:14 +01002799/** \defgroup asymmetric Asymmetric cryptography
2800 * @{
2801 */
2802
2803/**
2804 * \brief Sign a hash or short message with a private key.
2805 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002806 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002807 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002808 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2809 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2810 * to determine the hash algorithm to use.
2811 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002812 * \param key Identifier of the key to use for the operation.
2813 * It must be an asymmetric key pair. The key must
2814 * allow the usage PSA_KEY_USAGE_SIGN_HASH.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002815 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002816 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002817 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002818 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002819 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002820 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002821 * \param[out] signature_length On success, the number of bytes
2822 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002823 *
Gilles Peskine28538492018-07-11 17:34:00 +02002824 * \retval #PSA_SUCCESS
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002825 * \retval #PSA_ERROR_INVALID_HANDLE
2826 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002827 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002828 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002829 * determine a sufficient buffer size by calling
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002830 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002831 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002832 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002833 * \retval #PSA_ERROR_NOT_SUPPORTED
2834 * \retval #PSA_ERROR_INVALID_ARGUMENT
2835 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2836 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2837 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002838 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw27c12152019-08-08 11:10:32 +01002839 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002840 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002841 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002842 * The library has not been previously initialized by psa_crypto_init().
2843 * It is implementation-dependent whether a failure to initialize
2844 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002845 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002846psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002847 psa_algorithm_t alg,
2848 const uint8_t *hash,
2849 size_t hash_length,
2850 uint8_t *signature,
2851 size_t signature_size,
2852 size_t *signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002853
2854/**
2855 * \brief Verify the signature a hash or short message using a public key.
2856 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002857 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002858 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002859 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2860 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2861 * to determine the hash algorithm to use.
2862 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002863 * \param key Identifier of the key to use for the operation. It
2864 * must be a public key or an asymmetric key pair. The
2865 * key must allow the usage PSA_KEY_USAGE_VERIFY_HASH.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002866 * \param alg A signature algorithm that is compatible with
Ronald Croncf56a0a2020-08-04 09:51:30 +02002867 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002868 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002869 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002870 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002871 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002872 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002873 *
Gilles Peskine28538492018-07-11 17:34:00 +02002874 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002875 * The signature is valid.
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002876 * \retval #PSA_ERROR_INVALID_HANDLE
2877 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002878 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002879 * The calculation was perfomed successfully, but the passed
2880 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002881 * \retval #PSA_ERROR_NOT_SUPPORTED
2882 * \retval #PSA_ERROR_INVALID_ARGUMENT
2883 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2884 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2885 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002886 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw6e758c92019-08-08 11:11:43 +01002887 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03002888 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002889 * The library has not been previously initialized by psa_crypto_init().
2890 * It is implementation-dependent whether a failure to initialize
2891 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002892 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002893psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002894 psa_algorithm_t alg,
2895 const uint8_t *hash,
2896 size_t hash_length,
2897 const uint8_t *signature,
2898 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002899
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002900/**
2901 * \brief Encrypt a short message with a public key.
2902 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002903 * \param key Identifer of the key to use for the operation.
2904 * It must be a public key or an asymmetric key
2905 * pair. It must allow the usage
2906 * PSA_KEY_USAGE_ENCRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002907 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02002908 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002909 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002910 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002911 * \param[in] salt A salt or label, if supported by the
2912 * encryption algorithm.
2913 * If the algorithm does not support a
2914 * salt, pass \c NULL.
2915 * If the algorithm supports an optional
2916 * salt and you do not want to pass a salt,
2917 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002918 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002919 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2920 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002921 * \param salt_length Size of the \p salt buffer in bytes.
2922 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002923 * \param[out] output Buffer where the encrypted message is to
2924 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002925 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002926 * \param[out] output_length On success, the number of bytes
2927 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002928 *
Gilles Peskine28538492018-07-11 17:34:00 +02002929 * \retval #PSA_SUCCESS
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002930 * \retval #PSA_ERROR_INVALID_HANDLE
2931 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002932 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002933 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002934 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002935 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002936 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002937 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002938 * \retval #PSA_ERROR_NOT_SUPPORTED
2939 * \retval #PSA_ERROR_INVALID_ARGUMENT
2940 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2941 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2942 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02002943 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawf961d5c2019-08-08 10:27:50 +01002944 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02002945 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002946 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002947 * The library has not been previously initialized by psa_crypto_init().
2948 * It is implementation-dependent whether a failure to initialize
2949 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002950 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002951psa_status_t psa_asymmetric_encrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002952 psa_algorithm_t alg,
2953 const uint8_t *input,
2954 size_t input_length,
2955 const uint8_t *salt,
2956 size_t salt_length,
2957 uint8_t *output,
2958 size_t output_size,
2959 size_t *output_length);
2960
2961/**
2962 * \brief Decrypt a short message with a private key.
2963 *
Ronald Croncf56a0a2020-08-04 09:51:30 +02002964 * \param key Identifier of the key to use for the operation.
2965 * It must be an asymmetric key pair. It must
2966 * allow the usage PSA_KEY_USAGE_DECRYPT.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002967 * \param alg An asymmetric encryption algorithm that is
Ronald Croncf56a0a2020-08-04 09:51:30 +02002968 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002969 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002970 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002971 * \param[in] salt A salt or label, if supported by the
2972 * encryption algorithm.
2973 * If the algorithm does not support a
2974 * salt, pass \c NULL.
2975 * If the algorithm supports an optional
2976 * salt and you do not want to pass a salt,
2977 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002978 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002979 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2980 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002981 * \param salt_length Size of the \p salt buffer in bytes.
2982 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002983 * \param[out] output Buffer where the decrypted message is to
2984 * be written.
2985 * \param output_size Size of the \c output buffer in bytes.
2986 * \param[out] output_length On success, the number of bytes
2987 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002988 *
Gilles Peskine28538492018-07-11 17:34:00 +02002989 * \retval #PSA_SUCCESS
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01002990 * \retval #PSA_ERROR_INVALID_HANDLE
2991 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine28538492018-07-11 17:34:00 +02002992 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002993 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002994 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002995 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002996 * where \c key_type and \c key_bits are the type and bit-size
Ronald Croncf56a0a2020-08-04 09:51:30 +02002997 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002998 * \retval #PSA_ERROR_NOT_SUPPORTED
2999 * \retval #PSA_ERROR_INVALID_ARGUMENT
3000 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3001 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3002 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003003 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw96f31ad2019-08-08 10:30:58 +01003004 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02003005 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3006 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003007 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003008 * The library has not been previously initialized by psa_crypto_init().
3009 * It is implementation-dependent whether a failure to initialize
3010 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003011 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02003012psa_status_t psa_asymmetric_decrypt(mbedtls_svc_key_id_t key,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003013 psa_algorithm_t alg,
3014 const uint8_t *input,
3015 size_t input_length,
3016 const uint8_t *salt,
3017 size_t salt_length,
3018 uint8_t *output,
3019 size_t output_size,
3020 size_t *output_length);
3021
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003022/**@}*/
3023
Gilles Peskine35675b62019-05-16 17:26:11 +02003024/** \defgroup key_derivation Key derivation and pseudorandom generation
Gilles Peskineeab56e42018-07-12 17:12:33 +02003025 * @{
3026 */
3027
Gilles Peskine35675b62019-05-16 17:26:11 +02003028/** The type of the state data structure for key derivation operations.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003029 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003030 * Before calling any function on a key derivation operation object, the
3031 * application must initialize it by any of the following means:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003032 * - Set the structure to all-bits-zero, for example:
3033 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003034 * psa_key_derivation_operation_t operation;
3035 * memset(&operation, 0, sizeof(operation));
Gilles Peskineeab56e42018-07-12 17:12:33 +02003036 * \endcode
3037 * - Initialize the structure to logical zero values, for example:
3038 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003039 * psa_key_derivation_operation_t operation = {0};
Gilles Peskineeab56e42018-07-12 17:12:33 +02003040 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003041 * - Initialize the structure to the initializer #PSA_KEY_DERIVATION_OPERATION_INIT,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003042 * for example:
3043 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003044 * psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003045 * \endcode
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003046 * - Assign the result of the function psa_key_derivation_operation_init()
Gilles Peskineeab56e42018-07-12 17:12:33 +02003047 * to the structure, for example:
3048 * \code
Gilles Peskine35675b62019-05-16 17:26:11 +02003049 * psa_key_derivation_operation_t operation;
3050 * operation = psa_key_derivation_operation_init();
Gilles Peskineeab56e42018-07-12 17:12:33 +02003051 * \endcode
3052 *
3053 * This is an implementation-defined \c struct. Applications should not
3054 * make any assumptions about the content of this structure except
3055 * as directed by the documentation of a specific implementation.
3056 */
Gilles Peskinecbe66502019-05-16 16:59:18 +02003057typedef struct psa_key_derivation_s psa_key_derivation_operation_t;
Gilles Peskineeab56e42018-07-12 17:12:33 +02003058
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003059/** \def PSA_KEY_DERIVATION_OPERATION_INIT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003060 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003061 * This macro returns a suitable initializer for a key derivation operation
3062 * object of type #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003063 */
3064#ifdef __DOXYGEN_ONLY__
3065/* This is an example definition for documentation purposes.
3066 * Implementations should define a suitable value in `crypto_struct.h`.
3067 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003068#define PSA_KEY_DERIVATION_OPERATION_INIT {0}
Gilles Peskineeab56e42018-07-12 17:12:33 +02003069#endif
3070
Gilles Peskine35675b62019-05-16 17:26:11 +02003071/** Return an initial value for a key derivation operation object.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003072 */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02003073static psa_key_derivation_operation_t psa_key_derivation_operation_init(void);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003074
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003075/** Set up a key derivation operation.
3076 *
3077 * A key derivation algorithm takes some inputs and uses them to generate
3078 * a byte stream in a deterministic way.
3079 * This byte stream can be used to produce keys and other
3080 * cryptographic material.
3081 *
3082 * To derive a key:
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003083 * -# Start with an initialized object of type #psa_key_derivation_operation_t.
3084 * -# Call psa_key_derivation_setup() to select the algorithm.
3085 * -# Provide the inputs for the key derivation by calling
3086 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3087 * as appropriate. Which inputs are needed, in what order, and whether
3088 * they may be keys and if so of what type depends on the algorithm.
3089 * -# Optionally set the operation's maximum capacity with
3090 * psa_key_derivation_set_capacity(). You may do this before, in the middle
3091 * of or after providing inputs. For some algorithms, this step is mandatory
3092 * because the output depends on the maximum capacity.
3093 * -# To derive a key, call psa_key_derivation_output_key().
3094 * To derive a byte string for a different purpose, call
3095 * psa_key_derivation_output_bytes().
3096 * Successive calls to these functions use successive output bytes
3097 * calculated by the key derivation algorithm.
3098 * -# Clean up the key derivation operation object with
3099 * psa_key_derivation_abort().
3100 *
3101 * If this function returns an error, the key derivation operation object is
3102 * not changed.
3103 *
3104 * If an error occurs at any step after a call to psa_key_derivation_setup(),
3105 * the operation will need to be reset by a call to psa_key_derivation_abort().
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003106 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003107 * Implementations must reject an attempt to derive a key of size 0.
3108 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003109 * \param[in,out] operation The key derivation operation object
3110 * to set up. It must
3111 * have been initialized but not set up yet.
3112 * \param alg The key derivation algorithm to compute
3113 * (\c PSA_ALG_XXX value such that
3114 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3115 *
3116 * \retval #PSA_SUCCESS
3117 * Success.
3118 * \retval #PSA_ERROR_INVALID_ARGUMENT
3119 * \c alg is not a key derivation algorithm.
3120 * \retval #PSA_ERROR_NOT_SUPPORTED
3121 * \c alg is not supported or is not a key derivation algorithm.
3122 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3123 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3124 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003125 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawfa2cefa2019-09-03 16:51:19 +01003126 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003127 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003128 * The operation state is not valid (it must be inactive).
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003129 * \retval #PSA_ERROR_BAD_STATE
3130 * The library has not been previously initialized by psa_crypto_init().
3131 * It is implementation-dependent whether a failure to initialize
3132 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003133 */
3134psa_status_t psa_key_derivation_setup(
3135 psa_key_derivation_operation_t *operation,
3136 psa_algorithm_t alg);
3137
Gilles Peskine35675b62019-05-16 17:26:11 +02003138/** Retrieve the current capacity of a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003139 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003140 * The capacity of a key derivation is the maximum number of bytes that it can
3141 * return. When you get *N* bytes of output from a key derivation operation,
3142 * this reduces its capacity by *N*.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003143 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003144 * \param[in] operation The operation to query.
3145 * \param[out] capacity On success, the capacity of the operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003146 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003147 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003148 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003149 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003150 * The operation state is not valid (it must be active).
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003151 * \retval #PSA_ERROR_HARDWARE_FAILURE
3152 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003153 * \retval #PSA_ERROR_BAD_STATE
3154 * The library has not been previously initialized by psa_crypto_init().
3155 * It is implementation-dependent whether a failure to initialize
3156 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003157 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003158psa_status_t psa_key_derivation_get_capacity(
3159 const psa_key_derivation_operation_t *operation,
3160 size_t *capacity);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003161
Gilles Peskine35675b62019-05-16 17:26:11 +02003162/** Set the maximum capacity of a key derivation operation.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003163 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003164 * The capacity of a key derivation operation is the maximum number of bytes
3165 * that the key derivation operation can return from this point onwards.
3166 *
3167 * \param[in,out] operation The key derivation operation object to modify.
3168 * \param capacity The new capacity of the operation.
3169 * It must be less or equal to the operation's
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003170 * current capacity.
3171 *
3172 * \retval #PSA_SUCCESS
3173 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine35675b62019-05-16 17:26:11 +02003174 * \p capacity is larger than the operation's current capacity.
3175 * In this case, the operation object remains valid and its capacity
3176 * remains unchanged.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003177 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003178 * The operation state is not valid (it must be active).
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003179 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003180 * \retval #PSA_ERROR_HARDWARE_FAILURE
3181 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003182 * \retval #PSA_ERROR_BAD_STATE
3183 * The library has not been previously initialized by psa_crypto_init().
3184 * It is implementation-dependent whether a failure to initialize
3185 * results in this error code.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003186 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003187psa_status_t psa_key_derivation_set_capacity(
3188 psa_key_derivation_operation_t *operation,
3189 size_t capacity);
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003190
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003191/** Use the maximum possible capacity for a key derivation operation.
3192 *
3193 * Use this value as the capacity argument when setting up a key derivation
3194 * to indicate that the operation should have the maximum possible capacity.
3195 * The value of the maximum possible capacity depends on the key derivation
3196 * algorithm.
3197 */
3198#define PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ((size_t)(-1))
3199
3200/** Provide an input for key derivation or key agreement.
3201 *
3202 * Which inputs are required and in what order depends on the algorithm.
3203 * Refer to the documentation of each key derivation or key agreement
3204 * algorithm for information.
3205 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003206 * This function passes direct inputs, which is usually correct for
3207 * non-secret inputs. To pass a secret input, which should be in a key
3208 * object, call psa_key_derivation_input_key() instead of this function.
3209 * Refer to the documentation of individual step types
3210 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3211 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003212 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003213 * If this function returns an error status, the operation enters an error
3214 * state and must be aborted by calling psa_key_derivation_abort().
3215 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003216 * \param[in,out] operation The key derivation operation object to use.
3217 * It must have been set up with
3218 * psa_key_derivation_setup() and must not
3219 * have produced any output yet.
3220 * \param step Which step the input data is for.
3221 * \param[in] data Input data to use.
3222 * \param data_length Size of the \p data buffer in bytes.
3223 *
3224 * \retval #PSA_SUCCESS
3225 * Success.
3226 * \retval #PSA_ERROR_INVALID_ARGUMENT
3227 * \c step is not compatible with the operation's algorithm.
3228 * \retval #PSA_ERROR_INVALID_ARGUMENT
3229 * \c step does not allow direct inputs.
3230 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3231 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3232 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003233 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003234 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003235 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003236 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003237 * \retval #PSA_ERROR_BAD_STATE
3238 * The library has not been previously initialized by psa_crypto_init().
3239 * It is implementation-dependent whether a failure to initialize
3240 * results in this error code.
3241 */
3242psa_status_t psa_key_derivation_input_bytes(
3243 psa_key_derivation_operation_t *operation,
3244 psa_key_derivation_step_t step,
3245 const uint8_t *data,
3246 size_t data_length);
3247
3248/** Provide an input for key derivation in the form of a key.
3249 *
3250 * Which inputs are required and in what order depends on the algorithm.
3251 * Refer to the documentation of each key derivation or key agreement
3252 * algorithm for information.
3253 *
Gilles Peskine7ebd4dc2019-09-24 17:15:58 +02003254 * This function obtains input from a key object, which is usually correct for
3255 * secret inputs or for non-secret personalization strings kept in the key
3256 * store. To pass a non-secret parameter which is not in the key store,
3257 * call psa_key_derivation_input_bytes() instead of this function.
3258 * Refer to the documentation of individual step types
3259 * (`PSA_KEY_DERIVATION_INPUT_xxx` values of type ::psa_key_derivation_step_t)
3260 * for more information.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003261 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003262 * If this function returns an error status, the operation enters an error
3263 * state and must be aborted by calling psa_key_derivation_abort().
3264 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003265 * \param[in,out] operation The key derivation operation object to use.
3266 * It must have been set up with
3267 * psa_key_derivation_setup() and must not
3268 * have produced any output yet.
3269 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003270 * \param key Identifier of the key. It must have an
3271 * appropriate type for step and must allow the
3272 * usage PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003273 *
3274 * \retval #PSA_SUCCESS
3275 * Success.
3276 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003277 * \retval #PSA_ERROR_NOT_PERMITTED
3278 * \retval #PSA_ERROR_INVALID_ARGUMENT
3279 * \c step is not compatible with the operation's algorithm.
3280 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine224b0d62019-09-23 18:13:17 +02003281 * \c step does not allow key inputs of the given type
3282 * or does not allow key inputs at all.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003283 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3284 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3285 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003286 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003287 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003288 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003289 * The operation state is not valid for this input \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003290 * \retval #PSA_ERROR_BAD_STATE
3291 * The library has not been previously initialized by psa_crypto_init().
3292 * It is implementation-dependent whether a failure to initialize
3293 * results in this error code.
3294 */
3295psa_status_t psa_key_derivation_input_key(
3296 psa_key_derivation_operation_t *operation,
3297 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003298 mbedtls_svc_key_id_t key);
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003299
3300/** Perform a key agreement and use the shared secret as input to a key
3301 * derivation.
3302 *
3303 * A key agreement algorithm takes two inputs: a private key \p private_key
3304 * a public key \p peer_key.
3305 * The result of this function is passed as input to a key derivation.
3306 * The output of this key derivation can be extracted by reading from the
3307 * resulting operation to produce keys and other cryptographic material.
3308 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003309 * If this function returns an error status, the operation enters an error
3310 * state and must be aborted by calling psa_key_derivation_abort().
3311 *
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003312 * \param[in,out] operation The key derivation operation object to use.
3313 * It must have been set up with
3314 * psa_key_derivation_setup() with a
3315 * key agreement and derivation algorithm
3316 * \c alg (\c PSA_ALG_XXX value such that
3317 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3318 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
3319 * is false).
3320 * The operation must be ready for an
3321 * input of the type given by \p step.
3322 * \param step Which step the input data is for.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003323 * \param private_key Identifier of the private key to use. It must
3324 * allow the usage PSA_KEY_USAGE_DERIVE.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003325 * \param[in] peer_key Public key of the peer. The peer key must be in the
3326 * same format that psa_import_key() accepts for the
3327 * public key type corresponding to the type of
3328 * private_key. That is, this function performs the
3329 * equivalent of
3330 * #psa_import_key(...,
3331 * `peer_key`, `peer_key_length`) where
3332 * with key attributes indicating the public key
3333 * type corresponding to the type of `private_key`.
3334 * For example, for EC keys, this means that peer_key
3335 * is interpreted as a point on the curve that the
3336 * private key is on. The standard formats for public
3337 * keys are documented in the documentation of
3338 * psa_export_public_key().
3339 * \param peer_key_length Size of \p peer_key in bytes.
3340 *
3341 * \retval #PSA_SUCCESS
3342 * Success.
Andrew Thoelke340984b2019-09-11 21:33:41 +01003343 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003344 * The operation state is not valid for this key agreement \p step.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003345 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003346 * \retval #PSA_ERROR_NOT_PERMITTED
3347 * \retval #PSA_ERROR_INVALID_ARGUMENT
3348 * \c private_key is not compatible with \c alg,
3349 * or \p peer_key is not valid for \c alg or not compatible with
3350 * \c private_key.
3351 * \retval #PSA_ERROR_NOT_SUPPORTED
3352 * \c alg is not supported or is not a key derivation algorithm.
Gilles Peskine224b0d62019-09-23 18:13:17 +02003353 * \retval #PSA_ERROR_INVALID_ARGUMENT
3354 * \c step does not allow an input resulting from a key agreement.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003355 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3356 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3357 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003358 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003359 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003360 * \retval #PSA_ERROR_BAD_STATE
3361 * The library has not been previously initialized by psa_crypto_init().
3362 * It is implementation-dependent whether a failure to initialize
3363 * results in this error code.
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003364 */
3365psa_status_t psa_key_derivation_key_agreement(
3366 psa_key_derivation_operation_t *operation,
3367 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003368 mbedtls_svc_key_id_t private_key,
Gilles Peskine1cb9a082019-05-16 17:56:47 +02003369 const uint8_t *peer_key,
3370 size_t peer_key_length);
3371
Gilles Peskine35675b62019-05-16 17:26:11 +02003372/** Read some data from a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003373 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003374 * This function calculates output bytes from a key derivation algorithm and
3375 * return those bytes.
3376 * If you view the key derivation's output as a stream of bytes, this
3377 * function destructively reads the requested number of bytes from the
3378 * stream.
3379 * The operation's capacity decreases by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003380 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003381 * If this function returns an error status other than
3382 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003383 * state and must be aborted by calling psa_key_derivation_abort().
3384 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003385 * \param[in,out] operation The key derivation operation object to read from.
3386 * \param[out] output Buffer where the output will be written.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003387 * \param output_length Number of bytes to output.
3388 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003389 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02003390 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskine35675b62019-05-16 17:26:11 +02003391 * The operation's capacity was less than
3392 * \p output_length bytes. Note that in this case,
3393 * no output is written to the output buffer.
3394 * The operation's capacity is set to 0, thus
Gilles Peskineeab56e42018-07-12 17:12:33 +02003395 * subsequent calls to this function will not
3396 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003397 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003398 * The operation state is not valid (it must be active and completed
3399 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003400 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3401 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3402 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003403 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003404 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003405 * \retval #PSA_ERROR_BAD_STATE
3406 * The library has not been previously initialized by psa_crypto_init().
3407 * It is implementation-dependent whether a failure to initialize
3408 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003409 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003410psa_status_t psa_key_derivation_output_bytes(
3411 psa_key_derivation_operation_t *operation,
3412 uint8_t *output,
3413 size_t output_length);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003414
Gilles Peskine35675b62019-05-16 17:26:11 +02003415/** Derive a key from an ongoing key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003416 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003417 * This function calculates output bytes from a key derivation algorithm
3418 * and uses those bytes to generate a key deterministically.
Gilles Peskinea170d922019-09-12 16:59:37 +02003419 * The key's location, usage policy, type and size are taken from
3420 * \p attributes.
3421 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003422 * If you view the key derivation's output as a stream of bytes, this
3423 * function destructively reads as many bytes as required from the
3424 * stream.
3425 * The operation's capacity decreases by the number of bytes read.
3426 *
Andrew Thoelke51514f52019-09-18 17:50:01 +01003427 * If this function returns an error status other than
3428 * #PSA_ERROR_INSUFFICIENT_DATA, the operation enters an error
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003429 * state and must be aborted by calling psa_key_derivation_abort().
3430 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003431 * How much output is produced and consumed from the operation, and how
3432 * the key is derived, depends on the key type:
Gilles Peskineeab56e42018-07-12 17:12:33 +02003433 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003434 * - For key types for which the key is an arbitrary sequence of bytes
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003435 * of a given size, this function is functionally equivalent to
3436 * calling #psa_key_derivation_output_bytes
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003437 * and passing the resulting output to #psa_import_key.
3438 * However, this function has a security benefit:
3439 * if the implementation provides an isolation boundary then
3440 * the key material is not exposed outside the isolation boundary.
3441 * As a consequence, for these key types, this function always consumes
Gilles Peskine35675b62019-05-16 17:26:11 +02003442 * exactly (\p bits / 8) bytes from the operation.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003443 * The following key types defined in this specification follow this scheme:
3444 *
3445 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003446 * - #PSA_KEY_TYPE_ARC4;
3447 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003448 * - #PSA_KEY_TYPE_DERIVE;
3449 * - #PSA_KEY_TYPE_HMAC.
3450 *
3451 * - For ECC keys on a Montgomery elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003452 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003453 * Montgomery curve), this function always draws a byte string whose
3454 * length is determined by the curve, and sets the mandatory bits
3455 * accordingly. That is:
3456 *
Paul Elliott8ff510a2020-06-02 17:19:28 +01003457 * - Curve25519 (#PSA_ECC_FAMILY_MONTGOMERY, 255 bits): draw a 32-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003458 * string and process it as specified in RFC 7748 &sect;5.
Paul Elliott8ff510a2020-06-02 17:19:28 +01003459 * - Curve448 (#PSA_ECC_FAMILY_MONTGOMERY, 448 bits): draw a 56-byte
Gilles Peskine228abc52019-12-03 17:24:19 +01003460 * string and process it as specified in RFC 7748 &sect;5.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003461 *
3462 * - For key types for which the key is represented by a single sequence of
3463 * \p bits bits with constraints as to which bit sequences are acceptable,
3464 * this function draws a byte string of length (\p bits / 8) bytes rounded
3465 * up to the nearest whole number of bytes. If the resulting byte string
3466 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
3467 * This process is repeated until an acceptable byte string is drawn.
Gilles Peskine35675b62019-05-16 17:26:11 +02003468 * The byte string drawn from the operation is interpreted as specified
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003469 * for the output produced by psa_export_key().
3470 * The following key types defined in this specification follow this scheme:
3471 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003472 * - #PSA_KEY_TYPE_DES.
3473 * Force-set the parity bits, but discard forbidden weak keys.
3474 * For 2-key and 3-key triple-DES, the three keys are generated
3475 * successively (for example, for 3-key triple-DES,
3476 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
3477 * discard the first 8 bytes, use the next 8 bytes as the first key,
Gilles Peskine35675b62019-05-16 17:26:11 +02003478 * and continue reading output from the operation to derive the other
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003479 * two keys).
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003480 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEY_PAIR(\c group)
Gilles Peskinea1302192019-05-16 13:58:24 +02003481 * where \c group designates any Diffie-Hellman group) and
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003482 * ECC keys on a Weierstrass elliptic curve
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003483 * (#PSA_KEY_TYPE_ECC_KEY_PAIR(\c curve) where \c curve designates a
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003484 * Weierstrass curve).
3485 * For these key types, interpret the byte string as integer
3486 * in big-endian order. Discard it if it is not in the range
3487 * [0, *N* - 2] where *N* is the boundary of the private key domain
3488 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003489 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003490 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003491 * This method allows compliance to NIST standards, specifically
3492 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003493 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3494 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3495 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3496 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003497 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003498 * - For other key types, including #PSA_KEY_TYPE_RSA_KEY_PAIR,
Gilles Peskine35675b62019-05-16 17:26:11 +02003499 * the way in which the operation output is consumed is
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003500 * implementation-defined.
3501 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003502 * In all cases, the data that is read is discarded from the operation.
3503 * The operation's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003504 *
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003505 * For algorithms that take an input step #PSA_KEY_DERIVATION_INPUT_SECRET,
3506 * the input to that step must be provided with psa_key_derivation_input_key().
3507 * Future versions of this specification may include additional restrictions
3508 * on the derived key based on the attributes and strength of the secret key.
3509 *
Gilles Peskine20628592019-04-19 19:29:50 +02003510 * \param[in] attributes The attributes for the new key.
Gilles Peskine35675b62019-05-16 17:26:11 +02003511 * \param[in,out] operation The key derivation operation object to read from.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003512 * \param[out] key On success, an identifier for the newly created
3513 * key. \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003514 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003515 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003516 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003517 * If the key is persistent, the key material and the key's metadata
3518 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003519 * \retval #PSA_ERROR_ALREADY_EXISTS
3520 * This is an attempt to create a persistent key, and there is
3521 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003522 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003523 * There was not enough data to create the desired key.
3524 * Note that in this case, no output is written to the output buffer.
Gilles Peskine35675b62019-05-16 17:26:11 +02003525 * The operation's capacity is set to 0, thus subsequent calls to
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003526 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003527 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003528 * The key type or key size is not supported, either by the
Adrian L. Shaw67e1c7a2019-05-14 15:24:21 +01003529 * implementation in general or in this particular location.
k-stachowiakb9b4f092019-08-15 19:01:59 +02003530 * \retval #PSA_ERROR_INVALID_ARGUMENT
3531 * The provided key attributes are not valid for the operation.
Gilles Peskine178c9aa2019-09-24 18:21:06 +02003532 * \retval #PSA_ERROR_NOT_PERMITTED
3533 * The #PSA_KEY_DERIVATION_INPUT_SECRET input was not provided through
3534 * a key.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003535 * \retval #PSA_ERROR_BAD_STATE
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003536 * The operation state is not valid (it must be active and completed
3537 * all required input steps).
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003538 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3539 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3540 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3541 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003542 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw484ba882019-08-13 14:41:52 +01003543 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003544 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003545 * The library has not been previously initialized by psa_crypto_init().
3546 * It is implementation-dependent whether a failure to initialize
3547 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003548 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003549psa_status_t psa_key_derivation_output_key(
3550 const psa_key_attributes_t *attributes,
3551 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003552 mbedtls_svc_key_id_t *key);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003553
Gilles Peskine35675b62019-05-16 17:26:11 +02003554/** Abort a key derivation operation.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003555 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003556 * Aborting an operation frees all associated resources except for the \c
3557 * operation structure itself. Once aborted, the operation object can be reused
3558 * for another operation by calling psa_key_derivation_setup() again.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003559 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003560 * This function may be called at any time after the operation
3561 * object has been initialized as described in #psa_key_derivation_operation_t.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003562 *
Andrew Thoelkebeb97ba2019-09-13 15:27:46 +01003563 * In particular, it is valid to call psa_key_derivation_abort() twice, or to
3564 * call psa_key_derivation_abort() on an operation that has not been set up.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003565 *
Gilles Peskine35675b62019-05-16 17:26:11 +02003566 * \param[in,out] operation The operation to abort.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003567 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003568 * \retval #PSA_SUCCESS
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003569 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3570 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003571 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003572 * \retval #PSA_ERROR_BAD_STATE
3573 * The library has not been previously initialized by psa_crypto_init().
3574 * It is implementation-dependent whether a failure to initialize
3575 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003576 */
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003577psa_status_t psa_key_derivation_abort(
3578 psa_key_derivation_operation_t *operation);
Gilles Peskineeab56e42018-07-12 17:12:33 +02003579
Gilles Peskine58fe9e82019-05-16 18:01:45 +02003580/** Perform a key agreement and return the raw shared secret.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003581 *
3582 * \warning The raw result of a key agreement algorithm such as finite-field
3583 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3584 * not be used directly as key material. It should instead be passed as
3585 * input to a key derivation algorithm. To chain a key agreement with
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003586 * a key derivation, use psa_key_derivation_key_agreement() and other
3587 * functions from the key derivation interface.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003588 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003589 * \param alg The key agreement algorithm to compute
3590 * (\c PSA_ALG_XXX value such that
3591 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3592 * is true).
Ronald Croncf56a0a2020-08-04 09:51:30 +02003593 * \param private_key Identifier of the private key to use. It must
3594 * allow the usage PSA_KEY_USAGE_DERIVE.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003595 * \param[in] peer_key Public key of the peer. It must be
3596 * in the same format that psa_import_key()
3597 * accepts. The standard formats for public
3598 * keys are documented in the documentation
3599 * of psa_export_public_key().
3600 * \param peer_key_length Size of \p peer_key in bytes.
3601 * \param[out] output Buffer where the decrypted message is to
3602 * be written.
3603 * \param output_size Size of the \c output buffer in bytes.
3604 * \param[out] output_length On success, the number of bytes
3605 * that make up the returned output.
3606 *
3607 * \retval #PSA_SUCCESS
3608 * Success.
3609 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine769c7a62019-01-18 16:42:29 +01003610 * \retval #PSA_ERROR_NOT_PERMITTED
3611 * \retval #PSA_ERROR_INVALID_ARGUMENT
3612 * \p alg is not a key agreement algorithm
3613 * \retval #PSA_ERROR_INVALID_ARGUMENT
3614 * \p private_key is not compatible with \p alg,
3615 * or \p peer_key is not valid for \p alg or not compatible with
3616 * \p private_key.
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003617 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
3618 * \p output_size is too small
Gilles Peskine769c7a62019-01-18 16:42:29 +01003619 * \retval #PSA_ERROR_NOT_SUPPORTED
3620 * \p alg is not a supported key agreement algorithm.
3621 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3622 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3623 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003624 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shaw0d280b92019-08-08 15:07:07 +01003625 * \retval #PSA_ERROR_STORAGE_FAILURE
Adrian L. Shawdec47b62019-08-07 14:25:38 +01003626 * \retval #PSA_ERROR_BAD_STATE
3627 * The library has not been previously initialized by psa_crypto_init().
3628 * It is implementation-dependent whether a failure to initialize
3629 * results in this error code.
Gilles Peskine769c7a62019-01-18 16:42:29 +01003630 */
Gilles Peskinebe697d82019-05-16 18:00:41 +02003631psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003632 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02003633 const uint8_t *peer_key,
3634 size_t peer_key_length,
3635 uint8_t *output,
3636 size_t output_size,
3637 size_t *output_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003638
Gilles Peskineea0fb492018-07-12 17:17:20 +02003639/**@}*/
3640
Gilles Peskineedd76872018-07-20 17:42:05 +02003641/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003642 * @{
3643 */
3644
3645/**
3646 * \brief Generate random bytes.
3647 *
3648 * \warning This function **can** fail! Callers MUST check the return status
3649 * and MUST NOT use the content of the output buffer if the return
3650 * status is not #PSA_SUCCESS.
3651 *
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003652 * \note To generate a key, use psa_generate_key() instead.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003653 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003654 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003655 * \param output_size Number of bytes to generate and output.
3656 *
Gilles Peskine28538492018-07-11 17:34:00 +02003657 * \retval #PSA_SUCCESS
3658 * \retval #PSA_ERROR_NOT_SUPPORTED
3659 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Adrian L. Shaw71b33ff2019-08-08 15:07:57 +01003660 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine28538492018-07-11 17:34:00 +02003661 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3662 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003663 * \retval #PSA_ERROR_CORRUPTION_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003664 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003665 * The library has not been previously initialized by psa_crypto_init().
3666 * It is implementation-dependent whether a failure to initialize
3667 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003668 */
3669psa_status_t psa_generate_random(uint8_t *output,
3670 size_t output_size);
3671
3672/**
3673 * \brief Generate a key or key pair.
3674 *
Gilles Peskinee56e8782019-04-26 17:34:02 +02003675 * The key is generated randomly.
Gilles Peskinea170d922019-09-12 16:59:37 +02003676 * Its location, usage policy, type and size are taken from \p attributes.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003677 *
Gilles Peskine05c900b2019-09-12 18:29:43 +02003678 * Implementations must reject an attempt to generate a key of size 0.
Gilles Peskinee56e8782019-04-26 17:34:02 +02003679 *
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003680 * The following type-specific considerations apply:
Gilles Peskinec93b80c2019-05-16 19:39:54 +02003681 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine20a77ae2019-05-16 14:05:56 +02003682 * the public exponent is 65537.
3683 * The modulus is a product of two probabilistic primes
3684 * between 2^{n-1} and 2^n where n is the bit size specified in the
3685 * attributes.
3686 *
Gilles Peskine20628592019-04-19 19:29:50 +02003687 * \param[in] attributes The attributes for the new key.
Ronald Croncf56a0a2020-08-04 09:51:30 +02003688 * \param[out] key On success, an identifier for the newly created
3689 * key. \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003690 *
Gilles Peskine28538492018-07-11 17:34:00 +02003691 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003692 * Success.
3693 * If the key is persistent, the key material and the key's metadata
3694 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003695 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003696 * This is an attempt to create a persistent key, and there is
3697 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +02003698 * \retval #PSA_ERROR_NOT_SUPPORTED
3699 * \retval #PSA_ERROR_INVALID_ARGUMENT
3700 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3701 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3702 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3703 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskine4b3eb692019-05-16 21:35:18 +02003704 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Adrian L. Shawd21c6e62019-08-08 10:58:08 +01003705 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3706 * \retval #PSA_ERROR_STORAGE_FAILURE
itayzafrir90d8c7a2018-09-12 11:44:52 +03003707 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003708 * The library has not been previously initialized by psa_crypto_init().
3709 * It is implementation-dependent whether a failure to initialize
3710 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003711 */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02003712psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003713 mbedtls_svc_key_id_t *key);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003714
3715/**@}*/
3716
Gilles Peskinee59236f2018-01-27 23:32:46 +01003717#ifdef __cplusplus
3718}
3719#endif
3720
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003721/* The file "crypto_sizes.h" contains definitions for size calculation
3722 * macros whose definitions are implementation-specific. */
3723#include "crypto_sizes.h"
3724
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003725/* The file "crypto_struct.h" contains definitions for
3726 * implementation-specific structs that are declared above. */
3727#include "crypto_struct.h"
3728
3729/* The file "crypto_extra.h" contains vendor-specific definitions. This
3730 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003731#include "crypto_extra.h"
3732
3733#endif /* PSA_CRYPTO_H */