blob: 3ed5f0f51e0b9e8e1b5ead75715b2ef3aced1384 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
89 * \retval #PSA_ERROR_TAMPERING_DETECTED
90 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine4cb9dde2019-01-19 13:40:11 +010096/** \defgroup policy Key policies
97 * @{
98 */
99
100/** The type of the key policy data structure.
101 *
102 * Before calling any function on a key policy, the application must initialize
103 * it by any of the following means:
104 * - Set the structure to all-bits-zero, for example:
105 * \code
106 * psa_key_policy_t policy;
107 * memset(&policy, 0, sizeof(policy));
108 * \endcode
109 * - Initialize the structure to logical zero values, for example:
110 * \code
111 * psa_key_policy_t policy = {0};
112 * \endcode
113 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
114 * for example:
115 * \code
116 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
117 * \endcode
118 * - Assign the result of the function psa_key_policy_init()
119 * to the structure, for example:
120 * \code
121 * psa_key_policy_t policy;
122 * policy = psa_key_policy_init();
123 * \endcode
124 *
125 * This is an implementation-defined \c struct. Applications should not
126 * make any assumptions about the content of this structure except
127 * as directed by the documentation of a specific implementation. */
128typedef struct psa_key_policy_s psa_key_policy_t;
129
130/** \def PSA_KEY_POLICY_INIT
131 *
132 * This macro returns a suitable initializer for a key policy object of type
133 * #psa_key_policy_t.
134 */
135#ifdef __DOXYGEN_ONLY__
136/* This is an example definition for documentation purposes.
137 * Implementations should define a suitable value in `crypto_struct.h`.
138 */
139#define PSA_KEY_POLICY_INIT {0}
140#endif
141
142/** Return an initial value for a key policy that forbids all usage of the key.
143 */
144static psa_key_policy_t psa_key_policy_init(void);
145
146/** \brief Set the standard fields of a policy structure.
147 *
148 * Note that this function does not make any consistency check of the
149 * parameters. The values are only checked when applying the policy to
150 * a key slot with psa_set_key_policy().
151 *
152 * \param[in,out] policy The key policy to modify. It must have been
153 * initialized as per the documentation for
154 * #psa_key_policy_t.
155 * \param usage The permitted uses for the key.
156 * \param alg The algorithm that the key may be used for.
157 */
158void psa_key_policy_set_usage(psa_key_policy_t *policy,
159 psa_key_usage_t usage,
160 psa_algorithm_t alg);
161
162/** \brief Retrieve the usage field of a policy structure.
163 *
164 * \param[in] policy The policy object to query.
165 *
166 * \return The permitted uses for a key with this policy.
167 */
168psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
169
170/** \brief Retrieve the algorithm field of a policy structure.
171 *
172 * \param[in] policy The policy object to query.
173 *
174 * \return The permitted algorithm for a key with this policy.
175 */
176psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
177
178/** \brief Set the usage policy on a key slot.
179 *
180 * This function must be called on an empty key slot, before importing,
181 * generating or creating a key in the slot. Changing the policy of an
182 * existing key is not permitted.
183 *
184 * Implementations may set restrictions on supported key policies
185 * depending on the key type and the key slot.
186 *
187 * \param handle Handle to the key whose policy is to be changed.
188 * \param[in] policy The policy object to query.
189 *
190 * \retval #PSA_SUCCESS
191 * Success.
192 * If the key is persistent, it is implementation-defined whether
193 * the policy has been saved to persistent storage. Implementations
194 * may defer saving the policy until the key material is created.
195 * \retval #PSA_ERROR_INVALID_HANDLE
196 * \retval #PSA_ERROR_OCCUPIED_SLOT
197 * \retval #PSA_ERROR_NOT_SUPPORTED
198 * \retval #PSA_ERROR_INVALID_ARGUMENT
199 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
200 * \retval #PSA_ERROR_HARDWARE_FAILURE
201 * \retval #PSA_ERROR_TAMPERING_DETECTED
202 * \retval #PSA_ERROR_BAD_STATE
203 * The library has not been previously initialized by psa_crypto_init().
204 * It is implementation-dependent whether a failure to initialize
205 * results in this error code.
206 */
207psa_status_t psa_set_key_policy(psa_key_handle_t handle,
208 const psa_key_policy_t *policy);
209
210/** \brief Get the usage policy for a key slot.
211 *
212 * \param handle Handle to the key slot whose policy is being queried.
213 * \param[out] policy On success, the key's policy.
214 *
215 * \retval #PSA_SUCCESS
216 * \retval #PSA_ERROR_INVALID_HANDLE
217 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
218 * \retval #PSA_ERROR_HARDWARE_FAILURE
219 * \retval #PSA_ERROR_TAMPERING_DETECTED
220 * \retval #PSA_ERROR_BAD_STATE
221 * The library has not been previously initialized by psa_crypto_init().
222 * It is implementation-dependent whether a failure to initialize
223 * results in this error code.
224 */
225psa_status_t psa_get_key_policy(psa_key_handle_t handle,
226 psa_key_policy_t *policy);
227
228/**@}*/
229
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100230/** \defgroup key_management Key management
231 * @{
232 */
233
Gilles Peskineae32aac2018-11-30 14:39:32 +0100234/** \brief Retrieve the lifetime of an open key.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100235 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100236 * \param handle Handle to query.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100237 * \param[out] lifetime On success, the lifetime value.
238 *
239 * \retval #PSA_SUCCESS
240 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100241 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100242 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
243 * \retval #PSA_ERROR_HARDWARE_FAILURE
244 * \retval #PSA_ERROR_TAMPERING_DETECTED
245 * \retval #PSA_ERROR_BAD_STATE
246 * The library has not been previously initialized by psa_crypto_init().
247 * It is implementation-dependent whether a failure to initialize
248 * results in this error code.
249 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100250psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100251 psa_key_lifetime_t *lifetime);
252
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100253
Gilles Peskinef535eb22018-11-30 14:08:36 +0100254/** Allocate a key slot for a transient key, i.e. a key which is only stored
255 * in volatile memory.
256 *
257 * The allocated key slot and its handle remain valid until the
258 * application calls psa_close_key() or psa_destroy_key() or until the
259 * application terminates.
260 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100261 * \param[out] handle On success, a handle to a volatile key slot.
262 *
263 * \retval #PSA_SUCCESS
264 * Success. The application can now use the value of `*handle`
265 * to access the newly allocated key slot.
266 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
267 * There was not enough memory, or the maximum number of key slots
268 * has been reached.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100269 */
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100270psa_status_t psa_allocate_key(psa_key_handle_t *handle);
Gilles Peskinef535eb22018-11-30 14:08:36 +0100271
272/** Open a handle to an existing persistent key.
273 *
274 * Open a handle to a key which was previously created with psa_create_key().
275 *
276 * \param lifetime The lifetime of the key. This designates a storage
277 * area where the key material is stored. This must not
278 * be #PSA_KEY_LIFETIME_VOLATILE.
279 * \param id The persistent identifier of the key.
280 * \param[out] handle On success, a handle to a key slot which contains
281 * the data and metadata loaded from the specified
282 * persistent location.
283 *
284 * \retval #PSA_SUCCESS
285 * Success. The application can now use the value of `*handle`
286 * to access the newly allocated key slot.
287 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
288 * \retval #PSA_ERROR_EMPTY_SLOT
289 * \retval #PSA_ERROR_INVALID_ARGUMENT
290 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
291 * \retval #PSA_ERROR_INVALID_ARGUMENT
292 * \p id is invalid for the specified lifetime.
293 * \retval #PSA_ERROR_NOT_SUPPORTED
294 * \p lifetime is not supported.
295 * \retval #PSA_ERROR_NOT_PERMITTED
296 * The specified key exists, but the application does not have the
297 * permission to access it. Note that this specification does not
298 * define any way to create such a key, but it may be possible
299 * through implementation-specific means.
300 */
301psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
302 psa_key_id_t id,
303 psa_key_handle_t *handle);
304
305/** Create a new persistent key slot.
306 *
307 * Create a new persistent key slot and return a handle to it. The handle
308 * remains valid until the application calls psa_close_key() or terminates.
309 * The application can open the key again with psa_open_key() until it
310 * removes the key by calling psa_destroy_key().
311 *
312 * \param lifetime The lifetime of the key. This designates a storage
313 * area where the key material is stored. This must not
314 * be #PSA_KEY_LIFETIME_VOLATILE.
315 * \param id The persistent identifier of the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100316 * \param[out] handle On success, a handle to the newly created key slot.
317 * When key material is later created in this key slot,
318 * it will be saved to the specified persistent location.
319 *
320 * \retval #PSA_SUCCESS
321 * Success. The application can now use the value of `*handle`
322 * to access the newly allocated key slot.
323 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
324 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
325 * \retval #PSA_ERROR_OCCUPIED_SLOT
326 * There is already a key with the identifier \p id in the storage
327 * area designated by \p lifetime.
328 * \retval #PSA_ERROR_INVALID_ARGUMENT
329 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
330 * \retval #PSA_ERROR_INVALID_ARGUMENT
331 * \p id is invalid for the specified lifetime.
332 * \retval #PSA_ERROR_NOT_SUPPORTED
333 * \p lifetime is not supported.
334 * \retval #PSA_ERROR_NOT_PERMITTED
335 * \p lifetime is valid, but the application does not have the
336 * permission to create a key there.
337 */
338psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
339 psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100340 psa_key_handle_t *handle);
341
342/** Close a key handle.
343 *
344 * If the handle designates a volatile key, destroy the key material and
345 * free all associated resources, just like psa_destroy_key().
346 *
347 * If the handle designates a persistent key, free all resources associated
348 * with the key in volatile memory. The key slot in persistent storage is
349 * not affected and can be opened again later with psa_open_key().
350 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100351 * If the key is currently in use in a multipart operation,
352 * the multipart operation is aborted.
353 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100354 * \param handle The key handle to close.
355 *
356 * \retval #PSA_SUCCESS
357 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100358 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100359 */
360psa_status_t psa_close_key(psa_key_handle_t handle);
361
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100362/**@}*/
363
364/** \defgroup import_export Key import and export
365 * @{
366 */
367
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100368/**
369 * \brief Import a key in binary format.
370 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100371 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100372 * documentation of psa_export_public_key() for the format of public keys
373 * and to the documentation of psa_export_key() for the format for
374 * other key types.
375 *
376 * This specification supports a single format for each key type.
377 * Implementations may support other formats as long as the standard
378 * format is supported. Implementations that support other formats
379 * should ensure that the formats are clearly unambiguous so as to
380 * minimize the risk that an invalid input is accidentally interpreted
381 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100382 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100383 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100384 * It must have been obtained by calling
385 * psa_allocate_key() or psa_create_key() and must
386 * not contain key material yet.
Gilles Peskinef7933932018-10-31 14:07:52 +0100387 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
388 * import, the key slot will contain a key of this type.
389 * \param[in] data Buffer containing the key data. The content of this
390 * buffer is interpreted according to \p type. It must
391 * contain the format described in the documentation
392 * of psa_export_key() or psa_export_public_key() for
393 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200394 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100395 *
Gilles Peskine28538492018-07-11 17:34:00 +0200396 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100397 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100398 * If the key is persistent, the key material and the key's metadata
399 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100400 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200401 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200402 * The key type or key size is not supported, either by the
403 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200404 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100405 * The key slot is invalid,
406 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200407 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200408 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200409 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
410 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
411 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100412 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200413 * \retval #PSA_ERROR_HARDWARE_FAILURE
414 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300415 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300416 * The library has not been previously initialized by psa_crypto_init().
417 * It is implementation-dependent whether a failure to initialize
418 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100419 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100420psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100421 psa_key_type_t type,
422 const uint8_t *data,
423 size_t data_length);
424
425/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100426 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200427 *
428 * This function destroys the content of the key slot from both volatile
429 * memory and, if applicable, non-volatile storage. Implementations shall
430 * make a best effort to ensure that any previous content of the slot is
431 * unrecoverable.
432 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100433 * This function also erases any metadata such as policies and frees all
434 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200435 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100436 * If the key is currently in use in a multipart operation,
437 * the multipart operation is aborted.
438 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100439 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100440 *
Gilles Peskine28538492018-07-11 17:34:00 +0200441 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200442 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200443 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200444 * The slot holds content and cannot be erased because it is
445 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100446 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200447 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200448 * There was an failure in communication with the cryptoprocessor.
449 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200450 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200451 * The storage is corrupted. Implementations shall make a best effort
452 * to erase key material even in this stage, however applications
453 * should be aware that it may be impossible to guarantee that the
454 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200455 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200456 * An unexpected condition which is not a storage corruption or
457 * a communication failure occurred. The cryptoprocessor may have
458 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300459 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300460 * The library has not been previously initialized by psa_crypto_init().
461 * It is implementation-dependent whether a failure to initialize
462 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100463 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100464psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100465
466/**
467 * \brief Get basic metadata about a key.
468 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100469 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200470 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100471 * This may be a null pointer, in which case the key type
472 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200473 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100474 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100475 * is not written.
476 *
Gilles Peskine28538492018-07-11 17:34:00 +0200477 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100478 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200479 * \retval #PSA_ERROR_EMPTY_SLOT
Gilles Peskineae32aac2018-11-30 14:39:32 +0100480 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200481 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
482 * \retval #PSA_ERROR_HARDWARE_FAILURE
483 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300484 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300485 * The library has not been previously initialized by psa_crypto_init().
486 * It is implementation-dependent whether a failure to initialize
487 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100488 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100489psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100490 psa_key_type_t *type,
491 size_t *bits);
492
493/**
Jaeden Amero283dfd12019-01-11 12:06:22 +0000494 * \brief Set domain parameters for a key.
495 *
496 * Some key types require additional domain parameters to be set before import
497 * or generation of the key. The domain parameters can be set with this
498 * function or, for key generation, through the \c extra parameter of
499 * psa_generate_key().
500 *
501 * The format for the required domain parameters varies by the key type.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000502 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
503 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
504 * ```
505 * Dss-Parms ::= SEQUENCE {
506 * p INTEGER,
507 * q INTEGER,
508 * g INTEGER
509 * }
510 * ```
Jaeden Amero8851c402019-01-11 14:20:03 +0000511 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY), the
512 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
513 * ```
514 * DomainParameters ::= SEQUENCE {
515 * p INTEGER, -- odd prime, p=jq +1
516 * g INTEGER, -- generator, g
517 * q INTEGER, -- factor of p-1
518 * j INTEGER OPTIONAL, -- subgroup factor
519 * validationParms ValidationParms OPTIONAL
520 * }
521 * ValidationParms ::= SEQUENCE {
522 * seed BIT STRING,
523 * pgenCounter INTEGER
524 * }
525 * ```
Jaeden Amero283dfd12019-01-11 12:06:22 +0000526 *
Gilles Peskine3a74e002019-01-18 17:11:25 +0100527 * \param handle Handle to the slot where the key will be stored.
528 * This must be a valid slot for a key of the chosen
529 * type: it must have been obtained by calling
530 * psa_allocate_key() or psa_create_key() with the
531 * correct \p type and with a maximum size that is
532 * compatible with \p data. It must not contain
533 * key material yet.
534 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). When
535 * subsequently creating key material into \p handle,
536 * the type must be compatible.
Jaeden Amero283dfd12019-01-11 12:06:22 +0000537 * \param[in] data Buffer containing the key domain parameters. The content
538 * of this buffer is interpreted according to \p type. of
539 * psa_export_key() or psa_export_public_key() for the
540 * chosen type.
541 * \param data_length Size of the \p data buffer in bytes.
542 *
543 * \retval #PSA_SUCCESS
544 * \retval #PSA_ERROR_INVALID_HANDLE
545 * \retval #PSA_ERROR_OCCUPIED_SLOT
546 * There is already a key in the specified slot.
547 * \retval #PSA_ERROR_INVALID_ARGUMENT
548 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
549 * \retval #PSA_ERROR_HARDWARE_FAILURE
550 * \retval #PSA_ERROR_TAMPERING_DETECTED
551 * \retval #PSA_ERROR_BAD_STATE
552 * The library has not been previously initialized by psa_crypto_init().
553 * It is implementation-dependent whether a failure to initialize
554 * results in this error code.
555 */
556psa_status_t psa_set_key_domain_parameters(psa_key_handle_t handle,
Gilles Peskine3a74e002019-01-18 17:11:25 +0100557 psa_key_type_t type,
Jaeden Amero283dfd12019-01-11 12:06:22 +0000558 const uint8_t *data,
559 size_t data_length);
560
561/**
562 * \brief Get domain parameters for a key.
563 *
564 * Get the domain parameters for a key with this function, if any. The format
565 * of the domain parameters written to \p data is specified in the
566 * documentation for psa_set_key_domain_parameters().
567 *
568 * \param handle Handle to the key to get domain parameters from.
569 * \param[out] data On success, the key domain parameters.
570 * \param data_size Size of the \p data buffer in bytes.
571 * \param[out] data_length On success, the number of bytes
572 * that make up the key domain parameters data.
573 *
574 * \retval #PSA_SUCCESS
575 * \retval #PSA_ERROR_INVALID_HANDLE
576 * \retval #PSA_ERROR_EMPTY_SLOT
577 * There is no key in the specified slot.
578 * \retval #PSA_ERROR_INVALID_ARGUMENT
579 * \retval #PSA_ERROR_NOT_SUPPORTED
580 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
581 * \retval #PSA_ERROR_HARDWARE_FAILURE
582 * \retval #PSA_ERROR_TAMPERING_DETECTED
583 * \retval #PSA_ERROR_BAD_STATE
584 * The library has not been previously initialized by psa_crypto_init().
585 * It is implementation-dependent whether a failure to initialize
586 * results in this error code.
587 */
588psa_status_t psa_get_key_domain_parameters(psa_key_handle_t handle,
589 uint8_t *data,
590 size_t data_size,
591 size_t *data_length);
592
593/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100594 * \brief Export a key in binary format.
595 *
596 * The output of this function can be passed to psa_import_key() to
597 * create an equivalent object.
598 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100599 * If the implementation of psa_import_key() supports other formats
600 * beyond the format specified here, the output from psa_export_key()
601 * must use the representation specified here, not the original
602 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100604 * For standard key types, the output format is as follows:
605 *
606 * - For symmetric keys (including MAC keys), the format is the
607 * raw bytes of the key.
608 * - For DES, the key data consists of 8 bytes. The parity bits must be
609 * correct.
610 * - For Triple-DES, the format is the concatenation of the
611 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100612 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200613 * is the non-encrypted DER encoding of the representation defined by
614 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
615 * ```
616 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200617 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200618 * modulus INTEGER, -- n
619 * publicExponent INTEGER, -- e
620 * privateExponent INTEGER, -- d
621 * prime1 INTEGER, -- p
622 * prime2 INTEGER, -- q
623 * exponent1 INTEGER, -- d mod (p-1)
624 * exponent2 INTEGER, -- d mod (q-1)
625 * coefficient INTEGER, -- (inverse of q) mod p
626 * }
627 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000628 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
629 * representation of the private key `x` as a big-endian byte string. The
630 * length of the byte string is the private key size in bytes (leading zeroes
631 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200632 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100633 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100634 * a representation of the private value as a `ceiling(m/8)`-byte string
635 * where `m` is the bit size associated with the curve, i.e. the bit size
636 * of the order of the curve's coordinate field. This byte string is
637 * in little-endian order for Montgomery curves (curve types
638 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
639 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
640 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100641 * This is the content of the `privateKey` field of the `ECPrivateKey`
642 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000643 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
644 * format is the representation of the private key `x` as a big-endian byte
645 * string. The length of the byte string is the private key size in bytes
646 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200647 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
648 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100649 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100650 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200651 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200652 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200653 * \param[out] data_length On success, the number of bytes
654 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100655 *
Gilles Peskine28538492018-07-11 17:34:00 +0200656 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100657 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200658 * \retval #PSA_ERROR_EMPTY_SLOT
659 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100660 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200661 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
662 * The size of the \p data buffer is too small. You can determine a
663 * sufficient buffer size by calling
664 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
665 * where \c type is the key type
666 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200667 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
668 * \retval #PSA_ERROR_HARDWARE_FAILURE
669 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300670 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300671 * The library has not been previously initialized by psa_crypto_init().
672 * It is implementation-dependent whether a failure to initialize
673 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100675psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676 uint8_t *data,
677 size_t data_size,
678 size_t *data_length);
679
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100680/**
681 * \brief Export a public key or the public part of a key pair in binary format.
682 *
683 * The output of this function can be passed to psa_import_key() to
684 * create an object that is equivalent to the public key.
685 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000686 * This specification supports a single format for each key type.
687 * Implementations may support other formats as long as the standard
688 * format is supported. Implementations that support other formats
689 * should ensure that the formats are clearly unambiguous so as to
690 * minimize the risk that an invalid input is accidentally interpreted
691 * according to a different format.
692 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000693 * For standard key types, the output format is as follows:
694 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
695 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
696 * ```
697 * RSAPublicKey ::= SEQUENCE {
698 * modulus INTEGER, -- n
699 * publicExponent INTEGER } -- e
700 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000701 * - For elliptic curve public keys (key types for which
702 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
703 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
704 * Let `m` be the bit size associated with the curve, i.e. the bit size of
705 * `q` for a curve over `F_q`. The representation consists of:
706 * - The byte 0x04;
707 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
708 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000709 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
710 * representation of the public key `y = g^x mod p` as a big-endian byte
711 * string. The length of the byte string is the length of the base prime `p`
712 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000713 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
714 * the format is the representation of the public key `y = g^x mod p` as a
715 * big-endian byte string. The length of the byte string is the length of the
716 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100717 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100718 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200719 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200720 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200721 * \param[out] data_length On success, the number of bytes
722 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100723 *
Gilles Peskine28538492018-07-11 17:34:00 +0200724 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100725 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200726 * \retval #PSA_ERROR_EMPTY_SLOT
727 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200728 * The key is neither a public key nor a key pair.
729 * \retval #PSA_ERROR_NOT_SUPPORTED
730 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
731 * The size of the \p data buffer is too small. You can determine a
732 * sufficient buffer size by calling
733 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
734 * where \c type is the key type
735 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200736 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
737 * \retval #PSA_ERROR_HARDWARE_FAILURE
738 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300739 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300740 * The library has not been previously initialized by psa_crypto_init().
741 * It is implementation-dependent whether a failure to initialize
742 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100743 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100744psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100745 uint8_t *data,
746 size_t data_size,
747 size_t *data_length);
748
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100749/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100750 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100751 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000752 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100753 * This function is primarily useful to copy a key from one lifetime
754 * to another. The target key retains its lifetime and location.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200755 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100756 * In an implementation where slots have different ownerships,
757 * this functin may be used to share a key with a different party,
758 * subject to implementation-defined restrictions on key sharing.
759 * In this case \p constraint would typically prevent the recipient
760 * from exporting the key.
Gilles Peskine7e198532018-03-08 07:50:30 +0100761 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100762 * The resulting key may only be used in a way that conforms to all
763 * three of: the policy of the source key, the policy previously set
764 * on the target, and the \p constraint parameter passed when calling
765 * this function.
766 * - The usage flags on the resulting key are the bitwise-and of the
767 * usage flags on the source policy, the previously-set target policy
768 * and the policy constraint.
769 * - If all three policies allow the same algorithm or wildcard-based
770 * algorithm policy, the resulting key has the same algorithm policy.
771 * - If one of the policies allows an algorithm and all the other policies
772 * either allow the same algorithm or a wildcard-based algorithm policy
773 * that includes this algorithm, the resulting key allows the same
774 * algorithm.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200775 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100776 * The effect of this function on implementation-defined metadata is
777 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200778 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100779 * \param source_handle The key to copy. It must be a handle to an
780 * occupied slot.
781 * \param target_handle A handle to the target slot. It must not contain
782 * key material yet.
783 * \param[in] constraint An optional policy constraint. If this parameter
784 * is non-null then the resulting key will conform
785 * to this policy in addition to the source policy
786 * and the policy already present on the target
787 * slot. If this parameter is null then the
788 * function behaves in the same way as if it was
789 * the target policy, i.e. only the source and
790 * target policies apply.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200791 *
792 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100793 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200794 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100795 * \p target already contains key material.
796 * \retval #PSA_ERROR_EMPTY_SLOT
797 * \p source does not contain key material.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200798 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100799 * The policy constraints on the source, on the target and
800 * \p constraints are incompatible.
801 * \retval #PSA_ERROR_NOT_PERMITTED
802 * The source key is not exportable and its lifetime does not
803 * allow copying it to the target's lifetime.
804 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
805 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200806 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
807 * \retval #PSA_ERROR_HARDWARE_FAILURE
808 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100809 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100810psa_status_t psa_copy_key(psa_key_handle_t source_handle,
811 psa_key_handle_t target_handle,
812 const psa_key_policy_t *constraint);
Gilles Peskine20035e32018-02-03 22:44:14 +0100813
814/**@}*/
815
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100816/** \defgroup hash Message digests
817 * @{
818 */
819
Gilles Peskine69647a42019-01-14 20:18:12 +0100820/** Calculate the hash (digest) of a message.
821 *
822 * \note To verify the hash of a message against an
823 * expected value, use psa_hash_compare() instead.
824 *
825 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
826 * such that #PSA_ALG_IS_HASH(\p alg) is true).
827 * \param[in] input Buffer containing the message to hash.
828 * \param input_length Size of the \p input buffer in bytes.
829 * \param[out] hash Buffer where the hash is to be written.
830 * \param hash_size Size of the \p hash buffer in bytes.
831 * \param[out] hash_length On success, the number of bytes
832 * that make up the hash value. This is always
833 * #PSA_HASH_SIZE(\c alg) where \c alg is the
834 * hash algorithm that is calculated.
835 *
836 * \retval #PSA_SUCCESS
837 * Success.
838 * \retval #PSA_ERROR_NOT_SUPPORTED
839 * \p alg is not supported or is not a hash algorithm.
840 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
841 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
842 * \retval #PSA_ERROR_HARDWARE_FAILURE
843 * \retval #PSA_ERROR_TAMPERING_DETECTED
844 */
845psa_status_t psa_hash_compute(psa_algorithm_t alg,
846 const uint8_t *input,
847 size_t input_length,
848 uint8_t *hash,
849 size_t hash_size,
850 size_t *hash_length);
851
852/** Calculate the hash (digest) of a message and compare it with a
853 * reference value.
854 *
855 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
856 * such that #PSA_ALG_IS_HASH(\p alg) is true).
857 * \param[in] input Buffer containing the message to hash.
858 * \param input_length Size of the \p input buffer in bytes.
859 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100860 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100861 *
862 * \retval #PSA_SUCCESS
863 * The expected hash is identical to the actual hash of the input.
864 * \retval #PSA_ERROR_INVALID_SIGNATURE
865 * The hash of the message was calculated successfully, but it
866 * differs from the expected hash.
867 * \retval #PSA_ERROR_NOT_SUPPORTED
868 * \p alg is not supported or is not a hash algorithm.
869 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
870 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
871 * \retval #PSA_ERROR_HARDWARE_FAILURE
872 * \retval #PSA_ERROR_TAMPERING_DETECTED
873 */
874psa_status_t psa_hash_compare(psa_algorithm_t alg,
875 const uint8_t *input,
876 size_t input_length,
877 const uint8_t *hash,
878 const size_t hash_length);
879
Gilles Peskine308b91d2018-02-08 09:47:44 +0100880/** The type of the state data structure for multipart hash operations.
881 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000882 * Before calling any function on a hash operation object, the application must
883 * initialize it by any of the following means:
884 * - Set the structure to all-bits-zero, for example:
885 * \code
886 * psa_hash_operation_t operation;
887 * memset(&operation, 0, sizeof(operation));
888 * \endcode
889 * - Initialize the structure to logical zero values, for example:
890 * \code
891 * psa_hash_operation_t operation = {0};
892 * \endcode
893 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
894 * for example:
895 * \code
896 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
897 * \endcode
898 * - Assign the result of the function psa_hash_operation_init()
899 * to the structure, for example:
900 * \code
901 * psa_hash_operation_t operation;
902 * operation = psa_hash_operation_init();
903 * \endcode
904 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100905 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100906 * make any assumptions about the content of this structure except
907 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100908typedef struct psa_hash_operation_s psa_hash_operation_t;
909
Jaeden Amero6a25b412019-01-04 11:47:44 +0000910/** \def PSA_HASH_OPERATION_INIT
911 *
912 * This macro returns a suitable initializer for a hash operation object
913 * of type #psa_hash_operation_t.
914 */
915#ifdef __DOXYGEN_ONLY__
916/* This is an example definition for documentation purposes.
917 * Implementations should define a suitable value in `crypto_struct.h`.
918 */
919#define PSA_HASH_OPERATION_INIT {0}
920#endif
921
922/** Return an initial value for a hash operation object.
923 */
924static psa_hash_operation_t psa_hash_operation_init(void);
925
Gilles Peskinef45adda2019-01-14 18:29:18 +0100926/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100927 *
928 * The sequence of operations to calculate a hash (message digest)
929 * is as follows:
930 * -# Allocate an operation object which will be passed to all the functions
931 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000932 * -# Initialize the operation object with one of the methods described in the
933 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200934 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100935 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100936 * of the message each time. The hash that is calculated is the hash
937 * of the concatenation of these messages in order.
938 * -# To calculate the hash, call psa_hash_finish().
939 * To compare the hash with an expected value, call psa_hash_verify().
940 *
941 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000942 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100943 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200944 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100945 * eventually terminate the operation. The following events terminate an
946 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100947 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100948 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100949 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000950 * \param[in,out] operation The operation object to set up. It must have
951 * been initialized as per the documentation for
952 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200953 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
954 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100955 *
Gilles Peskine28538492018-07-11 17:34:00 +0200956 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100957 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200958 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200959 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200960 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
961 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
962 * \retval #PSA_ERROR_HARDWARE_FAILURE
963 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100964 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200965psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100966 psa_algorithm_t alg);
967
Gilles Peskine308b91d2018-02-08 09:47:44 +0100968/** Add a message fragment to a multipart hash operation.
969 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200970 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100971 *
972 * If this function returns an error status, the operation becomes inactive.
973 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200974 * \param[in,out] operation Active hash operation.
975 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200976 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100977 *
Gilles Peskine28538492018-07-11 17:34:00 +0200978 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100979 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200980 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100981 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200982 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
983 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
984 * \retval #PSA_ERROR_HARDWARE_FAILURE
985 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100986 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100987psa_status_t psa_hash_update(psa_hash_operation_t *operation,
988 const uint8_t *input,
989 size_t input_length);
990
Gilles Peskine308b91d2018-02-08 09:47:44 +0100991/** Finish the calculation of the hash of a message.
992 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200993 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100994 * This function calculates the hash of the message formed by concatenating
995 * the inputs passed to preceding calls to psa_hash_update().
996 *
997 * When this function returns, the operation becomes inactive.
998 *
999 * \warning Applications should not call this function if they expect
1000 * a specific value for the hash. Call psa_hash_verify() instead.
1001 * Beware that comparing integrity or authenticity data such as
1002 * hash values with a function such as \c memcmp is risky
1003 * because the time taken by the comparison may leak information
1004 * about the hashed data which could allow an attacker to guess
1005 * a valid hash and thereby bypass security controls.
1006 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001007 * \param[in,out] operation Active hash operation.
1008 * \param[out] hash Buffer where the hash is to be written.
1009 * \param hash_size Size of the \p hash buffer in bytes.
1010 * \param[out] hash_length On success, the number of bytes
1011 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001012 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001013 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001014 *
Gilles Peskine28538492018-07-11 17:34:00 +02001015 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001016 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001017 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001018 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001019 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001020 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001021 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001022 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001023 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1024 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1025 * \retval #PSA_ERROR_HARDWARE_FAILURE
1026 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001027 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001028psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1029 uint8_t *hash,
1030 size_t hash_size,
1031 size_t *hash_length);
1032
Gilles Peskine308b91d2018-02-08 09:47:44 +01001033/** Finish the calculation of the hash of a message and compare it with
1034 * an expected value.
1035 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001036 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001037 * This function calculates the hash of the message formed by concatenating
1038 * the inputs passed to preceding calls to psa_hash_update(). It then
1039 * compares the calculated hash with the expected hash passed as a
1040 * parameter to this function.
1041 *
1042 * When this function returns, the operation becomes inactive.
1043 *
Gilles Peskine19067982018-03-20 17:54:53 +01001044 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001045 * comparison between the actual hash and the expected hash is performed
1046 * in constant time.
1047 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001048 * \param[in,out] operation Active hash operation.
1049 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001050 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001051 *
Gilles Peskine28538492018-07-11 17:34:00 +02001052 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001053 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001054 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001055 * The hash of the message was calculated successfully, but it
1056 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001057 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001058 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001059 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1060 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1061 * \retval #PSA_ERROR_HARDWARE_FAILURE
1062 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001063 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001064psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1065 const uint8_t *hash,
1066 size_t hash_length);
1067
Gilles Peskine308b91d2018-02-08 09:47:44 +01001068/** Abort a hash operation.
1069 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001070 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001071 * \p operation structure itself. Once aborted, the operation object
1072 * can be reused for another operation by calling
1073 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001075 * You may call this function any time after the operation object has
1076 * been initialized by any of the following methods:
1077 * - A call to psa_hash_setup(), whether it succeeds or not.
1078 * - Initializing the \c struct to all-bits-zero.
1079 * - Initializing the \c struct to logical zeros, e.g.
1080 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001081 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001082 * In particular, calling psa_hash_abort() after the operation has been
1083 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1084 * psa_hash_verify() is safe and has no effect.
1085 *
1086 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001087 *
Gilles Peskine28538492018-07-11 17:34:00 +02001088 * \retval #PSA_SUCCESS
1089 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001090 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001091 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1092 * \retval #PSA_ERROR_HARDWARE_FAILURE
1093 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 */
1095psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001096
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001097/** Clone a hash operation.
1098 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001099 * This function copies the state of an ongoing hash operation to
1100 * a new operation object. In other words, this function is equivalent
1101 * to calling psa_hash_setup() on \p target_operation with the same
1102 * algorithm that \p source_operation was set up for, then
1103 * psa_hash_update() on \p target_operation with the same input that
1104 * that was passed to \p source_operation. After this function returns, the
1105 * two objects are independent, i.e. subsequent calls involving one of
1106 * the objects do not affect the other object.
1107 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001108 * \param[in] source_operation The active hash operation to clone.
1109 * \param[in,out] target_operation The operation object to set up.
1110 * It must be initialized but not active.
1111 *
1112 * \retval #PSA_SUCCESS
1113 * \retval #PSA_ERROR_BAD_STATE
1114 * \p source_operation is not an active hash operation.
1115 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001116 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001117 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1118 * \retval #PSA_ERROR_HARDWARE_FAILURE
1119 * \retval #PSA_ERROR_TAMPERING_DETECTED
1120 */
1121psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1122 psa_hash_operation_t *target_operation);
1123
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001124/**@}*/
1125
Gilles Peskine8c9def32018-02-08 10:02:12 +01001126/** \defgroup MAC Message authentication codes
1127 * @{
1128 */
1129
Gilles Peskine69647a42019-01-14 20:18:12 +01001130/** Calculate the MAC (message authentication code) of a message.
1131 *
1132 * \note To verify the MAC of a message against an
1133 * expected value, use psa_mac_verify() instead.
1134 * Beware that comparing integrity or authenticity data such as
1135 * MAC values with a function such as \c memcmp is risky
1136 * because the time taken by the comparison may leak information
1137 * about the MAC value which could allow an attacker to guess
1138 * a valid MAC and thereby bypass security controls.
1139 *
1140 * \param handle Handle to the key to use for the operation.
1141 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1142 * such that #PSA_ALG_IS_MAC(alg) is true).
1143 * \param[in] input Buffer containing the input message.
1144 * \param input_length Size of the \p input buffer in bytes.
1145 * \param[out] mac Buffer where the MAC value is to be written.
1146 * \param mac_size Size of the \p mac buffer in bytes.
1147 * \param[out] mac_length On success, the number of bytes
1148 * that make up the mac value. This is always
1149 * #PSA_HASH_SIZE(\c alg) where \c alg is the
1150 * hash algorithm that is calculated.
1151 *
1152 * \retval #PSA_SUCCESS
1153 * Success.
1154 * \retval #PSA_ERROR_INVALID_HANDLE
1155 * \retval #PSA_ERROR_EMPTY_SLOT
1156 * \retval #PSA_ERROR_NOT_PERMITTED
1157 * \retval #PSA_ERROR_INVALID_ARGUMENT
1158 * \p key is not compatible with \p alg.
1159 * \retval #PSA_ERROR_NOT_SUPPORTED
1160 * \p alg is not supported or is not a MAC algorithm.
1161 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1162 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1163 * \retval #PSA_ERROR_HARDWARE_FAILURE
1164 * \retval #PSA_ERROR_TAMPERING_DETECTED
1165 * \retval #PSA_ERROR_BAD_STATE
1166 * The library has not been previously initialized by psa_crypto_init().
1167 * It is implementation-dependent whether a failure to initialize
1168 * results in this error code.
1169 */
1170psa_status_t psa_mac_compute(psa_key_handle_t handle,
1171 psa_algorithm_t alg,
1172 const uint8_t *input,
1173 size_t input_length,
1174 uint8_t *mac,
1175 size_t mac_size,
1176 size_t *mac_length);
1177
1178/** Calculate the MAC of a message and compare it with a reference value.
1179 *
1180 * \param handle Handle to the key to use for the operation.
1181 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1182 * such that #PSA_ALG_IS_MAC(alg) is true).
1183 * \param[in] input Buffer containing the input message.
1184 * \param input_length Size of the \p input buffer in bytes.
1185 * \param[out] mac Buffer containing the expected MAC value.
1186 * \param mac_length Size of the \p mac buffer in bytes.
1187 *
1188 * \retval #PSA_SUCCESS
1189 * The expected MAC is identical to the actual MAC of the input.
1190 * \retval #PSA_ERROR_INVALID_SIGNATURE
1191 * The MAC of the message was calculated successfully, but it
1192 * differs from the expected value.
1193 * \retval #PSA_ERROR_INVALID_HANDLE
1194 * \retval #PSA_ERROR_EMPTY_SLOT
1195 * \retval #PSA_ERROR_NOT_PERMITTED
1196 * \retval #PSA_ERROR_INVALID_ARGUMENT
1197 * \p key is not compatible with \p alg.
1198 * \retval #PSA_ERROR_NOT_SUPPORTED
1199 * \p alg is not supported or is not a MAC algorithm.
1200 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1201 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1202 * \retval #PSA_ERROR_HARDWARE_FAILURE
1203 * \retval #PSA_ERROR_TAMPERING_DETECTED
1204 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001205psa_status_t psa_mac_verify(psa_key_handle_t handle,
1206 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001207 const uint8_t *input,
1208 size_t input_length,
1209 const uint8_t *mac,
1210 const size_t mac_length);
1211
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001212/** The type of the state data structure for multipart MAC operations.
1213 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001214 * Before calling any function on a MAC operation object, the application must
1215 * initialize it by any of the following means:
1216 * - Set the structure to all-bits-zero, for example:
1217 * \code
1218 * psa_mac_operation_t operation;
1219 * memset(&operation, 0, sizeof(operation));
1220 * \endcode
1221 * - Initialize the structure to logical zero values, for example:
1222 * \code
1223 * psa_mac_operation_t operation = {0};
1224 * \endcode
1225 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1226 * for example:
1227 * \code
1228 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1229 * \endcode
1230 * - Assign the result of the function psa_mac_operation_init()
1231 * to the structure, for example:
1232 * \code
1233 * psa_mac_operation_t operation;
1234 * operation = psa_mac_operation_init();
1235 * \endcode
1236 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001237 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001238 * make any assumptions about the content of this structure except
1239 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001240typedef struct psa_mac_operation_s psa_mac_operation_t;
1241
Jaeden Amero769ce272019-01-04 11:48:03 +00001242/** \def PSA_MAC_OPERATION_INIT
1243 *
1244 * This macro returns a suitable initializer for a MAC operation object of type
1245 * #psa_mac_operation_t.
1246 */
1247#ifdef __DOXYGEN_ONLY__
1248/* This is an example definition for documentation purposes.
1249 * Implementations should define a suitable value in `crypto_struct.h`.
1250 */
1251#define PSA_MAC_OPERATION_INIT {0}
1252#endif
1253
1254/** Return an initial value for a MAC operation object.
1255 */
1256static psa_mac_operation_t psa_mac_operation_init(void);
1257
Gilles Peskinef45adda2019-01-14 18:29:18 +01001258/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001259 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001260 * This function sets up the calculation of the MAC
1261 * (message authentication code) of a byte string.
1262 * To verify the MAC of a message against an
1263 * expected value, use psa_mac_verify_setup() instead.
1264 *
1265 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001266 * -# Allocate an operation object which will be passed to all the functions
1267 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001268 * -# Initialize the operation object with one of the methods described in the
1269 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001270 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001271 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1272 * of the message each time. The MAC that is calculated is the MAC
1273 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001274 * -# At the end of the message, call psa_mac_sign_finish() to finish
1275 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001276 *
1277 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001278 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001279 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001280 * After a successful call to psa_mac_sign_setup(), the application must
1281 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001282 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001283 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001284 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001285 * \param[in,out] operation The operation object to set up. It must have
1286 * been initialized as per the documentation for
1287 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001288 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001289 * It must remain valid until the operation
1290 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001291 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1292 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001293 *
Gilles Peskine28538492018-07-11 17:34:00 +02001294 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001295 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001296 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001297 * \retval #PSA_ERROR_EMPTY_SLOT
1298 * \retval #PSA_ERROR_NOT_PERMITTED
1299 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001300 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001301 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001302 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001303 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1304 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1305 * \retval #PSA_ERROR_HARDWARE_FAILURE
1306 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001307 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001308 * The library has not been previously initialized by psa_crypto_init().
1309 * It is implementation-dependent whether a failure to initialize
1310 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001311 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001312psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001313 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001314 psa_algorithm_t alg);
1315
Gilles Peskinef45adda2019-01-14 18:29:18 +01001316/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001317 *
1318 * This function sets up the verification of the MAC
1319 * (message authentication code) of a byte string against an expected value.
1320 *
1321 * The sequence of operations to verify a MAC is as follows:
1322 * -# Allocate an operation object which will be passed to all the functions
1323 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001324 * -# Initialize the operation object with one of the methods described in the
1325 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001326 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001327 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1328 * of the message each time. The MAC that is calculated is the MAC
1329 * of the concatenation of these messages in order.
1330 * -# At the end of the message, call psa_mac_verify_finish() to finish
1331 * calculating the actual MAC of the message and verify it against
1332 * the expected value.
1333 *
1334 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001335 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001336 *
1337 * After a successful call to psa_mac_verify_setup(), the application must
1338 * eventually terminate the operation through one of the following methods:
1339 * - A failed call to psa_mac_update().
1340 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1341 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001342 * \param[in,out] operation The operation object to set up. It must have
1343 * been initialized as per the documentation for
1344 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001345 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001346 * It must remain valid until the operation
1347 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001348 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1349 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001350 *
Gilles Peskine28538492018-07-11 17:34:00 +02001351 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001352 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001353 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001354 * \retval #PSA_ERROR_EMPTY_SLOT
1355 * \retval #PSA_ERROR_NOT_PERMITTED
1356 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001357 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001358 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001359 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001360 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1361 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1362 * \retval #PSA_ERROR_HARDWARE_FAILURE
1363 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001364 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001365 * The library has not been previously initialized by psa_crypto_init().
1366 * It is implementation-dependent whether a failure to initialize
1367 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001368 */
1369psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001370 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001371 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001372
Gilles Peskinedcd14942018-07-12 00:30:52 +02001373/** Add a message fragment to a multipart MAC operation.
1374 *
1375 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1376 * before calling this function.
1377 *
1378 * If this function returns an error status, the operation becomes inactive.
1379 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001380 * \param[in,out] operation Active MAC operation.
1381 * \param[in] input Buffer containing the message fragment to add to
1382 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001383 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001384 *
1385 * \retval #PSA_SUCCESS
1386 * Success.
1387 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001388 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001389 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1390 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1391 * \retval #PSA_ERROR_HARDWARE_FAILURE
1392 * \retval #PSA_ERROR_TAMPERING_DETECTED
1393 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001394psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1395 const uint8_t *input,
1396 size_t input_length);
1397
Gilles Peskinedcd14942018-07-12 00:30:52 +02001398/** Finish the calculation of the MAC of a message.
1399 *
1400 * The application must call psa_mac_sign_setup() before calling this function.
1401 * This function calculates the MAC of the message formed by concatenating
1402 * the inputs passed to preceding calls to psa_mac_update().
1403 *
1404 * When this function returns, the operation becomes inactive.
1405 *
1406 * \warning Applications should not call this function if they expect
1407 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1408 * Beware that comparing integrity or authenticity data such as
1409 * MAC values with a function such as \c memcmp is risky
1410 * because the time taken by the comparison may leak information
1411 * about the MAC value which could allow an attacker to guess
1412 * a valid MAC and thereby bypass security controls.
1413 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001414 * \param[in,out] operation Active MAC operation.
1415 * \param[out] mac Buffer where the MAC value is to be written.
1416 * \param mac_size Size of the \p mac buffer in bytes.
1417 * \param[out] mac_length On success, the number of bytes
1418 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001419 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001420 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001421 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001422 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001423 *
1424 * \retval #PSA_SUCCESS
1425 * Success.
1426 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001427 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001428 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001429 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001430 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1431 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1432 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1433 * \retval #PSA_ERROR_HARDWARE_FAILURE
1434 * \retval #PSA_ERROR_TAMPERING_DETECTED
1435 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001436psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1437 uint8_t *mac,
1438 size_t mac_size,
1439 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001440
Gilles Peskinedcd14942018-07-12 00:30:52 +02001441/** Finish the calculation of the MAC of a message and compare it with
1442 * an expected value.
1443 *
1444 * The application must call psa_mac_verify_setup() before calling this function.
1445 * This function calculates the MAC of the message formed by concatenating
1446 * the inputs passed to preceding calls to psa_mac_update(). It then
1447 * compares the calculated MAC with the expected MAC passed as a
1448 * parameter to this function.
1449 *
1450 * When this function returns, the operation becomes inactive.
1451 *
1452 * \note Implementations shall make the best effort to ensure that the
1453 * comparison between the actual MAC and the expected MAC is performed
1454 * in constant time.
1455 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001456 * \param[in,out] operation Active MAC operation.
1457 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001458 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001459 *
1460 * \retval #PSA_SUCCESS
1461 * The expected MAC is identical to the actual MAC of the message.
1462 * \retval #PSA_ERROR_INVALID_SIGNATURE
1463 * The MAC of the message was calculated successfully, but it
1464 * differs from the expected MAC.
1465 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001466 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001467 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1468 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1469 * \retval #PSA_ERROR_HARDWARE_FAILURE
1470 * \retval #PSA_ERROR_TAMPERING_DETECTED
1471 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001472psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1473 const uint8_t *mac,
1474 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001475
Gilles Peskinedcd14942018-07-12 00:30:52 +02001476/** Abort a MAC operation.
1477 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001478 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001479 * \p operation structure itself. Once aborted, the operation object
1480 * can be reused for another operation by calling
1481 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001482 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001483 * You may call this function any time after the operation object has
1484 * been initialized by any of the following methods:
1485 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1486 * it succeeds or not.
1487 * - Initializing the \c struct to all-bits-zero.
1488 * - Initializing the \c struct to logical zeros, e.g.
1489 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001490 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001491 * In particular, calling psa_mac_abort() after the operation has been
1492 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1493 * psa_mac_verify_finish() is safe and has no effect.
1494 *
1495 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001496 *
1497 * \retval #PSA_SUCCESS
1498 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001499 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001500 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1501 * \retval #PSA_ERROR_HARDWARE_FAILURE
1502 * \retval #PSA_ERROR_TAMPERING_DETECTED
1503 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001504psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1505
1506/**@}*/
1507
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001508/** \defgroup cipher Symmetric ciphers
1509 * @{
1510 */
1511
Gilles Peskine69647a42019-01-14 20:18:12 +01001512/** Encrypt a message using a symmetric cipher.
1513 *
1514 * This function encrypts a message with a random IV (initialization
1515 * vector).
1516 *
1517 * \param handle Handle to the key to use for the operation.
1518 * It must remain valid until the operation
1519 * terminates.
1520 * \param alg The cipher algorithm to compute
1521 * (\c PSA_ALG_XXX value such that
1522 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1523 * \param[in] input Buffer containing the message to encrypt.
1524 * \param input_length Size of the \p input buffer in bytes.
1525 * \param[out] output Buffer where the output is to be written.
1526 * The output contains the IV followed by
1527 * the ciphertext proper.
1528 * \param output_size Size of the \p output buffer in bytes.
1529 * \param[out] output_length On success, the number of bytes
1530 * that make up the output.
1531 *
1532 * \retval #PSA_SUCCESS
1533 * Success.
1534 * \retval #PSA_ERROR_INVALID_HANDLE
1535 * \retval #PSA_ERROR_EMPTY_SLOT
1536 * \retval #PSA_ERROR_NOT_PERMITTED
1537 * \retval #PSA_ERROR_INVALID_ARGUMENT
1538 * \p key is not compatible with \p alg.
1539 * \retval #PSA_ERROR_NOT_SUPPORTED
1540 * \p alg is not supported or is not a cipher algorithm.
1541 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1542 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1543 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1544 * \retval #PSA_ERROR_HARDWARE_FAILURE
1545 * \retval #PSA_ERROR_TAMPERING_DETECTED
1546 */
1547psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1548 psa_algorithm_t alg,
1549 const uint8_t *input,
1550 size_t input_length,
1551 uint8_t *output,
1552 size_t output_size,
1553 size_t *output_length);
1554
1555/** Decrypt a message using a symmetric cipher.
1556 *
1557 * This function decrypts a message encrypted with a symmetric cipher.
1558 *
1559 * \param handle Handle to the key to use for the operation.
1560 * It must remain valid until the operation
1561 * terminates.
1562 * \param alg The cipher algorithm to compute
1563 * (\c PSA_ALG_XXX value such that
1564 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1565 * \param[in] input Buffer containing the message to decrypt.
1566 * This consists of the IV followed by the
1567 * ciphertext proper.
1568 * \param input_length Size of the \p input buffer in bytes.
1569 * \param[out] output Buffer where the plaintext is to be written.
1570 * \param output_size Size of the \p output buffer in bytes.
1571 * \param[out] output_length On success, the number of bytes
1572 * that make up the output.
1573 *
1574 * \retval #PSA_SUCCESS
1575 * Success.
1576 * \retval #PSA_ERROR_INVALID_HANDLE
1577 * \retval #PSA_ERROR_EMPTY_SLOT
1578 * \retval #PSA_ERROR_NOT_PERMITTED
1579 * \retval #PSA_ERROR_INVALID_ARGUMENT
1580 * \p key is not compatible with \p alg.
1581 * \retval #PSA_ERROR_NOT_SUPPORTED
1582 * \p alg is not supported or is not a cipher algorithm.
1583 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1584 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1585 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1586 * \retval #PSA_ERROR_HARDWARE_FAILURE
1587 * \retval #PSA_ERROR_TAMPERING_DETECTED
1588 */
1589psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1590 psa_algorithm_t alg,
1591 const uint8_t *input,
1592 size_t input_length,
1593 uint8_t *output,
1594 size_t output_size,
1595 size_t *output_length);
1596
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001597/** The type of the state data structure for multipart cipher operations.
1598 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001599 * Before calling any function on a cipher operation object, the application
1600 * must initialize it by any of the following means:
1601 * - Set the structure to all-bits-zero, for example:
1602 * \code
1603 * psa_cipher_operation_t operation;
1604 * memset(&operation, 0, sizeof(operation));
1605 * \endcode
1606 * - Initialize the structure to logical zero values, for example:
1607 * \code
1608 * psa_cipher_operation_t operation = {0};
1609 * \endcode
1610 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1611 * for example:
1612 * \code
1613 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1614 * \endcode
1615 * - Assign the result of the function psa_cipher_operation_init()
1616 * to the structure, for example:
1617 * \code
1618 * psa_cipher_operation_t operation;
1619 * operation = psa_cipher_operation_init();
1620 * \endcode
1621 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001622 * This is an implementation-defined \c struct. Applications should not
1623 * make any assumptions about the content of this structure except
1624 * as directed by the documentation of a specific implementation. */
1625typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1626
Jaeden Amero5bae2272019-01-04 11:48:27 +00001627/** \def PSA_CIPHER_OPERATION_INIT
1628 *
1629 * This macro returns a suitable initializer for a cipher operation object of
1630 * type #psa_cipher_operation_t.
1631 */
1632#ifdef __DOXYGEN_ONLY__
1633/* This is an example definition for documentation purposes.
1634 * Implementations should define a suitable value in `crypto_struct.h`.
1635 */
1636#define PSA_CIPHER_OPERATION_INIT {0}
1637#endif
1638
1639/** Return an initial value for a cipher operation object.
1640 */
1641static psa_cipher_operation_t psa_cipher_operation_init(void);
1642
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001643/** Set the key for a multipart symmetric encryption operation.
1644 *
1645 * The sequence of operations to encrypt a message with a symmetric cipher
1646 * is as follows:
1647 * -# Allocate an operation object which will be passed to all the functions
1648 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001649 * -# Initialize the operation object with one of the methods described in the
1650 * documentation for #psa_cipher_operation_t, e.g.
1651 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001652 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001653 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001654 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001655 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001656 * requires a specific IV value.
1657 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1658 * of the message each time.
1659 * -# Call psa_cipher_finish().
1660 *
1661 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001662 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001663 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001664 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001665 * eventually terminate the operation. The following events terminate an
1666 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001667 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001668 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001669 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001670 * \param[in,out] operation The operation object to set up. It must have
1671 * been initialized as per the documentation for
1672 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001673 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001674 * It must remain valid until the operation
1675 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001676 * \param alg The cipher algorithm to compute
1677 * (\c PSA_ALG_XXX value such that
1678 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001679 *
Gilles Peskine28538492018-07-11 17:34:00 +02001680 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001681 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001682 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001683 * \retval #PSA_ERROR_EMPTY_SLOT
1684 * \retval #PSA_ERROR_NOT_PERMITTED
1685 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001686 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001687 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001688 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001689 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1690 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1691 * \retval #PSA_ERROR_HARDWARE_FAILURE
1692 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001693 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001694 * The library has not been previously initialized by psa_crypto_init().
1695 * It is implementation-dependent whether a failure to initialize
1696 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001697 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001698psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001699 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001700 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001701
1702/** Set the key for a multipart symmetric decryption operation.
1703 *
1704 * The sequence of operations to decrypt a message with a symmetric cipher
1705 * is as follows:
1706 * -# Allocate an operation object which will be passed to all the functions
1707 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001708 * -# Initialize the operation object with one of the methods described in the
1709 * documentation for #psa_cipher_operation_t, e.g.
1710 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001711 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001712 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001713 * decryption. If the IV is prepended to the ciphertext, you can call
1714 * psa_cipher_update() on a buffer containing the IV followed by the
1715 * beginning of the message.
1716 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1717 * of the message each time.
1718 * -# Call psa_cipher_finish().
1719 *
1720 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001721 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001722 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001723 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001724 * eventually terminate the operation. The following events terminate an
1725 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001726 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001727 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001728 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001729 * \param[in,out] operation The operation object to set up. It must have
1730 * been initialized as per the documentation for
1731 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001732 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001733 * It must remain valid until the operation
1734 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001735 * \param alg The cipher algorithm to compute
1736 * (\c PSA_ALG_XXX value such that
1737 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001738 *
Gilles Peskine28538492018-07-11 17:34:00 +02001739 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001740 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001741 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001742 * \retval #PSA_ERROR_EMPTY_SLOT
1743 * \retval #PSA_ERROR_NOT_PERMITTED
1744 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001745 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001746 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001747 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001748 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1749 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1750 * \retval #PSA_ERROR_HARDWARE_FAILURE
1751 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001752 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001753 * The library has not been previously initialized by psa_crypto_init().
1754 * It is implementation-dependent whether a failure to initialize
1755 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001756 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001757psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001758 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001759 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001760
Gilles Peskinedcd14942018-07-12 00:30:52 +02001761/** Generate an IV for a symmetric encryption operation.
1762 *
1763 * This function generates a random IV (initialization vector), nonce
1764 * or initial counter value for the encryption operation as appropriate
1765 * for the chosen algorithm, key type and key size.
1766 *
1767 * The application must call psa_cipher_encrypt_setup() before
1768 * calling this function.
1769 *
1770 * If this function returns an error status, the operation becomes inactive.
1771 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001772 * \param[in,out] operation Active cipher operation.
1773 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001774 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001775 * \param[out] iv_length On success, the number of bytes of the
1776 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001777 *
1778 * \retval #PSA_SUCCESS
1779 * Success.
1780 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001781 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001782 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001783 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001784 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1785 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1786 * \retval #PSA_ERROR_HARDWARE_FAILURE
1787 * \retval #PSA_ERROR_TAMPERING_DETECTED
1788 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001789psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1790 unsigned char *iv,
1791 size_t iv_size,
1792 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001793
Gilles Peskinedcd14942018-07-12 00:30:52 +02001794/** Set the IV for a symmetric encryption or decryption operation.
1795 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001796 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001797 * or initial counter value for the encryption or decryption operation.
1798 *
1799 * The application must call psa_cipher_encrypt_setup() before
1800 * calling this function.
1801 *
1802 * If this function returns an error status, the operation becomes inactive.
1803 *
1804 * \note When encrypting, applications should use psa_cipher_generate_iv()
1805 * instead of this function, unless implementing a protocol that requires
1806 * a non-random IV.
1807 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001808 * \param[in,out] operation Active cipher operation.
1809 * \param[in] iv Buffer containing the IV to use.
1810 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001811 *
1812 * \retval #PSA_SUCCESS
1813 * Success.
1814 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001815 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001816 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001817 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001818 * or the chosen algorithm does not use an IV.
1819 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1820 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1821 * \retval #PSA_ERROR_HARDWARE_FAILURE
1822 * \retval #PSA_ERROR_TAMPERING_DETECTED
1823 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001824psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1825 const unsigned char *iv,
1826 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001827
Gilles Peskinedcd14942018-07-12 00:30:52 +02001828/** Encrypt or decrypt a message fragment in an active cipher operation.
1829 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001830 * Before calling this function, you must:
1831 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1832 * The choice of setup function determines whether this function
1833 * encrypts or decrypts its input.
1834 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1835 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001836 *
1837 * If this function returns an error status, the operation becomes inactive.
1838 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001839 * \param[in,out] operation Active cipher operation.
1840 * \param[in] input Buffer containing the message fragment to
1841 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001842 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001843 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001844 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001845 * \param[out] output_length On success, the number of bytes
1846 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001847 *
1848 * \retval #PSA_SUCCESS
1849 * Success.
1850 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001851 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001852 * not set, or already completed).
1853 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1854 * The size of the \p output buffer is too small.
1855 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1856 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1857 * \retval #PSA_ERROR_HARDWARE_FAILURE
1858 * \retval #PSA_ERROR_TAMPERING_DETECTED
1859 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001860psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1861 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001862 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001863 unsigned char *output,
1864 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001865 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001866
Gilles Peskinedcd14942018-07-12 00:30:52 +02001867/** Finish encrypting or decrypting a message in a cipher operation.
1868 *
1869 * The application must call psa_cipher_encrypt_setup() or
1870 * psa_cipher_decrypt_setup() before calling this function. The choice
1871 * of setup function determines whether this function encrypts or
1872 * decrypts its input.
1873 *
1874 * This function finishes the encryption or decryption of the message
1875 * formed by concatenating the inputs passed to preceding calls to
1876 * psa_cipher_update().
1877 *
1878 * When this function returns, the operation becomes inactive.
1879 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001880 * \param[in,out] operation Active cipher operation.
1881 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001882 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001883 * \param[out] output_length On success, the number of bytes
1884 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001885 *
1886 * \retval #PSA_SUCCESS
1887 * Success.
1888 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001889 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001890 * not set, or already completed).
1891 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1892 * The size of the \p output buffer is too small.
1893 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1894 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1895 * \retval #PSA_ERROR_HARDWARE_FAILURE
1896 * \retval #PSA_ERROR_TAMPERING_DETECTED
1897 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001898psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001899 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001900 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001901 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001902
Gilles Peskinedcd14942018-07-12 00:30:52 +02001903/** Abort a cipher operation.
1904 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001905 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001906 * \p operation structure itself. Once aborted, the operation object
1907 * can be reused for another operation by calling
1908 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001909 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001910 * You may call this function any time after the operation object has
1911 * been initialized by any of the following methods:
1912 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1913 * whether it succeeds or not.
1914 * - Initializing the \c struct to all-bits-zero.
1915 * - Initializing the \c struct to logical zeros, e.g.
1916 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001917 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001918 * In particular, calling psa_cipher_abort() after the operation has been
1919 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1920 * is safe and has no effect.
1921 *
1922 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001923 *
1924 * \retval #PSA_SUCCESS
1925 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001926 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001927 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1928 * \retval #PSA_ERROR_HARDWARE_FAILURE
1929 * \retval #PSA_ERROR_TAMPERING_DETECTED
1930 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001931psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1932
1933/**@}*/
1934
Gilles Peskine3b555712018-03-03 21:27:57 +01001935/** \defgroup aead Authenticated encryption with associated data (AEAD)
1936 * @{
1937 */
1938
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001939/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001940 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001941 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001942 * \param alg The AEAD algorithm to compute
1943 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001944 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001945 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001946 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001947 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001948 * but not encrypted.
1949 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001950 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001951 * encrypted.
1952 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001953 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001954 * encrypted data. The additional data is not
1955 * part of this output. For algorithms where the
1956 * encrypted data and the authentication tag
1957 * are defined as separate outputs, the
1958 * authentication tag is appended to the
1959 * encrypted data.
1960 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1961 * This must be at least
1962 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1963 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001964 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001965 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001966 *
Gilles Peskine28538492018-07-11 17:34:00 +02001967 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001968 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001969 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001970 * \retval #PSA_ERROR_EMPTY_SLOT
1971 * \retval #PSA_ERROR_NOT_PERMITTED
1972 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001973 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001974 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001975 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001976 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1977 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1978 * \retval #PSA_ERROR_HARDWARE_FAILURE
1979 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001980 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001981 * The library has not been previously initialized by psa_crypto_init().
1982 * It is implementation-dependent whether a failure to initialize
1983 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001984 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001985psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001986 psa_algorithm_t alg,
1987 const uint8_t *nonce,
1988 size_t nonce_length,
1989 const uint8_t *additional_data,
1990 size_t additional_data_length,
1991 const uint8_t *plaintext,
1992 size_t plaintext_length,
1993 uint8_t *ciphertext,
1994 size_t ciphertext_size,
1995 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001996
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001997/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001998 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001999 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002000 * \param alg The AEAD algorithm to compute
2001 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002002 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002003 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002004 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002005 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002006 * but not encrypted.
2007 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002008 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002009 * encrypted. For algorithms where the
2010 * encrypted data and the authentication tag
2011 * are defined as separate inputs, the buffer
2012 * must contain the encrypted data followed
2013 * by the authentication tag.
2014 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002015 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002016 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2017 * This must be at least
2018 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2019 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002020 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002021 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002022 *
Gilles Peskine28538492018-07-11 17:34:00 +02002023 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002024 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002025 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002026 * \retval #PSA_ERROR_EMPTY_SLOT
2027 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002028 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002029 * \retval #PSA_ERROR_NOT_PERMITTED
2030 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002031 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002032 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002033 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002034 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2035 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2036 * \retval #PSA_ERROR_HARDWARE_FAILURE
2037 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002038 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002039 * The library has not been previously initialized by psa_crypto_init().
2040 * It is implementation-dependent whether a failure to initialize
2041 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002042 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002043psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002044 psa_algorithm_t alg,
2045 const uint8_t *nonce,
2046 size_t nonce_length,
2047 const uint8_t *additional_data,
2048 size_t additional_data_length,
2049 const uint8_t *ciphertext,
2050 size_t ciphertext_length,
2051 uint8_t *plaintext,
2052 size_t plaintext_size,
2053 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002054
Gilles Peskine30a9e412019-01-14 18:36:12 +01002055/** The type of the state data structure for multipart AEAD operations.
2056 *
2057 * Before calling any function on an AEAD operation object, the application
2058 * must initialize it by any of the following means:
2059 * - Set the structure to all-bits-zero, for example:
2060 * \code
2061 * psa_aead_operation_t operation;
2062 * memset(&operation, 0, sizeof(operation));
2063 * \endcode
2064 * - Initialize the structure to logical zero values, for example:
2065 * \code
2066 * psa_aead_operation_t operation = {0};
2067 * \endcode
2068 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2069 * for example:
2070 * \code
2071 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2072 * \endcode
2073 * - Assign the result of the function psa_aead_operation_init()
2074 * to the structure, for example:
2075 * \code
2076 * psa_aead_operation_t operation;
2077 * operation = psa_aead_operation_init();
2078 * \endcode
2079 *
2080 * This is an implementation-defined \c struct. Applications should not
2081 * make any assumptions about the content of this structure except
2082 * as directed by the documentation of a specific implementation. */
2083typedef struct psa_aead_operation_s psa_aead_operation_t;
2084
2085/** \def PSA_AEAD_OPERATION_INIT
2086 *
2087 * This macro returns a suitable initializer for an AEAD operation object of
2088 * type #psa_aead_operation_t.
2089 */
2090#ifdef __DOXYGEN_ONLY__
2091/* This is an example definition for documentation purposes.
2092 * Implementations should define a suitable value in `crypto_struct.h`.
2093 */
2094#define PSA_AEAD_OPERATION_INIT {0}
2095#endif
2096
2097/** Return an initial value for an AEAD operation object.
2098 */
2099static psa_aead_operation_t psa_aead_operation_init(void);
2100
2101/** Set the key for a multipart authenticated encryption operation.
2102 *
2103 * The sequence of operations to encrypt a message with authentication
2104 * is as follows:
2105 * -# Allocate an operation object which will be passed to all the functions
2106 * listed here.
2107 * -# Initialize the operation object with one of the methods described in the
2108 * documentation for #psa_aead_operation_t, e.g.
2109 * PSA_AEAD_OPERATION_INIT.
2110 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002111 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2112 * inputs to the subsequent calls to psa_aead_update_ad() and
2113 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2114 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002115 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2116 * generate or set the nonce. You should use
2117 * psa_aead_generate_nonce() unless the protocol you are implementing
2118 * requires a specific nonce value.
2119 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2120 * of the non-encrypted additional authenticated data each time.
2121 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002122 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002123 * -# Call psa_aead_finish().
2124 *
2125 * The application may call psa_aead_abort() at any time after the operation
2126 * has been initialized.
2127 *
2128 * After a successful call to psa_aead_encrypt_setup(), the application must
2129 * eventually terminate the operation. The following events terminate an
2130 * operation:
2131 * - A failed call to any of the \c psa_aead_xxx functions.
2132 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2133 *
2134 * \param[in,out] operation The operation object to set up. It must have
2135 * been initialized as per the documentation for
2136 * #psa_aead_operation_t and not yet in use.
2137 * \param handle Handle to the key to use for the operation.
2138 * It must remain valid until the operation
2139 * terminates.
2140 * \param alg The AEAD algorithm to compute
2141 * (\c PSA_ALG_XXX value such that
2142 * #PSA_ALG_IS_AEAD(\p alg) is true).
2143 *
2144 * \retval #PSA_SUCCESS
2145 * Success.
2146 * \retval #PSA_ERROR_INVALID_HANDLE
2147 * \retval #PSA_ERROR_EMPTY_SLOT
2148 * \retval #PSA_ERROR_NOT_PERMITTED
2149 * \retval #PSA_ERROR_INVALID_ARGUMENT
2150 * \p key is not compatible with \p alg.
2151 * \retval #PSA_ERROR_NOT_SUPPORTED
2152 * \p alg is not supported or is not an AEAD algorithm.
2153 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2154 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2155 * \retval #PSA_ERROR_HARDWARE_FAILURE
2156 * \retval #PSA_ERROR_TAMPERING_DETECTED
2157 * \retval #PSA_ERROR_BAD_STATE
2158 * The library has not been previously initialized by psa_crypto_init().
2159 * It is implementation-dependent whether a failure to initialize
2160 * results in this error code.
2161 */
2162psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2163 psa_key_handle_t handle,
2164 psa_algorithm_t alg);
2165
2166/** Set the key for a multipart authenticated decryption operation.
2167 *
2168 * The sequence of operations to decrypt a message with authentication
2169 * is as follows:
2170 * -# Allocate an operation object which will be passed to all the functions
2171 * listed here.
2172 * -# Initialize the operation object with one of the methods described in the
2173 * documentation for #psa_aead_operation_t, e.g.
2174 * PSA_AEAD_OPERATION_INIT.
2175 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002176 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2177 * inputs to the subsequent calls to psa_aead_update_ad() and
2178 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2179 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002180 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2181 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2182 * of the non-encrypted additional authenticated data each time.
2183 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002184 * of the ciphertext to decrypt each time.
2185 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002186 *
2187 * The application may call psa_aead_abort() at any time after the operation
2188 * has been initialized.
2189 *
2190 * After a successful call to psa_aead_decrypt_setup(), the application must
2191 * eventually terminate the operation. The following events terminate an
2192 * operation:
2193 * - A failed call to any of the \c psa_aead_xxx functions.
2194 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2195 *
2196 * \param[in,out] operation The operation object to set up. It must have
2197 * been initialized as per the documentation for
2198 * #psa_aead_operation_t and not yet in use.
2199 * \param handle Handle to the key to use for the operation.
2200 * It must remain valid until the operation
2201 * terminates.
2202 * \param alg The AEAD algorithm to compute
2203 * (\c PSA_ALG_XXX value such that
2204 * #PSA_ALG_IS_AEAD(\p alg) is true).
2205 *
2206 * \retval #PSA_SUCCESS
2207 * Success.
2208 * \retval #PSA_ERROR_INVALID_HANDLE
2209 * \retval #PSA_ERROR_EMPTY_SLOT
2210 * \retval #PSA_ERROR_NOT_PERMITTED
2211 * \retval #PSA_ERROR_INVALID_ARGUMENT
2212 * \p key is not compatible with \p alg.
2213 * \retval #PSA_ERROR_NOT_SUPPORTED
2214 * \p alg is not supported or is not an AEAD algorithm.
2215 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2216 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2217 * \retval #PSA_ERROR_HARDWARE_FAILURE
2218 * \retval #PSA_ERROR_TAMPERING_DETECTED
2219 * \retval #PSA_ERROR_BAD_STATE
2220 * The library has not been previously initialized by psa_crypto_init().
2221 * It is implementation-dependent whether a failure to initialize
2222 * results in this error code.
2223 */
2224psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2225 psa_key_handle_t handle,
2226 psa_algorithm_t alg);
2227
2228/** Generate a random nonce for an authenticated encryption operation.
2229 *
2230 * This function generates a random nonce for the authenticated encryption
2231 * operation with an appropriate size for the chosen algorithm, key type
2232 * and key size.
2233 *
2234 * The application must call psa_aead_encrypt_setup() before
2235 * calling this function.
2236 *
2237 * If this function returns an error status, the operation becomes inactive.
2238 *
2239 * \param[in,out] operation Active AEAD operation.
2240 * \param[out] nonce Buffer where the generated nonce is to be
2241 * written.
2242 * \param nonce_size Size of the \p nonce buffer in bytes.
2243 * \param[out] nonce_length On success, the number of bytes of the
2244 * generated nonce.
2245 *
2246 * \retval #PSA_SUCCESS
2247 * Success.
2248 * \retval #PSA_ERROR_BAD_STATE
2249 * The operation state is not valid (not set up, or nonce already set).
2250 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2251 * The size of the \p nonce buffer is too small.
2252 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2253 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2254 * \retval #PSA_ERROR_HARDWARE_FAILURE
2255 * \retval #PSA_ERROR_TAMPERING_DETECTED
2256 */
2257psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2258 unsigned char *nonce,
2259 size_t nonce_size,
2260 size_t *nonce_length);
2261
2262/** Set the nonce for an authenticated encryption or decryption operation.
2263 *
2264 * This function sets the nonce for the authenticated
2265 * encryption or decryption operation.
2266 *
2267 * The application must call psa_aead_encrypt_setup() before
2268 * calling this function.
2269 *
2270 * If this function returns an error status, the operation becomes inactive.
2271 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002272 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002273 * instead of this function, unless implementing a protocol that requires
2274 * a non-random IV.
2275 *
2276 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002277 * \param[in] nonce Buffer containing the nonce to use.
2278 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002279 *
2280 * \retval #PSA_SUCCESS
2281 * Success.
2282 * \retval #PSA_ERROR_BAD_STATE
2283 * The operation state is not valid (not set up, or nonce already set).
2284 * \retval #PSA_ERROR_INVALID_ARGUMENT
2285 * The size of \p nonce is not acceptable for the chosen algorithm.
2286 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2287 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2288 * \retval #PSA_ERROR_HARDWARE_FAILURE
2289 * \retval #PSA_ERROR_TAMPERING_DETECTED
2290 */
2291psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2292 const unsigned char *nonce,
2293 size_t nonce_length);
2294
Gilles Peskinebc59c852019-01-17 15:26:08 +01002295/** Declare the lengths of the message and additional data for AEAD.
2296 *
2297 * The application must call this function before calling
2298 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2299 * the operation requires it. If the algorithm does not require it,
2300 * calling this function is optional, but if this function is called
2301 * then the implementation must enforce the lengths.
2302 *
2303 * You may call this function before or after setting the nonce with
2304 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2305 *
2306 * - For #PSA_ALG_CCM, calling this function is required.
2307 * - For the other AEAD algorithms defined in this specification, calling
2308 * this function is not required.
2309 * - For vendor-defined algorithm, refer to the vendor documentation.
2310 *
2311 * \param[in,out] operation Active AEAD operation.
2312 * \param ad_length Size of the non-encrypted additional
2313 * authenticated data in bytes.
2314 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2315 *
2316 * \retval #PSA_SUCCESS
2317 * Success.
2318 * \retval #PSA_ERROR_BAD_STATE
2319 * The operation state is not valid (not set up, already completed,
2320 * or psa_aead_update_ad() or psa_aead_update() already called).
2321 * \retval #PSA_ERROR_INVALID_ARGUMENT
2322 * At least one of the lengths is not acceptable for the chosen
2323 * algorithm.
2324 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2325 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2326 * \retval #PSA_ERROR_HARDWARE_FAILURE
2327 * \retval #PSA_ERROR_TAMPERING_DETECTED
2328 */
2329psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2330 size_t ad_length,
2331 size_t plaintext_length);
2332
Gilles Peskine30a9e412019-01-14 18:36:12 +01002333/** Pass additional data to an active AEAD operation.
2334 *
2335 * Additional data is authenticated, but not encrypted.
2336 *
2337 * You may call this function multiple times to pass successive fragments
2338 * of the additional data. You may not call this function after passing
2339 * data to encrypt or decrypt with psa_aead_update().
2340 *
2341 * Before calling this function, you must:
2342 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2343 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2344 *
2345 * If this function returns an error status, the operation becomes inactive.
2346 *
2347 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2348 * there is no guarantee that the input is valid. Therefore, until
2349 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2350 * treat the input as untrusted and prepare to undo any action that
2351 * depends on the input if psa_aead_verify() returns an error status.
2352 *
2353 * \param[in,out] operation Active AEAD operation.
2354 * \param[in] input Buffer containing the fragment of
2355 * additional data.
2356 * \param input_length Size of the \p input buffer in bytes.
2357 *
2358 * \retval #PSA_SUCCESS
2359 * Success.
2360 * \retval #PSA_ERROR_BAD_STATE
2361 * The operation state is not valid (not set up, nonce not set,
2362 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002363 * \retval #PSA_ERROR_INVALID_ARGUMENT
2364 * The total input length overflows the additional data length that
2365 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002366 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2367 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2368 * \retval #PSA_ERROR_HARDWARE_FAILURE
2369 * \retval #PSA_ERROR_TAMPERING_DETECTED
2370 */
2371psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2372 const uint8_t *input,
2373 size_t input_length);
2374
2375/** Encrypt or decrypt a message fragment in an active AEAD operation.
2376 *
2377 * Before calling this function, you must:
2378 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2379 * The choice of setup function determines whether this function
2380 * encrypts or decrypts its input.
2381 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2382 * 3. Call psa_aead_update_ad() to pass all the additional data.
2383 *
2384 * If this function returns an error status, the operation becomes inactive.
2385 *
2386 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2387 * there is no guarantee that the input is valid. Therefore, until
2388 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2389 * - Do not use the output in any way other than storing it in a
2390 * confidential location. If you take any action that depends
2391 * on the tentative decrypted data, this action will need to be
2392 * undone if the input turns out not to be valid. Furthermore,
2393 * if an adversary can observe that this action took place
2394 * (for example through timing), they may be able to use this
2395 * fact as an oracle to decrypt any message encrypted with the
2396 * same key.
2397 * - In particular, do not copy the output anywhere but to a
2398 * memory or storage space that you have exclusive access to.
2399 *
2400 * \param[in,out] operation Active AEAD operation.
2401 * \param[in] input Buffer containing the message fragment to
2402 * encrypt or decrypt.
2403 * \param input_length Size of the \p input buffer in bytes.
2404 * \param[out] output Buffer where the output is to be written.
2405 * \param output_size Size of the \p output buffer in bytes.
2406 * \param[out] output_length On success, the number of bytes
2407 * that make up the returned output.
2408 *
2409 * \retval #PSA_SUCCESS
2410 * Success.
2411 * \retval #PSA_ERROR_BAD_STATE
2412 * The operation state is not valid (not set up, nonce not set
2413 * or already completed).
2414 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2415 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002416 * \retval #PSA_ERROR_INVALID_ARGUMENT
2417 * The total length of input to psa_aead_update_ad() so far is
2418 * less than the additional data length that was previously
2419 * specified with psa_aead_set_lengths().
2420 * \retval #PSA_ERROR_INVALID_ARGUMENT
2421 * The total input length overflows the plaintext length that
2422 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002423 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2424 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2425 * \retval #PSA_ERROR_HARDWARE_FAILURE
2426 * \retval #PSA_ERROR_TAMPERING_DETECTED
2427 */
2428psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2429 const uint8_t *input,
2430 size_t input_length,
2431 unsigned char *output,
2432 size_t output_size,
2433 size_t *output_length);
2434
2435/** Finish encrypting a message in an AEAD operation.
2436 *
2437 * The operation must have been set up with psa_aead_encrypt_setup().
2438 *
2439 * This function finishes the authentication of the additional data
2440 * formed by concatenating the inputs passed to preceding calls to
2441 * psa_aead_update_ad() with the plaintext formed by concatenating the
2442 * inputs passed to preceding calls to psa_aead_update().
2443 *
2444 * This function has two output buffers:
2445 * - \p ciphertext contains trailing ciphertext that was buffered from
2446 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2447 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2448 * will not contain any output and can be a 0-sized buffer.
2449 * - \p tag contains the authentication tag. Its length is always
2450 * #PSA_AEAD_TAG_LENGTH(\p alg) where \p alg is the AEAD algorithm
2451 * that the operation performs.
2452 *
2453 * When this function returns, the operation becomes inactive.
2454 *
2455 * \param[in,out] operation Active AEAD operation.
2456 * \param[out] ciphertext Buffer where the last part of the ciphertext
2457 * is to be written.
2458 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2459 * \param[out] ciphertext_length On success, the number of bytes of
2460 * returned ciphertext.
2461 * \param[out] tag Buffer where the authentication tag is
2462 * to be written.
2463 * \param tag_size Size of the \p tag buffer in bytes.
2464 * \param[out] tag_length On success, the number of bytes
2465 * that make up the returned tag.
2466 *
2467 * \retval #PSA_SUCCESS
2468 * Success.
2469 * \retval #PSA_ERROR_BAD_STATE
2470 * The operation state is not valid (not set up, nonce not set,
2471 * decryption, or already completed).
2472 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2473 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002474 * \retval #PSA_ERROR_INVALID_ARGUMENT
2475 * The total length of input to psa_aead_update_ad() so far is
2476 * less than the additional data length that was previously
2477 * specified with psa_aead_set_lengths().
2478 * \retval #PSA_ERROR_INVALID_ARGUMENT
2479 * The total length of input to psa_aead_update() so far is
2480 * less than the plaintext length that was previously
2481 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002482 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2483 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2484 * \retval #PSA_ERROR_HARDWARE_FAILURE
2485 * \retval #PSA_ERROR_TAMPERING_DETECTED
2486 */
2487psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002488 uint8_t *ciphertext,
2489 size_t ciphertext_size,
2490 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002491 uint8_t *tag,
2492 size_t tag_size,
2493 size_t *tag_length);
2494
2495/** Finish authenticating and decrypting a message in an AEAD operation.
2496 *
2497 * The operation must have been set up with psa_aead_decrypt_setup().
2498 *
2499 * This function finishes the authentication of the additional data
2500 * formed by concatenating the inputs passed to preceding calls to
2501 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2502 * inputs passed to preceding calls to psa_aead_update().
2503 *
2504 * When this function returns, the operation becomes inactive.
2505 *
2506 * \param[in,out] operation Active AEAD operation.
2507 * \param[in] tag Buffer containing the authentication tag.
2508 * \param tag_length Size of the \p tag buffer in bytes.
2509 *
2510 * \retval #PSA_SUCCESS
2511 * Success.
2512 * \retval #PSA_ERROR_BAD_STATE
2513 * The operation state is not valid (not set up, nonce not set,
2514 * encryption, or already completed).
2515 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2516 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002517 * \retval #PSA_ERROR_INVALID_ARGUMENT
2518 * The total length of input to psa_aead_update_ad() so far is
2519 * less than the additional data length that was previously
2520 * specified with psa_aead_set_lengths().
2521 * \retval #PSA_ERROR_INVALID_ARGUMENT
2522 * The total length of input to psa_aead_update() so far is
2523 * less than the plaintext length that was previously
2524 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002525 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2526 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2527 * \retval #PSA_ERROR_HARDWARE_FAILURE
2528 * \retval #PSA_ERROR_TAMPERING_DETECTED
2529 */
2530psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2531 const uint8_t *tag,
2532 size_t tag_length);
2533
2534/** Abort an AEAD operation.
2535 *
2536 * Aborting an operation frees all associated resources except for the
2537 * \p operation structure itself. Once aborted, the operation object
2538 * can be reused for another operation by calling
2539 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2540 *
2541 * You may call this function any time after the operation object has
2542 * been initialized by any of the following methods:
2543 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2544 * whether it succeeds or not.
2545 * - Initializing the \c struct to all-bits-zero.
2546 * - Initializing the \c struct to logical zeros, e.g.
2547 * `psa_aead_operation_t operation = {0}`.
2548 *
2549 * In particular, calling psa_aead_abort() after the operation has been
2550 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2551 * is safe and has no effect.
2552 *
2553 * \param[in,out] operation Initialized AEAD operation.
2554 *
2555 * \retval #PSA_SUCCESS
2556 * \retval #PSA_ERROR_BAD_STATE
2557 * \p operation is not an active AEAD operation.
2558 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2559 * \retval #PSA_ERROR_HARDWARE_FAILURE
2560 * \retval #PSA_ERROR_TAMPERING_DETECTED
2561 */
2562psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2563
Gilles Peskine3b555712018-03-03 21:27:57 +01002564/**@}*/
2565
Gilles Peskine20035e32018-02-03 22:44:14 +01002566/** \defgroup asymmetric Asymmetric cryptography
2567 * @{
2568 */
2569
2570/**
2571 * \brief Sign a hash or short message with a private key.
2572 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002573 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002574 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002575 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2576 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2577 * to determine the hash algorithm to use.
2578 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002579 * \param handle Handle to the key to use for the operation.
2580 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002581 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002582 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002583 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002584 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002585 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002586 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002587 * \param[out] signature_length On success, the number of bytes
2588 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002589 *
Gilles Peskine28538492018-07-11 17:34:00 +02002590 * \retval #PSA_SUCCESS
2591 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002592 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002593 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002594 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002595 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002596 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002597 * \retval #PSA_ERROR_NOT_SUPPORTED
2598 * \retval #PSA_ERROR_INVALID_ARGUMENT
2599 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2600 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2601 * \retval #PSA_ERROR_HARDWARE_FAILURE
2602 * \retval #PSA_ERROR_TAMPERING_DETECTED
2603 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002604 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002605 * The library has not been previously initialized by psa_crypto_init().
2606 * It is implementation-dependent whether a failure to initialize
2607 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002608 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002609psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002610 psa_algorithm_t alg,
2611 const uint8_t *hash,
2612 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002613 uint8_t *signature,
2614 size_t signature_size,
2615 size_t *signature_length);
2616
2617/**
2618 * \brief Verify the signature a hash or short message using a public key.
2619 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002620 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002621 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002622 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2623 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2624 * to determine the hash algorithm to use.
2625 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002626 * \param handle Handle to the key to use for the operation.
2627 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002628 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002629 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002630 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002631 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002632 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002633 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002634 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002635 *
Gilles Peskine28538492018-07-11 17:34:00 +02002636 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002637 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002638 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002639 * The calculation was perfomed successfully, but the passed
2640 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002641 * \retval #PSA_ERROR_NOT_SUPPORTED
2642 * \retval #PSA_ERROR_INVALID_ARGUMENT
2643 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2644 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2645 * \retval #PSA_ERROR_HARDWARE_FAILURE
2646 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002647 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002648 * The library has not been previously initialized by psa_crypto_init().
2649 * It is implementation-dependent whether a failure to initialize
2650 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002651 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002652psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002653 psa_algorithm_t alg,
2654 const uint8_t *hash,
2655 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002656 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002657 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002658
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002659/**
2660 * \brief Encrypt a short message with a public key.
2661 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002662 * \param handle Handle to the key to use for the operation.
2663 * It must be a public key or an asymmetric
2664 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002665 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002666 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002667 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002668 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002669 * \param[in] salt A salt or label, if supported by the
2670 * encryption algorithm.
2671 * If the algorithm does not support a
2672 * salt, pass \c NULL.
2673 * If the algorithm supports an optional
2674 * salt and you do not want to pass a salt,
2675 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002676 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002677 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2678 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002679 * \param salt_length Size of the \p salt buffer in bytes.
2680 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002681 * \param[out] output Buffer where the encrypted message is to
2682 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002683 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002684 * \param[out] output_length On success, the number of bytes
2685 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002686 *
Gilles Peskine28538492018-07-11 17:34:00 +02002687 * \retval #PSA_SUCCESS
2688 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002689 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002690 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002691 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002692 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002693 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002694 * \retval #PSA_ERROR_NOT_SUPPORTED
2695 * \retval #PSA_ERROR_INVALID_ARGUMENT
2696 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2697 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2698 * \retval #PSA_ERROR_HARDWARE_FAILURE
2699 * \retval #PSA_ERROR_TAMPERING_DETECTED
2700 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002701 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002702 * The library has not been previously initialized by psa_crypto_init().
2703 * It is implementation-dependent whether a failure to initialize
2704 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002705 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002706psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002707 psa_algorithm_t alg,
2708 const uint8_t *input,
2709 size_t input_length,
2710 const uint8_t *salt,
2711 size_t salt_length,
2712 uint8_t *output,
2713 size_t output_size,
2714 size_t *output_length);
2715
2716/**
2717 * \brief Decrypt a short message with a private key.
2718 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002719 * \param handle Handle to the key to use for the operation.
2720 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002721 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002722 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002723 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002724 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002725 * \param[in] salt A salt or label, if supported by the
2726 * encryption algorithm.
2727 * If the algorithm does not support a
2728 * salt, pass \c NULL.
2729 * If the algorithm supports an optional
2730 * salt and you do not want to pass a salt,
2731 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002732 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002733 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2734 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002735 * \param salt_length Size of the \p salt buffer in bytes.
2736 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002737 * \param[out] output Buffer where the decrypted message is to
2738 * be written.
2739 * \param output_size Size of the \c output buffer in bytes.
2740 * \param[out] output_length On success, the number of bytes
2741 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002742 *
Gilles Peskine28538492018-07-11 17:34:00 +02002743 * \retval #PSA_SUCCESS
2744 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002745 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002746 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002747 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002748 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002749 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002750 * \retval #PSA_ERROR_NOT_SUPPORTED
2751 * \retval #PSA_ERROR_INVALID_ARGUMENT
2752 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2753 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2754 * \retval #PSA_ERROR_HARDWARE_FAILURE
2755 * \retval #PSA_ERROR_TAMPERING_DETECTED
2756 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2757 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002758 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002759 * The library has not been previously initialized by psa_crypto_init().
2760 * It is implementation-dependent whether a failure to initialize
2761 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002762 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002763psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002764 psa_algorithm_t alg,
2765 const uint8_t *input,
2766 size_t input_length,
2767 const uint8_t *salt,
2768 size_t salt_length,
2769 uint8_t *output,
2770 size_t output_size,
2771 size_t *output_length);
2772
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002773/**@}*/
2774
Gilles Peskineedd76872018-07-20 17:42:05 +02002775/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002776 * @{
2777 */
2778
2779/** The type of the state data structure for generators.
2780 *
2781 * Before calling any function on a generator, the application must
2782 * initialize it by any of the following means:
2783 * - Set the structure to all-bits-zero, for example:
2784 * \code
2785 * psa_crypto_generator_t generator;
2786 * memset(&generator, 0, sizeof(generator));
2787 * \endcode
2788 * - Initialize the structure to logical zero values, for example:
2789 * \code
2790 * psa_crypto_generator_t generator = {0};
2791 * \endcode
2792 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2793 * for example:
2794 * \code
2795 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2796 * \endcode
2797 * - Assign the result of the function psa_crypto_generator_init()
2798 * to the structure, for example:
2799 * \code
2800 * psa_crypto_generator_t generator;
2801 * generator = psa_crypto_generator_init();
2802 * \endcode
2803 *
2804 * This is an implementation-defined \c struct. Applications should not
2805 * make any assumptions about the content of this structure except
2806 * as directed by the documentation of a specific implementation.
2807 */
2808typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2809
2810/** \def PSA_CRYPTO_GENERATOR_INIT
2811 *
2812 * This macro returns a suitable initializer for a generator object
2813 * of type #psa_crypto_generator_t.
2814 */
2815#ifdef __DOXYGEN_ONLY__
2816/* This is an example definition for documentation purposes.
2817 * Implementations should define a suitable value in `crypto_struct.h`.
2818 */
2819#define PSA_CRYPTO_GENERATOR_INIT {0}
2820#endif
2821
2822/** Return an initial value for a generator object.
2823 */
2824static psa_crypto_generator_t psa_crypto_generator_init(void);
2825
2826/** Retrieve the current capacity of a generator.
2827 *
2828 * The capacity of a generator is the maximum number of bytes that it can
2829 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2830 *
2831 * \param[in] generator The generator to query.
2832 * \param[out] capacity On success, the capacity of the generator.
2833 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002834 * \retval #PSA_SUCCESS
2835 * \retval #PSA_ERROR_BAD_STATE
2836 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002837 */
2838psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2839 size_t *capacity);
2840
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002841/** Set the maximum capacity of a generator.
2842 *
2843 * \param[in,out] generator The generator object to modify.
2844 * \param capacity The new capacity of the generator.
2845 * It must be less or equal to the generator's
2846 * current capacity.
2847 *
2848 * \retval #PSA_SUCCESS
2849 * \retval #PSA_ERROR_INVALID_ARGUMENT
2850 * \p capacity is larger than the generator's current capacity.
2851 * \retval #PSA_ERROR_BAD_STATE
2852 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2853 */
2854psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2855 size_t capacity);
2856
Gilles Peskineeab56e42018-07-12 17:12:33 +02002857/** Read some data from a generator.
2858 *
2859 * This function reads and returns a sequence of bytes from a generator.
2860 * The data that is read is discarded from the generator. The generator's
2861 * capacity is decreased by the number of bytes read.
2862 *
2863 * \param[in,out] generator The generator object to read from.
2864 * \param[out] output Buffer where the generator output will be
2865 * written.
2866 * \param output_length Number of bytes to output.
2867 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002868 * \retval #PSA_SUCCESS
2869 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002870 * There were fewer than \p output_length bytes
2871 * in the generator. Note that in this case, no
2872 * output is written to the output buffer.
2873 * The generator's capacity is set to 0, thus
2874 * subsequent calls to this function will not
2875 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002876 * \retval #PSA_ERROR_BAD_STATE
2877 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2878 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2879 * \retval #PSA_ERROR_HARDWARE_FAILURE
2880 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002881 */
2882psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2883 uint8_t *output,
2884 size_t output_length);
2885
2886/** Create a symmetric key from data read from a generator.
2887 *
2888 * This function reads a sequence of bytes from a generator and imports
2889 * these bytes as a key.
2890 * The data that is read is discarded from the generator. The generator's
2891 * capacity is decreased by the number of bytes read.
2892 *
2893 * This function is equivalent to calling #psa_generator_read and
2894 * passing the resulting output to #psa_import_key, but
2895 * if the implementation provides an isolation boundary then
2896 * the key material is not exposed outside the isolation boundary.
2897 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002898 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002899 * It must have been obtained by calling
2900 * psa_allocate_key() or psa_create_key() and must
2901 * not contain key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002902 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2903 * This must be a symmetric key type.
2904 * \param bits Key size in bits.
2905 * \param[in,out] generator The generator object to read from.
2906 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002907 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002908 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002909 * If the key is persistent, the key material and the key's metadata
2910 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002911 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002912 * There were fewer than \p output_length bytes
2913 * in the generator. Note that in this case, no
2914 * output is written to the output buffer.
2915 * The generator's capacity is set to 0, thus
2916 * subsequent calls to this function will not
2917 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002918 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002919 * The key type or key size is not supported, either by the
2920 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002921 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002922 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002923 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002924 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002925 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2926 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2927 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2928 * \retval #PSA_ERROR_HARDWARE_FAILURE
2929 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002930 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002931 * The library has not been previously initialized by psa_crypto_init().
2932 * It is implementation-dependent whether a failure to initialize
2933 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002934 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002935psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002936 psa_key_type_t type,
2937 size_t bits,
2938 psa_crypto_generator_t *generator);
2939
2940/** Abort a generator.
2941 *
2942 * Once a generator has been aborted, its capacity is zero.
2943 * Aborting a generator frees all associated resources except for the
2944 * \c generator structure itself.
2945 *
2946 * This function may be called at any time as long as the generator
2947 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2948 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2949 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2950 * on a generator that has not been set up.
2951 *
2952 * Once aborted, the generator object may be called.
2953 *
2954 * \param[in,out] generator The generator to abort.
2955 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002956 * \retval #PSA_SUCCESS
2957 * \retval #PSA_ERROR_BAD_STATE
2958 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2959 * \retval #PSA_ERROR_HARDWARE_FAILURE
2960 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002961 */
2962psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2963
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002964/** Use the maximum possible capacity for a generator.
2965 *
2966 * Use this value as the capacity argument when setting up a generator
2967 * to indicate that the generator should have the maximum possible capacity.
2968 * The value of the maximum possible capacity depends on the generator
2969 * algorithm.
2970 */
2971#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2972
Gilles Peskineeab56e42018-07-12 17:12:33 +02002973/**@}*/
2974
Gilles Peskineea0fb492018-07-12 17:17:20 +02002975/** \defgroup derivation Key derivation
2976 * @{
2977 */
2978
2979/** Set up a key derivation operation.
2980 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002981 * A key derivation algorithm takes some inputs and uses them to create
2982 * a byte generator which can be used to produce keys and other
2983 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002984 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002985 * To use a generator for key derivation:
2986 * - Start with an initialized object of type #psa_crypto_generator_t.
2987 * - Call psa_key_derivation_setup() to select the algorithm.
2988 * - Provide the inputs for the key derivation by calling
2989 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2990 * as appropriate. Which inputs are needed, in what order, and whether
2991 * they may be keys and if so of what type depends on the algorithm.
2992 * - Optionally set the generator's maximum capacity with
2993 * psa_set_generator_capacity(). You may do this before, in the middle of
2994 * or after providing inputs. For some algorithms, this step is mandatory
2995 * because the output depends on the maximum capacity.
2996 * - Generate output with psa_generator_read() or
2997 * psa_generator_import_key(). Successive calls to these functions
2998 * use successive output bytes from the generator.
2999 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003000 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003001 * \param[in,out] generator The generator object to set up. It must
3002 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003003 * \param alg The key derivation algorithm to compute
3004 * (\c PSA_ALG_XXX value such that
3005 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003006 *
3007 * \retval #PSA_SUCCESS
3008 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003009 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003010 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003011 * \retval #PSA_ERROR_NOT_SUPPORTED
3012 * \c alg is not supported or is not a key derivation algorithm.
3013 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3014 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3015 * \retval #PSA_ERROR_HARDWARE_FAILURE
3016 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003017 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003018 */
3019psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3020 psa_algorithm_t alg);
3021
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003022/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003023 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003024 * Which inputs are required and in what order depends on the algorithm.
3025 * Refer to the documentation of each key derivation or key agreement
3026 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003027 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003028 * This function passes direct inputs. Some inputs must be passed as keys
3029 * using psa_key_derivation_input_key() instead of this function. Refer to
3030 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003031 *
3032 * \param[in,out] generator The generator object to use. It must
3033 * have been set up with
3034 * psa_key_derivation_setup() and must not
3035 * have produced any output yet.
3036 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003037 * \param[in] data Input data to use.
3038 * \param data_length Size of the \p data buffer in bytes.
3039 *
3040 * \retval #PSA_SUCCESS
3041 * Success.
3042 * \retval #PSA_ERROR_INVALID_ARGUMENT
3043 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003044 * \retval #PSA_ERROR_INVALID_ARGUMENT
3045 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003046 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3047 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3048 * \retval #PSA_ERROR_HARDWARE_FAILURE
3049 * \retval #PSA_ERROR_TAMPERING_DETECTED
3050 * \retval #PSA_ERROR_BAD_STATE
3051 * The value of \p step is not valid given the state of \p generator.
3052 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003053 * The library has not been previously initialized by psa_crypto_init().
3054 * It is implementation-dependent whether a failure to initialize
3055 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003056 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003057psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3058 psa_key_derivation_step_t step,
3059 const uint8_t *data,
3060 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003061
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003062/** Provide an input for key derivation in the form of a key.
3063 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003064 * Which inputs are required and in what order depends on the algorithm.
3065 * Refer to the documentation of each key derivation or key agreement
3066 * algorithm for information.
3067 *
3068 * This function passes key inputs. Some inputs must be passed as keys
3069 * of the appropriate type using this function, while others must be
3070 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3071 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003072 *
3073 * \param[in,out] generator The generator object to use. It must
3074 * have been set up with
3075 * psa_key_derivation_setup() and must not
3076 * have produced any output yet.
3077 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003078 * \param handle Handle to the key. It must have an
3079 * appropriate type for \p step and must
3080 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003081 *
3082 * \retval #PSA_SUCCESS
3083 * Success.
3084 * \retval #PSA_ERROR_INVALID_HANDLE
3085 * \retval #PSA_ERROR_EMPTY_SLOT
3086 * \retval #PSA_ERROR_NOT_PERMITTED
3087 * \retval #PSA_ERROR_INVALID_ARGUMENT
3088 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003089 * \retval #PSA_ERROR_INVALID_ARGUMENT
3090 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003091 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3092 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3093 * \retval #PSA_ERROR_HARDWARE_FAILURE
3094 * \retval #PSA_ERROR_TAMPERING_DETECTED
3095 * \retval #PSA_ERROR_BAD_STATE
3096 * The value of \p step is not valid given the state of \p generator.
3097 * \retval #PSA_ERROR_BAD_STATE
3098 * The library has not been previously initialized by psa_crypto_init().
3099 * It is implementation-dependent whether a failure to initialize
3100 * results in this error code.
3101 */
3102psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3103 psa_key_derivation_step_t step,
3104 psa_key_handle_t handle);
3105
Gilles Peskine969c5d62019-01-16 15:53:06 +01003106/** Perform a key agreement and use the shared secret as input to a key
3107 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003108 *
3109 * A key agreement algorithm takes two inputs: a private key \p private_key
3110 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003111 * The result of this function is passed as input to a key derivation.
3112 * The output of this key derivation can be extracted by reading from the
3113 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003114 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003115 * \param[in,out] generator The generator object to use. It must
3116 * have been set up with
3117 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003118 * key agreement and derivation algorithm
3119 * \c alg (\c PSA_ALG_XXX value such that
3120 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true
3121 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3122 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003123 * The generator must be ready for an
3124 * input of the type given by \p step.
3125 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003126 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003127 * \param[in] peer_key Public key of the peer. The peer key must be in the
3128 * same format that psa_import_key() accepts for the
3129 * public key type corresponding to the type of
3130 * private_key. That is, this function performs the
3131 * equivalent of
3132 * `psa_import_key(internal_public_key_handle,
3133 * PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(private_key_type),
3134 * peer_key, peer_key_length)` where
3135 * `private_key_type` is the type of `private_key`.
3136 * For example, for EC keys, this means that peer_key
3137 * is interpreted as a point on the curve that the
3138 * private key is on. The standard formats for public
3139 * keys are documented in the documentation of
3140 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003141 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003142 *
3143 * \retval #PSA_SUCCESS
3144 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003145 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02003146 * \retval #PSA_ERROR_EMPTY_SLOT
3147 * \retval #PSA_ERROR_NOT_PERMITTED
3148 * \retval #PSA_ERROR_INVALID_ARGUMENT
3149 * \c private_key is not compatible with \c alg,
3150 * or \p peer_key is not valid for \c alg or not compatible with
3151 * \c private_key.
3152 * \retval #PSA_ERROR_NOT_SUPPORTED
3153 * \c alg is not supported or is not a key derivation algorithm.
3154 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3155 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3156 * \retval #PSA_ERROR_HARDWARE_FAILURE
3157 * \retval #PSA_ERROR_TAMPERING_DETECTED
3158 */
3159psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003160 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003161 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003162 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003163 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003164
Gilles Peskine769c7a62019-01-18 16:42:29 +01003165/** Perform a key agreement and use the shared secret as input to a key
3166 * derivation.
3167 *
3168 * A key agreement algorithm takes two inputs: a private key \p private_key
3169 * a public key \p peer_key.
3170 *
3171 * \warning The raw result of a key agreement algorithm such as finite-field
3172 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3173 * not be used directly as key material. It should instead be passed as
3174 * input to a key derivation algorithm. To chain a key agreement with
3175 * a key derivation, use psa_key_agreement() and other functions from
3176 * the key derivation and generator interface.
3177 *
3178 * \param private_key Handle to the private key to use.
3179 * \param[in] peer_key Public key of the peer. It must be
3180 * in the same format that psa_import_key()
3181 * accepts. The standard formats for public
3182 * keys are documented in the documentation
3183 * of psa_export_public_key().
3184 * \param peer_key_length Size of \p peer_key in bytes.
3185 * \param[out] output Buffer where the decrypted message is to
3186 * be written.
3187 * \param output_size Size of the \c output buffer in bytes.
3188 * \param[out] output_length On success, the number of bytes
3189 * that make up the returned output.
3190 *
3191 * \retval #PSA_SUCCESS
3192 * Success.
3193 * \retval #PSA_ERROR_INVALID_HANDLE
3194 * \retval #PSA_ERROR_EMPTY_SLOT
3195 * \retval #PSA_ERROR_NOT_PERMITTED
3196 * \retval #PSA_ERROR_INVALID_ARGUMENT
3197 * \p alg is not a key agreement algorithm
3198 * \retval #PSA_ERROR_INVALID_ARGUMENT
3199 * \p private_key is not compatible with \p alg,
3200 * or \p peer_key is not valid for \p alg or not compatible with
3201 * \p private_key.
3202 * \retval #PSA_ERROR_NOT_SUPPORTED
3203 * \p alg is not a supported key agreement algorithm.
3204 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3205 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3206 * \retval #PSA_ERROR_HARDWARE_FAILURE
3207 * \retval #PSA_ERROR_TAMPERING_DETECTED
3208 */
3209psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3210 psa_key_handle_t private_key,
3211 const uint8_t *peer_key,
3212 size_t peer_key_length,
3213 uint8_t *output,
3214 size_t output_size,
3215 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003216
3217/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003218
3219/** \defgroup random Random generation
3220 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003221 */
3222
3223/**
3224 * \brief Generate random bytes.
3225 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003226 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003227 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003228 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003229 *
3230 * \note To generate a key, use psa_generate_key() instead.
3231 *
3232 * \param[out] output Output buffer for the generated data.
3233 * \param output_size Number of bytes to generate and output.
3234 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003235 * \retval #PSA_SUCCESS
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003236 * \retval #PSA_ERROR_NOT_SUPPORTED
3237 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003238 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003239 * \retval #PSA_ERROR_HARDWARE_FAILURE
3240 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003241 * \retval #PSA_ERROR_BAD_STATE
3242 * The library has not been previously initialized by psa_crypto_init().
3243 * It is implementation-dependent whether a failure to initialize
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003244 * results in this error code.
3245 */
3246psa_status_t psa_generate_random(uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003247 size_t output_size);
3248
3249/** Extra parameters for RSA key generation.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003250 *
Gilles Peskine28538492018-07-11 17:34:00 +02003251 * You may pass a pointer to a structure of this type as the \c extra
3252 * parameter to psa_generate_key().
3253 */
3254typedef struct {
3255 uint32_t e; /**< Public exponent value. Default: 65537. */
3256} psa_generate_key_extra_rsa;
3257
3258/**
itayzafrir90d8c7a2018-09-12 11:44:52 +03003259 * \brief Generate a key or key pair.
itayzafrir18617092018-09-16 12:22:41 +03003260 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003261 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003262 * It must have been obtained by calling
3263 * psa_allocate_key() or psa_create_key() and must
3264 * not contain key material yet.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003265 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
3266 * \param bits Key size in bits.
3267 * \param[in] extra Extra parameters for key generation. The
3268 * interpretation of this parameter depends on
3269 * \p type. All types support \c NULL to use
3270 * default parameters. Implementation that support
3271 * the generation of vendor-specific key types
3272 * that allow extra parameters shall document
3273 * the format of these extra parameters and
3274 * the default values. For standard parameters,
3275 * the meaning of \p extra is as follows:
3276 * - For a symmetric key type (a type such
3277 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
Gilles Peskine53d991e2018-07-12 01:14:59 +02003278 * false), \p extra must be \c NULL.
3279 * - For an elliptic curve key type (a type
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003280 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
3281 * false), \p extra must be \c NULL.
3282 * - For an RSA key (\p type is
Gilles Peskinee59236f2018-01-27 23:32:46 +01003283 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3284 * optional #psa_generate_key_extra_rsa structure
3285 * specifying the public exponent. The
3286 * default public exponent used when \p extra
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003287 * is \c NULL is 65537.
Jaeden Amero1308fb52019-01-11 13:50:43 +00003288 * - For an DSA key (\p type is
3289 * #PSA_KEY_TYPE_DSA_KEYPAIR), \p extra is an
3290 * optional structure specifying the key domain
3291 * parameters. The key domain parameters can also be
3292 * provided by psa_set_key_domain_parameters(),
3293 * which documents the format of the structure.
Jaeden Amero8851c402019-01-11 14:20:03 +00003294 * - For a DH key (\p type is
3295 * #PSA_KEY_TYPE_DH_KEYPAIR), the \p extra is an
3296 * optional structure specifying the key domain
3297 * parameters. The key domain parameters can also be
3298 * provided by psa_set_key_domain_parameters(),
3299 * which documents the format of the structure.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003300 * \param extra_size Size of the buffer that \p extra
3301 * points to, in bytes. Note that if \p extra is
3302 * \c NULL then \p extra_size must be zero.
3303 *
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003304 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003305 * Success.
3306 * If the key is persistent, the key material and the key's metadata
3307 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003308 * \retval #PSA_ERROR_INVALID_HANDLE
3309 * \retval #PSA_ERROR_OCCUPIED_SLOT
3310 * There is already a key in the specified slot.
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003311 * \retval #PSA_ERROR_NOT_SUPPORTED
3312 * \retval #PSA_ERROR_INVALID_ARGUMENT
3313 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003314 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3315 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3316 * \retval #PSA_ERROR_HARDWARE_FAILURE
3317 * \retval #PSA_ERROR_TAMPERING_DETECTED
3318 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003319 * The library has not been previously initialized by psa_crypto_init().
3320 * It is implementation-dependent whether a failure to initialize
3321 * results in this error code.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003322 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003323psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskinee59236f2018-01-27 23:32:46 +01003324 psa_key_type_t type,
3325 size_t bits,
3326 const void *extra,
3327 size_t extra_size);
3328
3329/**@}*/
3330
3331#ifdef __cplusplus
3332}
3333#endif
3334
3335/* The file "crypto_sizes.h" contains definitions for size calculation
3336 * macros whose definitions are implementation-specific. */
3337#include "crypto_sizes.h"
3338
3339/* The file "crypto_struct.h" contains definitions for
3340 * implementation-specific structs that are declared above. */
3341#include "crypto_struct.h"
3342
3343/* The file "crypto_extra.h" contains vendor-specific definitions. This
3344 * can include vendor-defined algorithms, extra functions, etc. */
3345#include "crypto_extra.h"
3346
3347#endif /* PSA_CRYPTO_H */