blob: 5e7c860b3478e0583368295c7da8aab3fd2c26c8 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
89 * \retval #PSA_ERROR_TAMPERING_DETECTED
90 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine4cb9dde2019-01-19 13:40:11 +010096/** \defgroup policy Key policies
97 * @{
98 */
99
100/** The type of the key policy data structure.
101 *
102 * Before calling any function on a key policy, the application must initialize
103 * it by any of the following means:
104 * - Set the structure to all-bits-zero, for example:
105 * \code
106 * psa_key_policy_t policy;
107 * memset(&policy, 0, sizeof(policy));
108 * \endcode
109 * - Initialize the structure to logical zero values, for example:
110 * \code
111 * psa_key_policy_t policy = {0};
112 * \endcode
113 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
114 * for example:
115 * \code
116 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
117 * \endcode
118 * - Assign the result of the function psa_key_policy_init()
119 * to the structure, for example:
120 * \code
121 * psa_key_policy_t policy;
122 * policy = psa_key_policy_init();
123 * \endcode
124 *
125 * This is an implementation-defined \c struct. Applications should not
126 * make any assumptions about the content of this structure except
127 * as directed by the documentation of a specific implementation. */
128typedef struct psa_key_policy_s psa_key_policy_t;
129
130/** \def PSA_KEY_POLICY_INIT
131 *
132 * This macro returns a suitable initializer for a key policy object of type
133 * #psa_key_policy_t.
134 */
135#ifdef __DOXYGEN_ONLY__
136/* This is an example definition for documentation purposes.
137 * Implementations should define a suitable value in `crypto_struct.h`.
138 */
139#define PSA_KEY_POLICY_INIT {0}
140#endif
141
142/** Return an initial value for a key policy that forbids all usage of the key.
143 */
144static psa_key_policy_t psa_key_policy_init(void);
145
146/** \brief Set the standard fields of a policy structure.
147 *
148 * Note that this function does not make any consistency check of the
149 * parameters. The values are only checked when applying the policy to
150 * a key slot with psa_set_key_policy().
151 *
152 * \param[in,out] policy The key policy to modify. It must have been
153 * initialized as per the documentation for
154 * #psa_key_policy_t.
155 * \param usage The permitted uses for the key.
156 * \param alg The algorithm that the key may be used for.
157 */
158void psa_key_policy_set_usage(psa_key_policy_t *policy,
159 psa_key_usage_t usage,
160 psa_algorithm_t alg);
161
162/** \brief Retrieve the usage field of a policy structure.
163 *
164 * \param[in] policy The policy object to query.
165 *
166 * \return The permitted uses for a key with this policy.
167 */
168psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
169
170/** \brief Retrieve the algorithm field of a policy structure.
171 *
172 * \param[in] policy The policy object to query.
173 *
174 * \return The permitted algorithm for a key with this policy.
175 */
176psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
177
178/** \brief Set the usage policy on a key slot.
179 *
180 * This function must be called on an empty key slot, before importing,
181 * generating or creating a key in the slot. Changing the policy of an
182 * existing key is not permitted.
183 *
184 * Implementations may set restrictions on supported key policies
185 * depending on the key type and the key slot.
186 *
187 * \param handle Handle to the key whose policy is to be changed.
188 * \param[in] policy The policy object to query.
189 *
190 * \retval #PSA_SUCCESS
191 * Success.
192 * If the key is persistent, it is implementation-defined whether
193 * the policy has been saved to persistent storage. Implementations
194 * may defer saving the policy until the key material is created.
195 * \retval #PSA_ERROR_INVALID_HANDLE
196 * \retval #PSA_ERROR_OCCUPIED_SLOT
197 * \retval #PSA_ERROR_NOT_SUPPORTED
198 * \retval #PSA_ERROR_INVALID_ARGUMENT
199 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
200 * \retval #PSA_ERROR_HARDWARE_FAILURE
201 * \retval #PSA_ERROR_TAMPERING_DETECTED
202 * \retval #PSA_ERROR_BAD_STATE
203 * The library has not been previously initialized by psa_crypto_init().
204 * It is implementation-dependent whether a failure to initialize
205 * results in this error code.
206 */
207psa_status_t psa_set_key_policy(psa_key_handle_t handle,
208 const psa_key_policy_t *policy);
209
210/** \brief Get the usage policy for a key slot.
211 *
212 * \param handle Handle to the key slot whose policy is being queried.
213 * \param[out] policy On success, the key's policy.
214 *
215 * \retval #PSA_SUCCESS
216 * \retval #PSA_ERROR_INVALID_HANDLE
217 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
218 * \retval #PSA_ERROR_HARDWARE_FAILURE
219 * \retval #PSA_ERROR_TAMPERING_DETECTED
220 * \retval #PSA_ERROR_BAD_STATE
221 * The library has not been previously initialized by psa_crypto_init().
222 * It is implementation-dependent whether a failure to initialize
223 * results in this error code.
224 */
225psa_status_t psa_get_key_policy(psa_key_handle_t handle,
226 psa_key_policy_t *policy);
227
228/**@}*/
229
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100230/** \defgroup key_management Key management
231 * @{
232 */
233
Gilles Peskineae32aac2018-11-30 14:39:32 +0100234/** \brief Retrieve the lifetime of an open key.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100235 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100236 * \param handle Handle to query.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100237 * \param[out] lifetime On success, the lifetime value.
238 *
239 * \retval #PSA_SUCCESS
240 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100241 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100242 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
243 * \retval #PSA_ERROR_HARDWARE_FAILURE
244 * \retval #PSA_ERROR_TAMPERING_DETECTED
245 * \retval #PSA_ERROR_BAD_STATE
246 * The library has not been previously initialized by psa_crypto_init().
247 * It is implementation-dependent whether a failure to initialize
248 * results in this error code.
249 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100250psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100251 psa_key_lifetime_t *lifetime);
252
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100253
Gilles Peskinef535eb22018-11-30 14:08:36 +0100254/** Allocate a key slot for a transient key, i.e. a key which is only stored
255 * in volatile memory.
256 *
257 * The allocated key slot and its handle remain valid until the
258 * application calls psa_close_key() or psa_destroy_key() or until the
259 * application terminates.
260 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100261 * \param[out] handle On success, a handle to a volatile key slot.
262 *
263 * \retval #PSA_SUCCESS
264 * Success. The application can now use the value of `*handle`
265 * to access the newly allocated key slot.
266 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
267 * There was not enough memory, or the maximum number of key slots
268 * has been reached.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100269 */
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100270psa_status_t psa_allocate_key(psa_key_handle_t *handle);
Gilles Peskinef535eb22018-11-30 14:08:36 +0100271
272/** Open a handle to an existing persistent key.
273 *
274 * Open a handle to a key which was previously created with psa_create_key().
275 *
276 * \param lifetime The lifetime of the key. This designates a storage
277 * area where the key material is stored. This must not
278 * be #PSA_KEY_LIFETIME_VOLATILE.
279 * \param id The persistent identifier of the key.
280 * \param[out] handle On success, a handle to a key slot which contains
281 * the data and metadata loaded from the specified
282 * persistent location.
283 *
284 * \retval #PSA_SUCCESS
285 * Success. The application can now use the value of `*handle`
286 * to access the newly allocated key slot.
287 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
288 * \retval #PSA_ERROR_EMPTY_SLOT
289 * \retval #PSA_ERROR_INVALID_ARGUMENT
290 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
291 * \retval #PSA_ERROR_INVALID_ARGUMENT
292 * \p id is invalid for the specified lifetime.
293 * \retval #PSA_ERROR_NOT_SUPPORTED
294 * \p lifetime is not supported.
295 * \retval #PSA_ERROR_NOT_PERMITTED
296 * The specified key exists, but the application does not have the
297 * permission to access it. Note that this specification does not
298 * define any way to create such a key, but it may be possible
299 * through implementation-specific means.
300 */
301psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
302 psa_key_id_t id,
303 psa_key_handle_t *handle);
304
305/** Create a new persistent key slot.
306 *
307 * Create a new persistent key slot and return a handle to it. The handle
308 * remains valid until the application calls psa_close_key() or terminates.
309 * The application can open the key again with psa_open_key() until it
310 * removes the key by calling psa_destroy_key().
311 *
312 * \param lifetime The lifetime of the key. This designates a storage
313 * area where the key material is stored. This must not
314 * be #PSA_KEY_LIFETIME_VOLATILE.
315 * \param id The persistent identifier of the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100316 * \param[out] handle On success, a handle to the newly created key slot.
317 * When key material is later created in this key slot,
318 * it will be saved to the specified persistent location.
319 *
320 * \retval #PSA_SUCCESS
321 * Success. The application can now use the value of `*handle`
322 * to access the newly allocated key slot.
323 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
324 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
325 * \retval #PSA_ERROR_OCCUPIED_SLOT
326 * There is already a key with the identifier \p id in the storage
327 * area designated by \p lifetime.
328 * \retval #PSA_ERROR_INVALID_ARGUMENT
329 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
330 * \retval #PSA_ERROR_INVALID_ARGUMENT
331 * \p id is invalid for the specified lifetime.
332 * \retval #PSA_ERROR_NOT_SUPPORTED
333 * \p lifetime is not supported.
334 * \retval #PSA_ERROR_NOT_PERMITTED
335 * \p lifetime is valid, but the application does not have the
336 * permission to create a key there.
337 */
338psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
339 psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100340 psa_key_handle_t *handle);
341
342/** Close a key handle.
343 *
344 * If the handle designates a volatile key, destroy the key material and
345 * free all associated resources, just like psa_destroy_key().
346 *
347 * If the handle designates a persistent key, free all resources associated
348 * with the key in volatile memory. The key slot in persistent storage is
349 * not affected and can be opened again later with psa_open_key().
350 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100351 * If the key is currently in use in a multipart operation,
352 * the multipart operation is aborted.
353 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100354 * \param handle The key handle to close.
355 *
356 * \retval #PSA_SUCCESS
357 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100358 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100359 */
360psa_status_t psa_close_key(psa_key_handle_t handle);
361
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100362/**@}*/
363
364/** \defgroup import_export Key import and export
365 * @{
366 */
367
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100368/**
369 * \brief Import a key in binary format.
370 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100371 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100372 * documentation of psa_export_public_key() for the format of public keys
373 * and to the documentation of psa_export_key() for the format for
374 * other key types.
375 *
376 * This specification supports a single format for each key type.
377 * Implementations may support other formats as long as the standard
378 * format is supported. Implementations that support other formats
379 * should ensure that the formats are clearly unambiguous so as to
380 * minimize the risk that an invalid input is accidentally interpreted
381 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100382 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100383 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100384 * It must have been obtained by calling
385 * psa_allocate_key() or psa_create_key() and must
386 * not contain key material yet.
Gilles Peskinef7933932018-10-31 14:07:52 +0100387 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
388 * import, the key slot will contain a key of this type.
389 * \param[in] data Buffer containing the key data. The content of this
390 * buffer is interpreted according to \p type. It must
391 * contain the format described in the documentation
392 * of psa_export_key() or psa_export_public_key() for
393 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200394 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100395 *
Gilles Peskine28538492018-07-11 17:34:00 +0200396 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100397 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100398 * If the key is persistent, the key material and the key's metadata
399 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100400 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200401 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200402 * The key type or key size is not supported, either by the
403 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200404 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100405 * The key slot is invalid,
406 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200407 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200408 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200409 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
410 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
411 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100412 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200413 * \retval #PSA_ERROR_HARDWARE_FAILURE
414 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300415 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300416 * The library has not been previously initialized by psa_crypto_init().
417 * It is implementation-dependent whether a failure to initialize
418 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100419 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100420psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100421 psa_key_type_t type,
422 const uint8_t *data,
423 size_t data_length);
424
425/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100426 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200427 *
428 * This function destroys the content of the key slot from both volatile
429 * memory and, if applicable, non-volatile storage. Implementations shall
430 * make a best effort to ensure that any previous content of the slot is
431 * unrecoverable.
432 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100433 * This function also erases any metadata such as policies and frees all
434 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200435 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100436 * If the key is currently in use in a multipart operation,
437 * the multipart operation is aborted.
438 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100439 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100440 *
Gilles Peskine28538492018-07-11 17:34:00 +0200441 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200442 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200443 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200444 * The slot holds content and cannot be erased because it is
445 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100446 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200447 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200448 * There was an failure in communication with the cryptoprocessor.
449 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200450 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200451 * The storage is corrupted. Implementations shall make a best effort
452 * to erase key material even in this stage, however applications
453 * should be aware that it may be impossible to guarantee that the
454 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200455 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200456 * An unexpected condition which is not a storage corruption or
457 * a communication failure occurred. The cryptoprocessor may have
458 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300459 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300460 * The library has not been previously initialized by psa_crypto_init().
461 * It is implementation-dependent whether a failure to initialize
462 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100463 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100464psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100465
466/**
467 * \brief Get basic metadata about a key.
468 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100469 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200470 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100471 * This may be a null pointer, in which case the key type
472 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200473 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100474 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100475 * is not written.
476 *
Gilles Peskine28538492018-07-11 17:34:00 +0200477 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100478 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200479 * \retval #PSA_ERROR_EMPTY_SLOT
Gilles Peskineae32aac2018-11-30 14:39:32 +0100480 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200481 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
482 * \retval #PSA_ERROR_HARDWARE_FAILURE
483 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300484 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300485 * The library has not been previously initialized by psa_crypto_init().
486 * It is implementation-dependent whether a failure to initialize
487 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100488 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100489psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100490 psa_key_type_t *type,
491 size_t *bits);
492
493/**
Jaeden Amero283dfd12019-01-11 12:06:22 +0000494 * \brief Set domain parameters for a key.
495 *
496 * Some key types require additional domain parameters to be set before import
497 * or generation of the key. The domain parameters can be set with this
498 * function or, for key generation, through the \c extra parameter of
499 * psa_generate_key().
500 *
501 * The format for the required domain parameters varies by the key type.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000502 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
503 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
504 * ```
505 * Dss-Parms ::= SEQUENCE {
506 * p INTEGER,
507 * q INTEGER,
508 * g INTEGER
509 * }
510 * ```
Jaeden Amero8851c402019-01-11 14:20:03 +0000511 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY), the
512 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
513 * ```
514 * DomainParameters ::= SEQUENCE {
515 * p INTEGER, -- odd prime, p=jq +1
516 * g INTEGER, -- generator, g
517 * q INTEGER, -- factor of p-1
518 * j INTEGER OPTIONAL, -- subgroup factor
519 * validationParms ValidationParms OPTIONAL
520 * }
521 * ValidationParms ::= SEQUENCE {
522 * seed BIT STRING,
523 * pgenCounter INTEGER
524 * }
525 * ```
Jaeden Amero283dfd12019-01-11 12:06:22 +0000526 *
Gilles Peskine3a74e002019-01-18 17:11:25 +0100527 * \param handle Handle to the slot where the key will be stored.
528 * This must be a valid slot for a key of the chosen
529 * type: it must have been obtained by calling
530 * psa_allocate_key() or psa_create_key() with the
531 * correct \p type and with a maximum size that is
532 * compatible with \p data. It must not contain
533 * key material yet.
534 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). When
535 * subsequently creating key material into \p handle,
536 * the type must be compatible.
Jaeden Amero283dfd12019-01-11 12:06:22 +0000537 * \param[in] data Buffer containing the key domain parameters. The content
538 * of this buffer is interpreted according to \p type. of
539 * psa_export_key() or psa_export_public_key() for the
540 * chosen type.
541 * \param data_length Size of the \p data buffer in bytes.
542 *
543 * \retval #PSA_SUCCESS
544 * \retval #PSA_ERROR_INVALID_HANDLE
545 * \retval #PSA_ERROR_OCCUPIED_SLOT
546 * There is already a key in the specified slot.
547 * \retval #PSA_ERROR_INVALID_ARGUMENT
548 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
549 * \retval #PSA_ERROR_HARDWARE_FAILURE
550 * \retval #PSA_ERROR_TAMPERING_DETECTED
551 * \retval #PSA_ERROR_BAD_STATE
552 * The library has not been previously initialized by psa_crypto_init().
553 * It is implementation-dependent whether a failure to initialize
554 * results in this error code.
555 */
556psa_status_t psa_set_key_domain_parameters(psa_key_handle_t handle,
Gilles Peskine3a74e002019-01-18 17:11:25 +0100557 psa_key_type_t type,
Jaeden Amero283dfd12019-01-11 12:06:22 +0000558 const uint8_t *data,
559 size_t data_length);
560
561/**
562 * \brief Get domain parameters for a key.
563 *
564 * Get the domain parameters for a key with this function, if any. The format
565 * of the domain parameters written to \p data is specified in the
566 * documentation for psa_set_key_domain_parameters().
567 *
568 * \param handle Handle to the key to get domain parameters from.
569 * \param[out] data On success, the key domain parameters.
570 * \param data_size Size of the \p data buffer in bytes.
571 * \param[out] data_length On success, the number of bytes
572 * that make up the key domain parameters data.
573 *
574 * \retval #PSA_SUCCESS
575 * \retval #PSA_ERROR_INVALID_HANDLE
576 * \retval #PSA_ERROR_EMPTY_SLOT
577 * There is no key in the specified slot.
578 * \retval #PSA_ERROR_INVALID_ARGUMENT
579 * \retval #PSA_ERROR_NOT_SUPPORTED
580 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
581 * \retval #PSA_ERROR_HARDWARE_FAILURE
582 * \retval #PSA_ERROR_TAMPERING_DETECTED
583 * \retval #PSA_ERROR_BAD_STATE
584 * The library has not been previously initialized by psa_crypto_init().
585 * It is implementation-dependent whether a failure to initialize
586 * results in this error code.
587 */
588psa_status_t psa_get_key_domain_parameters(psa_key_handle_t handle,
589 uint8_t *data,
590 size_t data_size,
591 size_t *data_length);
592
593/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100594 * \brief Export a key in binary format.
595 *
596 * The output of this function can be passed to psa_import_key() to
597 * create an equivalent object.
598 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100599 * If the implementation of psa_import_key() supports other formats
600 * beyond the format specified here, the output from psa_export_key()
601 * must use the representation specified here, not the original
602 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100604 * For standard key types, the output format is as follows:
605 *
606 * - For symmetric keys (including MAC keys), the format is the
607 * raw bytes of the key.
608 * - For DES, the key data consists of 8 bytes. The parity bits must be
609 * correct.
610 * - For Triple-DES, the format is the concatenation of the
611 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100612 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200613 * is the non-encrypted DER encoding of the representation defined by
614 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
615 * ```
616 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200617 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200618 * modulus INTEGER, -- n
619 * publicExponent INTEGER, -- e
620 * privateExponent INTEGER, -- d
621 * prime1 INTEGER, -- p
622 * prime2 INTEGER, -- q
623 * exponent1 INTEGER, -- d mod (p-1)
624 * exponent2 INTEGER, -- d mod (q-1)
625 * coefficient INTEGER, -- (inverse of q) mod p
626 * }
627 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000628 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
629 * representation of the private key `x` as a big-endian byte string. The
630 * length of the byte string is the private key size in bytes (leading zeroes
631 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200632 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100633 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100634 * a representation of the private value as a `ceiling(m/8)`-byte string
635 * where `m` is the bit size associated with the curve, i.e. the bit size
636 * of the order of the curve's coordinate field. This byte string is
637 * in little-endian order for Montgomery curves (curve types
638 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
639 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
640 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100641 * This is the content of the `privateKey` field of the `ECPrivateKey`
642 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000643 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
644 * format is the representation of the private key `x` as a big-endian byte
645 * string. The length of the byte string is the private key size in bytes
646 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200647 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
648 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100649 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100650 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200651 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200652 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200653 * \param[out] data_length On success, the number of bytes
654 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100655 *
Gilles Peskine28538492018-07-11 17:34:00 +0200656 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100657 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200658 * \retval #PSA_ERROR_EMPTY_SLOT
659 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100660 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200661 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
662 * The size of the \p data buffer is too small. You can determine a
663 * sufficient buffer size by calling
664 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
665 * where \c type is the key type
666 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200667 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
668 * \retval #PSA_ERROR_HARDWARE_FAILURE
669 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300670 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300671 * The library has not been previously initialized by psa_crypto_init().
672 * It is implementation-dependent whether a failure to initialize
673 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100675psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676 uint8_t *data,
677 size_t data_size,
678 size_t *data_length);
679
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100680/**
681 * \brief Export a public key or the public part of a key pair in binary format.
682 *
683 * The output of this function can be passed to psa_import_key() to
684 * create an object that is equivalent to the public key.
685 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000686 * This specification supports a single format for each key type.
687 * Implementations may support other formats as long as the standard
688 * format is supported. Implementations that support other formats
689 * should ensure that the formats are clearly unambiguous so as to
690 * minimize the risk that an invalid input is accidentally interpreted
691 * according to a different format.
692 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000693 * For standard key types, the output format is as follows:
694 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
695 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
696 * ```
697 * RSAPublicKey ::= SEQUENCE {
698 * modulus INTEGER, -- n
699 * publicExponent INTEGER } -- e
700 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000701 * - For elliptic curve public keys (key types for which
702 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
703 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
704 * Let `m` be the bit size associated with the curve, i.e. the bit size of
705 * `q` for a curve over `F_q`. The representation consists of:
706 * - The byte 0x04;
707 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
708 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000709 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
710 * representation of the public key `y = g^x mod p` as a big-endian byte
711 * string. The length of the byte string is the length of the base prime `p`
712 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000713 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
714 * the format is the representation of the public key `y = g^x mod p` as a
715 * big-endian byte string. The length of the byte string is the length of the
716 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100717 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100718 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200719 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200720 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200721 * \param[out] data_length On success, the number of bytes
722 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100723 *
Gilles Peskine28538492018-07-11 17:34:00 +0200724 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100725 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200726 * \retval #PSA_ERROR_EMPTY_SLOT
727 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200728 * The key is neither a public key nor a key pair.
729 * \retval #PSA_ERROR_NOT_SUPPORTED
730 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
731 * The size of the \p data buffer is too small. You can determine a
732 * sufficient buffer size by calling
733 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
734 * where \c type is the key type
735 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200736 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
737 * \retval #PSA_ERROR_HARDWARE_FAILURE
738 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300739 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300740 * The library has not been previously initialized by psa_crypto_init().
741 * It is implementation-dependent whether a failure to initialize
742 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100743 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100744psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100745 uint8_t *data,
746 size_t data_size,
747 size_t *data_length);
748
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100749/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100750 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100751 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000752 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100753 * This function is primarily useful to copy a key from one lifetime
754 * to another. The target key retains its lifetime and location.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200755 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100756 * In an implementation where slots have different ownerships,
757 * this functin may be used to share a key with a different party,
758 * subject to implementation-defined restrictions on key sharing.
759 * In this case \p constraint would typically prevent the recipient
760 * from exporting the key.
Gilles Peskine7e198532018-03-08 07:50:30 +0100761 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100762 * The resulting key may only be used in a way that conforms to all
763 * three of: the policy of the source key, the policy previously set
764 * on the target, and the \p constraint parameter passed when calling
765 * this function.
766 * - The usage flags on the resulting key are the bitwise-and of the
767 * usage flags on the source policy, the previously-set target policy
768 * and the policy constraint.
769 * - If all three policies allow the same algorithm or wildcard-based
770 * algorithm policy, the resulting key has the same algorithm policy.
771 * - If one of the policies allows an algorithm and all the other policies
772 * either allow the same algorithm or a wildcard-based algorithm policy
773 * that includes this algorithm, the resulting key allows the same
774 * algorithm.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200775 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100776 * The effect of this function on implementation-defined metadata is
777 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200778 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100779 * \param source_handle The key to copy. It must be a handle to an
780 * occupied slot.
781 * \param target_handle A handle to the target slot. It must not contain
782 * key material yet.
783 * \param[in] constraint An optional policy constraint. If this parameter
784 * is non-null then the resulting key will conform
785 * to this policy in addition to the source policy
786 * and the policy already present on the target
787 * slot. If this parameter is null then the
788 * function behaves in the same way as if it was
789 * the target policy, i.e. only the source and
790 * target policies apply.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200791 *
792 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100793 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200794 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100795 * \p target already contains key material.
796 * \retval #PSA_ERROR_EMPTY_SLOT
797 * \p source does not contain key material.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200798 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100799 * The policy constraints on the source, on the target and
800 * \p constraints are incompatible.
801 * \retval #PSA_ERROR_NOT_PERMITTED
802 * The source key is not exportable and its lifetime does not
803 * allow copying it to the target's lifetime.
804 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
805 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200806 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
807 * \retval #PSA_ERROR_HARDWARE_FAILURE
808 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100809 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100810psa_status_t psa_copy_key(psa_key_handle_t source_handle,
811 psa_key_handle_t target_handle,
812 const psa_key_policy_t *constraint);
Gilles Peskine20035e32018-02-03 22:44:14 +0100813
814/**@}*/
815
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100816/** \defgroup hash Message digests
817 * @{
818 */
819
Gilles Peskine69647a42019-01-14 20:18:12 +0100820/** Calculate the hash (digest) of a message.
821 *
822 * \note To verify the hash of a message against an
823 * expected value, use psa_hash_compare() instead.
824 *
825 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
826 * such that #PSA_ALG_IS_HASH(\p alg) is true).
827 * \param[in] input Buffer containing the message to hash.
828 * \param input_length Size of the \p input buffer in bytes.
829 * \param[out] hash Buffer where the hash is to be written.
830 * \param hash_size Size of the \p hash buffer in bytes.
831 * \param[out] hash_length On success, the number of bytes
832 * that make up the hash value. This is always
833 * #PSA_HASH_SIZE(\c alg) where \c alg is the
834 * hash algorithm that is calculated.
835 *
836 * \retval #PSA_SUCCESS
837 * Success.
838 * \retval #PSA_ERROR_NOT_SUPPORTED
839 * \p alg is not supported or is not a hash algorithm.
840 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
841 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
842 * \retval #PSA_ERROR_HARDWARE_FAILURE
843 * \retval #PSA_ERROR_TAMPERING_DETECTED
844 */
845psa_status_t psa_hash_compute(psa_algorithm_t alg,
846 const uint8_t *input,
847 size_t input_length,
848 uint8_t *hash,
849 size_t hash_size,
850 size_t *hash_length);
851
852/** Calculate the hash (digest) of a message and compare it with a
853 * reference value.
854 *
855 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
856 * such that #PSA_ALG_IS_HASH(\p alg) is true).
857 * \param[in] input Buffer containing the message to hash.
858 * \param input_length Size of the \p input buffer in bytes.
859 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100860 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100861 *
862 * \retval #PSA_SUCCESS
863 * The expected hash is identical to the actual hash of the input.
864 * \retval #PSA_ERROR_INVALID_SIGNATURE
865 * The hash of the message was calculated successfully, but it
866 * differs from the expected hash.
867 * \retval #PSA_ERROR_NOT_SUPPORTED
868 * \p alg is not supported or is not a hash algorithm.
869 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
870 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
871 * \retval #PSA_ERROR_HARDWARE_FAILURE
872 * \retval #PSA_ERROR_TAMPERING_DETECTED
873 */
874psa_status_t psa_hash_compare(psa_algorithm_t alg,
875 const uint8_t *input,
876 size_t input_length,
877 const uint8_t *hash,
878 const size_t hash_length);
879
Gilles Peskine308b91d2018-02-08 09:47:44 +0100880/** The type of the state data structure for multipart hash operations.
881 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000882 * Before calling any function on a hash operation object, the application must
883 * initialize it by any of the following means:
884 * - Set the structure to all-bits-zero, for example:
885 * \code
886 * psa_hash_operation_t operation;
887 * memset(&operation, 0, sizeof(operation));
888 * \endcode
889 * - Initialize the structure to logical zero values, for example:
890 * \code
891 * psa_hash_operation_t operation = {0};
892 * \endcode
893 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
894 * for example:
895 * \code
896 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
897 * \endcode
898 * - Assign the result of the function psa_hash_operation_init()
899 * to the structure, for example:
900 * \code
901 * psa_hash_operation_t operation;
902 * operation = psa_hash_operation_init();
903 * \endcode
904 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100905 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100906 * make any assumptions about the content of this structure except
907 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100908typedef struct psa_hash_operation_s psa_hash_operation_t;
909
Jaeden Amero6a25b412019-01-04 11:47:44 +0000910/** \def PSA_HASH_OPERATION_INIT
911 *
912 * This macro returns a suitable initializer for a hash operation object
913 * of type #psa_hash_operation_t.
914 */
915#ifdef __DOXYGEN_ONLY__
916/* This is an example definition for documentation purposes.
917 * Implementations should define a suitable value in `crypto_struct.h`.
918 */
919#define PSA_HASH_OPERATION_INIT {0}
920#endif
921
922/** Return an initial value for a hash operation object.
923 */
924static psa_hash_operation_t psa_hash_operation_init(void);
925
Gilles Peskinef45adda2019-01-14 18:29:18 +0100926/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100927 *
928 * The sequence of operations to calculate a hash (message digest)
929 * is as follows:
930 * -# Allocate an operation object which will be passed to all the functions
931 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000932 * -# Initialize the operation object with one of the methods described in the
933 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200934 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100935 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100936 * of the message each time. The hash that is calculated is the hash
937 * of the concatenation of these messages in order.
938 * -# To calculate the hash, call psa_hash_finish().
939 * To compare the hash with an expected value, call psa_hash_verify().
940 *
941 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000942 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100943 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200944 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100945 * eventually terminate the operation. The following events terminate an
946 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100947 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100948 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100949 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000950 * \param[in,out] operation The operation object to set up. It must have
951 * been initialized as per the documentation for
952 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200953 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
954 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100955 *
Gilles Peskine28538492018-07-11 17:34:00 +0200956 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100957 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200958 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200959 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200960 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
961 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
962 * \retval #PSA_ERROR_HARDWARE_FAILURE
963 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100964 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200965psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100966 psa_algorithm_t alg);
967
Gilles Peskine308b91d2018-02-08 09:47:44 +0100968/** Add a message fragment to a multipart hash operation.
969 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200970 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100971 *
972 * If this function returns an error status, the operation becomes inactive.
973 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200974 * \param[in,out] operation Active hash operation.
975 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200976 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100977 *
Gilles Peskine28538492018-07-11 17:34:00 +0200978 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100979 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200980 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100981 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200982 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
983 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
984 * \retval #PSA_ERROR_HARDWARE_FAILURE
985 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100986 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100987psa_status_t psa_hash_update(psa_hash_operation_t *operation,
988 const uint8_t *input,
989 size_t input_length);
990
Gilles Peskine308b91d2018-02-08 09:47:44 +0100991/** Finish the calculation of the hash of a message.
992 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200993 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100994 * This function calculates the hash of the message formed by concatenating
995 * the inputs passed to preceding calls to psa_hash_update().
996 *
997 * When this function returns, the operation becomes inactive.
998 *
999 * \warning Applications should not call this function if they expect
1000 * a specific value for the hash. Call psa_hash_verify() instead.
1001 * Beware that comparing integrity or authenticity data such as
1002 * hash values with a function such as \c memcmp is risky
1003 * because the time taken by the comparison may leak information
1004 * about the hashed data which could allow an attacker to guess
1005 * a valid hash and thereby bypass security controls.
1006 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001007 * \param[in,out] operation Active hash operation.
1008 * \param[out] hash Buffer where the hash is to be written.
1009 * \param hash_size Size of the \p hash buffer in bytes.
1010 * \param[out] hash_length On success, the number of bytes
1011 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001012 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001013 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001014 *
Gilles Peskine28538492018-07-11 17:34:00 +02001015 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001016 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001017 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001018 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001019 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001020 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001021 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001022 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001023 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1024 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1025 * \retval #PSA_ERROR_HARDWARE_FAILURE
1026 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001027 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001028psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1029 uint8_t *hash,
1030 size_t hash_size,
1031 size_t *hash_length);
1032
Gilles Peskine308b91d2018-02-08 09:47:44 +01001033/** Finish the calculation of the hash of a message and compare it with
1034 * an expected value.
1035 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001036 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001037 * This function calculates the hash of the message formed by concatenating
1038 * the inputs passed to preceding calls to psa_hash_update(). It then
1039 * compares the calculated hash with the expected hash passed as a
1040 * parameter to this function.
1041 *
1042 * When this function returns, the operation becomes inactive.
1043 *
Gilles Peskine19067982018-03-20 17:54:53 +01001044 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001045 * comparison between the actual hash and the expected hash is performed
1046 * in constant time.
1047 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001048 * \param[in,out] operation Active hash operation.
1049 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001050 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001051 *
Gilles Peskine28538492018-07-11 17:34:00 +02001052 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001053 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001054 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001055 * The hash of the message was calculated successfully, but it
1056 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001057 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001058 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001059 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1060 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1061 * \retval #PSA_ERROR_HARDWARE_FAILURE
1062 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001063 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001064psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1065 const uint8_t *hash,
1066 size_t hash_length);
1067
Gilles Peskine308b91d2018-02-08 09:47:44 +01001068/** Abort a hash operation.
1069 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001070 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001071 * \p operation structure itself. Once aborted, the operation object
1072 * can be reused for another operation by calling
1073 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001075 * You may call this function any time after the operation object has
1076 * been initialized by any of the following methods:
1077 * - A call to psa_hash_setup(), whether it succeeds or not.
1078 * - Initializing the \c struct to all-bits-zero.
1079 * - Initializing the \c struct to logical zeros, e.g.
1080 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001081 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001082 * In particular, calling psa_hash_abort() after the operation has been
1083 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1084 * psa_hash_verify() is safe and has no effect.
1085 *
1086 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001087 *
Gilles Peskine28538492018-07-11 17:34:00 +02001088 * \retval #PSA_SUCCESS
1089 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001090 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001091 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1092 * \retval #PSA_ERROR_HARDWARE_FAILURE
1093 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 */
1095psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001096
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001097/** Clone a hash operation.
1098 *
1099 * \param[in] source_operation The active hash operation to clone.
1100 * \param[in,out] target_operation The operation object to set up.
1101 * It must be initialized but not active.
1102 *
1103 * \retval #PSA_SUCCESS
1104 * \retval #PSA_ERROR_BAD_STATE
1105 * \p source_operation is not an active hash operation.
1106 * \retval #PSA_ERROR_BAD_STATE
1107 * \p source_operation is active.
1108 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1109 * \retval #PSA_ERROR_HARDWARE_FAILURE
1110 * \retval #PSA_ERROR_TAMPERING_DETECTED
1111 */
1112psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1113 psa_hash_operation_t *target_operation);
1114
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001115/**@}*/
1116
Gilles Peskine8c9def32018-02-08 10:02:12 +01001117/** \defgroup MAC Message authentication codes
1118 * @{
1119 */
1120
Gilles Peskine69647a42019-01-14 20:18:12 +01001121/** Calculate the MAC (message authentication code) of a message.
1122 *
1123 * \note To verify the MAC of a message against an
1124 * expected value, use psa_mac_verify() instead.
1125 * Beware that comparing integrity or authenticity data such as
1126 * MAC values with a function such as \c memcmp is risky
1127 * because the time taken by the comparison may leak information
1128 * about the MAC value which could allow an attacker to guess
1129 * a valid MAC and thereby bypass security controls.
1130 *
1131 * \param handle Handle to the key to use for the operation.
1132 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1133 * such that #PSA_ALG_IS_MAC(alg) is true).
1134 * \param[in] input Buffer containing the input message.
1135 * \param input_length Size of the \p input buffer in bytes.
1136 * \param[out] mac Buffer where the MAC value is to be written.
1137 * \param mac_size Size of the \p mac buffer in bytes.
1138 * \param[out] mac_length On success, the number of bytes
1139 * that make up the mac value. This is always
1140 * #PSA_HASH_SIZE(\c alg) where \c alg is the
1141 * hash algorithm that is calculated.
1142 *
1143 * \retval #PSA_SUCCESS
1144 * Success.
1145 * \retval #PSA_ERROR_INVALID_HANDLE
1146 * \retval #PSA_ERROR_EMPTY_SLOT
1147 * \retval #PSA_ERROR_NOT_PERMITTED
1148 * \retval #PSA_ERROR_INVALID_ARGUMENT
1149 * \p key is not compatible with \p alg.
1150 * \retval #PSA_ERROR_NOT_SUPPORTED
1151 * \p alg is not supported or is not a MAC algorithm.
1152 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1153 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1154 * \retval #PSA_ERROR_HARDWARE_FAILURE
1155 * \retval #PSA_ERROR_TAMPERING_DETECTED
1156 * \retval #PSA_ERROR_BAD_STATE
1157 * The library has not been previously initialized by psa_crypto_init().
1158 * It is implementation-dependent whether a failure to initialize
1159 * results in this error code.
1160 */
1161psa_status_t psa_mac_compute(psa_key_handle_t handle,
1162 psa_algorithm_t alg,
1163 const uint8_t *input,
1164 size_t input_length,
1165 uint8_t *mac,
1166 size_t mac_size,
1167 size_t *mac_length);
1168
1169/** Calculate the MAC of a message and compare it with a reference value.
1170 *
1171 * \param handle Handle to the key to use for the operation.
1172 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1173 * such that #PSA_ALG_IS_MAC(alg) is true).
1174 * \param[in] input Buffer containing the input message.
1175 * \param input_length Size of the \p input buffer in bytes.
1176 * \param[out] mac Buffer containing the expected MAC value.
1177 * \param mac_length Size of the \p mac buffer in bytes.
1178 *
1179 * \retval #PSA_SUCCESS
1180 * The expected MAC is identical to the actual MAC of the input.
1181 * \retval #PSA_ERROR_INVALID_SIGNATURE
1182 * The MAC of the message was calculated successfully, but it
1183 * differs from the expected value.
1184 * \retval #PSA_ERROR_INVALID_HANDLE
1185 * \retval #PSA_ERROR_EMPTY_SLOT
1186 * \retval #PSA_ERROR_NOT_PERMITTED
1187 * \retval #PSA_ERROR_INVALID_ARGUMENT
1188 * \p key is not compatible with \p alg.
1189 * \retval #PSA_ERROR_NOT_SUPPORTED
1190 * \p alg is not supported or is not a MAC algorithm.
1191 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1192 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1193 * \retval #PSA_ERROR_HARDWARE_FAILURE
1194 * \retval #PSA_ERROR_TAMPERING_DETECTED
1195 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001196psa_status_t psa_mac_verify(psa_key_handle_t handle,
1197 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001198 const uint8_t *input,
1199 size_t input_length,
1200 const uint8_t *mac,
1201 const size_t mac_length);
1202
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001203/** The type of the state data structure for multipart MAC operations.
1204 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001205 * Before calling any function on a MAC operation object, the application must
1206 * initialize it by any of the following means:
1207 * - Set the structure to all-bits-zero, for example:
1208 * \code
1209 * psa_mac_operation_t operation;
1210 * memset(&operation, 0, sizeof(operation));
1211 * \endcode
1212 * - Initialize the structure to logical zero values, for example:
1213 * \code
1214 * psa_mac_operation_t operation = {0};
1215 * \endcode
1216 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1217 * for example:
1218 * \code
1219 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1220 * \endcode
1221 * - Assign the result of the function psa_mac_operation_init()
1222 * to the structure, for example:
1223 * \code
1224 * psa_mac_operation_t operation;
1225 * operation = psa_mac_operation_init();
1226 * \endcode
1227 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001228 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001229 * make any assumptions about the content of this structure except
1230 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001231typedef struct psa_mac_operation_s psa_mac_operation_t;
1232
Jaeden Amero769ce272019-01-04 11:48:03 +00001233/** \def PSA_MAC_OPERATION_INIT
1234 *
1235 * This macro returns a suitable initializer for a MAC operation object of type
1236 * #psa_mac_operation_t.
1237 */
1238#ifdef __DOXYGEN_ONLY__
1239/* This is an example definition for documentation purposes.
1240 * Implementations should define a suitable value in `crypto_struct.h`.
1241 */
1242#define PSA_MAC_OPERATION_INIT {0}
1243#endif
1244
1245/** Return an initial value for a MAC operation object.
1246 */
1247static psa_mac_operation_t psa_mac_operation_init(void);
1248
Gilles Peskinef45adda2019-01-14 18:29:18 +01001249/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001250 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001251 * This function sets up the calculation of the MAC
1252 * (message authentication code) of a byte string.
1253 * To verify the MAC of a message against an
1254 * expected value, use psa_mac_verify_setup() instead.
1255 *
1256 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001257 * -# Allocate an operation object which will be passed to all the functions
1258 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001259 * -# Initialize the operation object with one of the methods described in the
1260 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001261 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001262 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1263 * of the message each time. The MAC that is calculated is the MAC
1264 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001265 * -# At the end of the message, call psa_mac_sign_finish() to finish
1266 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001267 *
1268 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001269 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001270 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001271 * After a successful call to psa_mac_sign_setup(), the application must
1272 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001273 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001274 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001275 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001276 * \param[in,out] operation The operation object to set up. It must have
1277 * been initialized as per the documentation for
1278 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001279 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001280 * It must remain valid until the operation
1281 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001282 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1283 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001284 *
Gilles Peskine28538492018-07-11 17:34:00 +02001285 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001286 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001287 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001288 * \retval #PSA_ERROR_EMPTY_SLOT
1289 * \retval #PSA_ERROR_NOT_PERMITTED
1290 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001291 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001292 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001293 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001294 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1295 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1296 * \retval #PSA_ERROR_HARDWARE_FAILURE
1297 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001298 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001299 * The library has not been previously initialized by psa_crypto_init().
1300 * It is implementation-dependent whether a failure to initialize
1301 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001302 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001303psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001304 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001305 psa_algorithm_t alg);
1306
Gilles Peskinef45adda2019-01-14 18:29:18 +01001307/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001308 *
1309 * This function sets up the verification of the MAC
1310 * (message authentication code) of a byte string against an expected value.
1311 *
1312 * The sequence of operations to verify a MAC is as follows:
1313 * -# Allocate an operation object which will be passed to all the functions
1314 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001315 * -# Initialize the operation object with one of the methods described in the
1316 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001317 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001318 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1319 * of the message each time. The MAC that is calculated is the MAC
1320 * of the concatenation of these messages in order.
1321 * -# At the end of the message, call psa_mac_verify_finish() to finish
1322 * calculating the actual MAC of the message and verify it against
1323 * the expected value.
1324 *
1325 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001326 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001327 *
1328 * After a successful call to psa_mac_verify_setup(), the application must
1329 * eventually terminate the operation through one of the following methods:
1330 * - A failed call to psa_mac_update().
1331 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1332 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001333 * \param[in,out] operation The operation object to set up. It must have
1334 * been initialized as per the documentation for
1335 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001336 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001337 * It must remain valid until the operation
1338 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001339 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1340 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001341 *
Gilles Peskine28538492018-07-11 17:34:00 +02001342 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001343 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001344 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001345 * \retval #PSA_ERROR_EMPTY_SLOT
1346 * \retval #PSA_ERROR_NOT_PERMITTED
1347 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001348 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001349 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001350 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001351 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1352 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1353 * \retval #PSA_ERROR_HARDWARE_FAILURE
1354 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001355 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001356 * The library has not been previously initialized by psa_crypto_init().
1357 * It is implementation-dependent whether a failure to initialize
1358 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001359 */
1360psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001361 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001362 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001363
Gilles Peskinedcd14942018-07-12 00:30:52 +02001364/** Add a message fragment to a multipart MAC operation.
1365 *
1366 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1367 * before calling this function.
1368 *
1369 * If this function returns an error status, the operation becomes inactive.
1370 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001371 * \param[in,out] operation Active MAC operation.
1372 * \param[in] input Buffer containing the message fragment to add to
1373 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001374 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001375 *
1376 * \retval #PSA_SUCCESS
1377 * Success.
1378 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001379 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001380 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1381 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1382 * \retval #PSA_ERROR_HARDWARE_FAILURE
1383 * \retval #PSA_ERROR_TAMPERING_DETECTED
1384 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001385psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1386 const uint8_t *input,
1387 size_t input_length);
1388
Gilles Peskinedcd14942018-07-12 00:30:52 +02001389/** Finish the calculation of the MAC of a message.
1390 *
1391 * The application must call psa_mac_sign_setup() before calling this function.
1392 * This function calculates the MAC of the message formed by concatenating
1393 * the inputs passed to preceding calls to psa_mac_update().
1394 *
1395 * When this function returns, the operation becomes inactive.
1396 *
1397 * \warning Applications should not call this function if they expect
1398 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1399 * Beware that comparing integrity or authenticity data such as
1400 * MAC values with a function such as \c memcmp is risky
1401 * because the time taken by the comparison may leak information
1402 * about the MAC value which could allow an attacker to guess
1403 * a valid MAC and thereby bypass security controls.
1404 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001405 * \param[in,out] operation Active MAC operation.
1406 * \param[out] mac Buffer where the MAC value is to be written.
1407 * \param mac_size Size of the \p mac buffer in bytes.
1408 * \param[out] mac_length On success, the number of bytes
1409 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001410 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001411 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001412 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001413 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001414 *
1415 * \retval #PSA_SUCCESS
1416 * Success.
1417 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001418 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001419 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001420 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001421 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1422 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1423 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1424 * \retval #PSA_ERROR_HARDWARE_FAILURE
1425 * \retval #PSA_ERROR_TAMPERING_DETECTED
1426 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001427psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1428 uint8_t *mac,
1429 size_t mac_size,
1430 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001431
Gilles Peskinedcd14942018-07-12 00:30:52 +02001432/** Finish the calculation of the MAC of a message and compare it with
1433 * an expected value.
1434 *
1435 * The application must call psa_mac_verify_setup() before calling this function.
1436 * This function calculates the MAC of the message formed by concatenating
1437 * the inputs passed to preceding calls to psa_mac_update(). It then
1438 * compares the calculated MAC with the expected MAC passed as a
1439 * parameter to this function.
1440 *
1441 * When this function returns, the operation becomes inactive.
1442 *
1443 * \note Implementations shall make the best effort to ensure that the
1444 * comparison between the actual MAC and the expected MAC is performed
1445 * in constant time.
1446 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001447 * \param[in,out] operation Active MAC operation.
1448 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001449 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001450 *
1451 * \retval #PSA_SUCCESS
1452 * The expected MAC is identical to the actual MAC of the message.
1453 * \retval #PSA_ERROR_INVALID_SIGNATURE
1454 * The MAC of the message was calculated successfully, but it
1455 * differs from the expected MAC.
1456 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001457 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001458 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1459 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1460 * \retval #PSA_ERROR_HARDWARE_FAILURE
1461 * \retval #PSA_ERROR_TAMPERING_DETECTED
1462 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001463psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1464 const uint8_t *mac,
1465 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001466
Gilles Peskinedcd14942018-07-12 00:30:52 +02001467/** Abort a MAC operation.
1468 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001469 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001470 * \p operation structure itself. Once aborted, the operation object
1471 * can be reused for another operation by calling
1472 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001473 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001474 * You may call this function any time after the operation object has
1475 * been initialized by any of the following methods:
1476 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1477 * it succeeds or not.
1478 * - Initializing the \c struct to all-bits-zero.
1479 * - Initializing the \c struct to logical zeros, e.g.
1480 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001481 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001482 * In particular, calling psa_mac_abort() after the operation has been
1483 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1484 * psa_mac_verify_finish() is safe and has no effect.
1485 *
1486 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001487 *
1488 * \retval #PSA_SUCCESS
1489 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001490 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001491 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1492 * \retval #PSA_ERROR_HARDWARE_FAILURE
1493 * \retval #PSA_ERROR_TAMPERING_DETECTED
1494 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001495psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1496
1497/**@}*/
1498
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001499/** \defgroup cipher Symmetric ciphers
1500 * @{
1501 */
1502
Gilles Peskine69647a42019-01-14 20:18:12 +01001503/** Encrypt a message using a symmetric cipher.
1504 *
1505 * This function encrypts a message with a random IV (initialization
1506 * vector).
1507 *
1508 * \param handle Handle to the key to use for the operation.
1509 * It must remain valid until the operation
1510 * terminates.
1511 * \param alg The cipher algorithm to compute
1512 * (\c PSA_ALG_XXX value such that
1513 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1514 * \param[in] input Buffer containing the message to encrypt.
1515 * \param input_length Size of the \p input buffer in bytes.
1516 * \param[out] output Buffer where the output is to be written.
1517 * The output contains the IV followed by
1518 * the ciphertext proper.
1519 * \param output_size Size of the \p output buffer in bytes.
1520 * \param[out] output_length On success, the number of bytes
1521 * that make up the output.
1522 *
1523 * \retval #PSA_SUCCESS
1524 * Success.
1525 * \retval #PSA_ERROR_INVALID_HANDLE
1526 * \retval #PSA_ERROR_EMPTY_SLOT
1527 * \retval #PSA_ERROR_NOT_PERMITTED
1528 * \retval #PSA_ERROR_INVALID_ARGUMENT
1529 * \p key is not compatible with \p alg.
1530 * \retval #PSA_ERROR_NOT_SUPPORTED
1531 * \p alg is not supported or is not a cipher algorithm.
1532 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1533 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1534 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1535 * \retval #PSA_ERROR_HARDWARE_FAILURE
1536 * \retval #PSA_ERROR_TAMPERING_DETECTED
1537 */
1538psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1539 psa_algorithm_t alg,
1540 const uint8_t *input,
1541 size_t input_length,
1542 uint8_t *output,
1543 size_t output_size,
1544 size_t *output_length);
1545
1546/** Decrypt a message using a symmetric cipher.
1547 *
1548 * This function decrypts a message encrypted with a symmetric cipher.
1549 *
1550 * \param handle Handle to the key to use for the operation.
1551 * It must remain valid until the operation
1552 * terminates.
1553 * \param alg The cipher algorithm to compute
1554 * (\c PSA_ALG_XXX value such that
1555 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1556 * \param[in] input Buffer containing the message to decrypt.
1557 * This consists of the IV followed by the
1558 * ciphertext proper.
1559 * \param input_length Size of the \p input buffer in bytes.
1560 * \param[out] output Buffer where the plaintext is to be written.
1561 * \param output_size Size of the \p output buffer in bytes.
1562 * \param[out] output_length On success, the number of bytes
1563 * that make up the output.
1564 *
1565 * \retval #PSA_SUCCESS
1566 * Success.
1567 * \retval #PSA_ERROR_INVALID_HANDLE
1568 * \retval #PSA_ERROR_EMPTY_SLOT
1569 * \retval #PSA_ERROR_NOT_PERMITTED
1570 * \retval #PSA_ERROR_INVALID_ARGUMENT
1571 * \p key is not compatible with \p alg.
1572 * \retval #PSA_ERROR_NOT_SUPPORTED
1573 * \p alg is not supported or is not a cipher algorithm.
1574 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1575 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1576 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1577 * \retval #PSA_ERROR_HARDWARE_FAILURE
1578 * \retval #PSA_ERROR_TAMPERING_DETECTED
1579 */
1580psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1581 psa_algorithm_t alg,
1582 const uint8_t *input,
1583 size_t input_length,
1584 uint8_t *output,
1585 size_t output_size,
1586 size_t *output_length);
1587
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001588/** The type of the state data structure for multipart cipher operations.
1589 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001590 * Before calling any function on a cipher operation object, the application
1591 * must initialize it by any of the following means:
1592 * - Set the structure to all-bits-zero, for example:
1593 * \code
1594 * psa_cipher_operation_t operation;
1595 * memset(&operation, 0, sizeof(operation));
1596 * \endcode
1597 * - Initialize the structure to logical zero values, for example:
1598 * \code
1599 * psa_cipher_operation_t operation = {0};
1600 * \endcode
1601 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1602 * for example:
1603 * \code
1604 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1605 * \endcode
1606 * - Assign the result of the function psa_cipher_operation_init()
1607 * to the structure, for example:
1608 * \code
1609 * psa_cipher_operation_t operation;
1610 * operation = psa_cipher_operation_init();
1611 * \endcode
1612 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001613 * This is an implementation-defined \c struct. Applications should not
1614 * make any assumptions about the content of this structure except
1615 * as directed by the documentation of a specific implementation. */
1616typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1617
Jaeden Amero5bae2272019-01-04 11:48:27 +00001618/** \def PSA_CIPHER_OPERATION_INIT
1619 *
1620 * This macro returns a suitable initializer for a cipher operation object of
1621 * type #psa_cipher_operation_t.
1622 */
1623#ifdef __DOXYGEN_ONLY__
1624/* This is an example definition for documentation purposes.
1625 * Implementations should define a suitable value in `crypto_struct.h`.
1626 */
1627#define PSA_CIPHER_OPERATION_INIT {0}
1628#endif
1629
1630/** Return an initial value for a cipher operation object.
1631 */
1632static psa_cipher_operation_t psa_cipher_operation_init(void);
1633
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001634/** Set the key for a multipart symmetric encryption operation.
1635 *
1636 * The sequence of operations to encrypt a message with a symmetric cipher
1637 * is as follows:
1638 * -# Allocate an operation object which will be passed to all the functions
1639 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001640 * -# Initialize the operation object with one of the methods described in the
1641 * documentation for #psa_cipher_operation_t, e.g.
1642 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001643 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001644 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001645 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001646 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001647 * requires a specific IV value.
1648 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1649 * of the message each time.
1650 * -# Call psa_cipher_finish().
1651 *
1652 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001653 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001654 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001655 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001656 * eventually terminate the operation. The following events terminate an
1657 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001658 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001659 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001660 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001661 * \param[in,out] operation The operation object to set up. It must have
1662 * been initialized as per the documentation for
1663 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001664 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001665 * It must remain valid until the operation
1666 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001667 * \param alg The cipher algorithm to compute
1668 * (\c PSA_ALG_XXX value such that
1669 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001670 *
Gilles Peskine28538492018-07-11 17:34:00 +02001671 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001672 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001673 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001674 * \retval #PSA_ERROR_EMPTY_SLOT
1675 * \retval #PSA_ERROR_NOT_PERMITTED
1676 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001677 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001678 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001679 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001680 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1681 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1682 * \retval #PSA_ERROR_HARDWARE_FAILURE
1683 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001684 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001685 * The library has not been previously initialized by psa_crypto_init().
1686 * It is implementation-dependent whether a failure to initialize
1687 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001688 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001689psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001690 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001691 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001692
1693/** Set the key for a multipart symmetric decryption operation.
1694 *
1695 * The sequence of operations to decrypt a message with a symmetric cipher
1696 * is as follows:
1697 * -# Allocate an operation object which will be passed to all the functions
1698 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001699 * -# Initialize the operation object with one of the methods described in the
1700 * documentation for #psa_cipher_operation_t, e.g.
1701 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001702 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001703 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001704 * decryption. If the IV is prepended to the ciphertext, you can call
1705 * psa_cipher_update() on a buffer containing the IV followed by the
1706 * beginning of the message.
1707 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1708 * of the message each time.
1709 * -# Call psa_cipher_finish().
1710 *
1711 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001712 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001713 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001714 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001715 * eventually terminate the operation. The following events terminate an
1716 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001717 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001718 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001719 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001720 * \param[in,out] operation The operation object to set up. It must have
1721 * been initialized as per the documentation for
1722 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001723 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001724 * It must remain valid until the operation
1725 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001726 * \param alg The cipher algorithm to compute
1727 * (\c PSA_ALG_XXX value such that
1728 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001729 *
Gilles Peskine28538492018-07-11 17:34:00 +02001730 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001731 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001732 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001733 * \retval #PSA_ERROR_EMPTY_SLOT
1734 * \retval #PSA_ERROR_NOT_PERMITTED
1735 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001736 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001737 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001738 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001739 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1740 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1741 * \retval #PSA_ERROR_HARDWARE_FAILURE
1742 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001743 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001744 * The library has not been previously initialized by psa_crypto_init().
1745 * It is implementation-dependent whether a failure to initialize
1746 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001747 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001748psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001749 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001750 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001751
Gilles Peskinedcd14942018-07-12 00:30:52 +02001752/** Generate an IV for a symmetric encryption operation.
1753 *
1754 * This function generates a random IV (initialization vector), nonce
1755 * or initial counter value for the encryption operation as appropriate
1756 * for the chosen algorithm, key type and key size.
1757 *
1758 * The application must call psa_cipher_encrypt_setup() before
1759 * calling this function.
1760 *
1761 * If this function returns an error status, the operation becomes inactive.
1762 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001763 * \param[in,out] operation Active cipher operation.
1764 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001765 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001766 * \param[out] iv_length On success, the number of bytes of the
1767 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001768 *
1769 * \retval #PSA_SUCCESS
1770 * Success.
1771 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001772 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001773 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001774 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001775 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1776 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1777 * \retval #PSA_ERROR_HARDWARE_FAILURE
1778 * \retval #PSA_ERROR_TAMPERING_DETECTED
1779 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001780psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1781 unsigned char *iv,
1782 size_t iv_size,
1783 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001784
Gilles Peskinedcd14942018-07-12 00:30:52 +02001785/** Set the IV for a symmetric encryption or decryption operation.
1786 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001787 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001788 * or initial counter value for the encryption or decryption operation.
1789 *
1790 * The application must call psa_cipher_encrypt_setup() before
1791 * calling this function.
1792 *
1793 * If this function returns an error status, the operation becomes inactive.
1794 *
1795 * \note When encrypting, applications should use psa_cipher_generate_iv()
1796 * instead of this function, unless implementing a protocol that requires
1797 * a non-random IV.
1798 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001799 * \param[in,out] operation Active cipher operation.
1800 * \param[in] iv Buffer containing the IV to use.
1801 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001802 *
1803 * \retval #PSA_SUCCESS
1804 * Success.
1805 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001806 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001807 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001808 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001809 * or the chosen algorithm does not use an IV.
1810 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1811 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1812 * \retval #PSA_ERROR_HARDWARE_FAILURE
1813 * \retval #PSA_ERROR_TAMPERING_DETECTED
1814 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001815psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1816 const unsigned char *iv,
1817 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001818
Gilles Peskinedcd14942018-07-12 00:30:52 +02001819/** Encrypt or decrypt a message fragment in an active cipher operation.
1820 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001821 * Before calling this function, you must:
1822 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1823 * The choice of setup function determines whether this function
1824 * encrypts or decrypts its input.
1825 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1826 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001827 *
1828 * If this function returns an error status, the operation becomes inactive.
1829 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001830 * \param[in,out] operation Active cipher operation.
1831 * \param[in] input Buffer containing the message fragment to
1832 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001833 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001834 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001835 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001836 * \param[out] output_length On success, the number of bytes
1837 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001838 *
1839 * \retval #PSA_SUCCESS
1840 * Success.
1841 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001842 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001843 * not set, or already completed).
1844 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1845 * The size of the \p output buffer is too small.
1846 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1847 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1848 * \retval #PSA_ERROR_HARDWARE_FAILURE
1849 * \retval #PSA_ERROR_TAMPERING_DETECTED
1850 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001851psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1852 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001853 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001854 unsigned char *output,
1855 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001856 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001857
Gilles Peskinedcd14942018-07-12 00:30:52 +02001858/** Finish encrypting or decrypting a message in a cipher operation.
1859 *
1860 * The application must call psa_cipher_encrypt_setup() or
1861 * psa_cipher_decrypt_setup() before calling this function. The choice
1862 * of setup function determines whether this function encrypts or
1863 * decrypts its input.
1864 *
1865 * This function finishes the encryption or decryption of the message
1866 * formed by concatenating the inputs passed to preceding calls to
1867 * psa_cipher_update().
1868 *
1869 * When this function returns, the operation becomes inactive.
1870 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001871 * \param[in,out] operation Active cipher operation.
1872 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001873 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001874 * \param[out] output_length On success, the number of bytes
1875 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001876 *
1877 * \retval #PSA_SUCCESS
1878 * Success.
1879 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001880 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001881 * not set, or already completed).
1882 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1883 * The size of the \p output buffer is too small.
1884 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1885 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1886 * \retval #PSA_ERROR_HARDWARE_FAILURE
1887 * \retval #PSA_ERROR_TAMPERING_DETECTED
1888 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001889psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001890 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001891 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001892 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001893
Gilles Peskinedcd14942018-07-12 00:30:52 +02001894/** Abort a cipher operation.
1895 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001896 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001897 * \p operation structure itself. Once aborted, the operation object
1898 * can be reused for another operation by calling
1899 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001900 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001901 * You may call this function any time after the operation object has
1902 * been initialized by any of the following methods:
1903 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1904 * whether it succeeds or not.
1905 * - Initializing the \c struct to all-bits-zero.
1906 * - Initializing the \c struct to logical zeros, e.g.
1907 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001908 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001909 * In particular, calling psa_cipher_abort() after the operation has been
1910 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1911 * is safe and has no effect.
1912 *
1913 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001914 *
1915 * \retval #PSA_SUCCESS
1916 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001917 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001918 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1919 * \retval #PSA_ERROR_HARDWARE_FAILURE
1920 * \retval #PSA_ERROR_TAMPERING_DETECTED
1921 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001922psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1923
1924/**@}*/
1925
Gilles Peskine3b555712018-03-03 21:27:57 +01001926/** \defgroup aead Authenticated encryption with associated data (AEAD)
1927 * @{
1928 */
1929
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001930/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001931 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001932 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001933 * \param alg The AEAD algorithm to compute
1934 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001935 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001936 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001937 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001938 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001939 * but not encrypted.
1940 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001941 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001942 * encrypted.
1943 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001944 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001945 * encrypted data. The additional data is not
1946 * part of this output. For algorithms where the
1947 * encrypted data and the authentication tag
1948 * are defined as separate outputs, the
1949 * authentication tag is appended to the
1950 * encrypted data.
1951 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1952 * This must be at least
1953 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1954 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001955 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001956 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001957 *
Gilles Peskine28538492018-07-11 17:34:00 +02001958 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001959 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001960 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001961 * \retval #PSA_ERROR_EMPTY_SLOT
1962 * \retval #PSA_ERROR_NOT_PERMITTED
1963 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001964 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001965 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001966 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001967 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1968 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1969 * \retval #PSA_ERROR_HARDWARE_FAILURE
1970 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001971 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001972 * The library has not been previously initialized by psa_crypto_init().
1973 * It is implementation-dependent whether a failure to initialize
1974 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001975 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001976psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001977 psa_algorithm_t alg,
1978 const uint8_t *nonce,
1979 size_t nonce_length,
1980 const uint8_t *additional_data,
1981 size_t additional_data_length,
1982 const uint8_t *plaintext,
1983 size_t plaintext_length,
1984 uint8_t *ciphertext,
1985 size_t ciphertext_size,
1986 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001987
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001988/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001989 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001990 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001991 * \param alg The AEAD algorithm to compute
1992 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001993 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001994 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001995 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001996 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001997 * but not encrypted.
1998 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001999 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002000 * encrypted. For algorithms where the
2001 * encrypted data and the authentication tag
2002 * are defined as separate inputs, the buffer
2003 * must contain the encrypted data followed
2004 * by the authentication tag.
2005 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002006 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002007 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2008 * This must be at least
2009 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2010 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002011 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03002012 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002013 *
Gilles Peskine28538492018-07-11 17:34:00 +02002014 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002015 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002016 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002017 * \retval #PSA_ERROR_EMPTY_SLOT
2018 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002019 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002020 * \retval #PSA_ERROR_NOT_PERMITTED
2021 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002022 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002023 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002024 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002025 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2026 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2027 * \retval #PSA_ERROR_HARDWARE_FAILURE
2028 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002029 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002030 * The library has not been previously initialized by psa_crypto_init().
2031 * It is implementation-dependent whether a failure to initialize
2032 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002033 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002034psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002035 psa_algorithm_t alg,
2036 const uint8_t *nonce,
2037 size_t nonce_length,
2038 const uint8_t *additional_data,
2039 size_t additional_data_length,
2040 const uint8_t *ciphertext,
2041 size_t ciphertext_length,
2042 uint8_t *plaintext,
2043 size_t plaintext_size,
2044 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002045
Gilles Peskine30a9e412019-01-14 18:36:12 +01002046/** The type of the state data structure for multipart AEAD operations.
2047 *
2048 * Before calling any function on an AEAD operation object, the application
2049 * must initialize it by any of the following means:
2050 * - Set the structure to all-bits-zero, for example:
2051 * \code
2052 * psa_aead_operation_t operation;
2053 * memset(&operation, 0, sizeof(operation));
2054 * \endcode
2055 * - Initialize the structure to logical zero values, for example:
2056 * \code
2057 * psa_aead_operation_t operation = {0};
2058 * \endcode
2059 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2060 * for example:
2061 * \code
2062 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2063 * \endcode
2064 * - Assign the result of the function psa_aead_operation_init()
2065 * to the structure, for example:
2066 * \code
2067 * psa_aead_operation_t operation;
2068 * operation = psa_aead_operation_init();
2069 * \endcode
2070 *
2071 * This is an implementation-defined \c struct. Applications should not
2072 * make any assumptions about the content of this structure except
2073 * as directed by the documentation of a specific implementation. */
2074typedef struct psa_aead_operation_s psa_aead_operation_t;
2075
2076/** \def PSA_AEAD_OPERATION_INIT
2077 *
2078 * This macro returns a suitable initializer for an AEAD operation object of
2079 * type #psa_aead_operation_t.
2080 */
2081#ifdef __DOXYGEN_ONLY__
2082/* This is an example definition for documentation purposes.
2083 * Implementations should define a suitable value in `crypto_struct.h`.
2084 */
2085#define PSA_AEAD_OPERATION_INIT {0}
2086#endif
2087
2088/** Return an initial value for an AEAD operation object.
2089 */
2090static psa_aead_operation_t psa_aead_operation_init(void);
2091
2092/** Set the key for a multipart authenticated encryption operation.
2093 *
2094 * The sequence of operations to encrypt a message with authentication
2095 * is as follows:
2096 * -# Allocate an operation object which will be passed to all the functions
2097 * listed here.
2098 * -# Initialize the operation object with one of the methods described in the
2099 * documentation for #psa_aead_operation_t, e.g.
2100 * PSA_AEAD_OPERATION_INIT.
2101 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002102 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2103 * inputs to the subsequent calls to psa_aead_update_ad() and
2104 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2105 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002106 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2107 * generate or set the nonce. You should use
2108 * psa_aead_generate_nonce() unless the protocol you are implementing
2109 * requires a specific nonce value.
2110 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2111 * of the non-encrypted additional authenticated data each time.
2112 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002113 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002114 * -# Call psa_aead_finish().
2115 *
2116 * The application may call psa_aead_abort() at any time after the operation
2117 * has been initialized.
2118 *
2119 * After a successful call to psa_aead_encrypt_setup(), the application must
2120 * eventually terminate the operation. The following events terminate an
2121 * operation:
2122 * - A failed call to any of the \c psa_aead_xxx functions.
2123 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2124 *
2125 * \param[in,out] operation The operation object to set up. It must have
2126 * been initialized as per the documentation for
2127 * #psa_aead_operation_t and not yet in use.
2128 * \param handle Handle to the key to use for the operation.
2129 * It must remain valid until the operation
2130 * terminates.
2131 * \param alg The AEAD algorithm to compute
2132 * (\c PSA_ALG_XXX value such that
2133 * #PSA_ALG_IS_AEAD(\p alg) is true).
2134 *
2135 * \retval #PSA_SUCCESS
2136 * Success.
2137 * \retval #PSA_ERROR_INVALID_HANDLE
2138 * \retval #PSA_ERROR_EMPTY_SLOT
2139 * \retval #PSA_ERROR_NOT_PERMITTED
2140 * \retval #PSA_ERROR_INVALID_ARGUMENT
2141 * \p key is not compatible with \p alg.
2142 * \retval #PSA_ERROR_NOT_SUPPORTED
2143 * \p alg is not supported or is not an AEAD algorithm.
2144 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2145 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2146 * \retval #PSA_ERROR_HARDWARE_FAILURE
2147 * \retval #PSA_ERROR_TAMPERING_DETECTED
2148 * \retval #PSA_ERROR_BAD_STATE
2149 * The library has not been previously initialized by psa_crypto_init().
2150 * It is implementation-dependent whether a failure to initialize
2151 * results in this error code.
2152 */
2153psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2154 psa_key_handle_t handle,
2155 psa_algorithm_t alg);
2156
2157/** Set the key for a multipart authenticated decryption operation.
2158 *
2159 * The sequence of operations to decrypt a message with authentication
2160 * is as follows:
2161 * -# Allocate an operation object which will be passed to all the functions
2162 * listed here.
2163 * -# Initialize the operation object with one of the methods described in the
2164 * documentation for #psa_aead_operation_t, e.g.
2165 * PSA_AEAD_OPERATION_INIT.
2166 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002167 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2168 * inputs to the subsequent calls to psa_aead_update_ad() and
2169 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2170 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002171 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2172 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2173 * of the non-encrypted additional authenticated data each time.
2174 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002175 * of the ciphertext to decrypt each time.
2176 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002177 *
2178 * The application may call psa_aead_abort() at any time after the operation
2179 * has been initialized.
2180 *
2181 * After a successful call to psa_aead_decrypt_setup(), the application must
2182 * eventually terminate the operation. The following events terminate an
2183 * operation:
2184 * - A failed call to any of the \c psa_aead_xxx functions.
2185 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2186 *
2187 * \param[in,out] operation The operation object to set up. It must have
2188 * been initialized as per the documentation for
2189 * #psa_aead_operation_t and not yet in use.
2190 * \param handle Handle to the key to use for the operation.
2191 * It must remain valid until the operation
2192 * terminates.
2193 * \param alg The AEAD algorithm to compute
2194 * (\c PSA_ALG_XXX value such that
2195 * #PSA_ALG_IS_AEAD(\p alg) is true).
2196 *
2197 * \retval #PSA_SUCCESS
2198 * Success.
2199 * \retval #PSA_ERROR_INVALID_HANDLE
2200 * \retval #PSA_ERROR_EMPTY_SLOT
2201 * \retval #PSA_ERROR_NOT_PERMITTED
2202 * \retval #PSA_ERROR_INVALID_ARGUMENT
2203 * \p key is not compatible with \p alg.
2204 * \retval #PSA_ERROR_NOT_SUPPORTED
2205 * \p alg is not supported or is not an AEAD algorithm.
2206 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2207 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2208 * \retval #PSA_ERROR_HARDWARE_FAILURE
2209 * \retval #PSA_ERROR_TAMPERING_DETECTED
2210 * \retval #PSA_ERROR_BAD_STATE
2211 * The library has not been previously initialized by psa_crypto_init().
2212 * It is implementation-dependent whether a failure to initialize
2213 * results in this error code.
2214 */
2215psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2216 psa_key_handle_t handle,
2217 psa_algorithm_t alg);
2218
2219/** Generate a random nonce for an authenticated encryption operation.
2220 *
2221 * This function generates a random nonce for the authenticated encryption
2222 * operation with an appropriate size for the chosen algorithm, key type
2223 * and key size.
2224 *
2225 * The application must call psa_aead_encrypt_setup() before
2226 * calling this function.
2227 *
2228 * If this function returns an error status, the operation becomes inactive.
2229 *
2230 * \param[in,out] operation Active AEAD operation.
2231 * \param[out] nonce Buffer where the generated nonce is to be
2232 * written.
2233 * \param nonce_size Size of the \p nonce buffer in bytes.
2234 * \param[out] nonce_length On success, the number of bytes of the
2235 * generated nonce.
2236 *
2237 * \retval #PSA_SUCCESS
2238 * Success.
2239 * \retval #PSA_ERROR_BAD_STATE
2240 * The operation state is not valid (not set up, or nonce already set).
2241 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2242 * The size of the \p nonce buffer is too small.
2243 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2244 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2245 * \retval #PSA_ERROR_HARDWARE_FAILURE
2246 * \retval #PSA_ERROR_TAMPERING_DETECTED
2247 */
2248psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2249 unsigned char *nonce,
2250 size_t nonce_size,
2251 size_t *nonce_length);
2252
2253/** Set the nonce for an authenticated encryption or decryption operation.
2254 *
2255 * This function sets the nonce for the authenticated
2256 * encryption or decryption operation.
2257 *
2258 * The application must call psa_aead_encrypt_setup() before
2259 * calling this function.
2260 *
2261 * If this function returns an error status, the operation becomes inactive.
2262 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002263 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002264 * instead of this function, unless implementing a protocol that requires
2265 * a non-random IV.
2266 *
2267 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002268 * \param[in] nonce Buffer containing the nonce to use.
2269 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002270 *
2271 * \retval #PSA_SUCCESS
2272 * Success.
2273 * \retval #PSA_ERROR_BAD_STATE
2274 * The operation state is not valid (not set up, or nonce already set).
2275 * \retval #PSA_ERROR_INVALID_ARGUMENT
2276 * The size of \p nonce is not acceptable for the chosen algorithm.
2277 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2278 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2279 * \retval #PSA_ERROR_HARDWARE_FAILURE
2280 * \retval #PSA_ERROR_TAMPERING_DETECTED
2281 */
2282psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2283 const unsigned char *nonce,
2284 size_t nonce_length);
2285
Gilles Peskinebc59c852019-01-17 15:26:08 +01002286/** Declare the lengths of the message and additional data for AEAD.
2287 *
2288 * The application must call this function before calling
2289 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2290 * the operation requires it. If the algorithm does not require it,
2291 * calling this function is optional, but if this function is called
2292 * then the implementation must enforce the lengths.
2293 *
2294 * You may call this function before or after setting the nonce with
2295 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2296 *
2297 * - For #PSA_ALG_CCM, calling this function is required.
2298 * - For the other AEAD algorithms defined in this specification, calling
2299 * this function is not required.
2300 * - For vendor-defined algorithm, refer to the vendor documentation.
2301 *
2302 * \param[in,out] operation Active AEAD operation.
2303 * \param ad_length Size of the non-encrypted additional
2304 * authenticated data in bytes.
2305 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2306 *
2307 * \retval #PSA_SUCCESS
2308 * Success.
2309 * \retval #PSA_ERROR_BAD_STATE
2310 * The operation state is not valid (not set up, already completed,
2311 * or psa_aead_update_ad() or psa_aead_update() already called).
2312 * \retval #PSA_ERROR_INVALID_ARGUMENT
2313 * At least one of the lengths is not acceptable for the chosen
2314 * algorithm.
2315 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2316 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2317 * \retval #PSA_ERROR_HARDWARE_FAILURE
2318 * \retval #PSA_ERROR_TAMPERING_DETECTED
2319 */
2320psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2321 size_t ad_length,
2322 size_t plaintext_length);
2323
Gilles Peskine30a9e412019-01-14 18:36:12 +01002324/** Pass additional data to an active AEAD operation.
2325 *
2326 * Additional data is authenticated, but not encrypted.
2327 *
2328 * You may call this function multiple times to pass successive fragments
2329 * of the additional data. You may not call this function after passing
2330 * data to encrypt or decrypt with psa_aead_update().
2331 *
2332 * Before calling this function, you must:
2333 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2334 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2335 *
2336 * If this function returns an error status, the operation becomes inactive.
2337 *
2338 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2339 * there is no guarantee that the input is valid. Therefore, until
2340 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2341 * treat the input as untrusted and prepare to undo any action that
2342 * depends on the input if psa_aead_verify() returns an error status.
2343 *
2344 * \param[in,out] operation Active AEAD operation.
2345 * \param[in] input Buffer containing the fragment of
2346 * additional data.
2347 * \param input_length Size of the \p input buffer in bytes.
2348 *
2349 * \retval #PSA_SUCCESS
2350 * Success.
2351 * \retval #PSA_ERROR_BAD_STATE
2352 * The operation state is not valid (not set up, nonce not set,
2353 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002354 * \retval #PSA_ERROR_INVALID_ARGUMENT
2355 * The total input length overflows the additional data length that
2356 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002357 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2358 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2359 * \retval #PSA_ERROR_HARDWARE_FAILURE
2360 * \retval #PSA_ERROR_TAMPERING_DETECTED
2361 */
2362psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2363 const uint8_t *input,
2364 size_t input_length);
2365
2366/** Encrypt or decrypt a message fragment in an active AEAD operation.
2367 *
2368 * Before calling this function, you must:
2369 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2370 * The choice of setup function determines whether this function
2371 * encrypts or decrypts its input.
2372 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2373 * 3. Call psa_aead_update_ad() to pass all the additional data.
2374 *
2375 * If this function returns an error status, the operation becomes inactive.
2376 *
2377 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2378 * there is no guarantee that the input is valid. Therefore, until
2379 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2380 * - Do not use the output in any way other than storing it in a
2381 * confidential location. If you take any action that depends
2382 * on the tentative decrypted data, this action will need to be
2383 * undone if the input turns out not to be valid. Furthermore,
2384 * if an adversary can observe that this action took place
2385 * (for example through timing), they may be able to use this
2386 * fact as an oracle to decrypt any message encrypted with the
2387 * same key.
2388 * - In particular, do not copy the output anywhere but to a
2389 * memory or storage space that you have exclusive access to.
2390 *
2391 * \param[in,out] operation Active AEAD operation.
2392 * \param[in] input Buffer containing the message fragment to
2393 * encrypt or decrypt.
2394 * \param input_length Size of the \p input buffer in bytes.
2395 * \param[out] output Buffer where the output is to be written.
2396 * \param output_size Size of the \p output buffer in bytes.
2397 * \param[out] output_length On success, the number of bytes
2398 * that make up the returned output.
2399 *
2400 * \retval #PSA_SUCCESS
2401 * Success.
2402 * \retval #PSA_ERROR_BAD_STATE
2403 * The operation state is not valid (not set up, nonce not set
2404 * or already completed).
2405 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2406 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002407 * \retval #PSA_ERROR_INVALID_ARGUMENT
2408 * The total length of input to psa_aead_update_ad() so far is
2409 * less than the additional data length that was previously
2410 * specified with psa_aead_set_lengths().
2411 * \retval #PSA_ERROR_INVALID_ARGUMENT
2412 * The total input length overflows the plaintext length that
2413 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002414 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2415 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2416 * \retval #PSA_ERROR_HARDWARE_FAILURE
2417 * \retval #PSA_ERROR_TAMPERING_DETECTED
2418 */
2419psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2420 const uint8_t *input,
2421 size_t input_length,
2422 unsigned char *output,
2423 size_t output_size,
2424 size_t *output_length);
2425
2426/** Finish encrypting a message in an AEAD operation.
2427 *
2428 * The operation must have been set up with psa_aead_encrypt_setup().
2429 *
2430 * This function finishes the authentication of the additional data
2431 * formed by concatenating the inputs passed to preceding calls to
2432 * psa_aead_update_ad() with the plaintext formed by concatenating the
2433 * inputs passed to preceding calls to psa_aead_update().
2434 *
2435 * This function has two output buffers:
2436 * - \p ciphertext contains trailing ciphertext that was buffered from
2437 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2438 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2439 * will not contain any output and can be a 0-sized buffer.
2440 * - \p tag contains the authentication tag. Its length is always
2441 * #PSA_AEAD_TAG_LENGTH(\p alg) where \p alg is the AEAD algorithm
2442 * that the operation performs.
2443 *
2444 * When this function returns, the operation becomes inactive.
2445 *
2446 * \param[in,out] operation Active AEAD operation.
2447 * \param[out] ciphertext Buffer where the last part of the ciphertext
2448 * is to be written.
2449 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2450 * \param[out] ciphertext_length On success, the number of bytes of
2451 * returned ciphertext.
2452 * \param[out] tag Buffer where the authentication tag is
2453 * to be written.
2454 * \param tag_size Size of the \p tag buffer in bytes.
2455 * \param[out] tag_length On success, the number of bytes
2456 * that make up the returned tag.
2457 *
2458 * \retval #PSA_SUCCESS
2459 * Success.
2460 * \retval #PSA_ERROR_BAD_STATE
2461 * The operation state is not valid (not set up, nonce not set,
2462 * decryption, or already completed).
2463 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2464 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002465 * \retval #PSA_ERROR_INVALID_ARGUMENT
2466 * The total length of input to psa_aead_update_ad() so far is
2467 * less than the additional data length that was previously
2468 * specified with psa_aead_set_lengths().
2469 * \retval #PSA_ERROR_INVALID_ARGUMENT
2470 * The total length of input to psa_aead_update() so far is
2471 * less than the plaintext length that was previously
2472 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002473 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2474 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2475 * \retval #PSA_ERROR_HARDWARE_FAILURE
2476 * \retval #PSA_ERROR_TAMPERING_DETECTED
2477 */
2478psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002479 uint8_t *ciphertext,
2480 size_t ciphertext_size,
2481 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002482 uint8_t *tag,
2483 size_t tag_size,
2484 size_t *tag_length);
2485
2486/** Finish authenticating and decrypting a message in an AEAD operation.
2487 *
2488 * The operation must have been set up with psa_aead_decrypt_setup().
2489 *
2490 * This function finishes the authentication of the additional data
2491 * formed by concatenating the inputs passed to preceding calls to
2492 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2493 * inputs passed to preceding calls to psa_aead_update().
2494 *
2495 * When this function returns, the operation becomes inactive.
2496 *
2497 * \param[in,out] operation Active AEAD operation.
2498 * \param[in] tag Buffer containing the authentication tag.
2499 * \param tag_length Size of the \p tag buffer in bytes.
2500 *
2501 * \retval #PSA_SUCCESS
2502 * Success.
2503 * \retval #PSA_ERROR_BAD_STATE
2504 * The operation state is not valid (not set up, nonce not set,
2505 * encryption, or already completed).
2506 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2507 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002508 * \retval #PSA_ERROR_INVALID_ARGUMENT
2509 * The total length of input to psa_aead_update_ad() so far is
2510 * less than the additional data length that was previously
2511 * specified with psa_aead_set_lengths().
2512 * \retval #PSA_ERROR_INVALID_ARGUMENT
2513 * The total length of input to psa_aead_update() so far is
2514 * less than the plaintext length that was previously
2515 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002516 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2517 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2518 * \retval #PSA_ERROR_HARDWARE_FAILURE
2519 * \retval #PSA_ERROR_TAMPERING_DETECTED
2520 */
2521psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2522 const uint8_t *tag,
2523 size_t tag_length);
2524
2525/** Abort an AEAD operation.
2526 *
2527 * Aborting an operation frees all associated resources except for the
2528 * \p operation structure itself. Once aborted, the operation object
2529 * can be reused for another operation by calling
2530 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2531 *
2532 * You may call this function any time after the operation object has
2533 * been initialized by any of the following methods:
2534 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2535 * whether it succeeds or not.
2536 * - Initializing the \c struct to all-bits-zero.
2537 * - Initializing the \c struct to logical zeros, e.g.
2538 * `psa_aead_operation_t operation = {0}`.
2539 *
2540 * In particular, calling psa_aead_abort() after the operation has been
2541 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2542 * is safe and has no effect.
2543 *
2544 * \param[in,out] operation Initialized AEAD operation.
2545 *
2546 * \retval #PSA_SUCCESS
2547 * \retval #PSA_ERROR_BAD_STATE
2548 * \p operation is not an active AEAD operation.
2549 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2550 * \retval #PSA_ERROR_HARDWARE_FAILURE
2551 * \retval #PSA_ERROR_TAMPERING_DETECTED
2552 */
2553psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2554
Gilles Peskine3b555712018-03-03 21:27:57 +01002555/**@}*/
2556
Gilles Peskine20035e32018-02-03 22:44:14 +01002557/** \defgroup asymmetric Asymmetric cryptography
2558 * @{
2559 */
2560
2561/**
2562 * \brief Sign a hash or short message with a private key.
2563 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002564 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002565 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002566 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2567 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2568 * to determine the hash algorithm to use.
2569 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002570 * \param handle Handle to the key to use for the operation.
2571 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002572 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002573 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002574 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002575 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002576 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002577 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002578 * \param[out] signature_length On success, the number of bytes
2579 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002580 *
Gilles Peskine28538492018-07-11 17:34:00 +02002581 * \retval #PSA_SUCCESS
2582 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002583 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002584 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002585 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002586 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002587 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002588 * \retval #PSA_ERROR_NOT_SUPPORTED
2589 * \retval #PSA_ERROR_INVALID_ARGUMENT
2590 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2591 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2592 * \retval #PSA_ERROR_HARDWARE_FAILURE
2593 * \retval #PSA_ERROR_TAMPERING_DETECTED
2594 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002595 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002596 * The library has not been previously initialized by psa_crypto_init().
2597 * It is implementation-dependent whether a failure to initialize
2598 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002599 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002600psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002601 psa_algorithm_t alg,
2602 const uint8_t *hash,
2603 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002604 uint8_t *signature,
2605 size_t signature_size,
2606 size_t *signature_length);
2607
2608/**
2609 * \brief Verify the signature a hash or short message using a public key.
2610 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002611 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002612 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002613 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2614 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2615 * to determine the hash algorithm to use.
2616 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002617 * \param handle Handle to the key to use for the operation.
2618 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002619 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002620 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002621 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002622 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002623 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002624 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002625 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002626 *
Gilles Peskine28538492018-07-11 17:34:00 +02002627 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002628 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002629 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002630 * The calculation was perfomed successfully, but the passed
2631 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002632 * \retval #PSA_ERROR_NOT_SUPPORTED
2633 * \retval #PSA_ERROR_INVALID_ARGUMENT
2634 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2635 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2636 * \retval #PSA_ERROR_HARDWARE_FAILURE
2637 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002638 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002639 * The library has not been previously initialized by psa_crypto_init().
2640 * It is implementation-dependent whether a failure to initialize
2641 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002642 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002643psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002644 psa_algorithm_t alg,
2645 const uint8_t *hash,
2646 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002647 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002648 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002649
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002650/**
2651 * \brief Encrypt a short message with a public key.
2652 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002653 * \param handle Handle to the key to use for the operation.
2654 * It must be a public key or an asymmetric
2655 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002656 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002657 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002658 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002659 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002660 * \param[in] salt A salt or label, if supported by the
2661 * encryption algorithm.
2662 * If the algorithm does not support a
2663 * salt, pass \c NULL.
2664 * If the algorithm supports an optional
2665 * salt and you do not want to pass a salt,
2666 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002667 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002668 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2669 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002670 * \param salt_length Size of the \p salt buffer in bytes.
2671 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002672 * \param[out] output Buffer where the encrypted message is to
2673 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002674 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002675 * \param[out] output_length On success, the number of bytes
2676 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002677 *
Gilles Peskine28538492018-07-11 17:34:00 +02002678 * \retval #PSA_SUCCESS
2679 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002680 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002681 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002682 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002683 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002684 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002685 * \retval #PSA_ERROR_NOT_SUPPORTED
2686 * \retval #PSA_ERROR_INVALID_ARGUMENT
2687 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2688 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2689 * \retval #PSA_ERROR_HARDWARE_FAILURE
2690 * \retval #PSA_ERROR_TAMPERING_DETECTED
2691 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002692 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002693 * The library has not been previously initialized by psa_crypto_init().
2694 * It is implementation-dependent whether a failure to initialize
2695 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002696 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002697psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002698 psa_algorithm_t alg,
2699 const uint8_t *input,
2700 size_t input_length,
2701 const uint8_t *salt,
2702 size_t salt_length,
2703 uint8_t *output,
2704 size_t output_size,
2705 size_t *output_length);
2706
2707/**
2708 * \brief Decrypt a short message with a private key.
2709 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002710 * \param handle Handle to the key to use for the operation.
2711 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002712 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002713 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002714 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002715 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002716 * \param[in] salt A salt or label, if supported by the
2717 * encryption algorithm.
2718 * If the algorithm does not support a
2719 * salt, pass \c NULL.
2720 * If the algorithm supports an optional
2721 * salt and you do not want to pass a salt,
2722 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002723 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002724 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2725 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002726 * \param salt_length Size of the \p salt buffer in bytes.
2727 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002728 * \param[out] output Buffer where the decrypted message is to
2729 * be written.
2730 * \param output_size Size of the \c output buffer in bytes.
2731 * \param[out] output_length On success, the number of bytes
2732 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002733 *
Gilles Peskine28538492018-07-11 17:34:00 +02002734 * \retval #PSA_SUCCESS
2735 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002736 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002737 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002738 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002739 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002740 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002741 * \retval #PSA_ERROR_NOT_SUPPORTED
2742 * \retval #PSA_ERROR_INVALID_ARGUMENT
2743 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2744 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2745 * \retval #PSA_ERROR_HARDWARE_FAILURE
2746 * \retval #PSA_ERROR_TAMPERING_DETECTED
2747 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2748 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002749 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002750 * The library has not been previously initialized by psa_crypto_init().
2751 * It is implementation-dependent whether a failure to initialize
2752 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002753 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002754psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002755 psa_algorithm_t alg,
2756 const uint8_t *input,
2757 size_t input_length,
2758 const uint8_t *salt,
2759 size_t salt_length,
2760 uint8_t *output,
2761 size_t output_size,
2762 size_t *output_length);
2763
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002764/**@}*/
2765
Gilles Peskineedd76872018-07-20 17:42:05 +02002766/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002767 * @{
2768 */
2769
2770/** The type of the state data structure for generators.
2771 *
2772 * Before calling any function on a generator, the application must
2773 * initialize it by any of the following means:
2774 * - Set the structure to all-bits-zero, for example:
2775 * \code
2776 * psa_crypto_generator_t generator;
2777 * memset(&generator, 0, sizeof(generator));
2778 * \endcode
2779 * - Initialize the structure to logical zero values, for example:
2780 * \code
2781 * psa_crypto_generator_t generator = {0};
2782 * \endcode
2783 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2784 * for example:
2785 * \code
2786 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2787 * \endcode
2788 * - Assign the result of the function psa_crypto_generator_init()
2789 * to the structure, for example:
2790 * \code
2791 * psa_crypto_generator_t generator;
2792 * generator = psa_crypto_generator_init();
2793 * \endcode
2794 *
2795 * This is an implementation-defined \c struct. Applications should not
2796 * make any assumptions about the content of this structure except
2797 * as directed by the documentation of a specific implementation.
2798 */
2799typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2800
2801/** \def PSA_CRYPTO_GENERATOR_INIT
2802 *
2803 * This macro returns a suitable initializer for a generator object
2804 * of type #psa_crypto_generator_t.
2805 */
2806#ifdef __DOXYGEN_ONLY__
2807/* This is an example definition for documentation purposes.
2808 * Implementations should define a suitable value in `crypto_struct.h`.
2809 */
2810#define PSA_CRYPTO_GENERATOR_INIT {0}
2811#endif
2812
2813/** Return an initial value for a generator object.
2814 */
2815static psa_crypto_generator_t psa_crypto_generator_init(void);
2816
2817/** Retrieve the current capacity of a generator.
2818 *
2819 * The capacity of a generator is the maximum number of bytes that it can
2820 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2821 *
2822 * \param[in] generator The generator to query.
2823 * \param[out] capacity On success, the capacity of the generator.
2824 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002825 * \retval #PSA_SUCCESS
2826 * \retval #PSA_ERROR_BAD_STATE
2827 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002828 */
2829psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2830 size_t *capacity);
2831
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002832/** Set the maximum capacity of a generator.
2833 *
2834 * \param[in,out] generator The generator object to modify.
2835 * \param capacity The new capacity of the generator.
2836 * It must be less or equal to the generator's
2837 * current capacity.
2838 *
2839 * \retval #PSA_SUCCESS
2840 * \retval #PSA_ERROR_INVALID_ARGUMENT
2841 * \p capacity is larger than the generator's current capacity.
2842 * \retval #PSA_ERROR_BAD_STATE
2843 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2844 */
2845psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2846 size_t capacity);
2847
Gilles Peskineeab56e42018-07-12 17:12:33 +02002848/** Read some data from a generator.
2849 *
2850 * This function reads and returns a sequence of bytes from a generator.
2851 * The data that is read is discarded from the generator. The generator's
2852 * capacity is decreased by the number of bytes read.
2853 *
2854 * \param[in,out] generator The generator object to read from.
2855 * \param[out] output Buffer where the generator output will be
2856 * written.
2857 * \param output_length Number of bytes to output.
2858 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002859 * \retval #PSA_SUCCESS
2860 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002861 * There were fewer than \p output_length bytes
2862 * in the generator. Note that in this case, no
2863 * output is written to the output buffer.
2864 * The generator's capacity is set to 0, thus
2865 * subsequent calls to this function will not
2866 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002867 * \retval #PSA_ERROR_BAD_STATE
2868 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2869 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2870 * \retval #PSA_ERROR_HARDWARE_FAILURE
2871 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002872 */
2873psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2874 uint8_t *output,
2875 size_t output_length);
2876
2877/** Create a symmetric key from data read from a generator.
2878 *
2879 * This function reads a sequence of bytes from a generator and imports
2880 * these bytes as a key.
2881 * The data that is read is discarded from the generator. The generator's
2882 * capacity is decreased by the number of bytes read.
2883 *
2884 * This function is equivalent to calling #psa_generator_read and
2885 * passing the resulting output to #psa_import_key, but
2886 * if the implementation provides an isolation boundary then
2887 * the key material is not exposed outside the isolation boundary.
2888 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002889 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002890 * It must have been obtained by calling
2891 * psa_allocate_key() or psa_create_key() and must
2892 * not contain key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002893 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2894 * This must be a symmetric key type.
2895 * \param bits Key size in bits.
2896 * \param[in,out] generator The generator object to read from.
2897 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002898 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002899 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002900 * If the key is persistent, the key material and the key's metadata
2901 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002902 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002903 * There were fewer than \p output_length bytes
2904 * in the generator. Note that in this case, no
2905 * output is written to the output buffer.
2906 * The generator's capacity is set to 0, thus
2907 * subsequent calls to this function will not
2908 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002909 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002910 * The key type or key size is not supported, either by the
2911 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002912 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002913 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002914 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002915 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002916 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2917 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2918 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2919 * \retval #PSA_ERROR_HARDWARE_FAILURE
2920 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002921 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002922 * The library has not been previously initialized by psa_crypto_init().
2923 * It is implementation-dependent whether a failure to initialize
2924 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002925 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002926psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002927 psa_key_type_t type,
2928 size_t bits,
2929 psa_crypto_generator_t *generator);
2930
2931/** Abort a generator.
2932 *
2933 * Once a generator has been aborted, its capacity is zero.
2934 * Aborting a generator frees all associated resources except for the
2935 * \c generator structure itself.
2936 *
2937 * This function may be called at any time as long as the generator
2938 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2939 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2940 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2941 * on a generator that has not been set up.
2942 *
2943 * Once aborted, the generator object may be called.
2944 *
2945 * \param[in,out] generator The generator to abort.
2946 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002947 * \retval #PSA_SUCCESS
2948 * \retval #PSA_ERROR_BAD_STATE
2949 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2950 * \retval #PSA_ERROR_HARDWARE_FAILURE
2951 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002952 */
2953psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2954
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002955/** Use the maximum possible capacity for a generator.
2956 *
2957 * Use this value as the capacity argument when setting up a generator
2958 * to indicate that the generator should have the maximum possible capacity.
2959 * The value of the maximum possible capacity depends on the generator
2960 * algorithm.
2961 */
2962#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2963
Gilles Peskineeab56e42018-07-12 17:12:33 +02002964/**@}*/
2965
Gilles Peskineea0fb492018-07-12 17:17:20 +02002966/** \defgroup derivation Key derivation
2967 * @{
2968 */
2969
2970/** Set up a key derivation operation.
2971 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002972 * A key derivation algorithm takes some inputs and uses them to create
2973 * a byte generator which can be used to produce keys and other
2974 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002975 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002976 * To use a generator for key derivation:
2977 * - Start with an initialized object of type #psa_crypto_generator_t.
2978 * - Call psa_key_derivation_setup() to select the algorithm.
2979 * - Provide the inputs for the key derivation by calling
2980 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2981 * as appropriate. Which inputs are needed, in what order, and whether
2982 * they may be keys and if so of what type depends on the algorithm.
2983 * - Optionally set the generator's maximum capacity with
2984 * psa_set_generator_capacity(). You may do this before, in the middle of
2985 * or after providing inputs. For some algorithms, this step is mandatory
2986 * because the output depends on the maximum capacity.
2987 * - Generate output with psa_generator_read() or
2988 * psa_generator_import_key(). Successive calls to these functions
2989 * use successive output bytes from the generator.
2990 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02002991 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002992 * \param[in,out] generator The generator object to set up. It must
2993 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002994 * \param alg The key derivation algorithm to compute
2995 * (\c PSA_ALG_XXX value such that
2996 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02002997 *
2998 * \retval #PSA_SUCCESS
2999 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003000 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003001 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003002 * \retval #PSA_ERROR_NOT_SUPPORTED
3003 * \c alg is not supported or is not a key derivation algorithm.
3004 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3005 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3006 * \retval #PSA_ERROR_HARDWARE_FAILURE
3007 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003008 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003009 */
3010psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3011 psa_algorithm_t alg);
3012
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003013/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003014 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003015 * Which inputs are required and in what order depends on the algorithm.
3016 * Refer to the documentation of each key derivation or key agreement
3017 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003018 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003019 * This function passes direct inputs. Some inputs must be passed as keys
3020 * using psa_key_derivation_input_key() instead of this function. Refer to
3021 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003022 *
3023 * \param[in,out] generator The generator object to use. It must
3024 * have been set up with
3025 * psa_key_derivation_setup() and must not
3026 * have produced any output yet.
3027 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003028 * \param[in] data Input data to use.
3029 * \param data_length Size of the \p data buffer in bytes.
3030 *
3031 * \retval #PSA_SUCCESS
3032 * Success.
3033 * \retval #PSA_ERROR_INVALID_ARGUMENT
3034 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003035 * \retval #PSA_ERROR_INVALID_ARGUMENT
3036 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003037 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3038 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3039 * \retval #PSA_ERROR_HARDWARE_FAILURE
3040 * \retval #PSA_ERROR_TAMPERING_DETECTED
3041 * \retval #PSA_ERROR_BAD_STATE
3042 * The value of \p step is not valid given the state of \p generator.
3043 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003044 * The library has not been previously initialized by psa_crypto_init().
3045 * It is implementation-dependent whether a failure to initialize
3046 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003047 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003048psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3049 psa_key_derivation_step_t step,
3050 const uint8_t *data,
3051 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003052
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003053/** Provide an input for key derivation in the form of a key.
3054 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003055 * Which inputs are required and in what order depends on the algorithm.
3056 * Refer to the documentation of each key derivation or key agreement
3057 * algorithm for information.
3058 *
3059 * This function passes key inputs. Some inputs must be passed as keys
3060 * of the appropriate type using this function, while others must be
3061 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3062 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003063 *
3064 * \param[in,out] generator The generator object to use. It must
3065 * have been set up with
3066 * psa_key_derivation_setup() and must not
3067 * have produced any output yet.
3068 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003069 * \param handle Handle to the key. It must have an
3070 * appropriate type for \p step and must
3071 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003072 *
3073 * \retval #PSA_SUCCESS
3074 * Success.
3075 * \retval #PSA_ERROR_INVALID_HANDLE
3076 * \retval #PSA_ERROR_EMPTY_SLOT
3077 * \retval #PSA_ERROR_NOT_PERMITTED
3078 * \retval #PSA_ERROR_INVALID_ARGUMENT
3079 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003080 * \retval #PSA_ERROR_INVALID_ARGUMENT
3081 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003082 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3083 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3084 * \retval #PSA_ERROR_HARDWARE_FAILURE
3085 * \retval #PSA_ERROR_TAMPERING_DETECTED
3086 * \retval #PSA_ERROR_BAD_STATE
3087 * The value of \p step is not valid given the state of \p generator.
3088 * \retval #PSA_ERROR_BAD_STATE
3089 * The library has not been previously initialized by psa_crypto_init().
3090 * It is implementation-dependent whether a failure to initialize
3091 * results in this error code.
3092 */
3093psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3094 psa_key_derivation_step_t step,
3095 psa_key_handle_t handle);
3096
Gilles Peskine969c5d62019-01-16 15:53:06 +01003097/** Perform a key agreement and use the shared secret as input to a key
3098 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003099 *
3100 * A key agreement algorithm takes two inputs: a private key \p private_key
3101 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003102 * The result of this function is passed as input to a key derivation.
3103 * The output of this key derivation can be extracted by reading from the
3104 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003105 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003106 * \param[in,out] generator The generator object to use. It must
3107 * have been set up with
3108 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003109 * key agreement and derivation algorithm
3110 * \c alg (\c PSA_ALG_XXX value such that
3111 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true
3112 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3113 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003114 * The generator must be ready for an
3115 * input of the type given by \p step.
3116 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003117 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003118 * \param[in] peer_key Public key of the peer. The peer key must be in the
3119 * same format that psa_import_key() accepts for the
3120 * public key type corresponding to the type of
3121 * private_key. That is, this function performs the
3122 * equivalent of
3123 * `psa_import_key(internal_public_key_handle,
3124 * PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(private_key_type),
3125 * peer_key, peer_key_length)` where
3126 * `private_key_type` is the type of `private_key`.
3127 * For example, for EC keys, this means that peer_key
3128 * is interpreted as a point on the curve that the
3129 * private key is on. The standard formats for public
3130 * keys are documented in the documentation of
3131 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003132 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003133 *
3134 * \retval #PSA_SUCCESS
3135 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003136 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02003137 * \retval #PSA_ERROR_EMPTY_SLOT
3138 * \retval #PSA_ERROR_NOT_PERMITTED
3139 * \retval #PSA_ERROR_INVALID_ARGUMENT
3140 * \c private_key is not compatible with \c alg,
3141 * or \p peer_key is not valid for \c alg or not compatible with
3142 * \c private_key.
3143 * \retval #PSA_ERROR_NOT_SUPPORTED
3144 * \c alg is not supported or is not a key derivation algorithm.
3145 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3146 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3147 * \retval #PSA_ERROR_HARDWARE_FAILURE
3148 * \retval #PSA_ERROR_TAMPERING_DETECTED
3149 */
3150psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003151 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003152 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003153 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003154 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003155
Gilles Peskine769c7a62019-01-18 16:42:29 +01003156/** Perform a key agreement and use the shared secret as input to a key
3157 * derivation.
3158 *
3159 * A key agreement algorithm takes two inputs: a private key \p private_key
3160 * a public key \p peer_key.
3161 *
3162 * \warning The raw result of a key agreement algorithm such as finite-field
3163 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3164 * not be used directly as key material. It should instead be passed as
3165 * input to a key derivation algorithm. To chain a key agreement with
3166 * a key derivation, use psa_key_agreement() and other functions from
3167 * the key derivation and generator interface.
3168 *
3169 * \param private_key Handle to the private key to use.
3170 * \param[in] peer_key Public key of the peer. It must be
3171 * in the same format that psa_import_key()
3172 * accepts. The standard formats for public
3173 * keys are documented in the documentation
3174 * of psa_export_public_key().
3175 * \param peer_key_length Size of \p peer_key in bytes.
3176 * \param[out] output Buffer where the decrypted message is to
3177 * be written.
3178 * \param output_size Size of the \c output buffer in bytes.
3179 * \param[out] output_length On success, the number of bytes
3180 * that make up the returned output.
3181 *
3182 * \retval #PSA_SUCCESS
3183 * Success.
3184 * \retval #PSA_ERROR_INVALID_HANDLE
3185 * \retval #PSA_ERROR_EMPTY_SLOT
3186 * \retval #PSA_ERROR_NOT_PERMITTED
3187 * \retval #PSA_ERROR_INVALID_ARGUMENT
3188 * \p alg is not a key agreement algorithm
3189 * \retval #PSA_ERROR_INVALID_ARGUMENT
3190 * \p private_key is not compatible with \p alg,
3191 * or \p peer_key is not valid for \p alg or not compatible with
3192 * \p private_key.
3193 * \retval #PSA_ERROR_NOT_SUPPORTED
3194 * \p alg is not a supported key agreement algorithm.
3195 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3196 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3197 * \retval #PSA_ERROR_HARDWARE_FAILURE
3198 * \retval #PSA_ERROR_TAMPERING_DETECTED
3199 */
3200psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3201 psa_key_handle_t private_key,
3202 const uint8_t *peer_key,
3203 size_t peer_key_length,
3204 uint8_t *output,
3205 size_t output_size,
3206 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003207
3208/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003209
3210/** \defgroup random Random generation
3211 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003212 */
3213
3214/**
3215 * \brief Generate random bytes.
3216 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003217 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003218 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003219 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003220 *
3221 * \note To generate a key, use psa_generate_key() instead.
3222 *
3223 * \param[out] output Output buffer for the generated data.
3224 * \param output_size Number of bytes to generate and output.
3225 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003226 * \retval #PSA_SUCCESS
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003227 * \retval #PSA_ERROR_NOT_SUPPORTED
3228 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003229 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003230 * \retval #PSA_ERROR_HARDWARE_FAILURE
3231 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003232 * \retval #PSA_ERROR_BAD_STATE
3233 * The library has not been previously initialized by psa_crypto_init().
3234 * It is implementation-dependent whether a failure to initialize
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003235 * results in this error code.
3236 */
3237psa_status_t psa_generate_random(uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003238 size_t output_size);
3239
3240/** Extra parameters for RSA key generation.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003241 *
Gilles Peskine28538492018-07-11 17:34:00 +02003242 * You may pass a pointer to a structure of this type as the \c extra
3243 * parameter to psa_generate_key().
3244 */
3245typedef struct {
3246 uint32_t e; /**< Public exponent value. Default: 65537. */
3247} psa_generate_key_extra_rsa;
3248
3249/**
itayzafrir90d8c7a2018-09-12 11:44:52 +03003250 * \brief Generate a key or key pair.
itayzafrir18617092018-09-16 12:22:41 +03003251 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003252 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003253 * It must have been obtained by calling
3254 * psa_allocate_key() or psa_create_key() and must
3255 * not contain key material yet.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003256 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
3257 * \param bits Key size in bits.
3258 * \param[in] extra Extra parameters for key generation. The
3259 * interpretation of this parameter depends on
3260 * \p type. All types support \c NULL to use
3261 * default parameters. Implementation that support
3262 * the generation of vendor-specific key types
3263 * that allow extra parameters shall document
3264 * the format of these extra parameters and
3265 * the default values. For standard parameters,
3266 * the meaning of \p extra is as follows:
3267 * - For a symmetric key type (a type such
3268 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
Gilles Peskine53d991e2018-07-12 01:14:59 +02003269 * false), \p extra must be \c NULL.
3270 * - For an elliptic curve key type (a type
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003271 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
3272 * false), \p extra must be \c NULL.
3273 * - For an RSA key (\p type is
Gilles Peskinee59236f2018-01-27 23:32:46 +01003274 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3275 * optional #psa_generate_key_extra_rsa structure
3276 * specifying the public exponent. The
3277 * default public exponent used when \p extra
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003278 * is \c NULL is 65537.
Jaeden Amero1308fb52019-01-11 13:50:43 +00003279 * - For an DSA key (\p type is
3280 * #PSA_KEY_TYPE_DSA_KEYPAIR), \p extra is an
3281 * optional structure specifying the key domain
3282 * parameters. The key domain parameters can also be
3283 * provided by psa_set_key_domain_parameters(),
3284 * which documents the format of the structure.
Jaeden Amero8851c402019-01-11 14:20:03 +00003285 * - For a DH key (\p type is
3286 * #PSA_KEY_TYPE_DH_KEYPAIR), the \p extra is an
3287 * optional structure specifying the key domain
3288 * parameters. The key domain parameters can also be
3289 * provided by psa_set_key_domain_parameters(),
3290 * which documents the format of the structure.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003291 * \param extra_size Size of the buffer that \p extra
3292 * points to, in bytes. Note that if \p extra is
3293 * \c NULL then \p extra_size must be zero.
3294 *
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003295 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003296 * Success.
3297 * If the key is persistent, the key material and the key's metadata
3298 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003299 * \retval #PSA_ERROR_INVALID_HANDLE
3300 * \retval #PSA_ERROR_OCCUPIED_SLOT
3301 * There is already a key in the specified slot.
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003302 * \retval #PSA_ERROR_NOT_SUPPORTED
3303 * \retval #PSA_ERROR_INVALID_ARGUMENT
3304 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003305 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3306 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3307 * \retval #PSA_ERROR_HARDWARE_FAILURE
3308 * \retval #PSA_ERROR_TAMPERING_DETECTED
3309 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003310 * The library has not been previously initialized by psa_crypto_init().
3311 * It is implementation-dependent whether a failure to initialize
3312 * results in this error code.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003313 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003314psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskinee59236f2018-01-27 23:32:46 +01003315 psa_key_type_t type,
3316 size_t bits,
3317 const void *extra,
3318 size_t extra_size);
3319
3320/**@}*/
3321
3322#ifdef __cplusplus
3323}
3324#endif
3325
3326/* The file "crypto_sizes.h" contains definitions for size calculation
3327 * macros whose definitions are implementation-specific. */
3328#include "crypto_sizes.h"
3329
3330/* The file "crypto_struct.h" contains definitions for
3331 * implementation-specific structs that are declared above. */
3332#include "crypto_struct.h"
3333
3334/* The file "crypto_extra.h" contains vendor-specific definitions. This
3335 * can include vendor-defined algorithms, extra functions, etc. */
3336#include "crypto_extra.h"
3337
3338#endif /* PSA_CRYPTO_H */