blob: dd4599687b9110e4b2f8475b01bd130ef24d3708 [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
Gilles Peskined338b912019-02-15 13:01:41 +0100833 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100834 *
835 * \retval #PSA_SUCCESS
836 * Success.
837 * \retval #PSA_ERROR_NOT_SUPPORTED
838 * \p alg is not supported or is not a hash algorithm.
839 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
840 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
841 * \retval #PSA_ERROR_HARDWARE_FAILURE
842 * \retval #PSA_ERROR_TAMPERING_DETECTED
843 */
844psa_status_t psa_hash_compute(psa_algorithm_t alg,
845 const uint8_t *input,
846 size_t input_length,
847 uint8_t *hash,
848 size_t hash_size,
849 size_t *hash_length);
850
851/** Calculate the hash (digest) of a message and compare it with a
852 * reference value.
853 *
854 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
855 * such that #PSA_ALG_IS_HASH(\p alg) is true).
856 * \param[in] input Buffer containing the message to hash.
857 * \param input_length Size of the \p input buffer in bytes.
858 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100859 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100860 *
861 * \retval #PSA_SUCCESS
862 * The expected hash is identical to the actual hash of the input.
863 * \retval #PSA_ERROR_INVALID_SIGNATURE
864 * The hash of the message was calculated successfully, but it
865 * differs from the expected hash.
866 * \retval #PSA_ERROR_NOT_SUPPORTED
867 * \p alg is not supported or is not a hash algorithm.
868 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
869 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
870 * \retval #PSA_ERROR_HARDWARE_FAILURE
871 * \retval #PSA_ERROR_TAMPERING_DETECTED
872 */
873psa_status_t psa_hash_compare(psa_algorithm_t alg,
874 const uint8_t *input,
875 size_t input_length,
876 const uint8_t *hash,
877 const size_t hash_length);
878
Gilles Peskine308b91d2018-02-08 09:47:44 +0100879/** The type of the state data structure for multipart hash operations.
880 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000881 * Before calling any function on a hash operation object, the application must
882 * initialize it by any of the following means:
883 * - Set the structure to all-bits-zero, for example:
884 * \code
885 * psa_hash_operation_t operation;
886 * memset(&operation, 0, sizeof(operation));
887 * \endcode
888 * - Initialize the structure to logical zero values, for example:
889 * \code
890 * psa_hash_operation_t operation = {0};
891 * \endcode
892 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
893 * for example:
894 * \code
895 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
896 * \endcode
897 * - Assign the result of the function psa_hash_operation_init()
898 * to the structure, for example:
899 * \code
900 * psa_hash_operation_t operation;
901 * operation = psa_hash_operation_init();
902 * \endcode
903 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100904 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100905 * make any assumptions about the content of this structure except
906 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100907typedef struct psa_hash_operation_s psa_hash_operation_t;
908
Jaeden Amero6a25b412019-01-04 11:47:44 +0000909/** \def PSA_HASH_OPERATION_INIT
910 *
911 * This macro returns a suitable initializer for a hash operation object
912 * of type #psa_hash_operation_t.
913 */
914#ifdef __DOXYGEN_ONLY__
915/* This is an example definition for documentation purposes.
916 * Implementations should define a suitable value in `crypto_struct.h`.
917 */
918#define PSA_HASH_OPERATION_INIT {0}
919#endif
920
921/** Return an initial value for a hash operation object.
922 */
923static psa_hash_operation_t psa_hash_operation_init(void);
924
Gilles Peskinef45adda2019-01-14 18:29:18 +0100925/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100926 *
927 * The sequence of operations to calculate a hash (message digest)
928 * is as follows:
929 * -# Allocate an operation object which will be passed to all the functions
930 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000931 * -# Initialize the operation object with one of the methods described in the
932 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200933 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100934 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100935 * of the message each time. The hash that is calculated is the hash
936 * of the concatenation of these messages in order.
937 * -# To calculate the hash, call psa_hash_finish().
938 * To compare the hash with an expected value, call psa_hash_verify().
939 *
940 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000941 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100942 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200943 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100944 * eventually terminate the operation. The following events terminate an
945 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100946 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100947 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100948 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000949 * \param[in,out] operation The operation object to set up. It must have
950 * been initialized as per the documentation for
951 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200952 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
953 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100954 *
Gilles Peskine28538492018-07-11 17:34:00 +0200955 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100956 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200957 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200958 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200959 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
960 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
961 * \retval #PSA_ERROR_HARDWARE_FAILURE
962 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100963 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200964psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100965 psa_algorithm_t alg);
966
Gilles Peskine308b91d2018-02-08 09:47:44 +0100967/** Add a message fragment to a multipart hash operation.
968 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200969 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100970 *
971 * If this function returns an error status, the operation becomes inactive.
972 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200973 * \param[in,out] operation Active hash operation.
974 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200975 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100976 *
Gilles Peskine28538492018-07-11 17:34:00 +0200977 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100978 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200979 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100980 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200981 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
982 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
983 * \retval #PSA_ERROR_HARDWARE_FAILURE
984 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100985 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100986psa_status_t psa_hash_update(psa_hash_operation_t *operation,
987 const uint8_t *input,
988 size_t input_length);
989
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990/** Finish the calculation of the hash of a message.
991 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200992 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100993 * This function calculates the hash of the message formed by concatenating
994 * the inputs passed to preceding calls to psa_hash_update().
995 *
996 * When this function returns, the operation becomes inactive.
997 *
998 * \warning Applications should not call this function if they expect
999 * a specific value for the hash. Call psa_hash_verify() instead.
1000 * Beware that comparing integrity or authenticity data such as
1001 * hash values with a function such as \c memcmp is risky
1002 * because the time taken by the comparison may leak information
1003 * about the hashed data which could allow an attacker to guess
1004 * a valid hash and thereby bypass security controls.
1005 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001006 * \param[in,out] operation Active hash operation.
1007 * \param[out] hash Buffer where the hash is to be written.
1008 * \param hash_size Size of the \p hash buffer in bytes.
1009 * \param[out] hash_length On success, the number of bytes
1010 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001011 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001012 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001013 *
Gilles Peskine28538492018-07-11 17:34:00 +02001014 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001015 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001016 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001017 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001018 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001019 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001020 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001021 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001022 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1023 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1024 * \retval #PSA_ERROR_HARDWARE_FAILURE
1025 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001026 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001027psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1028 uint8_t *hash,
1029 size_t hash_size,
1030 size_t *hash_length);
1031
Gilles Peskine308b91d2018-02-08 09:47:44 +01001032/** Finish the calculation of the hash of a message and compare it with
1033 * an expected value.
1034 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001035 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001036 * This function calculates the hash of the message formed by concatenating
1037 * the inputs passed to preceding calls to psa_hash_update(). It then
1038 * compares the calculated hash with the expected hash passed as a
1039 * parameter to this function.
1040 *
1041 * When this function returns, the operation becomes inactive.
1042 *
Gilles Peskine19067982018-03-20 17:54:53 +01001043 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001044 * comparison between the actual hash and the expected hash is performed
1045 * in constant time.
1046 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001047 * \param[in,out] operation Active hash operation.
1048 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001049 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001050 *
Gilles Peskine28538492018-07-11 17:34:00 +02001051 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001052 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001053 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001054 * The hash of the message was calculated successfully, but it
1055 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001056 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001057 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001058 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1059 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1060 * \retval #PSA_ERROR_HARDWARE_FAILURE
1061 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001062 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001063psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1064 const uint8_t *hash,
1065 size_t hash_length);
1066
Gilles Peskine308b91d2018-02-08 09:47:44 +01001067/** Abort a hash operation.
1068 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001069 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001070 * \p operation structure itself. Once aborted, the operation object
1071 * can be reused for another operation by calling
1072 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001073 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001074 * You may call this function any time after the operation object has
1075 * been initialized by any of the following methods:
1076 * - A call to psa_hash_setup(), whether it succeeds or not.
1077 * - Initializing the \c struct to all-bits-zero.
1078 * - Initializing the \c struct to logical zeros, e.g.
1079 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001080 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001081 * In particular, calling psa_hash_abort() after the operation has been
1082 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1083 * psa_hash_verify() is safe and has no effect.
1084 *
1085 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001086 *
Gilles Peskine28538492018-07-11 17:34:00 +02001087 * \retval #PSA_SUCCESS
1088 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001089 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001090 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1091 * \retval #PSA_ERROR_HARDWARE_FAILURE
1092 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001093 */
1094psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001095
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001096/** Clone a hash operation.
1097 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001098 * This function copies the state of an ongoing hash operation to
1099 * a new operation object. In other words, this function is equivalent
1100 * to calling psa_hash_setup() on \p target_operation with the same
1101 * algorithm that \p source_operation was set up for, then
1102 * psa_hash_update() on \p target_operation with the same input that
1103 * that was passed to \p source_operation. After this function returns, the
1104 * two objects are independent, i.e. subsequent calls involving one of
1105 * the objects do not affect the other object.
1106 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001107 * \param[in] source_operation The active hash operation to clone.
1108 * \param[in,out] target_operation The operation object to set up.
1109 * It must be initialized but not active.
1110 *
1111 * \retval #PSA_SUCCESS
1112 * \retval #PSA_ERROR_BAD_STATE
1113 * \p source_operation is not an active hash operation.
1114 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001115 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001116 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1117 * \retval #PSA_ERROR_HARDWARE_FAILURE
1118 * \retval #PSA_ERROR_TAMPERING_DETECTED
1119 */
1120psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1121 psa_hash_operation_t *target_operation);
1122
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001123/**@}*/
1124
Gilles Peskine8c9def32018-02-08 10:02:12 +01001125/** \defgroup MAC Message authentication codes
1126 * @{
1127 */
1128
Gilles Peskine69647a42019-01-14 20:18:12 +01001129/** Calculate the MAC (message authentication code) of a message.
1130 *
1131 * \note To verify the MAC of a message against an
1132 * expected value, use psa_mac_verify() instead.
1133 * Beware that comparing integrity or authenticity data such as
1134 * MAC values with a function such as \c memcmp is risky
1135 * because the time taken by the comparison may leak information
1136 * about the MAC value which could allow an attacker to guess
1137 * a valid MAC and thereby bypass security controls.
1138 *
1139 * \param handle Handle to the key to use for the operation.
1140 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001141 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001142 * \param[in] input Buffer containing the input message.
1143 * \param input_length Size of the \p input buffer in bytes.
1144 * \param[out] mac Buffer where the MAC value is to be written.
1145 * \param mac_size Size of the \p mac buffer in bytes.
1146 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001147 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001148 *
1149 * \retval #PSA_SUCCESS
1150 * Success.
1151 * \retval #PSA_ERROR_INVALID_HANDLE
1152 * \retval #PSA_ERROR_EMPTY_SLOT
1153 * \retval #PSA_ERROR_NOT_PERMITTED
1154 * \retval #PSA_ERROR_INVALID_ARGUMENT
1155 * \p key is not compatible with \p alg.
1156 * \retval #PSA_ERROR_NOT_SUPPORTED
1157 * \p alg is not supported or is not a MAC algorithm.
1158 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1159 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1160 * \retval #PSA_ERROR_HARDWARE_FAILURE
1161 * \retval #PSA_ERROR_TAMPERING_DETECTED
1162 * \retval #PSA_ERROR_BAD_STATE
1163 * The library has not been previously initialized by psa_crypto_init().
1164 * It is implementation-dependent whether a failure to initialize
1165 * results in this error code.
1166 */
1167psa_status_t psa_mac_compute(psa_key_handle_t handle,
1168 psa_algorithm_t alg,
1169 const uint8_t *input,
1170 size_t input_length,
1171 uint8_t *mac,
1172 size_t mac_size,
1173 size_t *mac_length);
1174
1175/** Calculate the MAC of a message and compare it with a reference value.
1176 *
1177 * \param handle Handle to the key to use for the operation.
1178 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001179 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001180 * \param[in] input Buffer containing the input message.
1181 * \param input_length Size of the \p input buffer in bytes.
1182 * \param[out] mac Buffer containing the expected MAC value.
1183 * \param mac_length Size of the \p mac buffer in bytes.
1184 *
1185 * \retval #PSA_SUCCESS
1186 * The expected MAC is identical to the actual MAC of the input.
1187 * \retval #PSA_ERROR_INVALID_SIGNATURE
1188 * The MAC of the message was calculated successfully, but it
1189 * differs from the expected value.
1190 * \retval #PSA_ERROR_INVALID_HANDLE
1191 * \retval #PSA_ERROR_EMPTY_SLOT
1192 * \retval #PSA_ERROR_NOT_PERMITTED
1193 * \retval #PSA_ERROR_INVALID_ARGUMENT
1194 * \p key is not compatible with \p alg.
1195 * \retval #PSA_ERROR_NOT_SUPPORTED
1196 * \p alg is not supported or is not a MAC algorithm.
1197 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1198 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1199 * \retval #PSA_ERROR_HARDWARE_FAILURE
1200 * \retval #PSA_ERROR_TAMPERING_DETECTED
1201 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001202psa_status_t psa_mac_verify(psa_key_handle_t handle,
1203 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001204 const uint8_t *input,
1205 size_t input_length,
1206 const uint8_t *mac,
1207 const size_t mac_length);
1208
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001209/** The type of the state data structure for multipart MAC operations.
1210 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001211 * Before calling any function on a MAC operation object, the application must
1212 * initialize it by any of the following means:
1213 * - Set the structure to all-bits-zero, for example:
1214 * \code
1215 * psa_mac_operation_t operation;
1216 * memset(&operation, 0, sizeof(operation));
1217 * \endcode
1218 * - Initialize the structure to logical zero values, for example:
1219 * \code
1220 * psa_mac_operation_t operation = {0};
1221 * \endcode
1222 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1223 * for example:
1224 * \code
1225 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1226 * \endcode
1227 * - Assign the result of the function psa_mac_operation_init()
1228 * to the structure, for example:
1229 * \code
1230 * psa_mac_operation_t operation;
1231 * operation = psa_mac_operation_init();
1232 * \endcode
1233 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001234 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001235 * make any assumptions about the content of this structure except
1236 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001237typedef struct psa_mac_operation_s psa_mac_operation_t;
1238
Jaeden Amero769ce272019-01-04 11:48:03 +00001239/** \def PSA_MAC_OPERATION_INIT
1240 *
1241 * This macro returns a suitable initializer for a MAC operation object of type
1242 * #psa_mac_operation_t.
1243 */
1244#ifdef __DOXYGEN_ONLY__
1245/* This is an example definition for documentation purposes.
1246 * Implementations should define a suitable value in `crypto_struct.h`.
1247 */
1248#define PSA_MAC_OPERATION_INIT {0}
1249#endif
1250
1251/** Return an initial value for a MAC operation object.
1252 */
1253static psa_mac_operation_t psa_mac_operation_init(void);
1254
Gilles Peskinef45adda2019-01-14 18:29:18 +01001255/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001256 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001257 * This function sets up the calculation of the MAC
1258 * (message authentication code) of a byte string.
1259 * To verify the MAC of a message against an
1260 * expected value, use psa_mac_verify_setup() instead.
1261 *
1262 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001263 * -# Allocate an operation object which will be passed to all the functions
1264 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001265 * -# Initialize the operation object with one of the methods described in the
1266 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001267 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001268 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1269 * of the message each time. The MAC that is calculated is the MAC
1270 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001271 * -# At the end of the message, call psa_mac_sign_finish() to finish
1272 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001273 *
1274 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001275 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001276 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001277 * After a successful call to psa_mac_sign_setup(), the application must
1278 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001279 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001280 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001281 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001282 * \param[in,out] operation The operation object to set up. It must have
1283 * been initialized as per the documentation for
1284 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001285 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001286 * It must remain valid until the operation
1287 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001288 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001289 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001290 *
Gilles Peskine28538492018-07-11 17:34:00 +02001291 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001292 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001293 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001294 * \retval #PSA_ERROR_EMPTY_SLOT
1295 * \retval #PSA_ERROR_NOT_PERMITTED
1296 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001297 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001298 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001299 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001300 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1301 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1302 * \retval #PSA_ERROR_HARDWARE_FAILURE
1303 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001304 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001305 * The library has not been previously initialized by psa_crypto_init().
1306 * It is implementation-dependent whether a failure to initialize
1307 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001308 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001309psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001310 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001311 psa_algorithm_t alg);
1312
Gilles Peskinef45adda2019-01-14 18:29:18 +01001313/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001314 *
1315 * This function sets up the verification of the MAC
1316 * (message authentication code) of a byte string against an expected value.
1317 *
1318 * The sequence of operations to verify a MAC is as follows:
1319 * -# Allocate an operation object which will be passed to all the functions
1320 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001321 * -# Initialize the operation object with one of the methods described in the
1322 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001323 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001324 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1325 * of the message each time. The MAC that is calculated is the MAC
1326 * of the concatenation of these messages in order.
1327 * -# At the end of the message, call psa_mac_verify_finish() to finish
1328 * calculating the actual MAC of the message and verify it against
1329 * the expected value.
1330 *
1331 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001332 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001333 *
1334 * After a successful call to psa_mac_verify_setup(), the application must
1335 * eventually terminate the operation through one of the following methods:
1336 * - A failed call to psa_mac_update().
1337 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1338 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001339 * \param[in,out] operation The operation object to set up. It must have
1340 * been initialized as per the documentation for
1341 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001342 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001343 * It must remain valid until the operation
1344 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001345 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1346 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001347 *
Gilles Peskine28538492018-07-11 17:34:00 +02001348 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001349 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001350 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001351 * \retval #PSA_ERROR_EMPTY_SLOT
1352 * \retval #PSA_ERROR_NOT_PERMITTED
1353 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001354 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001355 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001356 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001357 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1358 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1359 * \retval #PSA_ERROR_HARDWARE_FAILURE
1360 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001361 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001362 * The library has not been previously initialized by psa_crypto_init().
1363 * It is implementation-dependent whether a failure to initialize
1364 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001365 */
1366psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001367 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001368 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001369
Gilles Peskinedcd14942018-07-12 00:30:52 +02001370/** Add a message fragment to a multipart MAC operation.
1371 *
1372 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1373 * before calling this function.
1374 *
1375 * If this function returns an error status, the operation becomes inactive.
1376 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001377 * \param[in,out] operation Active MAC operation.
1378 * \param[in] input Buffer containing the message fragment to add to
1379 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001380 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001381 *
1382 * \retval #PSA_SUCCESS
1383 * Success.
1384 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001385 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001386 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1387 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1388 * \retval #PSA_ERROR_HARDWARE_FAILURE
1389 * \retval #PSA_ERROR_TAMPERING_DETECTED
1390 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001391psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1392 const uint8_t *input,
1393 size_t input_length);
1394
Gilles Peskinedcd14942018-07-12 00:30:52 +02001395/** Finish the calculation of the MAC of a message.
1396 *
1397 * The application must call psa_mac_sign_setup() before calling this function.
1398 * This function calculates the MAC of the message formed by concatenating
1399 * the inputs passed to preceding calls to psa_mac_update().
1400 *
1401 * When this function returns, the operation becomes inactive.
1402 *
1403 * \warning Applications should not call this function if they expect
1404 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1405 * Beware that comparing integrity or authenticity data such as
1406 * MAC values with a function such as \c memcmp is risky
1407 * because the time taken by the comparison may leak information
1408 * about the MAC value which could allow an attacker to guess
1409 * a valid MAC and thereby bypass security controls.
1410 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001411 * \param[in,out] operation Active MAC operation.
1412 * \param[out] mac Buffer where the MAC value is to be written.
1413 * \param mac_size Size of the \p mac buffer in bytes.
1414 * \param[out] mac_length On success, the number of bytes
1415 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001416 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001417 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001418 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001419 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001420 *
1421 * \retval #PSA_SUCCESS
1422 * Success.
1423 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001424 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001425 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001426 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001427 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1428 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1429 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1430 * \retval #PSA_ERROR_HARDWARE_FAILURE
1431 * \retval #PSA_ERROR_TAMPERING_DETECTED
1432 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001433psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1434 uint8_t *mac,
1435 size_t mac_size,
1436 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001437
Gilles Peskinedcd14942018-07-12 00:30:52 +02001438/** Finish the calculation of the MAC of a message and compare it with
1439 * an expected value.
1440 *
1441 * The application must call psa_mac_verify_setup() before calling this function.
1442 * This function calculates the MAC of the message formed by concatenating
1443 * the inputs passed to preceding calls to psa_mac_update(). It then
1444 * compares the calculated MAC with the expected MAC passed as a
1445 * parameter to this function.
1446 *
1447 * When this function returns, the operation becomes inactive.
1448 *
1449 * \note Implementations shall make the best effort to ensure that the
1450 * comparison between the actual MAC and the expected MAC is performed
1451 * in constant time.
1452 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001453 * \param[in,out] operation Active MAC operation.
1454 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001455 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001456 *
1457 * \retval #PSA_SUCCESS
1458 * The expected MAC is identical to the actual MAC of the message.
1459 * \retval #PSA_ERROR_INVALID_SIGNATURE
1460 * The MAC of the message was calculated successfully, but it
1461 * differs from the expected MAC.
1462 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001463 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001464 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1465 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1466 * \retval #PSA_ERROR_HARDWARE_FAILURE
1467 * \retval #PSA_ERROR_TAMPERING_DETECTED
1468 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001469psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1470 const uint8_t *mac,
1471 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001472
Gilles Peskinedcd14942018-07-12 00:30:52 +02001473/** Abort a MAC operation.
1474 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001475 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001476 * \p operation structure itself. Once aborted, the operation object
1477 * can be reused for another operation by calling
1478 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001479 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001480 * You may call this function any time after the operation object has
1481 * been initialized by any of the following methods:
1482 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1483 * it succeeds or not.
1484 * - Initializing the \c struct to all-bits-zero.
1485 * - Initializing the \c struct to logical zeros, e.g.
1486 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001487 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001488 * In particular, calling psa_mac_abort() after the operation has been
1489 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1490 * psa_mac_verify_finish() is safe and has no effect.
1491 *
1492 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001493 *
1494 * \retval #PSA_SUCCESS
1495 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001496 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001497 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1498 * \retval #PSA_ERROR_HARDWARE_FAILURE
1499 * \retval #PSA_ERROR_TAMPERING_DETECTED
1500 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001501psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1502
1503/**@}*/
1504
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001505/** \defgroup cipher Symmetric ciphers
1506 * @{
1507 */
1508
Gilles Peskine69647a42019-01-14 20:18:12 +01001509/** Encrypt a message using a symmetric cipher.
1510 *
1511 * This function encrypts a message with a random IV (initialization
1512 * vector).
1513 *
1514 * \param handle Handle to the key to use for the operation.
1515 * It must remain valid until the operation
1516 * terminates.
1517 * \param alg The cipher algorithm to compute
1518 * (\c PSA_ALG_XXX value such that
1519 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1520 * \param[in] input Buffer containing the message to encrypt.
1521 * \param input_length Size of the \p input buffer in bytes.
1522 * \param[out] output Buffer where the output is to be written.
1523 * The output contains the IV followed by
1524 * the ciphertext proper.
1525 * \param output_size Size of the \p output buffer in bytes.
1526 * \param[out] output_length On success, the number of bytes
1527 * that make up the output.
1528 *
1529 * \retval #PSA_SUCCESS
1530 * Success.
1531 * \retval #PSA_ERROR_INVALID_HANDLE
1532 * \retval #PSA_ERROR_EMPTY_SLOT
1533 * \retval #PSA_ERROR_NOT_PERMITTED
1534 * \retval #PSA_ERROR_INVALID_ARGUMENT
1535 * \p key is not compatible with \p alg.
1536 * \retval #PSA_ERROR_NOT_SUPPORTED
1537 * \p alg is not supported or is not a cipher algorithm.
1538 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1539 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1540 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1541 * \retval #PSA_ERROR_HARDWARE_FAILURE
1542 * \retval #PSA_ERROR_TAMPERING_DETECTED
1543 */
1544psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1545 psa_algorithm_t alg,
1546 const uint8_t *input,
1547 size_t input_length,
1548 uint8_t *output,
1549 size_t output_size,
1550 size_t *output_length);
1551
1552/** Decrypt a message using a symmetric cipher.
1553 *
1554 * This function decrypts a message encrypted with a symmetric cipher.
1555 *
1556 * \param handle Handle to the key to use for the operation.
1557 * It must remain valid until the operation
1558 * terminates.
1559 * \param alg The cipher algorithm to compute
1560 * (\c PSA_ALG_XXX value such that
1561 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1562 * \param[in] input Buffer containing the message to decrypt.
1563 * This consists of the IV followed by the
1564 * ciphertext proper.
1565 * \param input_length Size of the \p input buffer in bytes.
1566 * \param[out] output Buffer where the plaintext is to be written.
1567 * \param output_size Size of the \p output buffer in bytes.
1568 * \param[out] output_length On success, the number of bytes
1569 * that make up the output.
1570 *
1571 * \retval #PSA_SUCCESS
1572 * Success.
1573 * \retval #PSA_ERROR_INVALID_HANDLE
1574 * \retval #PSA_ERROR_EMPTY_SLOT
1575 * \retval #PSA_ERROR_NOT_PERMITTED
1576 * \retval #PSA_ERROR_INVALID_ARGUMENT
1577 * \p key is not compatible with \p alg.
1578 * \retval #PSA_ERROR_NOT_SUPPORTED
1579 * \p alg is not supported or is not a cipher algorithm.
1580 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1581 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1582 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1583 * \retval #PSA_ERROR_HARDWARE_FAILURE
1584 * \retval #PSA_ERROR_TAMPERING_DETECTED
1585 */
1586psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1587 psa_algorithm_t alg,
1588 const uint8_t *input,
1589 size_t input_length,
1590 uint8_t *output,
1591 size_t output_size,
1592 size_t *output_length);
1593
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001594/** The type of the state data structure for multipart cipher operations.
1595 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001596 * Before calling any function on a cipher operation object, the application
1597 * must initialize it by any of the following means:
1598 * - Set the structure to all-bits-zero, for example:
1599 * \code
1600 * psa_cipher_operation_t operation;
1601 * memset(&operation, 0, sizeof(operation));
1602 * \endcode
1603 * - Initialize the structure to logical zero values, for example:
1604 * \code
1605 * psa_cipher_operation_t operation = {0};
1606 * \endcode
1607 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1608 * for example:
1609 * \code
1610 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1611 * \endcode
1612 * - Assign the result of the function psa_cipher_operation_init()
1613 * to the structure, for example:
1614 * \code
1615 * psa_cipher_operation_t operation;
1616 * operation = psa_cipher_operation_init();
1617 * \endcode
1618 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001619 * This is an implementation-defined \c struct. Applications should not
1620 * make any assumptions about the content of this structure except
1621 * as directed by the documentation of a specific implementation. */
1622typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1623
Jaeden Amero5bae2272019-01-04 11:48:27 +00001624/** \def PSA_CIPHER_OPERATION_INIT
1625 *
1626 * This macro returns a suitable initializer for a cipher operation object of
1627 * type #psa_cipher_operation_t.
1628 */
1629#ifdef __DOXYGEN_ONLY__
1630/* This is an example definition for documentation purposes.
1631 * Implementations should define a suitable value in `crypto_struct.h`.
1632 */
1633#define PSA_CIPHER_OPERATION_INIT {0}
1634#endif
1635
1636/** Return an initial value for a cipher operation object.
1637 */
1638static psa_cipher_operation_t psa_cipher_operation_init(void);
1639
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001640/** Set the key for a multipart symmetric encryption operation.
1641 *
1642 * The sequence of operations to encrypt a message with a symmetric cipher
1643 * is as follows:
1644 * -# Allocate an operation object which will be passed to all the functions
1645 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001646 * -# Initialize the operation object with one of the methods described in the
1647 * documentation for #psa_cipher_operation_t, e.g.
1648 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001649 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001650 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001651 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001652 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001653 * requires a specific IV value.
1654 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1655 * of the message each time.
1656 * -# Call psa_cipher_finish().
1657 *
1658 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001659 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001660 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001661 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001662 * eventually terminate the operation. The following events terminate an
1663 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001664 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001665 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001666 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001667 * \param[in,out] operation The operation object to set up. It must have
1668 * been initialized as per the documentation for
1669 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001670 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001671 * It must remain valid until the operation
1672 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001673 * \param alg The cipher algorithm to compute
1674 * (\c PSA_ALG_XXX value such that
1675 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001676 *
Gilles Peskine28538492018-07-11 17:34:00 +02001677 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001678 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001679 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001680 * \retval #PSA_ERROR_EMPTY_SLOT
1681 * \retval #PSA_ERROR_NOT_PERMITTED
1682 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001683 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001684 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001685 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001686 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1687 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1688 * \retval #PSA_ERROR_HARDWARE_FAILURE
1689 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001690 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001691 * The library has not been previously initialized by psa_crypto_init().
1692 * It is implementation-dependent whether a failure to initialize
1693 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001694 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001695psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001696 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001697 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001698
1699/** Set the key for a multipart symmetric decryption operation.
1700 *
1701 * The sequence of operations to decrypt a message with a symmetric cipher
1702 * is as follows:
1703 * -# Allocate an operation object which will be passed to all the functions
1704 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001705 * -# Initialize the operation object with one of the methods described in the
1706 * documentation for #psa_cipher_operation_t, e.g.
1707 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001708 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001709 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001710 * decryption. If the IV is prepended to the ciphertext, you can call
1711 * psa_cipher_update() on a buffer containing the IV followed by the
1712 * beginning of the message.
1713 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1714 * of the message each time.
1715 * -# Call psa_cipher_finish().
1716 *
1717 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001718 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001719 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001720 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001721 * eventually terminate the operation. The following events terminate an
1722 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001723 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001724 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001725 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001726 * \param[in,out] operation The operation object to set up. It must have
1727 * been initialized as per the documentation for
1728 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001729 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001730 * It must remain valid until the operation
1731 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001732 * \param alg The cipher algorithm to compute
1733 * (\c PSA_ALG_XXX value such that
1734 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001735 *
Gilles Peskine28538492018-07-11 17:34:00 +02001736 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001737 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001738 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001739 * \retval #PSA_ERROR_EMPTY_SLOT
1740 * \retval #PSA_ERROR_NOT_PERMITTED
1741 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001742 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001743 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001744 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001745 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1746 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1747 * \retval #PSA_ERROR_HARDWARE_FAILURE
1748 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001749 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001750 * The library has not been previously initialized by psa_crypto_init().
1751 * It is implementation-dependent whether a failure to initialize
1752 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001753 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001754psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001755 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001756 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001757
Gilles Peskinedcd14942018-07-12 00:30:52 +02001758/** Generate an IV for a symmetric encryption operation.
1759 *
1760 * This function generates a random IV (initialization vector), nonce
1761 * or initial counter value for the encryption operation as appropriate
1762 * for the chosen algorithm, key type and key size.
1763 *
1764 * The application must call psa_cipher_encrypt_setup() before
1765 * calling this function.
1766 *
1767 * If this function returns an error status, the operation becomes inactive.
1768 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001769 * \param[in,out] operation Active cipher operation.
1770 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001771 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001772 * \param[out] iv_length On success, the number of bytes of the
1773 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001774 *
1775 * \retval #PSA_SUCCESS
1776 * Success.
1777 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001778 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001779 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001780 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001781 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1782 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1783 * \retval #PSA_ERROR_HARDWARE_FAILURE
1784 * \retval #PSA_ERROR_TAMPERING_DETECTED
1785 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001786psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1787 unsigned char *iv,
1788 size_t iv_size,
1789 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001790
Gilles Peskinedcd14942018-07-12 00:30:52 +02001791/** Set the IV for a symmetric encryption or decryption operation.
1792 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001793 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001794 * or initial counter value for the encryption or decryption operation.
1795 *
1796 * The application must call psa_cipher_encrypt_setup() before
1797 * calling this function.
1798 *
1799 * If this function returns an error status, the operation becomes inactive.
1800 *
1801 * \note When encrypting, applications should use psa_cipher_generate_iv()
1802 * instead of this function, unless implementing a protocol that requires
1803 * a non-random IV.
1804 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001805 * \param[in,out] operation Active cipher operation.
1806 * \param[in] iv Buffer containing the IV to use.
1807 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001808 *
1809 * \retval #PSA_SUCCESS
1810 * Success.
1811 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001812 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001813 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001814 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001815 * or the chosen algorithm does not use an IV.
1816 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1817 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1818 * \retval #PSA_ERROR_HARDWARE_FAILURE
1819 * \retval #PSA_ERROR_TAMPERING_DETECTED
1820 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001821psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1822 const unsigned char *iv,
1823 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001824
Gilles Peskinedcd14942018-07-12 00:30:52 +02001825/** Encrypt or decrypt a message fragment in an active cipher operation.
1826 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001827 * Before calling this function, you must:
1828 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1829 * The choice of setup function determines whether this function
1830 * encrypts or decrypts its input.
1831 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1832 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001833 *
1834 * If this function returns an error status, the operation becomes inactive.
1835 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001836 * \param[in,out] operation Active cipher operation.
1837 * \param[in] input Buffer containing the message fragment to
1838 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001839 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001840 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001841 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001842 * \param[out] output_length On success, the number of bytes
1843 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001844 *
1845 * \retval #PSA_SUCCESS
1846 * Success.
1847 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001848 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001849 * not set, or already completed).
1850 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1851 * The size of the \p output buffer is too small.
1852 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1853 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1854 * \retval #PSA_ERROR_HARDWARE_FAILURE
1855 * \retval #PSA_ERROR_TAMPERING_DETECTED
1856 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001857psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1858 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001859 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001860 unsigned char *output,
1861 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001862 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001863
Gilles Peskinedcd14942018-07-12 00:30:52 +02001864/** Finish encrypting or decrypting a message in a cipher operation.
1865 *
1866 * The application must call psa_cipher_encrypt_setup() or
1867 * psa_cipher_decrypt_setup() before calling this function. The choice
1868 * of setup function determines whether this function encrypts or
1869 * decrypts its input.
1870 *
1871 * This function finishes the encryption or decryption of the message
1872 * formed by concatenating the inputs passed to preceding calls to
1873 * psa_cipher_update().
1874 *
1875 * When this function returns, the operation becomes inactive.
1876 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001877 * \param[in,out] operation Active cipher operation.
1878 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001879 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001880 * \param[out] output_length On success, the number of bytes
1881 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001882 *
1883 * \retval #PSA_SUCCESS
1884 * Success.
1885 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001886 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001887 * not set, or already completed).
1888 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1889 * The size of the \p output buffer is too small.
1890 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1891 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1892 * \retval #PSA_ERROR_HARDWARE_FAILURE
1893 * \retval #PSA_ERROR_TAMPERING_DETECTED
1894 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001895psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001896 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001897 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001898 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001899
Gilles Peskinedcd14942018-07-12 00:30:52 +02001900/** Abort a cipher operation.
1901 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001902 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001903 * \p operation structure itself. Once aborted, the operation object
1904 * can be reused for another operation by calling
1905 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001906 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001907 * You may call this function any time after the operation object has
1908 * been initialized by any of the following methods:
1909 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1910 * whether it succeeds or not.
1911 * - Initializing the \c struct to all-bits-zero.
1912 * - Initializing the \c struct to logical zeros, e.g.
1913 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001914 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001915 * In particular, calling psa_cipher_abort() after the operation has been
1916 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1917 * is safe and has no effect.
1918 *
1919 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001920 *
1921 * \retval #PSA_SUCCESS
1922 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001923 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001924 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1925 * \retval #PSA_ERROR_HARDWARE_FAILURE
1926 * \retval #PSA_ERROR_TAMPERING_DETECTED
1927 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001928psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1929
1930/**@}*/
1931
Gilles Peskine3b555712018-03-03 21:27:57 +01001932/** \defgroup aead Authenticated encryption with associated data (AEAD)
1933 * @{
1934 */
1935
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001936/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001937 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001938 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001939 * \param alg The AEAD algorithm to compute
1940 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001941 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001942 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001943 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001944 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001945 * but not encrypted.
1946 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001947 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001948 * encrypted.
1949 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001950 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001951 * encrypted data. The additional data is not
1952 * part of this output. For algorithms where the
1953 * encrypted data and the authentication tag
1954 * are defined as separate outputs, the
1955 * authentication tag is appended to the
1956 * encrypted data.
1957 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1958 * This must be at least
1959 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1960 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001961 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001962 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001963 *
Gilles Peskine28538492018-07-11 17:34:00 +02001964 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001965 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001966 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001967 * \retval #PSA_ERROR_EMPTY_SLOT
1968 * \retval #PSA_ERROR_NOT_PERMITTED
1969 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001970 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001971 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001972 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001973 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1974 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1975 * \retval #PSA_ERROR_HARDWARE_FAILURE
1976 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001977 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001978 * The library has not been previously initialized by psa_crypto_init().
1979 * It is implementation-dependent whether a failure to initialize
1980 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001981 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001982psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001983 psa_algorithm_t alg,
1984 const uint8_t *nonce,
1985 size_t nonce_length,
1986 const uint8_t *additional_data,
1987 size_t additional_data_length,
1988 const uint8_t *plaintext,
1989 size_t plaintext_length,
1990 uint8_t *ciphertext,
1991 size_t ciphertext_size,
1992 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001993
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001994/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001995 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001996 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001997 * \param alg The AEAD algorithm to compute
1998 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001999 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002000 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002001 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002002 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002003 * but not encrypted.
2004 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002005 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002006 * encrypted. For algorithms where the
2007 * encrypted data and the authentication tag
2008 * are defined as separate inputs, the buffer
2009 * must contain the encrypted data followed
2010 * by the authentication tag.
2011 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002012 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002013 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2014 * This must be at least
2015 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2016 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002017 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002018 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002019 *
Gilles Peskine28538492018-07-11 17:34:00 +02002020 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002021 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002022 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002023 * \retval #PSA_ERROR_EMPTY_SLOT
2024 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002025 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002026 * \retval #PSA_ERROR_NOT_PERMITTED
2027 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002028 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002029 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002030 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002031 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2032 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2033 * \retval #PSA_ERROR_HARDWARE_FAILURE
2034 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002035 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002036 * The library has not been previously initialized by psa_crypto_init().
2037 * It is implementation-dependent whether a failure to initialize
2038 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002039 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002040psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002041 psa_algorithm_t alg,
2042 const uint8_t *nonce,
2043 size_t nonce_length,
2044 const uint8_t *additional_data,
2045 size_t additional_data_length,
2046 const uint8_t *ciphertext,
2047 size_t ciphertext_length,
2048 uint8_t *plaintext,
2049 size_t plaintext_size,
2050 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002051
Gilles Peskine30a9e412019-01-14 18:36:12 +01002052/** The type of the state data structure for multipart AEAD operations.
2053 *
2054 * Before calling any function on an AEAD operation object, the application
2055 * must initialize it by any of the following means:
2056 * - Set the structure to all-bits-zero, for example:
2057 * \code
2058 * psa_aead_operation_t operation;
2059 * memset(&operation, 0, sizeof(operation));
2060 * \endcode
2061 * - Initialize the structure to logical zero values, for example:
2062 * \code
2063 * psa_aead_operation_t operation = {0};
2064 * \endcode
2065 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2066 * for example:
2067 * \code
2068 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2069 * \endcode
2070 * - Assign the result of the function psa_aead_operation_init()
2071 * to the structure, for example:
2072 * \code
2073 * psa_aead_operation_t operation;
2074 * operation = psa_aead_operation_init();
2075 * \endcode
2076 *
2077 * This is an implementation-defined \c struct. Applications should not
2078 * make any assumptions about the content of this structure except
2079 * as directed by the documentation of a specific implementation. */
2080typedef struct psa_aead_operation_s psa_aead_operation_t;
2081
2082/** \def PSA_AEAD_OPERATION_INIT
2083 *
2084 * This macro returns a suitable initializer for an AEAD operation object of
2085 * type #psa_aead_operation_t.
2086 */
2087#ifdef __DOXYGEN_ONLY__
2088/* This is an example definition for documentation purposes.
2089 * Implementations should define a suitable value in `crypto_struct.h`.
2090 */
2091#define PSA_AEAD_OPERATION_INIT {0}
2092#endif
2093
2094/** Return an initial value for an AEAD operation object.
2095 */
2096static psa_aead_operation_t psa_aead_operation_init(void);
2097
2098/** Set the key for a multipart authenticated encryption operation.
2099 *
2100 * The sequence of operations to encrypt a message with authentication
2101 * is as follows:
2102 * -# Allocate an operation object which will be passed to all the functions
2103 * listed here.
2104 * -# Initialize the operation object with one of the methods described in the
2105 * documentation for #psa_aead_operation_t, e.g.
2106 * PSA_AEAD_OPERATION_INIT.
2107 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002108 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2109 * inputs to the subsequent calls to psa_aead_update_ad() and
2110 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2111 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002112 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2113 * generate or set the nonce. You should use
2114 * psa_aead_generate_nonce() unless the protocol you are implementing
2115 * requires a specific nonce value.
2116 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2117 * of the non-encrypted additional authenticated data each time.
2118 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002119 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002120 * -# Call psa_aead_finish().
2121 *
2122 * The application may call psa_aead_abort() at any time after the operation
2123 * has been initialized.
2124 *
2125 * After a successful call to psa_aead_encrypt_setup(), the application must
2126 * eventually terminate the operation. The following events terminate an
2127 * operation:
2128 * - A failed call to any of the \c psa_aead_xxx functions.
2129 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2130 *
2131 * \param[in,out] operation The operation object to set up. It must have
2132 * been initialized as per the documentation for
2133 * #psa_aead_operation_t and not yet in use.
2134 * \param handle Handle to the key to use for the operation.
2135 * It must remain valid until the operation
2136 * terminates.
2137 * \param alg The AEAD algorithm to compute
2138 * (\c PSA_ALG_XXX value such that
2139 * #PSA_ALG_IS_AEAD(\p alg) is true).
2140 *
2141 * \retval #PSA_SUCCESS
2142 * Success.
2143 * \retval #PSA_ERROR_INVALID_HANDLE
2144 * \retval #PSA_ERROR_EMPTY_SLOT
2145 * \retval #PSA_ERROR_NOT_PERMITTED
2146 * \retval #PSA_ERROR_INVALID_ARGUMENT
2147 * \p key is not compatible with \p alg.
2148 * \retval #PSA_ERROR_NOT_SUPPORTED
2149 * \p alg is not supported or is not an AEAD algorithm.
2150 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2151 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2152 * \retval #PSA_ERROR_HARDWARE_FAILURE
2153 * \retval #PSA_ERROR_TAMPERING_DETECTED
2154 * \retval #PSA_ERROR_BAD_STATE
2155 * The library has not been previously initialized by psa_crypto_init().
2156 * It is implementation-dependent whether a failure to initialize
2157 * results in this error code.
2158 */
2159psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2160 psa_key_handle_t handle,
2161 psa_algorithm_t alg);
2162
2163/** Set the key for a multipart authenticated decryption operation.
2164 *
2165 * The sequence of operations to decrypt a message with authentication
2166 * is as follows:
2167 * -# Allocate an operation object which will be passed to all the functions
2168 * listed here.
2169 * -# Initialize the operation object with one of the methods described in the
2170 * documentation for #psa_aead_operation_t, e.g.
2171 * PSA_AEAD_OPERATION_INIT.
2172 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002173 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2174 * inputs to the subsequent calls to psa_aead_update_ad() and
2175 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2176 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002177 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2178 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2179 * of the non-encrypted additional authenticated data each time.
2180 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002181 * of the ciphertext to decrypt each time.
2182 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002183 *
2184 * The application may call psa_aead_abort() at any time after the operation
2185 * has been initialized.
2186 *
2187 * After a successful call to psa_aead_decrypt_setup(), the application must
2188 * eventually terminate the operation. The following events terminate an
2189 * operation:
2190 * - A failed call to any of the \c psa_aead_xxx functions.
2191 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2192 *
2193 * \param[in,out] operation The operation object to set up. It must have
2194 * been initialized as per the documentation for
2195 * #psa_aead_operation_t and not yet in use.
2196 * \param handle Handle to the key to use for the operation.
2197 * It must remain valid until the operation
2198 * terminates.
2199 * \param alg The AEAD algorithm to compute
2200 * (\c PSA_ALG_XXX value such that
2201 * #PSA_ALG_IS_AEAD(\p alg) is true).
2202 *
2203 * \retval #PSA_SUCCESS
2204 * Success.
2205 * \retval #PSA_ERROR_INVALID_HANDLE
2206 * \retval #PSA_ERROR_EMPTY_SLOT
2207 * \retval #PSA_ERROR_NOT_PERMITTED
2208 * \retval #PSA_ERROR_INVALID_ARGUMENT
2209 * \p key is not compatible with \p alg.
2210 * \retval #PSA_ERROR_NOT_SUPPORTED
2211 * \p alg is not supported or is not an AEAD algorithm.
2212 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2213 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2214 * \retval #PSA_ERROR_HARDWARE_FAILURE
2215 * \retval #PSA_ERROR_TAMPERING_DETECTED
2216 * \retval #PSA_ERROR_BAD_STATE
2217 * The library has not been previously initialized by psa_crypto_init().
2218 * It is implementation-dependent whether a failure to initialize
2219 * results in this error code.
2220 */
2221psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2222 psa_key_handle_t handle,
2223 psa_algorithm_t alg);
2224
2225/** Generate a random nonce for an authenticated encryption operation.
2226 *
2227 * This function generates a random nonce for the authenticated encryption
2228 * operation with an appropriate size for the chosen algorithm, key type
2229 * and key size.
2230 *
2231 * The application must call psa_aead_encrypt_setup() before
2232 * calling this function.
2233 *
2234 * If this function returns an error status, the operation becomes inactive.
2235 *
2236 * \param[in,out] operation Active AEAD operation.
2237 * \param[out] nonce Buffer where the generated nonce is to be
2238 * written.
2239 * \param nonce_size Size of the \p nonce buffer in bytes.
2240 * \param[out] nonce_length On success, the number of bytes of the
2241 * generated nonce.
2242 *
2243 * \retval #PSA_SUCCESS
2244 * Success.
2245 * \retval #PSA_ERROR_BAD_STATE
2246 * The operation state is not valid (not set up, or nonce already set).
2247 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2248 * The size of the \p nonce buffer is too small.
2249 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2250 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2251 * \retval #PSA_ERROR_HARDWARE_FAILURE
2252 * \retval #PSA_ERROR_TAMPERING_DETECTED
2253 */
2254psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2255 unsigned char *nonce,
2256 size_t nonce_size,
2257 size_t *nonce_length);
2258
2259/** Set the nonce for an authenticated encryption or decryption operation.
2260 *
2261 * This function sets the nonce for the authenticated
2262 * encryption or decryption operation.
2263 *
2264 * The application must call psa_aead_encrypt_setup() before
2265 * calling this function.
2266 *
2267 * If this function returns an error status, the operation becomes inactive.
2268 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002269 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002270 * instead of this function, unless implementing a protocol that requires
2271 * a non-random IV.
2272 *
2273 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002274 * \param[in] nonce Buffer containing the nonce to use.
2275 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002276 *
2277 * \retval #PSA_SUCCESS
2278 * Success.
2279 * \retval #PSA_ERROR_BAD_STATE
2280 * The operation state is not valid (not set up, or nonce already set).
2281 * \retval #PSA_ERROR_INVALID_ARGUMENT
2282 * The size of \p nonce is not acceptable for the chosen algorithm.
2283 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2284 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2285 * \retval #PSA_ERROR_HARDWARE_FAILURE
2286 * \retval #PSA_ERROR_TAMPERING_DETECTED
2287 */
2288psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2289 const unsigned char *nonce,
2290 size_t nonce_length);
2291
Gilles Peskinebc59c852019-01-17 15:26:08 +01002292/** Declare the lengths of the message and additional data for AEAD.
2293 *
2294 * The application must call this function before calling
2295 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2296 * the operation requires it. If the algorithm does not require it,
2297 * calling this function is optional, but if this function is called
2298 * then the implementation must enforce the lengths.
2299 *
2300 * You may call this function before or after setting the nonce with
2301 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2302 *
2303 * - For #PSA_ALG_CCM, calling this function is required.
2304 * - For the other AEAD algorithms defined in this specification, calling
2305 * this function is not required.
2306 * - For vendor-defined algorithm, refer to the vendor documentation.
2307 *
2308 * \param[in,out] operation Active AEAD operation.
2309 * \param ad_length Size of the non-encrypted additional
2310 * authenticated data in bytes.
2311 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2312 *
2313 * \retval #PSA_SUCCESS
2314 * Success.
2315 * \retval #PSA_ERROR_BAD_STATE
2316 * The operation state is not valid (not set up, already completed,
2317 * or psa_aead_update_ad() or psa_aead_update() already called).
2318 * \retval #PSA_ERROR_INVALID_ARGUMENT
2319 * At least one of the lengths is not acceptable for the chosen
2320 * algorithm.
2321 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2322 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2323 * \retval #PSA_ERROR_HARDWARE_FAILURE
2324 * \retval #PSA_ERROR_TAMPERING_DETECTED
2325 */
2326psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2327 size_t ad_length,
2328 size_t plaintext_length);
2329
Gilles Peskine30a9e412019-01-14 18:36:12 +01002330/** Pass additional data to an active AEAD operation.
2331 *
2332 * Additional data is authenticated, but not encrypted.
2333 *
2334 * You may call this function multiple times to pass successive fragments
2335 * of the additional data. You may not call this function after passing
2336 * data to encrypt or decrypt with psa_aead_update().
2337 *
2338 * Before calling this function, you must:
2339 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2340 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2341 *
2342 * If this function returns an error status, the operation becomes inactive.
2343 *
2344 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2345 * there is no guarantee that the input is valid. Therefore, until
2346 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2347 * treat the input as untrusted and prepare to undo any action that
2348 * depends on the input if psa_aead_verify() returns an error status.
2349 *
2350 * \param[in,out] operation Active AEAD operation.
2351 * \param[in] input Buffer containing the fragment of
2352 * additional data.
2353 * \param input_length Size of the \p input buffer in bytes.
2354 *
2355 * \retval #PSA_SUCCESS
2356 * Success.
2357 * \retval #PSA_ERROR_BAD_STATE
2358 * The operation state is not valid (not set up, nonce not set,
2359 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002360 * \retval #PSA_ERROR_INVALID_ARGUMENT
2361 * The total input length overflows the additional data length that
2362 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002363 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2364 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2365 * \retval #PSA_ERROR_HARDWARE_FAILURE
2366 * \retval #PSA_ERROR_TAMPERING_DETECTED
2367 */
2368psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2369 const uint8_t *input,
2370 size_t input_length);
2371
2372/** Encrypt or decrypt a message fragment in an active AEAD operation.
2373 *
2374 * Before calling this function, you must:
2375 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2376 * The choice of setup function determines whether this function
2377 * encrypts or decrypts its input.
2378 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2379 * 3. Call psa_aead_update_ad() to pass all the additional data.
2380 *
2381 * If this function returns an error status, the operation becomes inactive.
2382 *
2383 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2384 * there is no guarantee that the input is valid. Therefore, until
2385 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2386 * - Do not use the output in any way other than storing it in a
2387 * confidential location. If you take any action that depends
2388 * on the tentative decrypted data, this action will need to be
2389 * undone if the input turns out not to be valid. Furthermore,
2390 * if an adversary can observe that this action took place
2391 * (for example through timing), they may be able to use this
2392 * fact as an oracle to decrypt any message encrypted with the
2393 * same key.
2394 * - In particular, do not copy the output anywhere but to a
2395 * memory or storage space that you have exclusive access to.
2396 *
2397 * \param[in,out] operation Active AEAD operation.
2398 * \param[in] input Buffer containing the message fragment to
2399 * encrypt or decrypt.
2400 * \param input_length Size of the \p input buffer in bytes.
2401 * \param[out] output Buffer where the output is to be written.
2402 * \param output_size Size of the \p output buffer in bytes.
2403 * \param[out] output_length On success, the number of bytes
2404 * that make up the returned output.
2405 *
2406 * \retval #PSA_SUCCESS
2407 * Success.
2408 * \retval #PSA_ERROR_BAD_STATE
2409 * The operation state is not valid (not set up, nonce not set
2410 * or already completed).
2411 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2412 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002413 * \retval #PSA_ERROR_INVALID_ARGUMENT
2414 * The total length of input to psa_aead_update_ad() so far is
2415 * less than the additional data length that was previously
2416 * specified with psa_aead_set_lengths().
2417 * \retval #PSA_ERROR_INVALID_ARGUMENT
2418 * The total input length overflows the plaintext length that
2419 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002420 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2421 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2422 * \retval #PSA_ERROR_HARDWARE_FAILURE
2423 * \retval #PSA_ERROR_TAMPERING_DETECTED
2424 */
2425psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2426 const uint8_t *input,
2427 size_t input_length,
2428 unsigned char *output,
2429 size_t output_size,
2430 size_t *output_length);
2431
2432/** Finish encrypting a message in an AEAD operation.
2433 *
2434 * The operation must have been set up with psa_aead_encrypt_setup().
2435 *
2436 * This function finishes the authentication of the additional data
2437 * formed by concatenating the inputs passed to preceding calls to
2438 * psa_aead_update_ad() with the plaintext formed by concatenating the
2439 * inputs passed to preceding calls to psa_aead_update().
2440 *
2441 * This function has two output buffers:
2442 * - \p ciphertext contains trailing ciphertext that was buffered from
2443 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2444 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2445 * will not contain any output and can be a 0-sized buffer.
2446 * - \p tag contains the authentication tag. Its length is always
2447 * #PSA_AEAD_TAG_LENGTH(\p alg) where \p alg is the AEAD algorithm
2448 * that the operation performs.
2449 *
2450 * When this function returns, the operation becomes inactive.
2451 *
2452 * \param[in,out] operation Active AEAD operation.
2453 * \param[out] ciphertext Buffer where the last part of the ciphertext
2454 * is to be written.
2455 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2456 * \param[out] ciphertext_length On success, the number of bytes of
2457 * returned ciphertext.
2458 * \param[out] tag Buffer where the authentication tag is
2459 * to be written.
2460 * \param tag_size Size of the \p tag buffer in bytes.
2461 * \param[out] tag_length On success, the number of bytes
2462 * that make up the returned tag.
2463 *
2464 * \retval #PSA_SUCCESS
2465 * Success.
2466 * \retval #PSA_ERROR_BAD_STATE
2467 * The operation state is not valid (not set up, nonce not set,
2468 * decryption, or already completed).
2469 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2470 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002471 * \retval #PSA_ERROR_INVALID_ARGUMENT
2472 * The total length of input to psa_aead_update_ad() so far is
2473 * less than the additional data length that was previously
2474 * specified with psa_aead_set_lengths().
2475 * \retval #PSA_ERROR_INVALID_ARGUMENT
2476 * The total length of input to psa_aead_update() so far is
2477 * less than the plaintext length that was previously
2478 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002479 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2480 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2481 * \retval #PSA_ERROR_HARDWARE_FAILURE
2482 * \retval #PSA_ERROR_TAMPERING_DETECTED
2483 */
2484psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002485 uint8_t *ciphertext,
2486 size_t ciphertext_size,
2487 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002488 uint8_t *tag,
2489 size_t tag_size,
2490 size_t *tag_length);
2491
2492/** Finish authenticating and decrypting a message in an AEAD operation.
2493 *
2494 * The operation must have been set up with psa_aead_decrypt_setup().
2495 *
2496 * This function finishes the authentication of the additional data
2497 * formed by concatenating the inputs passed to preceding calls to
2498 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2499 * inputs passed to preceding calls to psa_aead_update().
2500 *
2501 * When this function returns, the operation becomes inactive.
2502 *
2503 * \param[in,out] operation Active AEAD operation.
2504 * \param[in] tag Buffer containing the authentication tag.
2505 * \param tag_length Size of the \p tag buffer in bytes.
2506 *
2507 * \retval #PSA_SUCCESS
2508 * Success.
2509 * \retval #PSA_ERROR_BAD_STATE
2510 * The operation state is not valid (not set up, nonce not set,
2511 * encryption, or already completed).
2512 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2513 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002514 * \retval #PSA_ERROR_INVALID_ARGUMENT
2515 * The total length of input to psa_aead_update_ad() so far is
2516 * less than the additional data length that was previously
2517 * specified with psa_aead_set_lengths().
2518 * \retval #PSA_ERROR_INVALID_ARGUMENT
2519 * The total length of input to psa_aead_update() so far is
2520 * less than the plaintext length that was previously
2521 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002522 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2523 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2524 * \retval #PSA_ERROR_HARDWARE_FAILURE
2525 * \retval #PSA_ERROR_TAMPERING_DETECTED
2526 */
2527psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2528 const uint8_t *tag,
2529 size_t tag_length);
2530
2531/** Abort an AEAD operation.
2532 *
2533 * Aborting an operation frees all associated resources except for the
2534 * \p operation structure itself. Once aborted, the operation object
2535 * can be reused for another operation by calling
2536 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2537 *
2538 * You may call this function any time after the operation object has
2539 * been initialized by any of the following methods:
2540 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2541 * whether it succeeds or not.
2542 * - Initializing the \c struct to all-bits-zero.
2543 * - Initializing the \c struct to logical zeros, e.g.
2544 * `psa_aead_operation_t operation = {0}`.
2545 *
2546 * In particular, calling psa_aead_abort() after the operation has been
2547 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2548 * is safe and has no effect.
2549 *
2550 * \param[in,out] operation Initialized AEAD operation.
2551 *
2552 * \retval #PSA_SUCCESS
2553 * \retval #PSA_ERROR_BAD_STATE
2554 * \p operation is not an active AEAD operation.
2555 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2556 * \retval #PSA_ERROR_HARDWARE_FAILURE
2557 * \retval #PSA_ERROR_TAMPERING_DETECTED
2558 */
2559psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2560
Gilles Peskine3b555712018-03-03 21:27:57 +01002561/**@}*/
2562
Gilles Peskine20035e32018-02-03 22:44:14 +01002563/** \defgroup asymmetric Asymmetric cryptography
2564 * @{
2565 */
2566
2567/**
2568 * \brief Sign a hash or short message with a private key.
2569 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002570 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002571 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002572 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2573 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2574 * to determine the hash algorithm to use.
2575 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002576 * \param handle Handle to the key to use for the operation.
2577 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002578 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002579 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002580 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002581 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002582 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002583 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002584 * \param[out] signature_length On success, the number of bytes
2585 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002586 *
Gilles Peskine28538492018-07-11 17:34:00 +02002587 * \retval #PSA_SUCCESS
2588 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002589 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002590 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002591 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002592 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002593 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002594 * \retval #PSA_ERROR_NOT_SUPPORTED
2595 * \retval #PSA_ERROR_INVALID_ARGUMENT
2596 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2597 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2598 * \retval #PSA_ERROR_HARDWARE_FAILURE
2599 * \retval #PSA_ERROR_TAMPERING_DETECTED
2600 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002601 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002602 * The library has not been previously initialized by psa_crypto_init().
2603 * It is implementation-dependent whether a failure to initialize
2604 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002605 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002606psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002607 psa_algorithm_t alg,
2608 const uint8_t *hash,
2609 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002610 uint8_t *signature,
2611 size_t signature_size,
2612 size_t *signature_length);
2613
2614/**
2615 * \brief Verify the signature a hash or short message using a public key.
2616 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002617 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002618 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002619 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2620 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2621 * to determine the hash algorithm to use.
2622 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002623 * \param handle Handle to the key to use for the operation.
2624 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002625 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002626 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002627 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002628 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002629 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002630 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002631 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002632 *
Gilles Peskine28538492018-07-11 17:34:00 +02002633 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002634 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002635 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002636 * The calculation was perfomed successfully, but the passed
2637 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002638 * \retval #PSA_ERROR_NOT_SUPPORTED
2639 * \retval #PSA_ERROR_INVALID_ARGUMENT
2640 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2641 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2642 * \retval #PSA_ERROR_HARDWARE_FAILURE
2643 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002644 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002645 * The library has not been previously initialized by psa_crypto_init().
2646 * It is implementation-dependent whether a failure to initialize
2647 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002648 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002649psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002650 psa_algorithm_t alg,
2651 const uint8_t *hash,
2652 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002653 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002654 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002655
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002656/**
2657 * \brief Encrypt a short message with a public key.
2658 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002659 * \param handle Handle to the key to use for the operation.
2660 * It must be a public key or an asymmetric
2661 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002662 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002663 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002664 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002665 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002666 * \param[in] salt A salt or label, if supported by the
2667 * encryption algorithm.
2668 * If the algorithm does not support a
2669 * salt, pass \c NULL.
2670 * If the algorithm supports an optional
2671 * salt and you do not want to pass a salt,
2672 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002673 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002674 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2675 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002676 * \param salt_length Size of the \p salt buffer in bytes.
2677 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002678 * \param[out] output Buffer where the encrypted message is to
2679 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002680 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002681 * \param[out] output_length On success, the number of bytes
2682 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002683 *
Gilles Peskine28538492018-07-11 17:34:00 +02002684 * \retval #PSA_SUCCESS
2685 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002686 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002687 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002688 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002689 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002690 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002691 * \retval #PSA_ERROR_NOT_SUPPORTED
2692 * \retval #PSA_ERROR_INVALID_ARGUMENT
2693 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2694 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2695 * \retval #PSA_ERROR_HARDWARE_FAILURE
2696 * \retval #PSA_ERROR_TAMPERING_DETECTED
2697 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002698 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002699 * The library has not been previously initialized by psa_crypto_init().
2700 * It is implementation-dependent whether a failure to initialize
2701 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002702 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002703psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002704 psa_algorithm_t alg,
2705 const uint8_t *input,
2706 size_t input_length,
2707 const uint8_t *salt,
2708 size_t salt_length,
2709 uint8_t *output,
2710 size_t output_size,
2711 size_t *output_length);
2712
2713/**
2714 * \brief Decrypt a short message with a private key.
2715 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002716 * \param handle Handle to the key to use for the operation.
2717 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002718 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002719 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002720 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002721 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002722 * \param[in] salt A salt or label, if supported by the
2723 * encryption algorithm.
2724 * If the algorithm does not support a
2725 * salt, pass \c NULL.
2726 * If the algorithm supports an optional
2727 * salt and you do not want to pass a salt,
2728 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002729 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002730 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2731 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002732 * \param salt_length Size of the \p salt buffer in bytes.
2733 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002734 * \param[out] output Buffer where the decrypted message is to
2735 * be written.
2736 * \param output_size Size of the \c output buffer in bytes.
2737 * \param[out] output_length On success, the number of bytes
2738 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002739 *
Gilles Peskine28538492018-07-11 17:34:00 +02002740 * \retval #PSA_SUCCESS
2741 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002742 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002743 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002744 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002745 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002746 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002747 * \retval #PSA_ERROR_NOT_SUPPORTED
2748 * \retval #PSA_ERROR_INVALID_ARGUMENT
2749 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2750 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2751 * \retval #PSA_ERROR_HARDWARE_FAILURE
2752 * \retval #PSA_ERROR_TAMPERING_DETECTED
2753 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2754 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002755 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002756 * The library has not been previously initialized by psa_crypto_init().
2757 * It is implementation-dependent whether a failure to initialize
2758 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002759 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002760psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002761 psa_algorithm_t alg,
2762 const uint8_t *input,
2763 size_t input_length,
2764 const uint8_t *salt,
2765 size_t salt_length,
2766 uint8_t *output,
2767 size_t output_size,
2768 size_t *output_length);
2769
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002770/**@}*/
2771
Gilles Peskineedd76872018-07-20 17:42:05 +02002772/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002773 * @{
2774 */
2775
2776/** The type of the state data structure for generators.
2777 *
2778 * Before calling any function on a generator, the application must
2779 * initialize it by any of the following means:
2780 * - Set the structure to all-bits-zero, for example:
2781 * \code
2782 * psa_crypto_generator_t generator;
2783 * memset(&generator, 0, sizeof(generator));
2784 * \endcode
2785 * - Initialize the structure to logical zero values, for example:
2786 * \code
2787 * psa_crypto_generator_t generator = {0};
2788 * \endcode
2789 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2790 * for example:
2791 * \code
2792 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2793 * \endcode
2794 * - Assign the result of the function psa_crypto_generator_init()
2795 * to the structure, for example:
2796 * \code
2797 * psa_crypto_generator_t generator;
2798 * generator = psa_crypto_generator_init();
2799 * \endcode
2800 *
2801 * This is an implementation-defined \c struct. Applications should not
2802 * make any assumptions about the content of this structure except
2803 * as directed by the documentation of a specific implementation.
2804 */
2805typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2806
2807/** \def PSA_CRYPTO_GENERATOR_INIT
2808 *
2809 * This macro returns a suitable initializer for a generator object
2810 * of type #psa_crypto_generator_t.
2811 */
2812#ifdef __DOXYGEN_ONLY__
2813/* This is an example definition for documentation purposes.
2814 * Implementations should define a suitable value in `crypto_struct.h`.
2815 */
2816#define PSA_CRYPTO_GENERATOR_INIT {0}
2817#endif
2818
2819/** Return an initial value for a generator object.
2820 */
2821static psa_crypto_generator_t psa_crypto_generator_init(void);
2822
2823/** Retrieve the current capacity of a generator.
2824 *
2825 * The capacity of a generator is the maximum number of bytes that it can
2826 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2827 *
2828 * \param[in] generator The generator to query.
2829 * \param[out] capacity On success, the capacity of the generator.
2830 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002831 * \retval #PSA_SUCCESS
2832 * \retval #PSA_ERROR_BAD_STATE
2833 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002834 */
2835psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2836 size_t *capacity);
2837
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002838/** Set the maximum capacity of a generator.
2839 *
2840 * \param[in,out] generator The generator object to modify.
2841 * \param capacity The new capacity of the generator.
2842 * It must be less or equal to the generator's
2843 * current capacity.
2844 *
2845 * \retval #PSA_SUCCESS
2846 * \retval #PSA_ERROR_INVALID_ARGUMENT
2847 * \p capacity is larger than the generator's current capacity.
2848 * \retval #PSA_ERROR_BAD_STATE
2849 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2850 */
2851psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2852 size_t capacity);
2853
Gilles Peskineeab56e42018-07-12 17:12:33 +02002854/** Read some data from a generator.
2855 *
2856 * This function reads and returns a sequence of bytes from a generator.
2857 * The data that is read is discarded from the generator. The generator's
2858 * capacity is decreased by the number of bytes read.
2859 *
2860 * \param[in,out] generator The generator object to read from.
2861 * \param[out] output Buffer where the generator output will be
2862 * written.
2863 * \param output_length Number of bytes to output.
2864 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002865 * \retval #PSA_SUCCESS
2866 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002867 * There were fewer than \p output_length bytes
2868 * in the generator. Note that in this case, no
2869 * output is written to the output buffer.
2870 * The generator's capacity is set to 0, thus
2871 * subsequent calls to this function will not
2872 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002873 * \retval #PSA_ERROR_BAD_STATE
2874 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2875 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2876 * \retval #PSA_ERROR_HARDWARE_FAILURE
2877 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002878 */
2879psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2880 uint8_t *output,
2881 size_t output_length);
2882
2883/** Create a symmetric key from data read from a generator.
2884 *
2885 * This function reads a sequence of bytes from a generator and imports
2886 * these bytes as a key.
2887 * The data that is read is discarded from the generator. The generator's
2888 * capacity is decreased by the number of bytes read.
2889 *
2890 * This function is equivalent to calling #psa_generator_read and
2891 * passing the resulting output to #psa_import_key, but
2892 * if the implementation provides an isolation boundary then
2893 * the key material is not exposed outside the isolation boundary.
2894 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002895 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002896 * It must have been obtained by calling
2897 * psa_allocate_key() or psa_create_key() and must
2898 * not contain key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002899 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2900 * This must be a symmetric key type.
2901 * \param bits Key size in bits.
2902 * \param[in,out] generator The generator object to read from.
2903 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002904 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002905 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002906 * If the key is persistent, the key material and the key's metadata
2907 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002908 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002909 * There were fewer than \p output_length bytes
2910 * in the generator. Note that in this case, no
2911 * output is written to the output buffer.
2912 * The generator's capacity is set to 0, thus
2913 * subsequent calls to this function will not
2914 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002915 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002916 * The key type or key size is not supported, either by the
2917 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002918 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002919 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002920 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002921 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002922 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2923 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2924 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2925 * \retval #PSA_ERROR_HARDWARE_FAILURE
2926 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002927 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002928 * The library has not been previously initialized by psa_crypto_init().
2929 * It is implementation-dependent whether a failure to initialize
2930 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002931 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002932psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002933 psa_key_type_t type,
2934 size_t bits,
2935 psa_crypto_generator_t *generator);
2936
2937/** Abort a generator.
2938 *
2939 * Once a generator has been aborted, its capacity is zero.
2940 * Aborting a generator frees all associated resources except for the
2941 * \c generator structure itself.
2942 *
2943 * This function may be called at any time as long as the generator
2944 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2945 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2946 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2947 * on a generator that has not been set up.
2948 *
2949 * Once aborted, the generator object may be called.
2950 *
2951 * \param[in,out] generator The generator to abort.
2952 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002953 * \retval #PSA_SUCCESS
2954 * \retval #PSA_ERROR_BAD_STATE
2955 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2956 * \retval #PSA_ERROR_HARDWARE_FAILURE
2957 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002958 */
2959psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2960
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002961/** Use the maximum possible capacity for a generator.
2962 *
2963 * Use this value as the capacity argument when setting up a generator
2964 * to indicate that the generator should have the maximum possible capacity.
2965 * The value of the maximum possible capacity depends on the generator
2966 * algorithm.
2967 */
2968#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2969
Gilles Peskineeab56e42018-07-12 17:12:33 +02002970/**@}*/
2971
Gilles Peskineea0fb492018-07-12 17:17:20 +02002972/** \defgroup derivation Key derivation
2973 * @{
2974 */
2975
2976/** Set up a key derivation operation.
2977 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002978 * A key derivation algorithm takes some inputs and uses them to create
2979 * a byte generator which can be used to produce keys and other
2980 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002981 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002982 * To use a generator for key derivation:
2983 * - Start with an initialized object of type #psa_crypto_generator_t.
2984 * - Call psa_key_derivation_setup() to select the algorithm.
2985 * - Provide the inputs for the key derivation by calling
2986 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2987 * as appropriate. Which inputs are needed, in what order, and whether
2988 * they may be keys and if so of what type depends on the algorithm.
2989 * - Optionally set the generator's maximum capacity with
2990 * psa_set_generator_capacity(). You may do this before, in the middle of
2991 * or after providing inputs. For some algorithms, this step is mandatory
2992 * because the output depends on the maximum capacity.
2993 * - Generate output with psa_generator_read() or
2994 * psa_generator_import_key(). Successive calls to these functions
2995 * use successive output bytes from the generator.
2996 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02002997 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002998 * \param[in,out] generator The generator object to set up. It must
2999 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003000 * \param alg The key derivation algorithm to compute
3001 * (\c PSA_ALG_XXX value such that
3002 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003003 *
3004 * \retval #PSA_SUCCESS
3005 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003006 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003007 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003008 * \retval #PSA_ERROR_NOT_SUPPORTED
3009 * \c alg is not supported or is not a key derivation algorithm.
3010 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3011 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3012 * \retval #PSA_ERROR_HARDWARE_FAILURE
3013 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003014 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003015 */
3016psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3017 psa_algorithm_t alg);
3018
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003019/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003020 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003021 * Which inputs are required and in what order depends on the algorithm.
3022 * Refer to the documentation of each key derivation or key agreement
3023 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003024 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003025 * This function passes direct inputs. Some inputs must be passed as keys
3026 * using psa_key_derivation_input_key() instead of this function. Refer to
3027 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003028 *
3029 * \param[in,out] generator The generator object to use. It must
3030 * have been set up with
3031 * psa_key_derivation_setup() and must not
3032 * have produced any output yet.
3033 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003034 * \param[in] data Input data to use.
3035 * \param data_length Size of the \p data buffer in bytes.
3036 *
3037 * \retval #PSA_SUCCESS
3038 * Success.
3039 * \retval #PSA_ERROR_INVALID_ARGUMENT
3040 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003041 * \retval #PSA_ERROR_INVALID_ARGUMENT
3042 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003043 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3044 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3045 * \retval #PSA_ERROR_HARDWARE_FAILURE
3046 * \retval #PSA_ERROR_TAMPERING_DETECTED
3047 * \retval #PSA_ERROR_BAD_STATE
3048 * The value of \p step is not valid given the state of \p generator.
3049 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003050 * The library has not been previously initialized by psa_crypto_init().
3051 * It is implementation-dependent whether a failure to initialize
3052 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003053 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003054psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3055 psa_key_derivation_step_t step,
3056 const uint8_t *data,
3057 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003058
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003059/** Provide an input for key derivation in the form of a key.
3060 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003061 * Which inputs are required and in what order depends on the algorithm.
3062 * Refer to the documentation of each key derivation or key agreement
3063 * algorithm for information.
3064 *
3065 * This function passes key inputs. Some inputs must be passed as keys
3066 * of the appropriate type using this function, while others must be
3067 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3068 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003069 *
3070 * \param[in,out] generator The generator object to use. It must
3071 * have been set up with
3072 * psa_key_derivation_setup() and must not
3073 * have produced any output yet.
3074 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003075 * \param handle Handle to the key. It must have an
3076 * appropriate type for \p step and must
3077 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003078 *
3079 * \retval #PSA_SUCCESS
3080 * Success.
3081 * \retval #PSA_ERROR_INVALID_HANDLE
3082 * \retval #PSA_ERROR_EMPTY_SLOT
3083 * \retval #PSA_ERROR_NOT_PERMITTED
3084 * \retval #PSA_ERROR_INVALID_ARGUMENT
3085 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003086 * \retval #PSA_ERROR_INVALID_ARGUMENT
3087 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003088 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3089 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3090 * \retval #PSA_ERROR_HARDWARE_FAILURE
3091 * \retval #PSA_ERROR_TAMPERING_DETECTED
3092 * \retval #PSA_ERROR_BAD_STATE
3093 * The value of \p step is not valid given the state of \p generator.
3094 * \retval #PSA_ERROR_BAD_STATE
3095 * The library has not been previously initialized by psa_crypto_init().
3096 * It is implementation-dependent whether a failure to initialize
3097 * results in this error code.
3098 */
3099psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3100 psa_key_derivation_step_t step,
3101 psa_key_handle_t handle);
3102
Gilles Peskine969c5d62019-01-16 15:53:06 +01003103/** Perform a key agreement and use the shared secret as input to a key
3104 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003105 *
3106 * A key agreement algorithm takes two inputs: a private key \p private_key
3107 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003108 * The result of this function is passed as input to a key derivation.
3109 * The output of this key derivation can be extracted by reading from the
3110 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003111 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003112 * \param[in,out] generator The generator object to use. It must
3113 * have been set up with
3114 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003115 * key agreement and derivation algorithm
3116 * \c alg (\c PSA_ALG_XXX value such that
3117 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true
3118 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3119 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003120 * The generator must be ready for an
3121 * input of the type given by \p step.
3122 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003123 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003124 * \param[in] peer_key Public key of the peer. The peer key must be in the
3125 * same format that psa_import_key() accepts for the
3126 * public key type corresponding to the type of
3127 * private_key. That is, this function performs the
3128 * equivalent of
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003129 * #psa_import_key(`internal_public_key_handle`,
3130 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(`private_key_type`),
3131 * `peer_key`, `peer_key_length`) where
Jaeden Amero8afbff82019-01-14 16:56:20 +00003132 * `private_key_type` is the type of `private_key`.
3133 * For example, for EC keys, this means that peer_key
3134 * is interpreted as a point on the curve that the
3135 * private key is on. The standard formats for public
3136 * keys are documented in the documentation of
3137 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003138 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003139 *
3140 * \retval #PSA_SUCCESS
3141 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003142 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02003143 * \retval #PSA_ERROR_EMPTY_SLOT
3144 * \retval #PSA_ERROR_NOT_PERMITTED
3145 * \retval #PSA_ERROR_INVALID_ARGUMENT
3146 * \c private_key is not compatible with \c alg,
3147 * or \p peer_key is not valid for \c alg or not compatible with
3148 * \c private_key.
3149 * \retval #PSA_ERROR_NOT_SUPPORTED
3150 * \c alg is not supported or is not a key derivation algorithm.
3151 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3152 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3153 * \retval #PSA_ERROR_HARDWARE_FAILURE
3154 * \retval #PSA_ERROR_TAMPERING_DETECTED
3155 */
3156psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003157 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003158 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003159 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003160 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003161
Gilles Peskine769c7a62019-01-18 16:42:29 +01003162/** Perform a key agreement and use the shared secret as input to a key
3163 * derivation.
3164 *
3165 * A key agreement algorithm takes two inputs: a private key \p private_key
3166 * a public key \p peer_key.
3167 *
3168 * \warning The raw result of a key agreement algorithm such as finite-field
3169 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3170 * not be used directly as key material. It should instead be passed as
3171 * input to a key derivation algorithm. To chain a key agreement with
3172 * a key derivation, use psa_key_agreement() and other functions from
3173 * the key derivation and generator interface.
3174 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003175 * \param alg The key agreement algorithm to compute
3176 * (\c PSA_ALG_XXX value such that
3177 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3178 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003179 * \param private_key Handle to the private key to use.
3180 * \param[in] peer_key Public key of the peer. It must be
3181 * in the same format that psa_import_key()
3182 * accepts. The standard formats for public
3183 * keys are documented in the documentation
3184 * of psa_export_public_key().
3185 * \param peer_key_length Size of \p peer_key in bytes.
3186 * \param[out] output Buffer where the decrypted message is to
3187 * be written.
3188 * \param output_size Size of the \c output buffer in bytes.
3189 * \param[out] output_length On success, the number of bytes
3190 * that make up the returned output.
3191 *
3192 * \retval #PSA_SUCCESS
3193 * Success.
3194 * \retval #PSA_ERROR_INVALID_HANDLE
3195 * \retval #PSA_ERROR_EMPTY_SLOT
3196 * \retval #PSA_ERROR_NOT_PERMITTED
3197 * \retval #PSA_ERROR_INVALID_ARGUMENT
3198 * \p alg is not a key agreement algorithm
3199 * \retval #PSA_ERROR_INVALID_ARGUMENT
3200 * \p private_key is not compatible with \p alg,
3201 * or \p peer_key is not valid for \p alg or not compatible with
3202 * \p private_key.
3203 * \retval #PSA_ERROR_NOT_SUPPORTED
3204 * \p alg is not a supported key agreement algorithm.
3205 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3206 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3207 * \retval #PSA_ERROR_HARDWARE_FAILURE
3208 * \retval #PSA_ERROR_TAMPERING_DETECTED
3209 */
3210psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3211 psa_key_handle_t private_key,
3212 const uint8_t *peer_key,
3213 size_t peer_key_length,
3214 uint8_t *output,
3215 size_t output_size,
3216 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003217
3218/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003219
3220/** \defgroup random Random generation
3221 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003222 */
3223
3224/**
3225 * \brief Generate random bytes.
3226 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003227 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003228 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003229 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003230 *
3231 * \note To generate a key, use psa_generate_key() instead.
3232 *
3233 * \param[out] output Output buffer for the generated data.
3234 * \param output_size Number of bytes to generate and output.
3235 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003236 * \retval #PSA_SUCCESS
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003237 * \retval #PSA_ERROR_NOT_SUPPORTED
3238 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003239 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003240 * \retval #PSA_ERROR_HARDWARE_FAILURE
3241 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003242 * \retval #PSA_ERROR_BAD_STATE
3243 * The library has not been previously initialized by psa_crypto_init().
3244 * It is implementation-dependent whether a failure to initialize
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003245 * results in this error code.
3246 */
3247psa_status_t psa_generate_random(uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003248 size_t output_size);
3249
3250/** Extra parameters for RSA key generation.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003251 *
Gilles Peskine28538492018-07-11 17:34:00 +02003252 * You may pass a pointer to a structure of this type as the \c extra
3253 * parameter to psa_generate_key().
3254 */
3255typedef struct {
3256 uint32_t e; /**< Public exponent value. Default: 65537. */
3257} psa_generate_key_extra_rsa;
3258
3259/**
itayzafrir90d8c7a2018-09-12 11:44:52 +03003260 * \brief Generate a key or key pair.
itayzafrir18617092018-09-16 12:22:41 +03003261 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003262 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003263 * It must have been obtained by calling
3264 * psa_allocate_key() or psa_create_key() and must
3265 * not contain key material yet.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003266 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
3267 * \param bits Key size in bits.
3268 * \param[in] extra Extra parameters for key generation. The
3269 * interpretation of this parameter depends on
3270 * \p type. All types support \c NULL to use
3271 * default parameters. Implementation that support
3272 * the generation of vendor-specific key types
3273 * that allow extra parameters shall document
3274 * the format of these extra parameters and
3275 * the default values. For standard parameters,
3276 * the meaning of \p extra is as follows:
3277 * - For a symmetric key type (a type such
3278 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
Gilles Peskine53d991e2018-07-12 01:14:59 +02003279 * false), \p extra must be \c NULL.
3280 * - For an elliptic curve key type (a type
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003281 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
3282 * false), \p extra must be \c NULL.
3283 * - For an RSA key (\p type is
Gilles Peskinee59236f2018-01-27 23:32:46 +01003284 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3285 * optional #psa_generate_key_extra_rsa structure
3286 * specifying the public exponent. The
3287 * default public exponent used when \p extra
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003288 * is \c NULL is 65537.
Jaeden Amero1308fb52019-01-11 13:50:43 +00003289 * - For an DSA key (\p type is
3290 * #PSA_KEY_TYPE_DSA_KEYPAIR), \p extra is an
3291 * optional structure specifying the key domain
3292 * parameters. The key domain parameters can also be
3293 * provided by psa_set_key_domain_parameters(),
3294 * which documents the format of the structure.
Jaeden Amero8851c402019-01-11 14:20:03 +00003295 * - For a DH key (\p type is
3296 * #PSA_KEY_TYPE_DH_KEYPAIR), the \p extra is an
3297 * optional structure specifying the key domain
3298 * parameters. The key domain parameters can also be
3299 * provided by psa_set_key_domain_parameters(),
3300 * which documents the format of the structure.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003301 * \param extra_size Size of the buffer that \p extra
3302 * points to, in bytes. Note that if \p extra is
3303 * \c NULL then \p extra_size must be zero.
3304 *
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003305 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003306 * Success.
3307 * If the key is persistent, the key material and the key's metadata
3308 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003309 * \retval #PSA_ERROR_INVALID_HANDLE
3310 * \retval #PSA_ERROR_OCCUPIED_SLOT
3311 * There is already a key in the specified slot.
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003312 * \retval #PSA_ERROR_NOT_SUPPORTED
3313 * \retval #PSA_ERROR_INVALID_ARGUMENT
3314 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003315 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3316 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3317 * \retval #PSA_ERROR_HARDWARE_FAILURE
3318 * \retval #PSA_ERROR_TAMPERING_DETECTED
3319 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003320 * The library has not been previously initialized by psa_crypto_init().
3321 * It is implementation-dependent whether a failure to initialize
3322 * results in this error code.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003323 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003324psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskinee59236f2018-01-27 23:32:46 +01003325 psa_key_type_t type,
3326 size_t bits,
3327 const void *extra,
3328 size_t extra_size);
3329
3330/**@}*/
3331
3332#ifdef __cplusplus
3333}
3334#endif
3335
3336/* The file "crypto_sizes.h" contains definitions for size calculation
3337 * macros whose definitions are implementation-specific. */
3338#include "crypto_sizes.h"
3339
3340/* The file "crypto_struct.h" contains definitions for
3341 * implementation-specific structs that are declared above. */
3342#include "crypto_struct.h"
3343
3344/* The file "crypto_extra.h" contains vendor-specific definitions. This
3345 * can include vendor-defined algorithms, extra functions, etc. */
3346#include "crypto_extra.h"
3347
3348#endif /* PSA_CRYPTO_H */