blob: 564dd872b246d54a5e1cdfde42e39a5932c31375 [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 Peskine87a5e562019-04-17 12:28:25 +020096/** \defgroup attributes Key attributes
97 * @{
98 */
99
100/** The type of a structure containing key attributes.
101 *
102 * This is an opaque structure that can represent the metadata of a key
103 * object, including the key type and size, domain parameters, usage policies,
104 * location in storage, and any other similar information.
105 *
106 * The actual key material is not considered an attribute of a key.
107 * Key attributes do not contain information that is generally considered
108 * highly confidential.
109 */
110typedef struct psa_key_attributes_s psa_key_attributes_t;
111
112/**@}*/
113
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100114/** \defgroup policy Key policies
115 * @{
116 */
117
118/** The type of the key policy data structure.
119 *
120 * Before calling any function on a key policy, the application must initialize
121 * it by any of the following means:
122 * - Set the structure to all-bits-zero, for example:
123 * \code
124 * psa_key_policy_t policy;
125 * memset(&policy, 0, sizeof(policy));
126 * \endcode
127 * - Initialize the structure to logical zero values, for example:
128 * \code
129 * psa_key_policy_t policy = {0};
130 * \endcode
131 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
132 * for example:
133 * \code
134 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
135 * \endcode
136 * - Assign the result of the function psa_key_policy_init()
137 * to the structure, for example:
138 * \code
139 * psa_key_policy_t policy;
140 * policy = psa_key_policy_init();
141 * \endcode
142 *
143 * This is an implementation-defined \c struct. Applications should not
144 * make any assumptions about the content of this structure except
145 * as directed by the documentation of a specific implementation. */
146typedef struct psa_key_policy_s psa_key_policy_t;
147
148/** \def PSA_KEY_POLICY_INIT
149 *
150 * This macro returns a suitable initializer for a key policy object of type
151 * #psa_key_policy_t.
152 */
153#ifdef __DOXYGEN_ONLY__
154/* This is an example definition for documentation purposes.
155 * Implementations should define a suitable value in `crypto_struct.h`.
156 */
157#define PSA_KEY_POLICY_INIT {0}
158#endif
159
160/** Return an initial value for a key policy that forbids all usage of the key.
161 */
162static psa_key_policy_t psa_key_policy_init(void);
163
164/** \brief Set the standard fields of a policy structure.
165 *
166 * Note that this function does not make any consistency check of the
167 * parameters. The values are only checked when applying the policy to
168 * a key slot with psa_set_key_policy().
169 *
170 * \param[in,out] policy The key policy to modify. It must have been
171 * initialized as per the documentation for
172 * #psa_key_policy_t.
173 * \param usage The permitted uses for the key.
174 * \param alg The algorithm that the key may be used for.
175 */
176void psa_key_policy_set_usage(psa_key_policy_t *policy,
177 psa_key_usage_t usage,
178 psa_algorithm_t alg);
179
180/** \brief Retrieve the usage field of a policy structure.
181 *
182 * \param[in] policy The policy object to query.
183 *
184 * \return The permitted uses for a key with this policy.
185 */
186psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
187
188/** \brief Retrieve the algorithm field of a policy structure.
189 *
190 * \param[in] policy The policy object to query.
191 *
192 * \return The permitted algorithm for a key with this policy.
193 */
194psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
195
196/** \brief Set the usage policy on a key slot.
197 *
198 * This function must be called on an empty key slot, before importing,
199 * generating or creating a key in the slot. Changing the policy of an
200 * existing key is not permitted.
201 *
202 * Implementations may set restrictions on supported key policies
203 * depending on the key type and the key slot.
204 *
205 * \param handle Handle to the key whose policy is to be changed.
206 * \param[in] policy The policy object to query.
207 *
208 * \retval #PSA_SUCCESS
209 * Success.
210 * If the key is persistent, it is implementation-defined whether
211 * the policy has been saved to persistent storage. Implementations
212 * may defer saving the policy until the key material is created.
213 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200214 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100215 * \retval #PSA_ERROR_NOT_SUPPORTED
216 * \retval #PSA_ERROR_INVALID_ARGUMENT
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_set_key_policy(psa_key_handle_t handle,
226 const psa_key_policy_t *policy);
227
228/** \brief Get the usage policy for a key slot.
229 *
230 * \param handle Handle to the key slot whose policy is being queried.
231 * \param[out] policy On success, the key's policy.
232 *
233 * \retval #PSA_SUCCESS
234 * \retval #PSA_ERROR_INVALID_HANDLE
235 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
236 * \retval #PSA_ERROR_HARDWARE_FAILURE
237 * \retval #PSA_ERROR_TAMPERING_DETECTED
238 * \retval #PSA_ERROR_BAD_STATE
239 * The library has not been previously initialized by psa_crypto_init().
240 * It is implementation-dependent whether a failure to initialize
241 * results in this error code.
242 */
243psa_status_t psa_get_key_policy(psa_key_handle_t handle,
244 psa_key_policy_t *policy);
245
246/**@}*/
247
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100248/** \defgroup key_management Key management
249 * @{
250 */
251
Gilles Peskinef535eb22018-11-30 14:08:36 +0100252/** Allocate a key slot for a transient key, i.e. a key which is only stored
253 * in volatile memory.
254 *
255 * The allocated key slot and its handle remain valid until the
256 * application calls psa_close_key() or psa_destroy_key() or until the
257 * application terminates.
258 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100259 * \param[out] handle On success, a handle to a volatile key slot.
260 *
261 * \retval #PSA_SUCCESS
262 * Success. The application can now use the value of `*handle`
263 * to access the newly allocated key slot.
264 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
265 * There was not enough memory, or the maximum number of key slots
266 * has been reached.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100267 */
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100268psa_status_t psa_allocate_key(psa_key_handle_t *handle);
Gilles Peskinef535eb22018-11-30 14:08:36 +0100269
270/** Open a handle to an existing persistent key.
271 *
272 * Open a handle to a key which was previously created with psa_create_key().
273 *
274 * \param lifetime The lifetime of the key. This designates a storage
275 * area where the key material is stored. This must not
276 * be #PSA_KEY_LIFETIME_VOLATILE.
277 * \param id The persistent identifier of the key.
278 * \param[out] handle On success, a handle to a key slot which contains
279 * the data and metadata loaded from the specified
280 * persistent location.
281 *
282 * \retval #PSA_SUCCESS
283 * Success. The application can now use the value of `*handle`
284 * to access the newly allocated key slot.
285 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200286 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100287 * \retval #PSA_ERROR_INVALID_ARGUMENT
288 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
289 * \retval #PSA_ERROR_INVALID_ARGUMENT
290 * \p id is invalid for the specified lifetime.
291 * \retval #PSA_ERROR_NOT_SUPPORTED
292 * \p lifetime is not supported.
293 * \retval #PSA_ERROR_NOT_PERMITTED
294 * The specified key exists, but the application does not have the
295 * permission to access it. Note that this specification does not
296 * define any way to create such a key, but it may be possible
297 * through implementation-specific means.
298 */
299psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
300 psa_key_id_t id,
301 psa_key_handle_t *handle);
302
Gilles Peskinef535eb22018-11-30 14:08:36 +0100303/** Close a key handle.
304 *
305 * If the handle designates a volatile key, destroy the key material and
306 * free all associated resources, just like psa_destroy_key().
307 *
308 * If the handle designates a persistent key, free all resources associated
309 * with the key in volatile memory. The key slot in persistent storage is
310 * not affected and can be opened again later with psa_open_key().
311 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100312 * If the key is currently in use in a multipart operation,
313 * the multipart operation is aborted.
314 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100315 * \param handle The key handle to close.
316 *
317 * \retval #PSA_SUCCESS
318 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100319 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100320 */
321psa_status_t psa_close_key(psa_key_handle_t handle);
322
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100323/**@}*/
324
325/** \defgroup import_export Key import and export
326 * @{
327 */
328
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100329/**
330 * \brief Import a key in binary format.
331 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100332 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100333 * documentation of psa_export_public_key() for the format of public keys
334 * and to the documentation of psa_export_key() for the format for
335 * other key types.
336 *
337 * This specification supports a single format for each key type.
338 * Implementations may support other formats as long as the standard
339 * format is supported. Implementations that support other formats
340 * should ensure that the formats are clearly unambiguous so as to
341 * minimize the risk that an invalid input is accidentally interpreted
342 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100343 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100344 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100345 * It must have been obtained by calling
346 * psa_allocate_key() or psa_create_key() and must
347 * not contain key material yet.
Gilles Peskinef7933932018-10-31 14:07:52 +0100348 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
349 * import, the key slot will contain a key of this type.
350 * \param[in] data Buffer containing the key data. The content of this
351 * buffer is interpreted according to \p type. It must
352 * contain the format described in the documentation
353 * of psa_export_key() or psa_export_public_key() for
354 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200355 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100356 *
Gilles Peskine28538492018-07-11 17:34:00 +0200357 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100358 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100359 * If the key is persistent, the key material and the key's metadata
360 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100361 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200362 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200363 * The key type or key size is not supported, either by the
364 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200365 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100366 * The key slot is invalid,
367 * or the key data is not correctly formatted.
David Saadab4ecc272019-02-14 13:48:10 +0200368 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200369 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200370 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
371 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
372 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100373 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200374 * \retval #PSA_ERROR_HARDWARE_FAILURE
375 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300376 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300377 * The library has not been previously initialized by psa_crypto_init().
378 * It is implementation-dependent whether a failure to initialize
379 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100380 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200381psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
382 psa_key_handle_t *handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100383 psa_key_type_t type,
384 const uint8_t *data,
385 size_t data_length);
386
387/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100388 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200389 *
390 * This function destroys the content of the key slot from both volatile
391 * memory and, if applicable, non-volatile storage. Implementations shall
392 * make a best effort to ensure that any previous content of the slot is
393 * unrecoverable.
394 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100395 * This function also erases any metadata such as policies and frees all
396 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200397 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100398 * If the key is currently in use in a multipart operation,
399 * the multipart operation is aborted.
400 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100401 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100402 *
Gilles Peskine28538492018-07-11 17:34:00 +0200403 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200404 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200405 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200406 * The slot holds content and cannot be erased because it is
407 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100408 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200409 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200410 * There was an failure in communication with the cryptoprocessor.
411 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200412 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200413 * The storage is corrupted. Implementations shall make a best effort
414 * to erase key material even in this stage, however applications
415 * should be aware that it may be impossible to guarantee that the
416 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200417 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200418 * An unexpected condition which is not a storage corruption or
419 * a communication failure occurred. The cryptoprocessor may have
420 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300421 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300422 * The library has not been previously initialized by psa_crypto_init().
423 * It is implementation-dependent whether a failure to initialize
424 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100425 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100426psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100427
428/**
429 * \brief Get basic metadata about a key.
430 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100431 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200432 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100433 * This may be a null pointer, in which case the key type
434 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200435 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100436 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100437 * is not written.
438 *
Gilles Peskine28538492018-07-11 17:34:00 +0200439 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100440 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200441 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineae32aac2018-11-30 14:39:32 +0100442 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200443 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
444 * \retval #PSA_ERROR_HARDWARE_FAILURE
445 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300446 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300447 * The library has not been previously initialized by psa_crypto_init().
448 * It is implementation-dependent whether a failure to initialize
449 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100450 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100451psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100452 psa_key_type_t *type,
453 size_t *bits);
454
455/**
Jaeden Amero283dfd12019-01-11 12:06:22 +0000456 * \brief Set domain parameters for a key.
457 *
458 * Some key types require additional domain parameters to be set before import
459 * or generation of the key. The domain parameters can be set with this
460 * function or, for key generation, through the \c extra parameter of
461 * psa_generate_key().
462 *
463 * The format for the required domain parameters varies by the key type.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000464 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
465 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
466 * ```
467 * Dss-Parms ::= SEQUENCE {
468 * p INTEGER,
469 * q INTEGER,
470 * g INTEGER
471 * }
472 * ```
Jaeden Amero8851c402019-01-11 14:20:03 +0000473 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY), the
474 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
475 * ```
476 * DomainParameters ::= SEQUENCE {
477 * p INTEGER, -- odd prime, p=jq +1
478 * g INTEGER, -- generator, g
479 * q INTEGER, -- factor of p-1
480 * j INTEGER OPTIONAL, -- subgroup factor
481 * validationParms ValidationParms OPTIONAL
482 * }
483 * ValidationParms ::= SEQUENCE {
484 * seed BIT STRING,
485 * pgenCounter INTEGER
486 * }
487 * ```
Jaeden Amero283dfd12019-01-11 12:06:22 +0000488 *
Gilles Peskine3a74e002019-01-18 17:11:25 +0100489 * \param handle Handle to the slot where the key will be stored.
490 * This must be a valid slot for a key of the chosen
491 * type: it must have been obtained by calling
492 * psa_allocate_key() or psa_create_key() with the
493 * correct \p type and with a maximum size that is
494 * compatible with \p data. It must not contain
495 * key material yet.
496 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). When
497 * subsequently creating key material into \p handle,
498 * the type must be compatible.
Jaeden Amero283dfd12019-01-11 12:06:22 +0000499 * \param[in] data Buffer containing the key domain parameters. The content
500 * of this buffer is interpreted according to \p type. of
501 * psa_export_key() or psa_export_public_key() for the
502 * chosen type.
503 * \param data_length Size of the \p data buffer in bytes.
504 *
505 * \retval #PSA_SUCCESS
506 * \retval #PSA_ERROR_INVALID_HANDLE
507 * \retval #PSA_ERROR_OCCUPIED_SLOT
508 * There is already a key in the specified slot.
509 * \retval #PSA_ERROR_INVALID_ARGUMENT
510 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
511 * \retval #PSA_ERROR_HARDWARE_FAILURE
512 * \retval #PSA_ERROR_TAMPERING_DETECTED
513 * \retval #PSA_ERROR_BAD_STATE
514 * The library has not been previously initialized by psa_crypto_init().
515 * It is implementation-dependent whether a failure to initialize
516 * results in this error code.
517 */
518psa_status_t psa_set_key_domain_parameters(psa_key_handle_t handle,
Gilles Peskine3a74e002019-01-18 17:11:25 +0100519 psa_key_type_t type,
Jaeden Amero283dfd12019-01-11 12:06:22 +0000520 const uint8_t *data,
521 size_t data_length);
522
523/**
524 * \brief Get domain parameters for a key.
525 *
526 * Get the domain parameters for a key with this function, if any. The format
527 * of the domain parameters written to \p data is specified in the
528 * documentation for psa_set_key_domain_parameters().
529 *
530 * \param handle Handle to the key to get domain parameters from.
531 * \param[out] data On success, the key domain parameters.
532 * \param data_size Size of the \p data buffer in bytes.
533 * \param[out] data_length On success, the number of bytes
534 * that make up the key domain parameters data.
535 *
536 * \retval #PSA_SUCCESS
537 * \retval #PSA_ERROR_INVALID_HANDLE
538 * \retval #PSA_ERROR_EMPTY_SLOT
539 * There is no key in the specified slot.
540 * \retval #PSA_ERROR_INVALID_ARGUMENT
541 * \retval #PSA_ERROR_NOT_SUPPORTED
542 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
543 * \retval #PSA_ERROR_HARDWARE_FAILURE
544 * \retval #PSA_ERROR_TAMPERING_DETECTED
545 * \retval #PSA_ERROR_BAD_STATE
546 * The library has not been previously initialized by psa_crypto_init().
547 * It is implementation-dependent whether a failure to initialize
548 * results in this error code.
549 */
550psa_status_t psa_get_key_domain_parameters(psa_key_handle_t handle,
551 uint8_t *data,
552 size_t data_size,
553 size_t *data_length);
554
555/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556 * \brief Export a key in binary format.
557 *
558 * The output of this function can be passed to psa_import_key() to
559 * create an equivalent object.
560 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100561 * If the implementation of psa_import_key() supports other formats
562 * beyond the format specified here, the output from psa_export_key()
563 * must use the representation specified here, not the original
564 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100565 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100566 * For standard key types, the output format is as follows:
567 *
568 * - For symmetric keys (including MAC keys), the format is the
569 * raw bytes of the key.
570 * - For DES, the key data consists of 8 bytes. The parity bits must be
571 * correct.
572 * - For Triple-DES, the format is the concatenation of the
573 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100574 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200575 * is the non-encrypted DER encoding of the representation defined by
576 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
577 * ```
578 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200579 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200580 * modulus INTEGER, -- n
581 * publicExponent INTEGER, -- e
582 * privateExponent INTEGER, -- d
583 * prime1 INTEGER, -- p
584 * prime2 INTEGER, -- q
585 * exponent1 INTEGER, -- d mod (p-1)
586 * exponent2 INTEGER, -- d mod (q-1)
587 * coefficient INTEGER, -- (inverse of q) mod p
588 * }
589 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000590 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
591 * representation of the private key `x` as a big-endian byte string. The
592 * length of the byte string is the private key size in bytes (leading zeroes
593 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200594 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100595 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100596 * a representation of the private value as a `ceiling(m/8)`-byte string
597 * where `m` is the bit size associated with the curve, i.e. the bit size
598 * of the order of the curve's coordinate field. This byte string is
599 * in little-endian order for Montgomery curves (curve types
600 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
601 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
602 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100603 * This is the content of the `privateKey` field of the `ECPrivateKey`
604 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000605 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
606 * format is the representation of the private key `x` as a big-endian byte
607 * string. The length of the byte string is the private key size in bytes
608 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200609 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
610 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100611 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100612 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200613 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200614 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200615 * \param[out] data_length On success, the number of bytes
616 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100617 *
Gilles Peskine28538492018-07-11 17:34:00 +0200618 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100619 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200620 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200621 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100622 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200623 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
624 * The size of the \p data buffer is too small. You can determine a
625 * sufficient buffer size by calling
626 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
627 * where \c type is the key type
628 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200629 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
630 * \retval #PSA_ERROR_HARDWARE_FAILURE
631 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300632 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300633 * The library has not been previously initialized by psa_crypto_init().
634 * It is implementation-dependent whether a failure to initialize
635 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100636 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100637psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100638 uint8_t *data,
639 size_t data_size,
640 size_t *data_length);
641
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100642/**
643 * \brief Export a public key or the public part of a key pair in binary format.
644 *
645 * The output of this function can be passed to psa_import_key() to
646 * create an object that is equivalent to the public key.
647 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000648 * This specification supports a single format for each key type.
649 * Implementations may support other formats as long as the standard
650 * format is supported. Implementations that support other formats
651 * should ensure that the formats are clearly unambiguous so as to
652 * minimize the risk that an invalid input is accidentally interpreted
653 * according to a different format.
654 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000655 * For standard key types, the output format is as follows:
656 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
657 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
658 * ```
659 * RSAPublicKey ::= SEQUENCE {
660 * modulus INTEGER, -- n
661 * publicExponent INTEGER } -- e
662 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000663 * - For elliptic curve public keys (key types for which
664 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
665 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
666 * Let `m` be the bit size associated with the curve, i.e. the bit size of
667 * `q` for a curve over `F_q`. The representation consists of:
668 * - The byte 0x04;
669 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
670 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000671 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
672 * representation of the public key `y = g^x mod p` as a big-endian byte
673 * string. The length of the byte string is the length of the base prime `p`
674 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000675 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
676 * the format is the representation of the public key `y = g^x mod p` as a
677 * big-endian byte string. The length of the byte string is the length of the
678 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100679 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100680 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200681 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200682 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200683 * \param[out] data_length On success, the number of bytes
684 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100685 *
Gilles Peskine28538492018-07-11 17:34:00 +0200686 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100687 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200688 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200689 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200690 * The key is neither a public key nor a key pair.
691 * \retval #PSA_ERROR_NOT_SUPPORTED
692 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
693 * The size of the \p data buffer is too small. You can determine a
694 * sufficient buffer size by calling
695 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
696 * where \c type is the key type
697 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200698 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
699 * \retval #PSA_ERROR_HARDWARE_FAILURE
700 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300701 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300702 * The library has not been previously initialized by psa_crypto_init().
703 * It is implementation-dependent whether a failure to initialize
704 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100705 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100706psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100707 uint8_t *data,
708 size_t data_size,
709 size_t *data_length);
710
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100711/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100712 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100713 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000714 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100715 * This function is primarily useful to copy a key from one location
716 * to another, since it populates a key using the material from
717 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200718 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100719 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100720 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100721 * subject to implementation-defined restrictions on key sharing.
722 * In this case \p constraint would typically prevent the recipient
723 * from exporting the key.
Gilles Peskine7e198532018-03-08 07:50:30 +0100724 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100725 * The resulting key may only be used in a way that conforms to all
726 * three of: the policy of the source key, the policy previously set
727 * on the target, and the \p constraint parameter passed when calling
728 * this function.
729 * - The usage flags on the resulting key are the bitwise-and of the
730 * usage flags on the source policy, the previously-set target policy
731 * and the policy constraint.
732 * - If all three policies allow the same algorithm or wildcard-based
733 * algorithm policy, the resulting key has the same algorithm policy.
734 * - If one of the policies allows an algorithm and all the other policies
735 * either allow the same algorithm or a wildcard-based algorithm policy
736 * that includes this algorithm, the resulting key allows the same
737 * algorithm.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200738 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100739 * The effect of this function on implementation-defined metadata is
740 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200741 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100742 * \param source_handle The key to copy. It must be a handle to an
743 * occupied slot.
744 * \param target_handle A handle to the target slot. It must not contain
745 * key material yet.
746 * \param[in] constraint An optional policy constraint. If this parameter
747 * is non-null then the resulting key will conform
748 * to this policy in addition to the source policy
749 * and the policy already present on the target
750 * slot. If this parameter is null then the
751 * function behaves in the same way as if it was
752 * the target policy, i.e. only the source and
753 * target policies apply.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200754 *
755 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100756 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200757 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine3be6b7f2019-03-05 19:32:26 +0100758 * \p target_handle already contains key material.
David Saadab4ecc272019-02-14 13:48:10 +0200759 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine3be6b7f2019-03-05 19:32:26 +0100760 * \p source_handle does not contain key material.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200761 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100762 * The policy constraints on the source, on the target and
Gilles Peskine3be6b7f2019-03-05 19:32:26 +0100763 * \p constraint are incompatible.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100764 * \retval #PSA_ERROR_NOT_PERMITTED
765 * The source key is not exportable and its lifetime does not
766 * allow copying it to the target's lifetime.
767 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
768 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200769 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
770 * \retval #PSA_ERROR_HARDWARE_FAILURE
771 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100772 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100773psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200774 const psa_key_attributes_t *attributes,
775 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100776
777/**@}*/
778
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100779/** \defgroup hash Message digests
780 * @{
781 */
782
Gilles Peskine69647a42019-01-14 20:18:12 +0100783/** Calculate the hash (digest) of a message.
784 *
785 * \note To verify the hash of a message against an
786 * expected value, use psa_hash_compare() instead.
787 *
788 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
789 * such that #PSA_ALG_IS_HASH(\p alg) is true).
790 * \param[in] input Buffer containing the message to hash.
791 * \param input_length Size of the \p input buffer in bytes.
792 * \param[out] hash Buffer where the hash is to be written.
793 * \param hash_size Size of the \p hash buffer in bytes.
794 * \param[out] hash_length On success, the number of bytes
795 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100796 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100797 *
798 * \retval #PSA_SUCCESS
799 * Success.
800 * \retval #PSA_ERROR_NOT_SUPPORTED
801 * \p alg is not supported or is not a hash algorithm.
802 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
803 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
804 * \retval #PSA_ERROR_HARDWARE_FAILURE
805 * \retval #PSA_ERROR_TAMPERING_DETECTED
806 */
807psa_status_t psa_hash_compute(psa_algorithm_t alg,
808 const uint8_t *input,
809 size_t input_length,
810 uint8_t *hash,
811 size_t hash_size,
812 size_t *hash_length);
813
814/** Calculate the hash (digest) of a message and compare it with a
815 * reference value.
816 *
817 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
818 * such that #PSA_ALG_IS_HASH(\p alg) is true).
819 * \param[in] input Buffer containing the message to hash.
820 * \param input_length Size of the \p input buffer in bytes.
821 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100822 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100823 *
824 * \retval #PSA_SUCCESS
825 * The expected hash is identical to the actual hash of the input.
826 * \retval #PSA_ERROR_INVALID_SIGNATURE
827 * The hash of the message was calculated successfully, but it
828 * differs from the expected hash.
829 * \retval #PSA_ERROR_NOT_SUPPORTED
830 * \p alg is not supported or is not a hash algorithm.
831 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
832 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
833 * \retval #PSA_ERROR_HARDWARE_FAILURE
834 * \retval #PSA_ERROR_TAMPERING_DETECTED
835 */
836psa_status_t psa_hash_compare(psa_algorithm_t alg,
837 const uint8_t *input,
838 size_t input_length,
839 const uint8_t *hash,
840 const size_t hash_length);
841
Gilles Peskine308b91d2018-02-08 09:47:44 +0100842/** The type of the state data structure for multipart hash operations.
843 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000844 * Before calling any function on a hash operation object, the application must
845 * initialize it by any of the following means:
846 * - Set the structure to all-bits-zero, for example:
847 * \code
848 * psa_hash_operation_t operation;
849 * memset(&operation, 0, sizeof(operation));
850 * \endcode
851 * - Initialize the structure to logical zero values, for example:
852 * \code
853 * psa_hash_operation_t operation = {0};
854 * \endcode
855 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
856 * for example:
857 * \code
858 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
859 * \endcode
860 * - Assign the result of the function psa_hash_operation_init()
861 * to the structure, for example:
862 * \code
863 * psa_hash_operation_t operation;
864 * operation = psa_hash_operation_init();
865 * \endcode
866 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100867 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100868 * make any assumptions about the content of this structure except
869 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100870typedef struct psa_hash_operation_s psa_hash_operation_t;
871
Jaeden Amero6a25b412019-01-04 11:47:44 +0000872/** \def PSA_HASH_OPERATION_INIT
873 *
874 * This macro returns a suitable initializer for a hash operation object
875 * of type #psa_hash_operation_t.
876 */
877#ifdef __DOXYGEN_ONLY__
878/* This is an example definition for documentation purposes.
879 * Implementations should define a suitable value in `crypto_struct.h`.
880 */
881#define PSA_HASH_OPERATION_INIT {0}
882#endif
883
884/** Return an initial value for a hash operation object.
885 */
886static psa_hash_operation_t psa_hash_operation_init(void);
887
Gilles Peskinef45adda2019-01-14 18:29:18 +0100888/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100889 *
890 * The sequence of operations to calculate a hash (message digest)
891 * is as follows:
892 * -# Allocate an operation object which will be passed to all the functions
893 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000894 * -# Initialize the operation object with one of the methods described in the
895 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200896 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100897 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100898 * of the message each time. The hash that is calculated is the hash
899 * of the concatenation of these messages in order.
900 * -# To calculate the hash, call psa_hash_finish().
901 * To compare the hash with an expected value, call psa_hash_verify().
902 *
903 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000904 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100905 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200906 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100907 * eventually terminate the operation. The following events terminate an
908 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100909 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100910 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100911 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000912 * \param[in,out] operation The operation object to set up. It must have
913 * been initialized as per the documentation for
914 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200915 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
916 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100917 *
Gilles Peskine28538492018-07-11 17:34:00 +0200918 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100919 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200920 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200921 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100922 * \retval #PSA_ERROR_BAD_STATE
923 * The operation state is not valid (already set up and not
924 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200925 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
926 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
927 * \retval #PSA_ERROR_HARDWARE_FAILURE
928 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100929 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200930psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100931 psa_algorithm_t alg);
932
Gilles Peskine308b91d2018-02-08 09:47:44 +0100933/** Add a message fragment to a multipart hash operation.
934 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200935 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100936 *
937 * If this function returns an error status, the operation becomes inactive.
938 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200939 * \param[in,out] operation Active hash operation.
940 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200941 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100942 *
Gilles Peskine28538492018-07-11 17:34:00 +0200943 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100944 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200945 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100946 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200947 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
948 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
949 * \retval #PSA_ERROR_HARDWARE_FAILURE
950 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100951 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100952psa_status_t psa_hash_update(psa_hash_operation_t *operation,
953 const uint8_t *input,
954 size_t input_length);
955
Gilles Peskine308b91d2018-02-08 09:47:44 +0100956/** Finish the calculation of the hash of a message.
957 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200958 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100959 * This function calculates the hash of the message formed by concatenating
960 * the inputs passed to preceding calls to psa_hash_update().
961 *
962 * When this function returns, the operation becomes inactive.
963 *
964 * \warning Applications should not call this function if they expect
965 * a specific value for the hash. Call psa_hash_verify() instead.
966 * Beware that comparing integrity or authenticity data such as
967 * hash values with a function such as \c memcmp is risky
968 * because the time taken by the comparison may leak information
969 * about the hashed data which could allow an attacker to guess
970 * a valid hash and thereby bypass security controls.
971 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200972 * \param[in,out] operation Active hash operation.
973 * \param[out] hash Buffer where the hash is to be written.
974 * \param hash_size Size of the \p hash buffer in bytes.
975 * \param[out] hash_length On success, the number of bytes
976 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200977 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200978 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100979 *
Gilles Peskine28538492018-07-11 17:34:00 +0200980 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100981 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200982 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100983 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200984 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200985 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200986 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100987 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200988 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
989 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
990 * \retval #PSA_ERROR_HARDWARE_FAILURE
991 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100992 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100993psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
994 uint8_t *hash,
995 size_t hash_size,
996 size_t *hash_length);
997
Gilles Peskine308b91d2018-02-08 09:47:44 +0100998/** Finish the calculation of the hash of a message and compare it with
999 * an expected value.
1000 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001001 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001002 * This function calculates the hash of the message formed by concatenating
1003 * the inputs passed to preceding calls to psa_hash_update(). It then
1004 * compares the calculated hash with the expected hash passed as a
1005 * parameter to this function.
1006 *
1007 * When this function returns, the operation becomes inactive.
1008 *
Gilles Peskine19067982018-03-20 17:54:53 +01001009 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001010 * comparison between the actual hash and the expected hash is performed
1011 * in constant time.
1012 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001013 * \param[in,out] operation Active hash operation.
1014 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001015 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001016 *
Gilles Peskine28538492018-07-11 17:34:00 +02001017 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001018 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001019 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001020 * The hash of the message was calculated successfully, but it
1021 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001022 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001023 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001024 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1025 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1026 * \retval #PSA_ERROR_HARDWARE_FAILURE
1027 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001028 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001029psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1030 const uint8_t *hash,
1031 size_t hash_length);
1032
Gilles Peskine308b91d2018-02-08 09:47:44 +01001033/** Abort a hash operation.
1034 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001035 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001036 * \p operation structure itself. Once aborted, the operation object
1037 * can be reused for another operation by calling
1038 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001039 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001040 * You may call this function any time after the operation object has
1041 * been initialized by any of the following methods:
1042 * - A call to psa_hash_setup(), whether it succeeds or not.
1043 * - Initializing the \c struct to all-bits-zero.
1044 * - Initializing the \c struct to logical zeros, e.g.
1045 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001046 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001047 * In particular, calling psa_hash_abort() after the operation has been
1048 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1049 * psa_hash_verify() is safe and has no effect.
1050 *
1051 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001052 *
Gilles Peskine28538492018-07-11 17:34:00 +02001053 * \retval #PSA_SUCCESS
1054 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001055 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001056 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1057 * \retval #PSA_ERROR_HARDWARE_FAILURE
1058 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001059 */
1060psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001061
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001062/** Clone a hash operation.
1063 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001064 * This function copies the state of an ongoing hash operation to
1065 * a new operation object. In other words, this function is equivalent
1066 * to calling psa_hash_setup() on \p target_operation with the same
1067 * algorithm that \p source_operation was set up for, then
1068 * psa_hash_update() on \p target_operation with the same input that
1069 * that was passed to \p source_operation. After this function returns, the
1070 * two objects are independent, i.e. subsequent calls involving one of
1071 * the objects do not affect the other object.
1072 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001073 * \param[in] source_operation The active hash operation to clone.
1074 * \param[in,out] target_operation The operation object to set up.
1075 * It must be initialized but not active.
1076 *
1077 * \retval #PSA_SUCCESS
1078 * \retval #PSA_ERROR_BAD_STATE
1079 * \p source_operation is not an active hash operation.
1080 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001081 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001082 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1083 * \retval #PSA_ERROR_HARDWARE_FAILURE
1084 * \retval #PSA_ERROR_TAMPERING_DETECTED
1085 */
1086psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1087 psa_hash_operation_t *target_operation);
1088
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001089/**@}*/
1090
Gilles Peskine8c9def32018-02-08 10:02:12 +01001091/** \defgroup MAC Message authentication codes
1092 * @{
1093 */
1094
Gilles Peskine69647a42019-01-14 20:18:12 +01001095/** Calculate the MAC (message authentication code) of a message.
1096 *
1097 * \note To verify the MAC of a message against an
1098 * expected value, use psa_mac_verify() instead.
1099 * Beware that comparing integrity or authenticity data such as
1100 * MAC values with a function such as \c memcmp is risky
1101 * because the time taken by the comparison may leak information
1102 * about the MAC value which could allow an attacker to guess
1103 * a valid MAC and thereby bypass security controls.
1104 *
1105 * \param handle Handle to the key to use for the operation.
1106 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001107 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001108 * \param[in] input Buffer containing the input message.
1109 * \param input_length Size of the \p input buffer in bytes.
1110 * \param[out] mac Buffer where the MAC value is to be written.
1111 * \param mac_size Size of the \p mac buffer in bytes.
1112 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001113 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001114 *
1115 * \retval #PSA_SUCCESS
1116 * Success.
1117 * \retval #PSA_ERROR_INVALID_HANDLE
1118 * \retval #PSA_ERROR_EMPTY_SLOT
1119 * \retval #PSA_ERROR_NOT_PERMITTED
1120 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001121 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001122 * \retval #PSA_ERROR_NOT_SUPPORTED
1123 * \p alg is not supported or is not a MAC algorithm.
1124 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1125 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1126 * \retval #PSA_ERROR_HARDWARE_FAILURE
1127 * \retval #PSA_ERROR_TAMPERING_DETECTED
1128 * \retval #PSA_ERROR_BAD_STATE
1129 * The library has not been previously initialized by psa_crypto_init().
1130 * It is implementation-dependent whether a failure to initialize
1131 * results in this error code.
1132 */
1133psa_status_t psa_mac_compute(psa_key_handle_t handle,
1134 psa_algorithm_t alg,
1135 const uint8_t *input,
1136 size_t input_length,
1137 uint8_t *mac,
1138 size_t mac_size,
1139 size_t *mac_length);
1140
1141/** Calculate the MAC of a message and compare it with a reference value.
1142 *
1143 * \param handle Handle to the key to use for the operation.
1144 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001145 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001146 * \param[in] input Buffer containing the input message.
1147 * \param input_length Size of the \p input buffer in bytes.
1148 * \param[out] mac Buffer containing the expected MAC value.
1149 * \param mac_length Size of the \p mac buffer in bytes.
1150 *
1151 * \retval #PSA_SUCCESS
1152 * The expected MAC is identical to the actual MAC of the input.
1153 * \retval #PSA_ERROR_INVALID_SIGNATURE
1154 * The MAC of the message was calculated successfully, but it
1155 * differs from the expected value.
1156 * \retval #PSA_ERROR_INVALID_HANDLE
1157 * \retval #PSA_ERROR_EMPTY_SLOT
1158 * \retval #PSA_ERROR_NOT_PERMITTED
1159 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001160 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001161 * \retval #PSA_ERROR_NOT_SUPPORTED
1162 * \p alg is not supported or is not a MAC algorithm.
1163 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1164 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1165 * \retval #PSA_ERROR_HARDWARE_FAILURE
1166 * \retval #PSA_ERROR_TAMPERING_DETECTED
1167 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001168psa_status_t psa_mac_verify(psa_key_handle_t handle,
1169 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001170 const uint8_t *input,
1171 size_t input_length,
1172 const uint8_t *mac,
1173 const size_t mac_length);
1174
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001175/** The type of the state data structure for multipart MAC operations.
1176 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001177 * Before calling any function on a MAC operation object, the application must
1178 * initialize it by any of the following means:
1179 * - Set the structure to all-bits-zero, for example:
1180 * \code
1181 * psa_mac_operation_t operation;
1182 * memset(&operation, 0, sizeof(operation));
1183 * \endcode
1184 * - Initialize the structure to logical zero values, for example:
1185 * \code
1186 * psa_mac_operation_t operation = {0};
1187 * \endcode
1188 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1189 * for example:
1190 * \code
1191 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1192 * \endcode
1193 * - Assign the result of the function psa_mac_operation_init()
1194 * to the structure, for example:
1195 * \code
1196 * psa_mac_operation_t operation;
1197 * operation = psa_mac_operation_init();
1198 * \endcode
1199 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001200 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001201 * make any assumptions about the content of this structure except
1202 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001203typedef struct psa_mac_operation_s psa_mac_operation_t;
1204
Jaeden Amero769ce272019-01-04 11:48:03 +00001205/** \def PSA_MAC_OPERATION_INIT
1206 *
1207 * This macro returns a suitable initializer for a MAC operation object of type
1208 * #psa_mac_operation_t.
1209 */
1210#ifdef __DOXYGEN_ONLY__
1211/* This is an example definition for documentation purposes.
1212 * Implementations should define a suitable value in `crypto_struct.h`.
1213 */
1214#define PSA_MAC_OPERATION_INIT {0}
1215#endif
1216
1217/** Return an initial value for a MAC operation object.
1218 */
1219static psa_mac_operation_t psa_mac_operation_init(void);
1220
Gilles Peskinef45adda2019-01-14 18:29:18 +01001221/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001222 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001223 * This function sets up the calculation of the MAC
1224 * (message authentication code) of a byte string.
1225 * To verify the MAC of a message against an
1226 * expected value, use psa_mac_verify_setup() instead.
1227 *
1228 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001229 * -# Allocate an operation object which will be passed to all the functions
1230 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001231 * -# Initialize the operation object with one of the methods described in the
1232 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001233 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001234 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1235 * of the message each time. The MAC that is calculated is the MAC
1236 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001237 * -# At the end of the message, call psa_mac_sign_finish() to finish
1238 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001239 *
1240 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001241 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001242 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001243 * After a successful call to psa_mac_sign_setup(), the application must
1244 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001245 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001246 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001247 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001248 * \param[in,out] operation The operation object to set up. It must have
1249 * been initialized as per the documentation for
1250 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001251 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001252 * It must remain valid until the operation
1253 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001254 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001255 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001256 *
Gilles Peskine28538492018-07-11 17:34:00 +02001257 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001258 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001259 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001260 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001261 * \retval #PSA_ERROR_NOT_PERMITTED
1262 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001263 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001264 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001265 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001266 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1267 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1268 * \retval #PSA_ERROR_HARDWARE_FAILURE
1269 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001270 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001271 * The operation state is not valid (already set up and not
1272 * subsequently completed).
1273 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001274 * The library has not been previously initialized by psa_crypto_init().
1275 * It is implementation-dependent whether a failure to initialize
1276 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001277 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001278psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001279 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001280 psa_algorithm_t alg);
1281
Gilles Peskinef45adda2019-01-14 18:29:18 +01001282/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001283 *
1284 * This function sets up the verification of the MAC
1285 * (message authentication code) of a byte string against an expected value.
1286 *
1287 * The sequence of operations to verify a MAC is as follows:
1288 * -# Allocate an operation object which will be passed to all the functions
1289 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001290 * -# Initialize the operation object with one of the methods described in the
1291 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001292 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001293 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1294 * of the message each time. The MAC that is calculated is the MAC
1295 * of the concatenation of these messages in order.
1296 * -# At the end of the message, call psa_mac_verify_finish() to finish
1297 * calculating the actual MAC of the message and verify it against
1298 * the expected value.
1299 *
1300 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001301 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001302 *
1303 * After a successful call to psa_mac_verify_setup(), the application must
1304 * eventually terminate the operation through one of the following methods:
1305 * - A failed call to psa_mac_update().
1306 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1307 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001308 * \param[in,out] operation The operation object to set up. It must have
1309 * been initialized as per the documentation for
1310 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001311 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001312 * It must remain valid until the operation
1313 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001314 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1315 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001316 *
Gilles Peskine28538492018-07-11 17:34:00 +02001317 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001318 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001319 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001320 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001321 * \retval #PSA_ERROR_NOT_PERMITTED
1322 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001323 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001324 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001325 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001326 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1327 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1328 * \retval #PSA_ERROR_HARDWARE_FAILURE
1329 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001330 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001331 * The operation state is not valid (already set up and not
1332 * subsequently completed).
1333 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001334 * The library has not been previously initialized by psa_crypto_init().
1335 * It is implementation-dependent whether a failure to initialize
1336 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001337 */
1338psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001339 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001340 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001341
Gilles Peskinedcd14942018-07-12 00:30:52 +02001342/** Add a message fragment to a multipart MAC operation.
1343 *
1344 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1345 * before calling this function.
1346 *
1347 * If this function returns an error status, the operation becomes inactive.
1348 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001349 * \param[in,out] operation Active MAC operation.
1350 * \param[in] input Buffer containing the message fragment to add to
1351 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001352 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001353 *
1354 * \retval #PSA_SUCCESS
1355 * Success.
1356 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001357 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001358 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1359 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1360 * \retval #PSA_ERROR_HARDWARE_FAILURE
1361 * \retval #PSA_ERROR_TAMPERING_DETECTED
1362 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001363psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1364 const uint8_t *input,
1365 size_t input_length);
1366
Gilles Peskinedcd14942018-07-12 00:30:52 +02001367/** Finish the calculation of the MAC of a message.
1368 *
1369 * The application must call psa_mac_sign_setup() before calling this function.
1370 * This function calculates the MAC of the message formed by concatenating
1371 * the inputs passed to preceding calls to psa_mac_update().
1372 *
1373 * When this function returns, the operation becomes inactive.
1374 *
1375 * \warning Applications should not call this function if they expect
1376 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1377 * Beware that comparing integrity or authenticity data such as
1378 * MAC values with a function such as \c memcmp is risky
1379 * because the time taken by the comparison may leak information
1380 * about the MAC value which could allow an attacker to guess
1381 * a valid MAC and thereby bypass security controls.
1382 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001383 * \param[in,out] operation Active MAC operation.
1384 * \param[out] mac Buffer where the MAC value is to be written.
1385 * \param mac_size Size of the \p mac buffer in bytes.
1386 * \param[out] mac_length On success, the number of bytes
1387 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001388 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001389 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001390 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001391 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001392 *
1393 * \retval #PSA_SUCCESS
1394 * Success.
1395 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001396 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001397 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001398 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001399 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1400 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1401 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1402 * \retval #PSA_ERROR_HARDWARE_FAILURE
1403 * \retval #PSA_ERROR_TAMPERING_DETECTED
1404 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001405psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1406 uint8_t *mac,
1407 size_t mac_size,
1408 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001409
Gilles Peskinedcd14942018-07-12 00:30:52 +02001410/** Finish the calculation of the MAC of a message and compare it with
1411 * an expected value.
1412 *
1413 * The application must call psa_mac_verify_setup() before calling this function.
1414 * This function calculates the MAC of the message formed by concatenating
1415 * the inputs passed to preceding calls to psa_mac_update(). It then
1416 * compares the calculated MAC with the expected MAC passed as a
1417 * parameter to this function.
1418 *
1419 * When this function returns, the operation becomes inactive.
1420 *
1421 * \note Implementations shall make the best effort to ensure that the
1422 * comparison between the actual MAC and the expected MAC is performed
1423 * in constant time.
1424 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001425 * \param[in,out] operation Active MAC operation.
1426 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001427 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001428 *
1429 * \retval #PSA_SUCCESS
1430 * The expected MAC is identical to the actual MAC of the message.
1431 * \retval #PSA_ERROR_INVALID_SIGNATURE
1432 * The MAC of the message was calculated successfully, but it
1433 * differs from the expected MAC.
1434 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001435 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001436 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1437 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1438 * \retval #PSA_ERROR_HARDWARE_FAILURE
1439 * \retval #PSA_ERROR_TAMPERING_DETECTED
1440 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001441psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1442 const uint8_t *mac,
1443 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001444
Gilles Peskinedcd14942018-07-12 00:30:52 +02001445/** Abort a MAC operation.
1446 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001447 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001448 * \p operation structure itself. Once aborted, the operation object
1449 * can be reused for another operation by calling
1450 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001451 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001452 * You may call this function any time after the operation object has
1453 * been initialized by any of the following methods:
1454 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1455 * it succeeds or not.
1456 * - Initializing the \c struct to all-bits-zero.
1457 * - Initializing the \c struct to logical zeros, e.g.
1458 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001459 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001460 * In particular, calling psa_mac_abort() after the operation has been
1461 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1462 * psa_mac_verify_finish() is safe and has no effect.
1463 *
1464 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001465 *
1466 * \retval #PSA_SUCCESS
1467 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001468 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001469 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1470 * \retval #PSA_ERROR_HARDWARE_FAILURE
1471 * \retval #PSA_ERROR_TAMPERING_DETECTED
1472 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001473psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1474
1475/**@}*/
1476
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001477/** \defgroup cipher Symmetric ciphers
1478 * @{
1479 */
1480
Gilles Peskine69647a42019-01-14 20:18:12 +01001481/** Encrypt a message using a symmetric cipher.
1482 *
1483 * This function encrypts a message with a random IV (initialization
1484 * vector).
1485 *
1486 * \param handle Handle to the key to use for the operation.
1487 * It must remain valid until the operation
1488 * terminates.
1489 * \param alg The cipher algorithm to compute
1490 * (\c PSA_ALG_XXX value such that
1491 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1492 * \param[in] input Buffer containing the message to encrypt.
1493 * \param input_length Size of the \p input buffer in bytes.
1494 * \param[out] output Buffer where the output is to be written.
1495 * The output contains the IV followed by
1496 * the ciphertext proper.
1497 * \param output_size Size of the \p output buffer in bytes.
1498 * \param[out] output_length On success, the number of bytes
1499 * that make up the output.
1500 *
1501 * \retval #PSA_SUCCESS
1502 * Success.
1503 * \retval #PSA_ERROR_INVALID_HANDLE
1504 * \retval #PSA_ERROR_EMPTY_SLOT
1505 * \retval #PSA_ERROR_NOT_PERMITTED
1506 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001507 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001508 * \retval #PSA_ERROR_NOT_SUPPORTED
1509 * \p alg is not supported or is not a cipher algorithm.
1510 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1511 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1512 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1513 * \retval #PSA_ERROR_HARDWARE_FAILURE
1514 * \retval #PSA_ERROR_TAMPERING_DETECTED
1515 */
1516psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1517 psa_algorithm_t alg,
1518 const uint8_t *input,
1519 size_t input_length,
1520 uint8_t *output,
1521 size_t output_size,
1522 size_t *output_length);
1523
1524/** Decrypt a message using a symmetric cipher.
1525 *
1526 * This function decrypts a message encrypted with a symmetric cipher.
1527 *
1528 * \param handle Handle to the key to use for the operation.
1529 * It must remain valid until the operation
1530 * terminates.
1531 * \param alg The cipher algorithm to compute
1532 * (\c PSA_ALG_XXX value such that
1533 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1534 * \param[in] input Buffer containing the message to decrypt.
1535 * This consists of the IV followed by the
1536 * ciphertext proper.
1537 * \param input_length Size of the \p input buffer in bytes.
1538 * \param[out] output Buffer where the plaintext is to be written.
1539 * \param output_size Size of the \p output buffer in bytes.
1540 * \param[out] output_length On success, the number of bytes
1541 * that make up the output.
1542 *
1543 * \retval #PSA_SUCCESS
1544 * Success.
1545 * \retval #PSA_ERROR_INVALID_HANDLE
1546 * \retval #PSA_ERROR_EMPTY_SLOT
1547 * \retval #PSA_ERROR_NOT_PERMITTED
1548 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001549 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001550 * \retval #PSA_ERROR_NOT_SUPPORTED
1551 * \p alg is not supported or is not a cipher algorithm.
1552 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1553 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1554 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1555 * \retval #PSA_ERROR_HARDWARE_FAILURE
1556 * \retval #PSA_ERROR_TAMPERING_DETECTED
1557 */
1558psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1559 psa_algorithm_t alg,
1560 const uint8_t *input,
1561 size_t input_length,
1562 uint8_t *output,
1563 size_t output_size,
1564 size_t *output_length);
1565
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001566/** The type of the state data structure for multipart cipher operations.
1567 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001568 * Before calling any function on a cipher operation object, the application
1569 * must initialize it by any of the following means:
1570 * - Set the structure to all-bits-zero, for example:
1571 * \code
1572 * psa_cipher_operation_t operation;
1573 * memset(&operation, 0, sizeof(operation));
1574 * \endcode
1575 * - Initialize the structure to logical zero values, for example:
1576 * \code
1577 * psa_cipher_operation_t operation = {0};
1578 * \endcode
1579 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1580 * for example:
1581 * \code
1582 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1583 * \endcode
1584 * - Assign the result of the function psa_cipher_operation_init()
1585 * to the structure, for example:
1586 * \code
1587 * psa_cipher_operation_t operation;
1588 * operation = psa_cipher_operation_init();
1589 * \endcode
1590 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001591 * This is an implementation-defined \c struct. Applications should not
1592 * make any assumptions about the content of this structure except
1593 * as directed by the documentation of a specific implementation. */
1594typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1595
Jaeden Amero5bae2272019-01-04 11:48:27 +00001596/** \def PSA_CIPHER_OPERATION_INIT
1597 *
1598 * This macro returns a suitable initializer for a cipher operation object of
1599 * type #psa_cipher_operation_t.
1600 */
1601#ifdef __DOXYGEN_ONLY__
1602/* This is an example definition for documentation purposes.
1603 * Implementations should define a suitable value in `crypto_struct.h`.
1604 */
1605#define PSA_CIPHER_OPERATION_INIT {0}
1606#endif
1607
1608/** Return an initial value for a cipher operation object.
1609 */
1610static psa_cipher_operation_t psa_cipher_operation_init(void);
1611
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001612/** Set the key for a multipart symmetric encryption operation.
1613 *
1614 * The sequence of operations to encrypt a message with a symmetric cipher
1615 * is as follows:
1616 * -# Allocate an operation object which will be passed to all the functions
1617 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001618 * -# Initialize the operation object with one of the methods described in the
1619 * documentation for #psa_cipher_operation_t, e.g.
1620 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001621 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001622 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001623 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001624 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001625 * requires a specific IV value.
1626 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1627 * of the message each time.
1628 * -# Call psa_cipher_finish().
1629 *
1630 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001631 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001632 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001633 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001634 * eventually terminate the operation. The following events terminate an
1635 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001636 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001637 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001638 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001639 * \param[in,out] operation The operation object to set up. It must have
1640 * been initialized as per the documentation for
1641 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001642 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001643 * It must remain valid until the operation
1644 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001645 * \param alg The cipher algorithm to compute
1646 * (\c PSA_ALG_XXX value such that
1647 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001648 *
Gilles Peskine28538492018-07-11 17:34:00 +02001649 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001650 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001651 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001652 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001653 * \retval #PSA_ERROR_NOT_PERMITTED
1654 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001655 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001656 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001657 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001658 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1659 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1660 * \retval #PSA_ERROR_HARDWARE_FAILURE
1661 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001662 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001663 * The operation state is not valid (already set up and not
1664 * subsequently completed).
1665 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001666 * The library has not been previously initialized by psa_crypto_init().
1667 * It is implementation-dependent whether a failure to initialize
1668 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001669 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001670psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001671 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001672 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001673
1674/** Set the key for a multipart symmetric decryption operation.
1675 *
1676 * The sequence of operations to decrypt a message with a symmetric cipher
1677 * is as follows:
1678 * -# Allocate an operation object which will be passed to all the functions
1679 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001680 * -# Initialize the operation object with one of the methods described in the
1681 * documentation for #psa_cipher_operation_t, e.g.
1682 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001683 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001684 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001685 * decryption. If the IV is prepended to the ciphertext, you can call
1686 * psa_cipher_update() on a buffer containing the IV followed by the
1687 * beginning of the message.
1688 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1689 * of the message each time.
1690 * -# Call psa_cipher_finish().
1691 *
1692 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001693 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001694 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001695 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001696 * eventually terminate the operation. The following events terminate an
1697 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001698 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001699 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001700 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001701 * \param[in,out] operation The operation object to set up. It must have
1702 * been initialized as per the documentation for
1703 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001704 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001705 * It must remain valid until the operation
1706 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001707 * \param alg The cipher algorithm to compute
1708 * (\c PSA_ALG_XXX value such that
1709 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001710 *
Gilles Peskine28538492018-07-11 17:34:00 +02001711 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001712 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001713 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001714 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001715 * \retval #PSA_ERROR_NOT_PERMITTED
1716 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001717 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001718 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001719 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001720 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1721 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1722 * \retval #PSA_ERROR_HARDWARE_FAILURE
1723 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001724 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001725 * The operation state is not valid (already set up and not
1726 * subsequently completed).
1727 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001728 * The library has not been previously initialized by psa_crypto_init().
1729 * It is implementation-dependent whether a failure to initialize
1730 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001731 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001732psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001733 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001734 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001735
Gilles Peskinedcd14942018-07-12 00:30:52 +02001736/** Generate an IV for a symmetric encryption operation.
1737 *
1738 * This function generates a random IV (initialization vector), nonce
1739 * or initial counter value for the encryption operation as appropriate
1740 * for the chosen algorithm, key type and key size.
1741 *
1742 * The application must call psa_cipher_encrypt_setup() before
1743 * calling this function.
1744 *
1745 * If this function returns an error status, the operation becomes inactive.
1746 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001747 * \param[in,out] operation Active cipher operation.
1748 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001749 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001750 * \param[out] iv_length On success, the number of bytes of the
1751 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001752 *
1753 * \retval #PSA_SUCCESS
1754 * Success.
1755 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001756 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001757 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001758 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001759 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1760 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1761 * \retval #PSA_ERROR_HARDWARE_FAILURE
1762 * \retval #PSA_ERROR_TAMPERING_DETECTED
1763 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001764psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1765 unsigned char *iv,
1766 size_t iv_size,
1767 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001768
Gilles Peskinedcd14942018-07-12 00:30:52 +02001769/** Set the IV for a symmetric encryption or decryption operation.
1770 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001771 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001772 * or initial counter value for the encryption or decryption operation.
1773 *
1774 * The application must call psa_cipher_encrypt_setup() before
1775 * calling this function.
1776 *
1777 * If this function returns an error status, the operation becomes inactive.
1778 *
1779 * \note When encrypting, applications should use psa_cipher_generate_iv()
1780 * instead of this function, unless implementing a protocol that requires
1781 * a non-random IV.
1782 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001783 * \param[in,out] operation Active cipher operation.
1784 * \param[in] iv Buffer containing the IV to use.
1785 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001786 *
1787 * \retval #PSA_SUCCESS
1788 * Success.
1789 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001790 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001791 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001792 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001793 * or the chosen algorithm does not use an IV.
1794 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1795 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1796 * \retval #PSA_ERROR_HARDWARE_FAILURE
1797 * \retval #PSA_ERROR_TAMPERING_DETECTED
1798 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001799psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1800 const unsigned char *iv,
1801 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001802
Gilles Peskinedcd14942018-07-12 00:30:52 +02001803/** Encrypt or decrypt a message fragment in an active cipher operation.
1804 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001805 * Before calling this function, you must:
1806 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1807 * The choice of setup function determines whether this function
1808 * encrypts or decrypts its input.
1809 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1810 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001811 *
1812 * If this function returns an error status, the operation becomes inactive.
1813 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001814 * \param[in,out] operation Active cipher operation.
1815 * \param[in] input Buffer containing the message fragment to
1816 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001817 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001818 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001819 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001820 * \param[out] output_length On success, the number of bytes
1821 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001822 *
1823 * \retval #PSA_SUCCESS
1824 * Success.
1825 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001826 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001827 * not set, or already completed).
1828 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1829 * The size of the \p output buffer is too small.
1830 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1831 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1832 * \retval #PSA_ERROR_HARDWARE_FAILURE
1833 * \retval #PSA_ERROR_TAMPERING_DETECTED
1834 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001835psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1836 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001837 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001838 unsigned char *output,
1839 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001840 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001841
Gilles Peskinedcd14942018-07-12 00:30:52 +02001842/** Finish encrypting or decrypting a message in a cipher operation.
1843 *
1844 * The application must call psa_cipher_encrypt_setup() or
1845 * psa_cipher_decrypt_setup() before calling this function. The choice
1846 * of setup function determines whether this function encrypts or
1847 * decrypts its input.
1848 *
1849 * This function finishes the encryption or decryption of the message
1850 * formed by concatenating the inputs passed to preceding calls to
1851 * psa_cipher_update().
1852 *
1853 * When this function returns, the operation becomes inactive.
1854 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001855 * \param[in,out] operation Active cipher operation.
1856 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001857 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001858 * \param[out] output_length On success, the number of bytes
1859 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001860 *
1861 * \retval #PSA_SUCCESS
1862 * Success.
1863 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001864 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001865 * not set, or already completed).
1866 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1867 * The size of the \p output buffer is too small.
1868 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1869 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1870 * \retval #PSA_ERROR_HARDWARE_FAILURE
1871 * \retval #PSA_ERROR_TAMPERING_DETECTED
1872 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001873psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001874 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001875 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001876 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001877
Gilles Peskinedcd14942018-07-12 00:30:52 +02001878/** Abort a cipher operation.
1879 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001880 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001881 * \p operation structure itself. Once aborted, the operation object
1882 * can be reused for another operation by calling
1883 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001884 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001885 * You may call this function any time after the operation object has
1886 * been initialized by any of the following methods:
1887 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1888 * whether it succeeds or not.
1889 * - Initializing the \c struct to all-bits-zero.
1890 * - Initializing the \c struct to logical zeros, e.g.
1891 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001892 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001893 * In particular, calling psa_cipher_abort() after the operation has been
1894 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1895 * is safe and has no effect.
1896 *
1897 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001898 *
1899 * \retval #PSA_SUCCESS
1900 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001901 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001902 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1903 * \retval #PSA_ERROR_HARDWARE_FAILURE
1904 * \retval #PSA_ERROR_TAMPERING_DETECTED
1905 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001906psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1907
1908/**@}*/
1909
Gilles Peskine3b555712018-03-03 21:27:57 +01001910/** \defgroup aead Authenticated encryption with associated data (AEAD)
1911 * @{
1912 */
1913
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001914/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001915 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001916 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001917 * \param alg The AEAD algorithm to compute
1918 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001919 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001920 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001921 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001922 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001923 * but not encrypted.
1924 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001925 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001926 * encrypted.
1927 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001928 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001929 * encrypted data. The additional data is not
1930 * part of this output. For algorithms where the
1931 * encrypted data and the authentication tag
1932 * are defined as separate outputs, the
1933 * authentication tag is appended to the
1934 * encrypted data.
1935 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1936 * This must be at least
1937 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1938 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001939 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001940 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001941 *
Gilles Peskine28538492018-07-11 17:34:00 +02001942 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001943 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001944 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001945 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001946 * \retval #PSA_ERROR_NOT_PERMITTED
1947 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001948 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001949 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001950 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001951 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1952 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1953 * \retval #PSA_ERROR_HARDWARE_FAILURE
1954 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001955 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001956 * The library has not been previously initialized by psa_crypto_init().
1957 * It is implementation-dependent whether a failure to initialize
1958 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001959 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001960psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001961 psa_algorithm_t alg,
1962 const uint8_t *nonce,
1963 size_t nonce_length,
1964 const uint8_t *additional_data,
1965 size_t additional_data_length,
1966 const uint8_t *plaintext,
1967 size_t plaintext_length,
1968 uint8_t *ciphertext,
1969 size_t ciphertext_size,
1970 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001971
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001972/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001973 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001974 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001975 * \param alg The AEAD algorithm to compute
1976 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001977 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001978 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001979 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001980 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001981 * but not encrypted.
1982 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001983 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001984 * encrypted. For algorithms where the
1985 * encrypted data and the authentication tag
1986 * are defined as separate inputs, the buffer
1987 * must contain the encrypted data followed
1988 * by the authentication tag.
1989 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001990 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001991 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1992 * This must be at least
1993 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1994 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001995 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001996 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001997 *
Gilles Peskine28538492018-07-11 17:34:00 +02001998 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001999 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002000 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002001 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002002 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002003 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002004 * \retval #PSA_ERROR_NOT_PERMITTED
2005 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002006 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002007 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002008 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002009 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2010 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2011 * \retval #PSA_ERROR_HARDWARE_FAILURE
2012 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002013 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002014 * The library has not been previously initialized by psa_crypto_init().
2015 * It is implementation-dependent whether a failure to initialize
2016 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002017 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002018psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002019 psa_algorithm_t alg,
2020 const uint8_t *nonce,
2021 size_t nonce_length,
2022 const uint8_t *additional_data,
2023 size_t additional_data_length,
2024 const uint8_t *ciphertext,
2025 size_t ciphertext_length,
2026 uint8_t *plaintext,
2027 size_t plaintext_size,
2028 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002029
Gilles Peskine30a9e412019-01-14 18:36:12 +01002030/** The type of the state data structure for multipart AEAD operations.
2031 *
2032 * Before calling any function on an AEAD operation object, the application
2033 * must initialize it by any of the following means:
2034 * - Set the structure to all-bits-zero, for example:
2035 * \code
2036 * psa_aead_operation_t operation;
2037 * memset(&operation, 0, sizeof(operation));
2038 * \endcode
2039 * - Initialize the structure to logical zero values, for example:
2040 * \code
2041 * psa_aead_operation_t operation = {0};
2042 * \endcode
2043 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2044 * for example:
2045 * \code
2046 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2047 * \endcode
2048 * - Assign the result of the function psa_aead_operation_init()
2049 * to the structure, for example:
2050 * \code
2051 * psa_aead_operation_t operation;
2052 * operation = psa_aead_operation_init();
2053 * \endcode
2054 *
2055 * This is an implementation-defined \c struct. Applications should not
2056 * make any assumptions about the content of this structure except
2057 * as directed by the documentation of a specific implementation. */
2058typedef struct psa_aead_operation_s psa_aead_operation_t;
2059
2060/** \def PSA_AEAD_OPERATION_INIT
2061 *
2062 * This macro returns a suitable initializer for an AEAD operation object of
2063 * type #psa_aead_operation_t.
2064 */
2065#ifdef __DOXYGEN_ONLY__
2066/* This is an example definition for documentation purposes.
2067 * Implementations should define a suitable value in `crypto_struct.h`.
2068 */
2069#define PSA_AEAD_OPERATION_INIT {0}
2070#endif
2071
2072/** Return an initial value for an AEAD operation object.
2073 */
2074static psa_aead_operation_t psa_aead_operation_init(void);
2075
2076/** Set the key for a multipart authenticated encryption operation.
2077 *
2078 * The sequence of operations to encrypt a message with authentication
2079 * is as follows:
2080 * -# Allocate an operation object which will be passed to all the functions
2081 * listed here.
2082 * -# Initialize the operation object with one of the methods described in the
2083 * documentation for #psa_aead_operation_t, e.g.
2084 * PSA_AEAD_OPERATION_INIT.
2085 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002086 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2087 * inputs to the subsequent calls to psa_aead_update_ad() and
2088 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2089 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002090 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2091 * generate or set the nonce. You should use
2092 * psa_aead_generate_nonce() unless the protocol you are implementing
2093 * requires a specific nonce value.
2094 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2095 * of the non-encrypted additional authenticated data each time.
2096 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002097 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002098 * -# Call psa_aead_finish().
2099 *
2100 * The application may call psa_aead_abort() at any time after the operation
2101 * has been initialized.
2102 *
2103 * After a successful call to psa_aead_encrypt_setup(), the application must
2104 * eventually terminate the operation. The following events terminate an
2105 * operation:
2106 * - A failed call to any of the \c psa_aead_xxx functions.
2107 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2108 *
2109 * \param[in,out] operation The operation object to set up. It must have
2110 * been initialized as per the documentation for
2111 * #psa_aead_operation_t and not yet in use.
2112 * \param handle Handle to the key to use for the operation.
2113 * It must remain valid until the operation
2114 * terminates.
2115 * \param alg The AEAD algorithm to compute
2116 * (\c PSA_ALG_XXX value such that
2117 * #PSA_ALG_IS_AEAD(\p alg) is true).
2118 *
2119 * \retval #PSA_SUCCESS
2120 * Success.
2121 * \retval #PSA_ERROR_INVALID_HANDLE
2122 * \retval #PSA_ERROR_EMPTY_SLOT
2123 * \retval #PSA_ERROR_NOT_PERMITTED
2124 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002125 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002126 * \retval #PSA_ERROR_NOT_SUPPORTED
2127 * \p alg is not supported or is not an AEAD algorithm.
2128 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2129 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2130 * \retval #PSA_ERROR_HARDWARE_FAILURE
2131 * \retval #PSA_ERROR_TAMPERING_DETECTED
2132 * \retval #PSA_ERROR_BAD_STATE
2133 * The library has not been previously initialized by psa_crypto_init().
2134 * It is implementation-dependent whether a failure to initialize
2135 * results in this error code.
2136 */
2137psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2138 psa_key_handle_t handle,
2139 psa_algorithm_t alg);
2140
2141/** Set the key for a multipart authenticated decryption operation.
2142 *
2143 * The sequence of operations to decrypt a message with authentication
2144 * is as follows:
2145 * -# Allocate an operation object which will be passed to all the functions
2146 * listed here.
2147 * -# Initialize the operation object with one of the methods described in the
2148 * documentation for #psa_aead_operation_t, e.g.
2149 * PSA_AEAD_OPERATION_INIT.
2150 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002151 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2152 * inputs to the subsequent calls to psa_aead_update_ad() and
2153 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2154 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002155 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2156 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2157 * of the non-encrypted additional authenticated data each time.
2158 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002159 * of the ciphertext to decrypt each time.
2160 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002161 *
2162 * The application may call psa_aead_abort() at any time after the operation
2163 * has been initialized.
2164 *
2165 * After a successful call to psa_aead_decrypt_setup(), the application must
2166 * eventually terminate the operation. The following events terminate an
2167 * operation:
2168 * - A failed call to any of the \c psa_aead_xxx functions.
2169 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2170 *
2171 * \param[in,out] operation The operation object to set up. It must have
2172 * been initialized as per the documentation for
2173 * #psa_aead_operation_t and not yet in use.
2174 * \param handle Handle to the key to use for the operation.
2175 * It must remain valid until the operation
2176 * terminates.
2177 * \param alg The AEAD algorithm to compute
2178 * (\c PSA_ALG_XXX value such that
2179 * #PSA_ALG_IS_AEAD(\p alg) is true).
2180 *
2181 * \retval #PSA_SUCCESS
2182 * Success.
2183 * \retval #PSA_ERROR_INVALID_HANDLE
2184 * \retval #PSA_ERROR_EMPTY_SLOT
2185 * \retval #PSA_ERROR_NOT_PERMITTED
2186 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002187 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002188 * \retval #PSA_ERROR_NOT_SUPPORTED
2189 * \p alg is not supported or is not an AEAD algorithm.
2190 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2191 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2192 * \retval #PSA_ERROR_HARDWARE_FAILURE
2193 * \retval #PSA_ERROR_TAMPERING_DETECTED
2194 * \retval #PSA_ERROR_BAD_STATE
2195 * The library has not been previously initialized by psa_crypto_init().
2196 * It is implementation-dependent whether a failure to initialize
2197 * results in this error code.
2198 */
2199psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2200 psa_key_handle_t handle,
2201 psa_algorithm_t alg);
2202
2203/** Generate a random nonce for an authenticated encryption operation.
2204 *
2205 * This function generates a random nonce for the authenticated encryption
2206 * operation with an appropriate size for the chosen algorithm, key type
2207 * and key size.
2208 *
2209 * The application must call psa_aead_encrypt_setup() before
2210 * calling this function.
2211 *
2212 * If this function returns an error status, the operation becomes inactive.
2213 *
2214 * \param[in,out] operation Active AEAD operation.
2215 * \param[out] nonce Buffer where the generated nonce is to be
2216 * written.
2217 * \param nonce_size Size of the \p nonce buffer in bytes.
2218 * \param[out] nonce_length On success, the number of bytes of the
2219 * generated nonce.
2220 *
2221 * \retval #PSA_SUCCESS
2222 * Success.
2223 * \retval #PSA_ERROR_BAD_STATE
2224 * The operation state is not valid (not set up, or nonce already set).
2225 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2226 * The size of the \p nonce buffer is too small.
2227 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2228 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2229 * \retval #PSA_ERROR_HARDWARE_FAILURE
2230 * \retval #PSA_ERROR_TAMPERING_DETECTED
2231 */
2232psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2233 unsigned char *nonce,
2234 size_t nonce_size,
2235 size_t *nonce_length);
2236
2237/** Set the nonce for an authenticated encryption or decryption operation.
2238 *
2239 * This function sets the nonce for the authenticated
2240 * encryption or decryption operation.
2241 *
2242 * The application must call psa_aead_encrypt_setup() before
2243 * calling this function.
2244 *
2245 * If this function returns an error status, the operation becomes inactive.
2246 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002247 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002248 * instead of this function, unless implementing a protocol that requires
2249 * a non-random IV.
2250 *
2251 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002252 * \param[in] nonce Buffer containing the nonce to use.
2253 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002254 *
2255 * \retval #PSA_SUCCESS
2256 * Success.
2257 * \retval #PSA_ERROR_BAD_STATE
2258 * The operation state is not valid (not set up, or nonce already set).
2259 * \retval #PSA_ERROR_INVALID_ARGUMENT
2260 * The size of \p nonce is not acceptable for the chosen algorithm.
2261 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2262 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2263 * \retval #PSA_ERROR_HARDWARE_FAILURE
2264 * \retval #PSA_ERROR_TAMPERING_DETECTED
2265 */
2266psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2267 const unsigned char *nonce,
2268 size_t nonce_length);
2269
Gilles Peskinebc59c852019-01-17 15:26:08 +01002270/** Declare the lengths of the message and additional data for AEAD.
2271 *
2272 * The application must call this function before calling
2273 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2274 * the operation requires it. If the algorithm does not require it,
2275 * calling this function is optional, but if this function is called
2276 * then the implementation must enforce the lengths.
2277 *
2278 * You may call this function before or after setting the nonce with
2279 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2280 *
2281 * - For #PSA_ALG_CCM, calling this function is required.
2282 * - For the other AEAD algorithms defined in this specification, calling
2283 * this function is not required.
2284 * - For vendor-defined algorithm, refer to the vendor documentation.
2285 *
2286 * \param[in,out] operation Active AEAD operation.
2287 * \param ad_length Size of the non-encrypted additional
2288 * authenticated data in bytes.
2289 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2290 *
2291 * \retval #PSA_SUCCESS
2292 * Success.
2293 * \retval #PSA_ERROR_BAD_STATE
2294 * The operation state is not valid (not set up, already completed,
2295 * or psa_aead_update_ad() or psa_aead_update() already called).
2296 * \retval #PSA_ERROR_INVALID_ARGUMENT
2297 * At least one of the lengths is not acceptable for the chosen
2298 * algorithm.
2299 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2300 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2301 * \retval #PSA_ERROR_HARDWARE_FAILURE
2302 * \retval #PSA_ERROR_TAMPERING_DETECTED
2303 */
2304psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2305 size_t ad_length,
2306 size_t plaintext_length);
2307
Gilles Peskine30a9e412019-01-14 18:36:12 +01002308/** Pass additional data to an active AEAD operation.
2309 *
2310 * Additional data is authenticated, but not encrypted.
2311 *
2312 * You may call this function multiple times to pass successive fragments
2313 * of the additional data. You may not call this function after passing
2314 * data to encrypt or decrypt with psa_aead_update().
2315 *
2316 * Before calling this function, you must:
2317 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2318 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2319 *
2320 * If this function returns an error status, the operation becomes inactive.
2321 *
2322 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2323 * there is no guarantee that the input is valid. Therefore, until
2324 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2325 * treat the input as untrusted and prepare to undo any action that
2326 * depends on the input if psa_aead_verify() returns an error status.
2327 *
2328 * \param[in,out] operation Active AEAD operation.
2329 * \param[in] input Buffer containing the fragment of
2330 * additional data.
2331 * \param input_length Size of the \p input buffer in bytes.
2332 *
2333 * \retval #PSA_SUCCESS
2334 * Success.
2335 * \retval #PSA_ERROR_BAD_STATE
2336 * The operation state is not valid (not set up, nonce not set,
2337 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002338 * \retval #PSA_ERROR_INVALID_ARGUMENT
2339 * The total input length overflows the additional data length that
2340 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002341 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2342 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2343 * \retval #PSA_ERROR_HARDWARE_FAILURE
2344 * \retval #PSA_ERROR_TAMPERING_DETECTED
2345 */
2346psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2347 const uint8_t *input,
2348 size_t input_length);
2349
2350/** Encrypt or decrypt a message fragment in an active AEAD operation.
2351 *
2352 * Before calling this function, you must:
2353 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2354 * The choice of setup function determines whether this function
2355 * encrypts or decrypts its input.
2356 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2357 * 3. Call psa_aead_update_ad() to pass all the additional data.
2358 *
2359 * If this function returns an error status, the operation becomes inactive.
2360 *
2361 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2362 * there is no guarantee that the input is valid. Therefore, until
2363 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2364 * - Do not use the output in any way other than storing it in a
2365 * confidential location. If you take any action that depends
2366 * on the tentative decrypted data, this action will need to be
2367 * undone if the input turns out not to be valid. Furthermore,
2368 * if an adversary can observe that this action took place
2369 * (for example through timing), they may be able to use this
2370 * fact as an oracle to decrypt any message encrypted with the
2371 * same key.
2372 * - In particular, do not copy the output anywhere but to a
2373 * memory or storage space that you have exclusive access to.
2374 *
2375 * \param[in,out] operation Active AEAD operation.
2376 * \param[in] input Buffer containing the message fragment to
2377 * encrypt or decrypt.
2378 * \param input_length Size of the \p input buffer in bytes.
2379 * \param[out] output Buffer where the output is to be written.
2380 * \param output_size Size of the \p output buffer in bytes.
2381 * \param[out] output_length On success, the number of bytes
2382 * that make up the returned output.
2383 *
2384 * \retval #PSA_SUCCESS
2385 * Success.
2386 * \retval #PSA_ERROR_BAD_STATE
2387 * The operation state is not valid (not set up, nonce not set
2388 * or already completed).
2389 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2390 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002391 * \retval #PSA_ERROR_INVALID_ARGUMENT
2392 * The total length of input to psa_aead_update_ad() so far is
2393 * less than the additional data length that was previously
2394 * specified with psa_aead_set_lengths().
2395 * \retval #PSA_ERROR_INVALID_ARGUMENT
2396 * The total input length overflows the plaintext length that
2397 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002398 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2399 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2400 * \retval #PSA_ERROR_HARDWARE_FAILURE
2401 * \retval #PSA_ERROR_TAMPERING_DETECTED
2402 */
2403psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2404 const uint8_t *input,
2405 size_t input_length,
2406 unsigned char *output,
2407 size_t output_size,
2408 size_t *output_length);
2409
2410/** Finish encrypting a message in an AEAD operation.
2411 *
2412 * The operation must have been set up with psa_aead_encrypt_setup().
2413 *
2414 * This function finishes the authentication of the additional data
2415 * formed by concatenating the inputs passed to preceding calls to
2416 * psa_aead_update_ad() with the plaintext formed by concatenating the
2417 * inputs passed to preceding calls to psa_aead_update().
2418 *
2419 * This function has two output buffers:
2420 * - \p ciphertext contains trailing ciphertext that was buffered from
2421 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2422 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2423 * will not contain any output and can be a 0-sized buffer.
2424 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002425 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002426 * that the operation performs.
2427 *
2428 * When this function returns, the operation becomes inactive.
2429 *
2430 * \param[in,out] operation Active AEAD operation.
2431 * \param[out] ciphertext Buffer where the last part of the ciphertext
2432 * is to be written.
2433 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2434 * \param[out] ciphertext_length On success, the number of bytes of
2435 * returned ciphertext.
2436 * \param[out] tag Buffer where the authentication tag is
2437 * to be written.
2438 * \param tag_size Size of the \p tag buffer in bytes.
2439 * \param[out] tag_length On success, the number of bytes
2440 * that make up the returned tag.
2441 *
2442 * \retval #PSA_SUCCESS
2443 * Success.
2444 * \retval #PSA_ERROR_BAD_STATE
2445 * The operation state is not valid (not set up, nonce not set,
2446 * decryption, or already completed).
2447 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002448 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002449 * \retval #PSA_ERROR_INVALID_ARGUMENT
2450 * The total length of input to psa_aead_update_ad() so far is
2451 * less than the additional data length that was previously
2452 * specified with psa_aead_set_lengths().
2453 * \retval #PSA_ERROR_INVALID_ARGUMENT
2454 * The total length of input to psa_aead_update() so far is
2455 * less than the plaintext length that was previously
2456 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002457 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2458 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2459 * \retval #PSA_ERROR_HARDWARE_FAILURE
2460 * \retval #PSA_ERROR_TAMPERING_DETECTED
2461 */
2462psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002463 uint8_t *ciphertext,
2464 size_t ciphertext_size,
2465 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002466 uint8_t *tag,
2467 size_t tag_size,
2468 size_t *tag_length);
2469
2470/** Finish authenticating and decrypting a message in an AEAD operation.
2471 *
2472 * The operation must have been set up with psa_aead_decrypt_setup().
2473 *
2474 * This function finishes the authentication of the additional data
2475 * formed by concatenating the inputs passed to preceding calls to
2476 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2477 * inputs passed to preceding calls to psa_aead_update().
2478 *
2479 * When this function returns, the operation becomes inactive.
2480 *
2481 * \param[in,out] operation Active AEAD operation.
2482 * \param[in] tag Buffer containing the authentication tag.
2483 * \param tag_length Size of the \p tag buffer in bytes.
2484 *
2485 * \retval #PSA_SUCCESS
2486 * Success.
2487 * \retval #PSA_ERROR_BAD_STATE
2488 * The operation state is not valid (not set up, nonce not set,
2489 * encryption, or already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002490 * \retval #PSA_ERROR_INVALID_ARGUMENT
2491 * The total length of input to psa_aead_update_ad() so far is
2492 * less than the additional data length that was previously
2493 * specified with psa_aead_set_lengths().
2494 * \retval #PSA_ERROR_INVALID_ARGUMENT
2495 * The total length of input to psa_aead_update() so far is
2496 * less than the plaintext length that was previously
2497 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002498 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2499 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2500 * \retval #PSA_ERROR_HARDWARE_FAILURE
2501 * \retval #PSA_ERROR_TAMPERING_DETECTED
2502 */
2503psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2504 const uint8_t *tag,
2505 size_t tag_length);
2506
2507/** Abort an AEAD operation.
2508 *
2509 * Aborting an operation frees all associated resources except for the
2510 * \p operation structure itself. Once aborted, the operation object
2511 * can be reused for another operation by calling
2512 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2513 *
2514 * You may call this function any time after the operation object has
2515 * been initialized by any of the following methods:
2516 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2517 * whether it succeeds or not.
2518 * - Initializing the \c struct to all-bits-zero.
2519 * - Initializing the \c struct to logical zeros, e.g.
2520 * `psa_aead_operation_t operation = {0}`.
2521 *
2522 * In particular, calling psa_aead_abort() after the operation has been
2523 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2524 * is safe and has no effect.
2525 *
2526 * \param[in,out] operation Initialized AEAD operation.
2527 *
2528 * \retval #PSA_SUCCESS
2529 * \retval #PSA_ERROR_BAD_STATE
2530 * \p operation is not an active AEAD operation.
2531 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2532 * \retval #PSA_ERROR_HARDWARE_FAILURE
2533 * \retval #PSA_ERROR_TAMPERING_DETECTED
2534 */
2535psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2536
Gilles Peskine3b555712018-03-03 21:27:57 +01002537/**@}*/
2538
Gilles Peskine20035e32018-02-03 22:44:14 +01002539/** \defgroup asymmetric Asymmetric cryptography
2540 * @{
2541 */
2542
2543/**
2544 * \brief Sign a hash or short message with a private key.
2545 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002546 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002547 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002548 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2549 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2550 * to determine the hash algorithm to use.
2551 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002552 * \param handle Handle to the key to use for the operation.
2553 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002554 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002555 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002556 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002557 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002558 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002559 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002560 * \param[out] signature_length On success, the number of bytes
2561 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002562 *
Gilles Peskine28538492018-07-11 17:34:00 +02002563 * \retval #PSA_SUCCESS
2564 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002565 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002566 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002567 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002568 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002569 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002570 * \retval #PSA_ERROR_NOT_SUPPORTED
2571 * \retval #PSA_ERROR_INVALID_ARGUMENT
2572 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2573 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2574 * \retval #PSA_ERROR_HARDWARE_FAILURE
2575 * \retval #PSA_ERROR_TAMPERING_DETECTED
2576 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002577 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002578 * The library has not been previously initialized by psa_crypto_init().
2579 * It is implementation-dependent whether a failure to initialize
2580 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002581 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002582psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002583 psa_algorithm_t alg,
2584 const uint8_t *hash,
2585 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002586 uint8_t *signature,
2587 size_t signature_size,
2588 size_t *signature_length);
2589
2590/**
2591 * \brief Verify the signature a hash or short message using a public key.
2592 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002593 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002594 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002595 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2596 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2597 * to determine the hash algorithm to use.
2598 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002599 * \param handle Handle to the key to use for the operation.
2600 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002601 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002602 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002603 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002604 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002605 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002606 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002607 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002608 *
Gilles Peskine28538492018-07-11 17:34:00 +02002609 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002610 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002611 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002612 * The calculation was perfomed successfully, but the passed
2613 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002614 * \retval #PSA_ERROR_NOT_SUPPORTED
2615 * \retval #PSA_ERROR_INVALID_ARGUMENT
2616 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2617 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2618 * \retval #PSA_ERROR_HARDWARE_FAILURE
2619 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002620 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002621 * The library has not been previously initialized by psa_crypto_init().
2622 * It is implementation-dependent whether a failure to initialize
2623 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002624 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002625psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002626 psa_algorithm_t alg,
2627 const uint8_t *hash,
2628 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002629 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002630 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002631
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002632/**
2633 * \brief Encrypt a short message with a public key.
2634 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002635 * \param handle Handle to the key to use for the operation.
2636 * It must be a public key or an asymmetric
2637 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002638 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002639 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002640 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002641 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002642 * \param[in] salt A salt or label, if supported by the
2643 * encryption algorithm.
2644 * If the algorithm does not support a
2645 * salt, pass \c NULL.
2646 * If the algorithm supports an optional
2647 * salt and you do not want to pass a salt,
2648 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002649 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002650 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2651 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002652 * \param salt_length Size of the \p salt buffer in bytes.
2653 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002654 * \param[out] output Buffer where the encrypted message is to
2655 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002656 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002657 * \param[out] output_length On success, the number of bytes
2658 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002659 *
Gilles Peskine28538492018-07-11 17:34:00 +02002660 * \retval #PSA_SUCCESS
2661 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002662 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002663 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002664 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002665 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002666 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002667 * \retval #PSA_ERROR_NOT_SUPPORTED
2668 * \retval #PSA_ERROR_INVALID_ARGUMENT
2669 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2670 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2671 * \retval #PSA_ERROR_HARDWARE_FAILURE
2672 * \retval #PSA_ERROR_TAMPERING_DETECTED
2673 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002674 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002675 * The library has not been previously initialized by psa_crypto_init().
2676 * It is implementation-dependent whether a failure to initialize
2677 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002678 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002679psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002680 psa_algorithm_t alg,
2681 const uint8_t *input,
2682 size_t input_length,
2683 const uint8_t *salt,
2684 size_t salt_length,
2685 uint8_t *output,
2686 size_t output_size,
2687 size_t *output_length);
2688
2689/**
2690 * \brief Decrypt a short message with a private key.
2691 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002692 * \param handle Handle to the key to use for the operation.
2693 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002694 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002695 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002696 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002697 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002698 * \param[in] salt A salt or label, if supported by the
2699 * encryption algorithm.
2700 * If the algorithm does not support a
2701 * salt, pass \c NULL.
2702 * If the algorithm supports an optional
2703 * salt and you do not want to pass a salt,
2704 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002705 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002706 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2707 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002708 * \param salt_length Size of the \p salt buffer in bytes.
2709 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002710 * \param[out] output Buffer where the decrypted message is to
2711 * be written.
2712 * \param output_size Size of the \c output buffer in bytes.
2713 * \param[out] output_length On success, the number of bytes
2714 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002715 *
Gilles Peskine28538492018-07-11 17:34:00 +02002716 * \retval #PSA_SUCCESS
2717 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002718 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002719 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002720 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002721 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002722 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002723 * \retval #PSA_ERROR_NOT_SUPPORTED
2724 * \retval #PSA_ERROR_INVALID_ARGUMENT
2725 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2726 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2727 * \retval #PSA_ERROR_HARDWARE_FAILURE
2728 * \retval #PSA_ERROR_TAMPERING_DETECTED
2729 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2730 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002731 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002732 * The library has not been previously initialized by psa_crypto_init().
2733 * It is implementation-dependent whether a failure to initialize
2734 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002735 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002736psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002737 psa_algorithm_t alg,
2738 const uint8_t *input,
2739 size_t input_length,
2740 const uint8_t *salt,
2741 size_t salt_length,
2742 uint8_t *output,
2743 size_t output_size,
2744 size_t *output_length);
2745
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002746/**@}*/
2747
Gilles Peskineedd76872018-07-20 17:42:05 +02002748/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002749 * @{
2750 */
2751
2752/** The type of the state data structure for generators.
2753 *
2754 * Before calling any function on a generator, the application must
2755 * initialize it by any of the following means:
2756 * - Set the structure to all-bits-zero, for example:
2757 * \code
2758 * psa_crypto_generator_t generator;
2759 * memset(&generator, 0, sizeof(generator));
2760 * \endcode
2761 * - Initialize the structure to logical zero values, for example:
2762 * \code
2763 * psa_crypto_generator_t generator = {0};
2764 * \endcode
2765 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2766 * for example:
2767 * \code
2768 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2769 * \endcode
2770 * - Assign the result of the function psa_crypto_generator_init()
2771 * to the structure, for example:
2772 * \code
2773 * psa_crypto_generator_t generator;
2774 * generator = psa_crypto_generator_init();
2775 * \endcode
2776 *
2777 * This is an implementation-defined \c struct. Applications should not
2778 * make any assumptions about the content of this structure except
2779 * as directed by the documentation of a specific implementation.
2780 */
2781typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2782
2783/** \def PSA_CRYPTO_GENERATOR_INIT
2784 *
2785 * This macro returns a suitable initializer for a generator object
2786 * of type #psa_crypto_generator_t.
2787 */
2788#ifdef __DOXYGEN_ONLY__
2789/* This is an example definition for documentation purposes.
2790 * Implementations should define a suitable value in `crypto_struct.h`.
2791 */
2792#define PSA_CRYPTO_GENERATOR_INIT {0}
2793#endif
2794
2795/** Return an initial value for a generator object.
2796 */
2797static psa_crypto_generator_t psa_crypto_generator_init(void);
2798
2799/** Retrieve the current capacity of a generator.
2800 *
2801 * The capacity of a generator is the maximum number of bytes that it can
2802 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2803 *
2804 * \param[in] generator The generator to query.
2805 * \param[out] capacity On success, the capacity of the generator.
2806 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002807 * \retval #PSA_SUCCESS
2808 * \retval #PSA_ERROR_BAD_STATE
2809 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002810 */
2811psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2812 size_t *capacity);
2813
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002814/** Set the maximum capacity of a generator.
2815 *
2816 * \param[in,out] generator The generator object to modify.
2817 * \param capacity The new capacity of the generator.
2818 * It must be less or equal to the generator's
2819 * current capacity.
2820 *
2821 * \retval #PSA_SUCCESS
2822 * \retval #PSA_ERROR_INVALID_ARGUMENT
2823 * \p capacity is larger than the generator's current capacity.
2824 * \retval #PSA_ERROR_BAD_STATE
2825 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2826 */
2827psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2828 size_t capacity);
2829
Gilles Peskineeab56e42018-07-12 17:12:33 +02002830/** Read some data from a generator.
2831 *
2832 * This function reads and returns a sequence of bytes from a generator.
2833 * The data that is read is discarded from the generator. The generator's
2834 * capacity is decreased by the number of bytes read.
2835 *
2836 * \param[in,out] generator The generator object to read from.
2837 * \param[out] output Buffer where the generator output will be
2838 * written.
2839 * \param output_length Number of bytes to output.
2840 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002841 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02002842 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02002843 * There were fewer than \p output_length bytes
2844 * in the generator. Note that in this case, no
2845 * output is written to the output buffer.
2846 * The generator's capacity is set to 0, thus
2847 * subsequent calls to this function will not
2848 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002849 * \retval #PSA_ERROR_BAD_STATE
2850 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2851 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2852 * \retval #PSA_ERROR_HARDWARE_FAILURE
2853 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002854 */
2855psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2856 uint8_t *output,
2857 size_t output_length);
2858
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002859/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002860 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002861 * This function uses the output of a generator to derive a key.
2862 * How much output it consumes and how the key is derived depends on the
2863 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002864 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002865 * - For key types for which the key is an arbitrary sequence of bytes
2866 * of a given size,
2867 * this function is functionally equivalent to calling #psa_generator_read
2868 * and passing the resulting output to #psa_import_key.
2869 * However, this function has a security benefit:
2870 * if the implementation provides an isolation boundary then
2871 * the key material is not exposed outside the isolation boundary.
2872 * As a consequence, for these key types, this function always consumes
2873 * exactly (\p bits / 8) bytes from the generator.
2874 * The following key types defined in this specification follow this scheme:
2875 *
2876 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002877 * - #PSA_KEY_TYPE_ARC4;
2878 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002879 * - #PSA_KEY_TYPE_DERIVE;
2880 * - #PSA_KEY_TYPE_HMAC.
2881 *
2882 * - For ECC keys on a Montgomery elliptic curve
2883 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
2884 * Montgomery curve), this function always draws a byte string whose
2885 * length is determined by the curve, and sets the mandatory bits
2886 * accordingly. That is:
2887 *
2888 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
2889 * and process it as specified in RFC 7748 &sect;5.
2890 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
2891 * and process it as specified in RFC 7748 &sect;5.
2892 *
2893 * - For key types for which the key is represented by a single sequence of
2894 * \p bits bits with constraints as to which bit sequences are acceptable,
2895 * this function draws a byte string of length (\p bits / 8) bytes rounded
2896 * up to the nearest whole number of bytes. If the resulting byte string
2897 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
2898 * This process is repeated until an acceptable byte string is drawn.
2899 * The byte string drawn from the generator is interpreted as specified
2900 * for the output produced by psa_export_key().
2901 * The following key types defined in this specification follow this scheme:
2902 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01002903 * - #PSA_KEY_TYPE_DES.
2904 * Force-set the parity bits, but discard forbidden weak keys.
2905 * For 2-key and 3-key triple-DES, the three keys are generated
2906 * successively (for example, for 3-key triple-DES,
2907 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
2908 * discard the first 8 bytes, use the next 8 bytes as the first key,
2909 * and continue reading output from the generator to derive the other
2910 * two keys).
2911 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
2912 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
2913 * ECC keys on a Weierstrass elliptic curve
2914 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
2915 * Weierstrass curve).
2916 * For these key types, interpret the byte string as integer
2917 * in big-endian order. Discard it if it is not in the range
2918 * [0, *N* - 2] where *N* is the boundary of the private key domain
2919 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01002920 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01002921 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01002922 * This method allows compliance to NIST standards, specifically
2923 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01002924 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
2925 * in FIPS 186-4 &sect;B.1.2 for DSA, and
2926 * in NIST SP 800-56A &sect;5.6.1.2.2 or
2927 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002928 *
2929 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
2930 * the way in which the generator output is consumed is
2931 * implementation-defined.
2932 *
2933 * In all cases, the data that is read is discarded from the generator.
2934 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002935 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002936 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002937 * It must have been obtained by calling
2938 * psa_allocate_key() or psa_create_key() and must
2939 * not contain key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002940 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskineee67dd62019-03-12 13:23:17 +01002941 * This must be a secret key type or a key pair type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002942 * \param bits Key size in bits.
2943 * \param[in,out] generator The generator object to read from.
2944 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002945 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002946 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002947 * If the key is persistent, the key material and the key's metadata
2948 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02002949 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002950 * There was not enough data to create the desired key.
2951 * Note that in this case, no output is written to the output buffer.
2952 * The generator's capacity is set to 0, thus subsequent calls to
2953 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002954 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002955 * The key type or key size is not supported, either by the
2956 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002957 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002958 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002959 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002960 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002961 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2962 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2963 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2964 * \retval #PSA_ERROR_HARDWARE_FAILURE
2965 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002966 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002967 * The library has not been previously initialized by psa_crypto_init().
2968 * It is implementation-dependent whether a failure to initialize
2969 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002970 */
Gilles Peskine87a5e562019-04-17 12:28:25 +02002971psa_status_t psa_generator_import_key(const psa_key_attributes_t *attributes,
2972 psa_key_handle_t *handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002973 psa_key_type_t type,
2974 size_t bits,
2975 psa_crypto_generator_t *generator);
2976
2977/** Abort a generator.
2978 *
2979 * Once a generator has been aborted, its capacity is zero.
2980 * Aborting a generator frees all associated resources except for the
2981 * \c generator structure itself.
2982 *
2983 * This function may be called at any time as long as the generator
2984 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2985 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2986 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2987 * on a generator that has not been set up.
2988 *
2989 * Once aborted, the generator object may be called.
2990 *
2991 * \param[in,out] generator The generator to abort.
2992 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002993 * \retval #PSA_SUCCESS
2994 * \retval #PSA_ERROR_BAD_STATE
2995 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2996 * \retval #PSA_ERROR_HARDWARE_FAILURE
2997 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002998 */
2999psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3000
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003001/** Use the maximum possible capacity for a generator.
3002 *
3003 * Use this value as the capacity argument when setting up a generator
3004 * to indicate that the generator should have the maximum possible capacity.
3005 * The value of the maximum possible capacity depends on the generator
3006 * algorithm.
3007 */
3008#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3009
Gilles Peskineeab56e42018-07-12 17:12:33 +02003010/**@}*/
3011
Gilles Peskineea0fb492018-07-12 17:17:20 +02003012/** \defgroup derivation Key derivation
3013 * @{
3014 */
3015
3016/** Set up a key derivation operation.
3017 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003018 * A key derivation algorithm takes some inputs and uses them to create
3019 * a byte generator which can be used to produce keys and other
3020 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003021 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003022 * To use a generator for key derivation:
3023 * - Start with an initialized object of type #psa_crypto_generator_t.
3024 * - Call psa_key_derivation_setup() to select the algorithm.
3025 * - Provide the inputs for the key derivation by calling
3026 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3027 * as appropriate. Which inputs are needed, in what order, and whether
3028 * they may be keys and if so of what type depends on the algorithm.
3029 * - Optionally set the generator's maximum capacity with
3030 * psa_set_generator_capacity(). You may do this before, in the middle of
3031 * or after providing inputs. For some algorithms, this step is mandatory
3032 * because the output depends on the maximum capacity.
3033 * - Generate output with psa_generator_read() or
3034 * psa_generator_import_key(). Successive calls to these functions
3035 * use successive output bytes from the generator.
3036 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003037 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003038 * \param[in,out] generator The generator object to set up. It must
3039 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003040 * \param alg The key derivation algorithm to compute
3041 * (\c PSA_ALG_XXX value such that
3042 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003043 *
3044 * \retval #PSA_SUCCESS
3045 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003046 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003047 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003048 * \retval #PSA_ERROR_NOT_SUPPORTED
3049 * \c alg is not supported or is not a key derivation algorithm.
3050 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3051 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3052 * \retval #PSA_ERROR_HARDWARE_FAILURE
3053 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003054 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003055 */
3056psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3057 psa_algorithm_t alg);
3058
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003059/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003060 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003061 * Which inputs are required and in what order depends on the algorithm.
3062 * Refer to the documentation of each key derivation or key agreement
3063 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003064 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003065 * This function passes direct inputs. Some inputs must be passed as keys
3066 * using psa_key_derivation_input_key() instead of this function. Refer to
3067 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003068 *
3069 * \param[in,out] generator The generator object to use. It must
3070 * have been set up with
3071 * psa_key_derivation_setup() and must not
3072 * have produced any output yet.
3073 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003074 * \param[in] data Input data to use.
3075 * \param data_length Size of the \p data buffer in bytes.
3076 *
3077 * \retval #PSA_SUCCESS
3078 * Success.
3079 * \retval #PSA_ERROR_INVALID_ARGUMENT
3080 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003081 * \retval #PSA_ERROR_INVALID_ARGUMENT
3082 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003083 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3084 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3085 * \retval #PSA_ERROR_HARDWARE_FAILURE
3086 * \retval #PSA_ERROR_TAMPERING_DETECTED
3087 * \retval #PSA_ERROR_BAD_STATE
3088 * The value of \p step is not valid given the state of \p generator.
3089 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003090 * The library has not been previously initialized by psa_crypto_init().
3091 * It is implementation-dependent whether a failure to initialize
3092 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003093 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003094psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3095 psa_key_derivation_step_t step,
3096 const uint8_t *data,
3097 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003098
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003099/** Provide an input for key derivation in the form of a key.
3100 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003101 * Which inputs are required and in what order depends on the algorithm.
3102 * Refer to the documentation of each key derivation or key agreement
3103 * algorithm for information.
3104 *
3105 * This function passes key inputs. Some inputs must be passed as keys
3106 * of the appropriate type using this function, while others must be
3107 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3108 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003109 *
3110 * \param[in,out] generator The generator object to use. It must
3111 * have been set up with
3112 * psa_key_derivation_setup() and must not
3113 * have produced any output yet.
3114 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003115 * \param handle Handle to the key. It must have an
3116 * appropriate type for \p step and must
3117 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003118 *
3119 * \retval #PSA_SUCCESS
3120 * Success.
3121 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003122 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003123 * \retval #PSA_ERROR_NOT_PERMITTED
3124 * \retval #PSA_ERROR_INVALID_ARGUMENT
3125 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003126 * \retval #PSA_ERROR_INVALID_ARGUMENT
3127 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003128 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3129 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3130 * \retval #PSA_ERROR_HARDWARE_FAILURE
3131 * \retval #PSA_ERROR_TAMPERING_DETECTED
3132 * \retval #PSA_ERROR_BAD_STATE
3133 * The value of \p step is not valid given the state of \p generator.
3134 * \retval #PSA_ERROR_BAD_STATE
3135 * The library has not been previously initialized by psa_crypto_init().
3136 * It is implementation-dependent whether a failure to initialize
3137 * results in this error code.
3138 */
3139psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3140 psa_key_derivation_step_t step,
3141 psa_key_handle_t handle);
3142
Gilles Peskine969c5d62019-01-16 15:53:06 +01003143/** Perform a key agreement and use the shared secret as input to a key
3144 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003145 *
3146 * A key agreement algorithm takes two inputs: a private key \p private_key
3147 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003148 * The result of this function is passed as input to a key derivation.
3149 * The output of this key derivation can be extracted by reading from the
3150 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003151 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003152 * \param[in,out] generator The generator object to use. It must
3153 * have been set up with
3154 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003155 * key agreement and derivation algorithm
3156 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003157 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3158 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003159 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003160 * The generator must be ready for an
3161 * input of the type given by \p step.
3162 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003163 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003164 * \param[in] peer_key Public key of the peer. The peer key must be in the
3165 * same format that psa_import_key() accepts for the
3166 * public key type corresponding to the type of
3167 * private_key. That is, this function performs the
3168 * equivalent of
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003169 * #psa_import_key(`internal_public_key_handle`,
3170 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(`private_key_type`),
3171 * `peer_key`, `peer_key_length`) where
Jaeden Amero8afbff82019-01-14 16:56:20 +00003172 * `private_key_type` is the type of `private_key`.
3173 * For example, for EC keys, this means that peer_key
3174 * is interpreted as a point on the curve that the
3175 * private key is on. The standard formats for public
3176 * keys are documented in the documentation of
3177 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003178 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003179 *
3180 * \retval #PSA_SUCCESS
3181 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003182 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003183 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003184 * \retval #PSA_ERROR_NOT_PERMITTED
3185 * \retval #PSA_ERROR_INVALID_ARGUMENT
3186 * \c private_key is not compatible with \c alg,
3187 * or \p peer_key is not valid for \c alg or not compatible with
3188 * \c private_key.
3189 * \retval #PSA_ERROR_NOT_SUPPORTED
3190 * \c alg is not supported or is not a key derivation algorithm.
3191 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3192 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3193 * \retval #PSA_ERROR_HARDWARE_FAILURE
3194 * \retval #PSA_ERROR_TAMPERING_DETECTED
3195 */
3196psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003197 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003198 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003199 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003200 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003201
Gilles Peskine769c7a62019-01-18 16:42:29 +01003202/** Perform a key agreement and use the shared secret as input to a key
3203 * derivation.
3204 *
3205 * A key agreement algorithm takes two inputs: a private key \p private_key
3206 * a public key \p peer_key.
3207 *
3208 * \warning The raw result of a key agreement algorithm such as finite-field
3209 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3210 * not be used directly as key material. It should instead be passed as
3211 * input to a key derivation algorithm. To chain a key agreement with
3212 * a key derivation, use psa_key_agreement() and other functions from
3213 * the key derivation and generator interface.
3214 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003215 * \param alg The key agreement algorithm to compute
3216 * (\c PSA_ALG_XXX value such that
3217 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3218 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003219 * \param private_key Handle to the private key to use.
3220 * \param[in] peer_key Public key of the peer. It must be
3221 * in the same format that psa_import_key()
3222 * accepts. The standard formats for public
3223 * keys are documented in the documentation
3224 * of psa_export_public_key().
3225 * \param peer_key_length Size of \p peer_key in bytes.
3226 * \param[out] output Buffer where the decrypted message is to
3227 * be written.
3228 * \param output_size Size of the \c output buffer in bytes.
3229 * \param[out] output_length On success, the number of bytes
3230 * that make up the returned output.
3231 *
3232 * \retval #PSA_SUCCESS
3233 * Success.
3234 * \retval #PSA_ERROR_INVALID_HANDLE
3235 * \retval #PSA_ERROR_EMPTY_SLOT
3236 * \retval #PSA_ERROR_NOT_PERMITTED
3237 * \retval #PSA_ERROR_INVALID_ARGUMENT
3238 * \p alg is not a key agreement algorithm
3239 * \retval #PSA_ERROR_INVALID_ARGUMENT
3240 * \p private_key is not compatible with \p alg,
3241 * or \p peer_key is not valid for \p alg or not compatible with
3242 * \p private_key.
3243 * \retval #PSA_ERROR_NOT_SUPPORTED
3244 * \p alg is not a supported key agreement algorithm.
3245 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3246 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3247 * \retval #PSA_ERROR_HARDWARE_FAILURE
3248 * \retval #PSA_ERROR_TAMPERING_DETECTED
3249 */
3250psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3251 psa_key_handle_t private_key,
3252 const uint8_t *peer_key,
3253 size_t peer_key_length,
3254 uint8_t *output,
3255 size_t output_size,
3256 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003257
3258/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003259
3260/** \defgroup random Random generation
3261 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003262 */
3263
3264/**
3265 * \brief Generate random bytes.
3266 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003267 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003268 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003269 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003270 *
3271 * \note To generate a key, use psa_generate_key() instead.
3272 *
3273 * \param[out] output Output buffer for the generated data.
3274 * \param output_size Number of bytes to generate and output.
3275 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003276 * \retval #PSA_SUCCESS
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003277 * \retval #PSA_ERROR_NOT_SUPPORTED
3278 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003279 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003280 * \retval #PSA_ERROR_HARDWARE_FAILURE
3281 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003282 * \retval #PSA_ERROR_BAD_STATE
3283 * The library has not been previously initialized by psa_crypto_init().
3284 * It is implementation-dependent whether a failure to initialize
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003285 * results in this error code.
3286 */
3287psa_status_t psa_generate_random(uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003288 size_t output_size);
3289
3290/** Extra parameters for RSA key generation.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003291 *
Gilles Peskine28538492018-07-11 17:34:00 +02003292 * You may pass a pointer to a structure of this type as the \c extra
3293 * parameter to psa_generate_key().
3294 */
3295typedef struct {
3296 uint32_t e; /**< Public exponent value. Default: 65537. */
3297} psa_generate_key_extra_rsa;
3298
3299/**
itayzafrir90d8c7a2018-09-12 11:44:52 +03003300 * \brief Generate a key or key pair.
itayzafrir18617092018-09-16 12:22:41 +03003301 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003302 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003303 * It must have been obtained by calling
3304 * psa_allocate_key() or psa_create_key() and must
3305 * not contain key material yet.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003306 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
3307 * \param bits Key size in bits.
3308 * \param[in] extra Extra parameters for key generation. The
3309 * interpretation of this parameter depends on
3310 * \p type. All types support \c NULL to use
3311 * default parameters. Implementation that support
3312 * the generation of vendor-specific key types
3313 * that allow extra parameters shall document
3314 * the format of these extra parameters and
3315 * the default values. For standard parameters,
3316 * the meaning of \p extra is as follows:
3317 * - For a symmetric key type (a type such
3318 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
Gilles Peskine53d991e2018-07-12 01:14:59 +02003319 * false), \p extra must be \c NULL.
3320 * - For an elliptic curve key type (a type
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003321 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
3322 * false), \p extra must be \c NULL.
3323 * - For an RSA key (\p type is
Gilles Peskinee59236f2018-01-27 23:32:46 +01003324 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3325 * optional #psa_generate_key_extra_rsa structure
3326 * specifying the public exponent. The
3327 * default public exponent used when \p extra
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003328 * is \c NULL is 65537.
Jaeden Amero1308fb52019-01-11 13:50:43 +00003329 * - For an DSA key (\p type is
3330 * #PSA_KEY_TYPE_DSA_KEYPAIR), \p extra is an
3331 * optional structure specifying the key domain
3332 * parameters. The key domain parameters can also be
3333 * provided by psa_set_key_domain_parameters(),
3334 * which documents the format of the structure.
Jaeden Amero8851c402019-01-11 14:20:03 +00003335 * - For a DH key (\p type is
3336 * #PSA_KEY_TYPE_DH_KEYPAIR), the \p extra is an
3337 * optional structure specifying the key domain
3338 * parameters. The key domain parameters can also be
3339 * provided by psa_set_key_domain_parameters(),
3340 * which documents the format of the structure.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003341 * \param extra_size Size of the buffer that \p extra
3342 * points to, in bytes. Note that if \p extra is
3343 * \c NULL then \p extra_size must be zero.
3344 *
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003345 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003346 * Success.
3347 * If the key is persistent, the key material and the key's metadata
3348 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003349 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003350 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskineae32aac2018-11-30 14:39:32 +01003351 * There is already a key in the specified slot.
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003352 * \retval #PSA_ERROR_NOT_SUPPORTED
3353 * \retval #PSA_ERROR_INVALID_ARGUMENT
3354 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003355 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3356 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3357 * \retval #PSA_ERROR_HARDWARE_FAILURE
3358 * \retval #PSA_ERROR_TAMPERING_DETECTED
3359 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003360 * The library has not been previously initialized by psa_crypto_init().
3361 * It is implementation-dependent whether a failure to initialize
3362 * results in this error code.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003363 */
Gilles Peskine87a5e562019-04-17 12:28:25 +02003364psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
3365 psa_key_handle_t *handle,
Gilles Peskinee59236f2018-01-27 23:32:46 +01003366 psa_key_type_t type,
3367 size_t bits,
3368 const void *extra,
3369 size_t extra_size);
3370
3371/**@}*/
3372
3373#ifdef __cplusplus
3374}
3375#endif
3376
3377/* The file "crypto_sizes.h" contains definitions for size calculation
3378 * macros whose definitions are implementation-specific. */
3379#include "crypto_sizes.h"
3380
3381/* The file "crypto_struct.h" contains definitions for
3382 * implementation-specific structs that are declared above. */
3383#include "crypto_struct.h"
3384
3385/* The file "crypto_extra.h" contains vendor-specific definitions. This
3386 * can include vendor-defined algorithms, extra functions, etc. */
3387#include "crypto_extra.h"
3388
3389#endif /* PSA_CRYPTO_H */