blob: 74a36b0b343013b9cf0aed45fcdbe65da6b20fc1 [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
Gilles Peskine4747d192019-04-17 15:05:45 +0200112static void psa_make_key_persistent(psa_key_attributes_t *attributes,
113 psa_key_id_t id,
114 psa_key_lifetime_t lifetime);
115
116static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
117
118static psa_key_lifetime_t psa_get_key_lifetime(
119 const psa_key_attributes_t *attributes);
120
121static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
122 psa_key_usage_t usage_flags);
123
124static psa_key_usage_t psa_get_key_usage_flags(
125 const psa_key_attributes_t *attributes);
126
127static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
128 psa_algorithm_t alg);
129
130static psa_algorithm_t psa_get_key_algorithm(
131 const psa_key_attributes_t *attributes);
132
133static void psa_set_key_type(psa_key_attributes_t *attributes,
134 psa_key_type_t type);
135
136static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
137
138static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
139
140psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
141 psa_key_attributes_t *attributes);
142
143psa_status_t psa_reset_key_attributes(psa_key_attributes_t *attributes);
144
Gilles Peskine87a5e562019-04-17 12:28:25 +0200145/**@}*/
146
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100147/** \defgroup policy Key policies
148 * @{
149 */
150
151/** The type of the key policy data structure.
152 *
153 * Before calling any function on a key policy, the application must initialize
154 * it by any of the following means:
155 * - Set the structure to all-bits-zero, for example:
156 * \code
157 * psa_key_policy_t policy;
158 * memset(&policy, 0, sizeof(policy));
159 * \endcode
160 * - Initialize the structure to logical zero values, for example:
161 * \code
162 * psa_key_policy_t policy = {0};
163 * \endcode
164 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
165 * for example:
166 * \code
167 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
168 * \endcode
169 * - Assign the result of the function psa_key_policy_init()
170 * to the structure, for example:
171 * \code
172 * psa_key_policy_t policy;
173 * policy = psa_key_policy_init();
174 * \endcode
175 *
176 * This is an implementation-defined \c struct. Applications should not
177 * make any assumptions about the content of this structure except
178 * as directed by the documentation of a specific implementation. */
179typedef struct psa_key_policy_s psa_key_policy_t;
180
181/** \def PSA_KEY_POLICY_INIT
182 *
183 * This macro returns a suitable initializer for a key policy object of type
184 * #psa_key_policy_t.
185 */
186#ifdef __DOXYGEN_ONLY__
187/* This is an example definition for documentation purposes.
188 * Implementations should define a suitable value in `crypto_struct.h`.
189 */
190#define PSA_KEY_POLICY_INIT {0}
191#endif
192
193/** Return an initial value for a key policy that forbids all usage of the key.
194 */
195static psa_key_policy_t psa_key_policy_init(void);
196
197/** \brief Set the standard fields of a policy structure.
198 *
199 * Note that this function does not make any consistency check of the
200 * parameters. The values are only checked when applying the policy to
201 * a key slot with psa_set_key_policy().
202 *
203 * \param[in,out] policy The key policy to modify. It must have been
204 * initialized as per the documentation for
205 * #psa_key_policy_t.
206 * \param usage The permitted uses for the key.
207 * \param alg The algorithm that the key may be used for.
208 */
209void psa_key_policy_set_usage(psa_key_policy_t *policy,
210 psa_key_usage_t usage,
211 psa_algorithm_t alg);
212
213/** \brief Retrieve the usage field of a policy structure.
214 *
215 * \param[in] policy The policy object to query.
216 *
217 * \return The permitted uses for a key with this policy.
218 */
219psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
220
221/** \brief Retrieve the algorithm field of a policy structure.
222 *
223 * \param[in] policy The policy object to query.
224 *
225 * \return The permitted algorithm for a key with this policy.
226 */
227psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
228
229/** \brief Set the usage policy on a key slot.
230 *
231 * This function must be called on an empty key slot, before importing,
232 * generating or creating a key in the slot. Changing the policy of an
233 * existing key is not permitted.
234 *
235 * Implementations may set restrictions on supported key policies
236 * depending on the key type and the key slot.
237 *
238 * \param handle Handle to the key whose policy is to be changed.
239 * \param[in] policy The policy object to query.
240 *
241 * \retval #PSA_SUCCESS
242 * Success.
243 * If the key is persistent, it is implementation-defined whether
244 * the policy has been saved to persistent storage. Implementations
245 * may defer saving the policy until the key material is created.
246 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200247 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100248 * \retval #PSA_ERROR_NOT_SUPPORTED
249 * \retval #PSA_ERROR_INVALID_ARGUMENT
250 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
251 * \retval #PSA_ERROR_HARDWARE_FAILURE
252 * \retval #PSA_ERROR_TAMPERING_DETECTED
253 * \retval #PSA_ERROR_BAD_STATE
254 * The library has not been previously initialized by psa_crypto_init().
255 * It is implementation-dependent whether a failure to initialize
256 * results in this error code.
257 */
258psa_status_t psa_set_key_policy(psa_key_handle_t handle,
259 const psa_key_policy_t *policy);
260
261/** \brief Get the usage policy for a key slot.
262 *
263 * \param handle Handle to the key slot whose policy is being queried.
264 * \param[out] policy On success, the key's policy.
265 *
266 * \retval #PSA_SUCCESS
267 * \retval #PSA_ERROR_INVALID_HANDLE
268 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
269 * \retval #PSA_ERROR_HARDWARE_FAILURE
270 * \retval #PSA_ERROR_TAMPERING_DETECTED
271 * \retval #PSA_ERROR_BAD_STATE
272 * The library has not been previously initialized by psa_crypto_init().
273 * It is implementation-dependent whether a failure to initialize
274 * results in this error code.
275 */
276psa_status_t psa_get_key_policy(psa_key_handle_t handle,
277 psa_key_policy_t *policy);
278
279/**@}*/
280
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100281/** \defgroup key_management Key management
282 * @{
283 */
284
Gilles Peskinef535eb22018-11-30 14:08:36 +0100285/** Allocate a key slot for a transient key, i.e. a key which is only stored
286 * in volatile memory.
287 *
288 * The allocated key slot and its handle remain valid until the
289 * application calls psa_close_key() or psa_destroy_key() or until the
290 * application terminates.
291 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100292 * \param[out] handle On success, a handle to a volatile key slot.
293 *
294 * \retval #PSA_SUCCESS
295 * Success. The application can now use the value of `*handle`
296 * to access the newly allocated key slot.
297 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
298 * There was not enough memory, or the maximum number of key slots
299 * has been reached.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100300 */
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100301psa_status_t psa_allocate_key(psa_key_handle_t *handle);
Gilles Peskinef535eb22018-11-30 14:08:36 +0100302
303/** Open a handle to an existing persistent key.
304 *
305 * Open a handle to a key which was previously created with psa_create_key().
306 *
307 * \param lifetime The lifetime of the key. This designates a storage
308 * area where the key material is stored. This must not
309 * be #PSA_KEY_LIFETIME_VOLATILE.
310 * \param id The persistent identifier of the key.
311 * \param[out] handle On success, a handle to a key slot which contains
312 * the data and metadata loaded from the specified
313 * persistent location.
314 *
315 * \retval #PSA_SUCCESS
316 * Success. The application can now use the value of `*handle`
317 * to access the newly allocated key slot.
318 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200319 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100320 * \retval #PSA_ERROR_INVALID_ARGUMENT
321 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
322 * \retval #PSA_ERROR_INVALID_ARGUMENT
323 * \p id is invalid for the specified lifetime.
324 * \retval #PSA_ERROR_NOT_SUPPORTED
325 * \p lifetime is not supported.
326 * \retval #PSA_ERROR_NOT_PERMITTED
327 * The specified key exists, but the application does not have the
328 * permission to access it. Note that this specification does not
329 * define any way to create such a key, but it may be possible
330 * through implementation-specific means.
331 */
332psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
333 psa_key_id_t id,
334 psa_key_handle_t *handle);
335
Gilles Peskinef535eb22018-11-30 14:08:36 +0100336/** Close a key handle.
337 *
338 * If the handle designates a volatile key, destroy the key material and
339 * free all associated resources, just like psa_destroy_key().
340 *
341 * If the handle designates a persistent key, free all resources associated
342 * with the key in volatile memory. The key slot in persistent storage is
343 * not affected and can be opened again later with psa_open_key().
344 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100345 * If the key is currently in use in a multipart operation,
346 * the multipart operation is aborted.
347 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100348 * \param handle The key handle to close.
349 *
350 * \retval #PSA_SUCCESS
351 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100352 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100353 */
354psa_status_t psa_close_key(psa_key_handle_t handle);
355
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100356/**@}*/
357
358/** \defgroup import_export Key import and export
359 * @{
360 */
361
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362/**
363 * \brief Import a key in binary format.
364 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100365 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100366 * documentation of psa_export_public_key() for the format of public keys
367 * and to the documentation of psa_export_key() for the format for
368 * other key types.
369 *
370 * This specification supports a single format for each key type.
371 * Implementations may support other formats as long as the standard
372 * format is supported. Implementations that support other formats
373 * should ensure that the formats are clearly unambiguous so as to
374 * minimize the risk that an invalid input is accidentally interpreted
375 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100376 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100377 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100378 * It must have been obtained by calling
379 * psa_allocate_key() or psa_create_key() and must
380 * not contain key material yet.
Gilles Peskinef7933932018-10-31 14:07:52 +0100381 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
382 * import, the key slot will contain a key of this type.
383 * \param[in] data Buffer containing the key data. The content of this
384 * buffer is interpreted according to \p type. It must
385 * contain the format described in the documentation
386 * of psa_export_key() or psa_export_public_key() for
387 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200388 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100389 *
Gilles Peskine28538492018-07-11 17:34:00 +0200390 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100391 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100392 * If the key is persistent, the key material and the key's metadata
393 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100394 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200395 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200396 * The key type or key size is not supported, either by the
397 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200398 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100399 * The key slot is invalid,
400 * or the key data is not correctly formatted.
David Saadab4ecc272019-02-14 13:48:10 +0200401 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200402 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200403 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
404 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
405 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100406 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200407 * \retval #PSA_ERROR_HARDWARE_FAILURE
408 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300409 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300410 * The library has not been previously initialized by psa_crypto_init().
411 * It is implementation-dependent whether a failure to initialize
412 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100413 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200414psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
415 psa_key_handle_t *handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100416 const uint8_t *data,
417 size_t data_length);
418
419/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100420 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200421 *
422 * This function destroys the content of the key slot from both volatile
423 * memory and, if applicable, non-volatile storage. Implementations shall
424 * make a best effort to ensure that any previous content of the slot is
425 * unrecoverable.
426 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100427 * This function also erases any metadata such as policies and frees all
428 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200429 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100430 * If the key is currently in use in a multipart operation,
431 * the multipart operation is aborted.
432 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100433 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100434 *
Gilles Peskine28538492018-07-11 17:34:00 +0200435 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200436 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200437 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200438 * The slot holds content and cannot be erased because it is
439 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100440 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200441 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200442 * There was an failure in communication with the cryptoprocessor.
443 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200444 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200445 * The storage is corrupted. Implementations shall make a best effort
446 * to erase key material even in this stage, however applications
447 * should be aware that it may be impossible to guarantee that the
448 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200449 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200450 * An unexpected condition which is not a storage corruption or
451 * a communication failure occurred. The cryptoprocessor may have
452 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300453 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300454 * The library has not been previously initialized by psa_crypto_init().
455 * It is implementation-dependent whether a failure to initialize
456 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100457 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100458psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100459
460/**
461 * \brief Get basic metadata about a key.
462 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100463 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200464 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100465 * This may be a null pointer, in which case the key type
466 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200467 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100468 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100469 * is not written.
470 *
Gilles Peskine28538492018-07-11 17:34:00 +0200471 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100472 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200473 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineae32aac2018-11-30 14:39:32 +0100474 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200475 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
476 * \retval #PSA_ERROR_HARDWARE_FAILURE
477 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300478 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300479 * The library has not been previously initialized by psa_crypto_init().
480 * It is implementation-dependent whether a failure to initialize
481 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100482 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100483psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100484 psa_key_type_t *type,
485 size_t *bits);
486
487/**
Jaeden Amero283dfd12019-01-11 12:06:22 +0000488 * \brief Set domain parameters for a key.
489 *
490 * Some key types require additional domain parameters to be set before import
491 * or generation of the key. The domain parameters can be set with this
492 * function or, for key generation, through the \c extra parameter of
493 * psa_generate_key().
494 *
495 * The format for the required domain parameters varies by the key type.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000496 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
497 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
498 * ```
499 * Dss-Parms ::= SEQUENCE {
500 * p INTEGER,
501 * q INTEGER,
502 * g INTEGER
503 * }
504 * ```
Jaeden Amero8851c402019-01-11 14:20:03 +0000505 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY), the
506 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
507 * ```
508 * DomainParameters ::= SEQUENCE {
509 * p INTEGER, -- odd prime, p=jq +1
510 * g INTEGER, -- generator, g
511 * q INTEGER, -- factor of p-1
512 * j INTEGER OPTIONAL, -- subgroup factor
513 * validationParms ValidationParms OPTIONAL
514 * }
515 * ValidationParms ::= SEQUENCE {
516 * seed BIT STRING,
517 * pgenCounter INTEGER
518 * }
519 * ```
Jaeden Amero283dfd12019-01-11 12:06:22 +0000520 *
Gilles Peskine3a74e002019-01-18 17:11:25 +0100521 * \param handle Handle to the slot where the key will be stored.
522 * This must be a valid slot for a key of the chosen
523 * type: it must have been obtained by calling
524 * psa_allocate_key() or psa_create_key() with the
525 * correct \p type and with a maximum size that is
526 * compatible with \p data. It must not contain
527 * key material yet.
528 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). When
529 * subsequently creating key material into \p handle,
530 * the type must be compatible.
Jaeden Amero283dfd12019-01-11 12:06:22 +0000531 * \param[in] data Buffer containing the key domain parameters. The content
532 * of this buffer is interpreted according to \p type. of
533 * psa_export_key() or psa_export_public_key() for the
534 * chosen type.
535 * \param data_length Size of the \p data buffer in bytes.
536 *
537 * \retval #PSA_SUCCESS
538 * \retval #PSA_ERROR_INVALID_HANDLE
539 * \retval #PSA_ERROR_OCCUPIED_SLOT
540 * There is already a key in the specified slot.
541 * \retval #PSA_ERROR_INVALID_ARGUMENT
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_set_key_domain_parameters(psa_key_handle_t handle,
Gilles Peskine3a74e002019-01-18 17:11:25 +0100551 psa_key_type_t type,
Jaeden Amero283dfd12019-01-11 12:06:22 +0000552 const uint8_t *data,
553 size_t data_length);
554
555/**
556 * \brief Get domain parameters for a key.
557 *
558 * Get the domain parameters for a key with this function, if any. The format
559 * of the domain parameters written to \p data is specified in the
560 * documentation for psa_set_key_domain_parameters().
561 *
562 * \param handle Handle to the key to get domain parameters from.
563 * \param[out] data On success, the key domain parameters.
564 * \param data_size Size of the \p data buffer in bytes.
565 * \param[out] data_length On success, the number of bytes
566 * that make up the key domain parameters data.
567 *
568 * \retval #PSA_SUCCESS
569 * \retval #PSA_ERROR_INVALID_HANDLE
570 * \retval #PSA_ERROR_EMPTY_SLOT
571 * There is no key in the specified slot.
572 * \retval #PSA_ERROR_INVALID_ARGUMENT
573 * \retval #PSA_ERROR_NOT_SUPPORTED
574 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
575 * \retval #PSA_ERROR_HARDWARE_FAILURE
576 * \retval #PSA_ERROR_TAMPERING_DETECTED
577 * \retval #PSA_ERROR_BAD_STATE
578 * The library has not been previously initialized by psa_crypto_init().
579 * It is implementation-dependent whether a failure to initialize
580 * results in this error code.
581 */
582psa_status_t psa_get_key_domain_parameters(psa_key_handle_t handle,
583 uint8_t *data,
584 size_t data_size,
585 size_t *data_length);
586
587/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100588 * \brief Export a key in binary format.
589 *
590 * The output of this function can be passed to psa_import_key() to
591 * create an equivalent object.
592 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100593 * If the implementation of psa_import_key() supports other formats
594 * beyond the format specified here, the output from psa_export_key()
595 * must use the representation specified here, not the original
596 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100597 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100598 * For standard key types, the output format is as follows:
599 *
600 * - For symmetric keys (including MAC keys), the format is the
601 * raw bytes of the key.
602 * - For DES, the key data consists of 8 bytes. The parity bits must be
603 * correct.
604 * - For Triple-DES, the format is the concatenation of the
605 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100606 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200607 * is the non-encrypted DER encoding of the representation defined by
608 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
609 * ```
610 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200611 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200612 * modulus INTEGER, -- n
613 * publicExponent INTEGER, -- e
614 * privateExponent INTEGER, -- d
615 * prime1 INTEGER, -- p
616 * prime2 INTEGER, -- q
617 * exponent1 INTEGER, -- d mod (p-1)
618 * exponent2 INTEGER, -- d mod (q-1)
619 * coefficient INTEGER, -- (inverse of q) mod p
620 * }
621 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000622 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
623 * representation of the private key `x` as a big-endian byte string. The
624 * length of the byte string is the private key size in bytes (leading zeroes
625 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200626 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100627 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100628 * a representation of the private value as a `ceiling(m/8)`-byte string
629 * where `m` is the bit size associated with the curve, i.e. the bit size
630 * of the order of the curve's coordinate field. This byte string is
631 * in little-endian order for Montgomery curves (curve types
632 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
633 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
634 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100635 * This is the content of the `privateKey` field of the `ECPrivateKey`
636 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000637 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
638 * format is the representation of the private key `x` as a big-endian byte
639 * string. The length of the byte string is the private key size in bytes
640 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200641 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
642 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100643 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100644 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200645 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200646 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200647 * \param[out] data_length On success, the number of bytes
648 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100649 *
Gilles Peskine28538492018-07-11 17:34:00 +0200650 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100651 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200652 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200653 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100654 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200655 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
656 * The size of the \p data buffer is too small. You can determine a
657 * sufficient buffer size by calling
658 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
659 * where \c type is the key type
660 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200661 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
662 * \retval #PSA_ERROR_HARDWARE_FAILURE
663 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300664 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300665 * The library has not been previously initialized by psa_crypto_init().
666 * It is implementation-dependent whether a failure to initialize
667 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100668 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100669psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100670 uint8_t *data,
671 size_t data_size,
672 size_t *data_length);
673
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100674/**
675 * \brief Export a public key or the public part of a key pair in binary format.
676 *
677 * The output of this function can be passed to psa_import_key() to
678 * create an object that is equivalent to the public key.
679 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000680 * This specification supports a single format for each key type.
681 * Implementations may support other formats as long as the standard
682 * format is supported. Implementations that support other formats
683 * should ensure that the formats are clearly unambiguous so as to
684 * minimize the risk that an invalid input is accidentally interpreted
685 * according to a different format.
686 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000687 * For standard key types, the output format is as follows:
688 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
689 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
690 * ```
691 * RSAPublicKey ::= SEQUENCE {
692 * modulus INTEGER, -- n
693 * publicExponent INTEGER } -- e
694 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000695 * - For elliptic curve public keys (key types for which
696 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
697 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
698 * Let `m` be the bit size associated with the curve, i.e. the bit size of
699 * `q` for a curve over `F_q`. The representation consists of:
700 * - The byte 0x04;
701 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
702 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000703 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
704 * representation of the public key `y = g^x mod p` as a big-endian byte
705 * string. The length of the byte string is the length of the base prime `p`
706 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000707 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
708 * the format is the representation of the public key `y = g^x mod p` as a
709 * big-endian byte string. The length of the byte string is the length of the
710 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100711 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100712 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200713 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200714 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200715 * \param[out] data_length On success, the number of bytes
716 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100717 *
Gilles Peskine28538492018-07-11 17:34:00 +0200718 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100719 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200720 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200721 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200722 * The key is neither a public key nor a key pair.
723 * \retval #PSA_ERROR_NOT_SUPPORTED
724 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
725 * The size of the \p data buffer is too small. You can determine a
726 * sufficient buffer size by calling
727 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
728 * where \c type is the key type
729 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200730 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
731 * \retval #PSA_ERROR_HARDWARE_FAILURE
732 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300733 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300734 * The library has not been previously initialized by psa_crypto_init().
735 * It is implementation-dependent whether a failure to initialize
736 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100737 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100738psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100739 uint8_t *data,
740 size_t data_size,
741 size_t *data_length);
742
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100743/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100744 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100745 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000746 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100747 * This function is primarily useful to copy a key from one location
748 * to another, since it populates a key using the material from
749 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200750 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100751 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100752 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100753 * subject to implementation-defined restrictions on key sharing.
754 * In this case \p constraint would typically prevent the recipient
755 * from exporting the key.
Gilles Peskine7e198532018-03-08 07:50:30 +0100756 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100757 * The resulting key may only be used in a way that conforms to all
758 * three of: the policy of the source key, the policy previously set
759 * on the target, and the \p constraint parameter passed when calling
760 * this function.
761 * - The usage flags on the resulting key are the bitwise-and of the
762 * usage flags on the source policy, the previously-set target policy
763 * and the policy constraint.
764 * - If all three policies allow the same algorithm or wildcard-based
765 * algorithm policy, the resulting key has the same algorithm policy.
766 * - If one of the policies allows an algorithm and all the other policies
767 * either allow the same algorithm or a wildcard-based algorithm policy
768 * that includes this algorithm, the resulting key allows the same
769 * algorithm.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200770 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100771 * The effect of this function on implementation-defined metadata is
772 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200773 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100774 * \param source_handle The key to copy. It must be a handle to an
775 * occupied slot.
776 * \param target_handle A handle to the target slot. It must not contain
777 * key material yet.
778 * \param[in] constraint An optional policy constraint. If this parameter
779 * is non-null then the resulting key will conform
780 * to this policy in addition to the source policy
781 * and the policy already present on the target
782 * slot. If this parameter is null then the
783 * function behaves in the same way as if it was
784 * the target policy, i.e. only the source and
785 * target policies apply.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200786 *
787 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100788 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200789 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine3be6b7f2019-03-05 19:32:26 +0100790 * \p target_handle already contains key material.
David Saadab4ecc272019-02-14 13:48:10 +0200791 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine3be6b7f2019-03-05 19:32:26 +0100792 * \p source_handle does not contain key material.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200793 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100794 * The policy constraints on the source, on the target and
Gilles Peskine3be6b7f2019-03-05 19:32:26 +0100795 * \p constraint are incompatible.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100796 * \retval #PSA_ERROR_NOT_PERMITTED
797 * The source key is not exportable and its lifetime does not
798 * allow copying it to the target's lifetime.
799 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
800 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200801 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
802 * \retval #PSA_ERROR_HARDWARE_FAILURE
803 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100804 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100805psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200806 const psa_key_attributes_t *attributes,
807 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100808
809/**@}*/
810
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100811/** \defgroup hash Message digests
812 * @{
813 */
814
Gilles Peskine69647a42019-01-14 20:18:12 +0100815/** Calculate the hash (digest) of a message.
816 *
817 * \note To verify the hash of a message against an
818 * expected value, use psa_hash_compare() instead.
819 *
820 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
821 * such that #PSA_ALG_IS_HASH(\p alg) is true).
822 * \param[in] input Buffer containing the message to hash.
823 * \param input_length Size of the \p input buffer in bytes.
824 * \param[out] hash Buffer where the hash is to be written.
825 * \param hash_size Size of the \p hash buffer in bytes.
826 * \param[out] hash_length On success, the number of bytes
827 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100828 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100829 *
830 * \retval #PSA_SUCCESS
831 * Success.
832 * \retval #PSA_ERROR_NOT_SUPPORTED
833 * \p alg is not supported or is not a hash algorithm.
834 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
835 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
836 * \retval #PSA_ERROR_HARDWARE_FAILURE
837 * \retval #PSA_ERROR_TAMPERING_DETECTED
838 */
839psa_status_t psa_hash_compute(psa_algorithm_t alg,
840 const uint8_t *input,
841 size_t input_length,
842 uint8_t *hash,
843 size_t hash_size,
844 size_t *hash_length);
845
846/** Calculate the hash (digest) of a message and compare it with a
847 * reference value.
848 *
849 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
850 * such that #PSA_ALG_IS_HASH(\p alg) is true).
851 * \param[in] input Buffer containing the message to hash.
852 * \param input_length Size of the \p input buffer in bytes.
853 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100854 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100855 *
856 * \retval #PSA_SUCCESS
857 * The expected hash is identical to the actual hash of the input.
858 * \retval #PSA_ERROR_INVALID_SIGNATURE
859 * The hash of the message was calculated successfully, but it
860 * differs from the expected hash.
861 * \retval #PSA_ERROR_NOT_SUPPORTED
862 * \p alg is not supported or is not a hash algorithm.
863 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
864 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
865 * \retval #PSA_ERROR_HARDWARE_FAILURE
866 * \retval #PSA_ERROR_TAMPERING_DETECTED
867 */
868psa_status_t psa_hash_compare(psa_algorithm_t alg,
869 const uint8_t *input,
870 size_t input_length,
871 const uint8_t *hash,
872 const size_t hash_length);
873
Gilles Peskine308b91d2018-02-08 09:47:44 +0100874/** The type of the state data structure for multipart hash operations.
875 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000876 * Before calling any function on a hash operation object, the application must
877 * initialize it by any of the following means:
878 * - Set the structure to all-bits-zero, for example:
879 * \code
880 * psa_hash_operation_t operation;
881 * memset(&operation, 0, sizeof(operation));
882 * \endcode
883 * - Initialize the structure to logical zero values, for example:
884 * \code
885 * psa_hash_operation_t operation = {0};
886 * \endcode
887 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
888 * for example:
889 * \code
890 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
891 * \endcode
892 * - Assign the result of the function psa_hash_operation_init()
893 * to the structure, for example:
894 * \code
895 * psa_hash_operation_t operation;
896 * operation = psa_hash_operation_init();
897 * \endcode
898 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100899 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100900 * make any assumptions about the content of this structure except
901 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100902typedef struct psa_hash_operation_s psa_hash_operation_t;
903
Jaeden Amero6a25b412019-01-04 11:47:44 +0000904/** \def PSA_HASH_OPERATION_INIT
905 *
906 * This macro returns a suitable initializer for a hash operation object
907 * of type #psa_hash_operation_t.
908 */
909#ifdef __DOXYGEN_ONLY__
910/* This is an example definition for documentation purposes.
911 * Implementations should define a suitable value in `crypto_struct.h`.
912 */
913#define PSA_HASH_OPERATION_INIT {0}
914#endif
915
916/** Return an initial value for a hash operation object.
917 */
918static psa_hash_operation_t psa_hash_operation_init(void);
919
Gilles Peskinef45adda2019-01-14 18:29:18 +0100920/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100921 *
922 * The sequence of operations to calculate a hash (message digest)
923 * is as follows:
924 * -# Allocate an operation object which will be passed to all the functions
925 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000926 * -# Initialize the operation object with one of the methods described in the
927 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200928 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100929 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100930 * of the message each time. The hash that is calculated is the hash
931 * of the concatenation of these messages in order.
932 * -# To calculate the hash, call psa_hash_finish().
933 * To compare the hash with an expected value, call psa_hash_verify().
934 *
935 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000936 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100937 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200938 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100939 * eventually terminate the operation. The following events terminate an
940 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100941 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100942 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100943 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000944 * \param[in,out] operation The operation object to set up. It must have
945 * been initialized as per the documentation for
946 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200947 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
948 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100949 *
Gilles Peskine28538492018-07-11 17:34:00 +0200950 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100951 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200952 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200953 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100954 * \retval #PSA_ERROR_BAD_STATE
955 * The operation state is not valid (already set up and not
956 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200957 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
958 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
959 * \retval #PSA_ERROR_HARDWARE_FAILURE
960 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100961 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200962psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100963 psa_algorithm_t alg);
964
Gilles Peskine308b91d2018-02-08 09:47:44 +0100965/** Add a message fragment to a multipart hash operation.
966 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200967 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100968 *
969 * If this function returns an error status, the operation becomes inactive.
970 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200971 * \param[in,out] operation Active hash operation.
972 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200973 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100974 *
Gilles Peskine28538492018-07-11 17:34:00 +0200975 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100976 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200977 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100978 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200979 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
980 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
981 * \retval #PSA_ERROR_HARDWARE_FAILURE
982 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100983 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100984psa_status_t psa_hash_update(psa_hash_operation_t *operation,
985 const uint8_t *input,
986 size_t input_length);
987
Gilles Peskine308b91d2018-02-08 09:47:44 +0100988/** Finish the calculation of the hash of a message.
989 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200990 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100991 * This function calculates the hash of the message formed by concatenating
992 * the inputs passed to preceding calls to psa_hash_update().
993 *
994 * When this function returns, the operation becomes inactive.
995 *
996 * \warning Applications should not call this function if they expect
997 * a specific value for the hash. Call psa_hash_verify() instead.
998 * Beware that comparing integrity or authenticity data such as
999 * hash values with a function such as \c memcmp is risky
1000 * because the time taken by the comparison may leak information
1001 * about the hashed data which could allow an attacker to guess
1002 * a valid hash and thereby bypass security controls.
1003 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001004 * \param[in,out] operation Active hash operation.
1005 * \param[out] hash Buffer where the hash is to be written.
1006 * \param hash_size Size of the \p hash buffer in bytes.
1007 * \param[out] hash_length On success, the number of bytes
1008 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001009 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001010 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001011 *
Gilles Peskine28538492018-07-11 17:34:00 +02001012 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001013 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001014 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001015 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001016 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001017 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001018 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001019 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001020 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1021 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1022 * \retval #PSA_ERROR_HARDWARE_FAILURE
1023 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001024 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001025psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1026 uint8_t *hash,
1027 size_t hash_size,
1028 size_t *hash_length);
1029
Gilles Peskine308b91d2018-02-08 09:47:44 +01001030/** Finish the calculation of the hash of a message and compare it with
1031 * an expected value.
1032 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001033 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001034 * This function calculates the hash of the message formed by concatenating
1035 * the inputs passed to preceding calls to psa_hash_update(). It then
1036 * compares the calculated hash with the expected hash passed as a
1037 * parameter to this function.
1038 *
1039 * When this function returns, the operation becomes inactive.
1040 *
Gilles Peskine19067982018-03-20 17:54:53 +01001041 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001042 * comparison between the actual hash and the expected hash is performed
1043 * in constant time.
1044 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001045 * \param[in,out] operation Active hash operation.
1046 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001047 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001048 *
Gilles Peskine28538492018-07-11 17:34:00 +02001049 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001050 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001051 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001052 * The hash of the message was calculated successfully, but it
1053 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001054 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001055 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001056 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1057 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1058 * \retval #PSA_ERROR_HARDWARE_FAILURE
1059 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001060 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001061psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1062 const uint8_t *hash,
1063 size_t hash_length);
1064
Gilles Peskine308b91d2018-02-08 09:47:44 +01001065/** Abort a hash operation.
1066 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001067 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001068 * \p operation structure itself. Once aborted, the operation object
1069 * can be reused for another operation by calling
1070 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001071 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001072 * You may call this function any time after the operation object has
1073 * been initialized by any of the following methods:
1074 * - A call to psa_hash_setup(), whether it succeeds or not.
1075 * - Initializing the \c struct to all-bits-zero.
1076 * - Initializing the \c struct to logical zeros, e.g.
1077 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001078 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001079 * In particular, calling psa_hash_abort() after the operation has been
1080 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1081 * psa_hash_verify() is safe and has no effect.
1082 *
1083 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001084 *
Gilles Peskine28538492018-07-11 17:34:00 +02001085 * \retval #PSA_SUCCESS
1086 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001087 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001088 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1089 * \retval #PSA_ERROR_HARDWARE_FAILURE
1090 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001091 */
1092psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001093
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001094/** Clone a hash operation.
1095 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001096 * This function copies the state of an ongoing hash operation to
1097 * a new operation object. In other words, this function is equivalent
1098 * to calling psa_hash_setup() on \p target_operation with the same
1099 * algorithm that \p source_operation was set up for, then
1100 * psa_hash_update() on \p target_operation with the same input that
1101 * that was passed to \p source_operation. After this function returns, the
1102 * two objects are independent, i.e. subsequent calls involving one of
1103 * the objects do not affect the other object.
1104 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001105 * \param[in] source_operation The active hash operation to clone.
1106 * \param[in,out] target_operation The operation object to set up.
1107 * It must be initialized but not active.
1108 *
1109 * \retval #PSA_SUCCESS
1110 * \retval #PSA_ERROR_BAD_STATE
1111 * \p source_operation is not an active hash operation.
1112 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001113 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001114 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1115 * \retval #PSA_ERROR_HARDWARE_FAILURE
1116 * \retval #PSA_ERROR_TAMPERING_DETECTED
1117 */
1118psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1119 psa_hash_operation_t *target_operation);
1120
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001121/**@}*/
1122
Gilles Peskine8c9def32018-02-08 10:02:12 +01001123/** \defgroup MAC Message authentication codes
1124 * @{
1125 */
1126
Gilles Peskine69647a42019-01-14 20:18:12 +01001127/** Calculate the MAC (message authentication code) of a message.
1128 *
1129 * \note To verify the MAC of a message against an
1130 * expected value, use psa_mac_verify() instead.
1131 * Beware that comparing integrity or authenticity data such as
1132 * MAC values with a function such as \c memcmp is risky
1133 * because the time taken by the comparison may leak information
1134 * about the MAC value which could allow an attacker to guess
1135 * a valid MAC and thereby bypass security controls.
1136 *
1137 * \param handle Handle to the key to use for the operation.
1138 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001139 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001140 * \param[in] input Buffer containing the input message.
1141 * \param input_length Size of the \p input buffer in bytes.
1142 * \param[out] mac Buffer where the MAC value is to be written.
1143 * \param mac_size Size of the \p mac buffer in bytes.
1144 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001145 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001146 *
1147 * \retval #PSA_SUCCESS
1148 * Success.
1149 * \retval #PSA_ERROR_INVALID_HANDLE
1150 * \retval #PSA_ERROR_EMPTY_SLOT
1151 * \retval #PSA_ERROR_NOT_PERMITTED
1152 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001153 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001154 * \retval #PSA_ERROR_NOT_SUPPORTED
1155 * \p alg is not supported or is not a MAC algorithm.
1156 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1157 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1158 * \retval #PSA_ERROR_HARDWARE_FAILURE
1159 * \retval #PSA_ERROR_TAMPERING_DETECTED
1160 * \retval #PSA_ERROR_BAD_STATE
1161 * The library has not been previously initialized by psa_crypto_init().
1162 * It is implementation-dependent whether a failure to initialize
1163 * results in this error code.
1164 */
1165psa_status_t psa_mac_compute(psa_key_handle_t handle,
1166 psa_algorithm_t alg,
1167 const uint8_t *input,
1168 size_t input_length,
1169 uint8_t *mac,
1170 size_t mac_size,
1171 size_t *mac_length);
1172
1173/** Calculate the MAC of a message and compare it with a reference value.
1174 *
1175 * \param handle Handle to the key to use for the operation.
1176 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001177 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001178 * \param[in] input Buffer containing the input message.
1179 * \param input_length Size of the \p input buffer in bytes.
1180 * \param[out] mac Buffer containing the expected MAC value.
1181 * \param mac_length Size of the \p mac buffer in bytes.
1182 *
1183 * \retval #PSA_SUCCESS
1184 * The expected MAC is identical to the actual MAC of the input.
1185 * \retval #PSA_ERROR_INVALID_SIGNATURE
1186 * The MAC of the message was calculated successfully, but it
1187 * differs from the expected value.
1188 * \retval #PSA_ERROR_INVALID_HANDLE
1189 * \retval #PSA_ERROR_EMPTY_SLOT
1190 * \retval #PSA_ERROR_NOT_PERMITTED
1191 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001192 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001193 * \retval #PSA_ERROR_NOT_SUPPORTED
1194 * \p alg is not supported or is not a MAC algorithm.
1195 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1196 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1197 * \retval #PSA_ERROR_HARDWARE_FAILURE
1198 * \retval #PSA_ERROR_TAMPERING_DETECTED
1199 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001200psa_status_t psa_mac_verify(psa_key_handle_t handle,
1201 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001202 const uint8_t *input,
1203 size_t input_length,
1204 const uint8_t *mac,
1205 const size_t mac_length);
1206
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001207/** The type of the state data structure for multipart MAC operations.
1208 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001209 * Before calling any function on a MAC operation object, the application must
1210 * initialize it by any of the following means:
1211 * - Set the structure to all-bits-zero, for example:
1212 * \code
1213 * psa_mac_operation_t operation;
1214 * memset(&operation, 0, sizeof(operation));
1215 * \endcode
1216 * - Initialize the structure to logical zero values, for example:
1217 * \code
1218 * psa_mac_operation_t operation = {0};
1219 * \endcode
1220 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1221 * for example:
1222 * \code
1223 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1224 * \endcode
1225 * - Assign the result of the function psa_mac_operation_init()
1226 * to the structure, for example:
1227 * \code
1228 * psa_mac_operation_t operation;
1229 * operation = psa_mac_operation_init();
1230 * \endcode
1231 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001232 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001233 * make any assumptions about the content of this structure except
1234 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001235typedef struct psa_mac_operation_s psa_mac_operation_t;
1236
Jaeden Amero769ce272019-01-04 11:48:03 +00001237/** \def PSA_MAC_OPERATION_INIT
1238 *
1239 * This macro returns a suitable initializer for a MAC operation object of type
1240 * #psa_mac_operation_t.
1241 */
1242#ifdef __DOXYGEN_ONLY__
1243/* This is an example definition for documentation purposes.
1244 * Implementations should define a suitable value in `crypto_struct.h`.
1245 */
1246#define PSA_MAC_OPERATION_INIT {0}
1247#endif
1248
1249/** Return an initial value for a MAC operation object.
1250 */
1251static psa_mac_operation_t psa_mac_operation_init(void);
1252
Gilles Peskinef45adda2019-01-14 18:29:18 +01001253/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001254 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001255 * This function sets up the calculation of the MAC
1256 * (message authentication code) of a byte string.
1257 * To verify the MAC of a message against an
1258 * expected value, use psa_mac_verify_setup() instead.
1259 *
1260 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001261 * -# Allocate an operation object which will be passed to all the functions
1262 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001263 * -# Initialize the operation object with one of the methods described in the
1264 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001265 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001266 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1267 * of the message each time. The MAC that is calculated is the MAC
1268 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001269 * -# At the end of the message, call psa_mac_sign_finish() to finish
1270 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001271 *
1272 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001273 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001274 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001275 * After a successful call to psa_mac_sign_setup(), the application must
1276 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001277 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001278 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001279 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001280 * \param[in,out] operation The operation object to set up. It must have
1281 * been initialized as per the documentation for
1282 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001283 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001284 * It must remain valid until the operation
1285 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001286 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001287 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001288 *
Gilles Peskine28538492018-07-11 17:34:00 +02001289 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001290 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001291 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001292 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001293 * \retval #PSA_ERROR_NOT_PERMITTED
1294 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001295 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001296 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001297 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001298 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1299 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1300 * \retval #PSA_ERROR_HARDWARE_FAILURE
1301 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001302 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001303 * The operation state is not valid (already set up and not
1304 * subsequently completed).
1305 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001306 * The library has not been previously initialized by psa_crypto_init().
1307 * It is implementation-dependent whether a failure to initialize
1308 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001309 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001310psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001311 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001312 psa_algorithm_t alg);
1313
Gilles Peskinef45adda2019-01-14 18:29:18 +01001314/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001315 *
1316 * This function sets up the verification of the MAC
1317 * (message authentication code) of a byte string against an expected value.
1318 *
1319 * The sequence of operations to verify a MAC is as follows:
1320 * -# Allocate an operation object which will be passed to all the functions
1321 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001322 * -# Initialize the operation object with one of the methods described in the
1323 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001324 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001325 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1326 * of the message each time. The MAC that is calculated is the MAC
1327 * of the concatenation of these messages in order.
1328 * -# At the end of the message, call psa_mac_verify_finish() to finish
1329 * calculating the actual MAC of the message and verify it against
1330 * the expected value.
1331 *
1332 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001333 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001334 *
1335 * After a successful call to psa_mac_verify_setup(), the application must
1336 * eventually terminate the operation through one of the following methods:
1337 * - A failed call to psa_mac_update().
1338 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1339 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001340 * \param[in,out] operation The operation object to set up. It must have
1341 * been initialized as per the documentation for
1342 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001343 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001344 * It must remain valid until the operation
1345 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001346 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1347 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001348 *
Gilles Peskine28538492018-07-11 17:34:00 +02001349 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001350 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001351 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001352 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001353 * \retval #PSA_ERROR_NOT_PERMITTED
1354 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001355 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001356 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001357 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001358 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1359 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1360 * \retval #PSA_ERROR_HARDWARE_FAILURE
1361 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001362 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001363 * The operation state is not valid (already set up and not
1364 * subsequently completed).
1365 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001366 * The library has not been previously initialized by psa_crypto_init().
1367 * It is implementation-dependent whether a failure to initialize
1368 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001369 */
1370psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001371 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001372 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001373
Gilles Peskinedcd14942018-07-12 00:30:52 +02001374/** Add a message fragment to a multipart MAC operation.
1375 *
1376 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1377 * before calling this function.
1378 *
1379 * If this function returns an error status, the operation becomes inactive.
1380 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001381 * \param[in,out] operation Active MAC operation.
1382 * \param[in] input Buffer containing the message fragment to add to
1383 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001384 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001385 *
1386 * \retval #PSA_SUCCESS
1387 * Success.
1388 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001389 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001390 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1391 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1392 * \retval #PSA_ERROR_HARDWARE_FAILURE
1393 * \retval #PSA_ERROR_TAMPERING_DETECTED
1394 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001395psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1396 const uint8_t *input,
1397 size_t input_length);
1398
Gilles Peskinedcd14942018-07-12 00:30:52 +02001399/** Finish the calculation of the MAC of a message.
1400 *
1401 * The application must call psa_mac_sign_setup() before calling this function.
1402 * This function calculates the MAC of the message formed by concatenating
1403 * the inputs passed to preceding calls to psa_mac_update().
1404 *
1405 * When this function returns, the operation becomes inactive.
1406 *
1407 * \warning Applications should not call this function if they expect
1408 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1409 * Beware that comparing integrity or authenticity data such as
1410 * MAC values with a function such as \c memcmp is risky
1411 * because the time taken by the comparison may leak information
1412 * about the MAC value which could allow an attacker to guess
1413 * a valid MAC and thereby bypass security controls.
1414 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001415 * \param[in,out] operation Active MAC operation.
1416 * \param[out] mac Buffer where the MAC value is to be written.
1417 * \param mac_size Size of the \p mac buffer in bytes.
1418 * \param[out] mac_length On success, the number of bytes
1419 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001420 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001421 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001422 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001423 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001424 *
1425 * \retval #PSA_SUCCESS
1426 * Success.
1427 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001428 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001429 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001430 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001431 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1432 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1433 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1434 * \retval #PSA_ERROR_HARDWARE_FAILURE
1435 * \retval #PSA_ERROR_TAMPERING_DETECTED
1436 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001437psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1438 uint8_t *mac,
1439 size_t mac_size,
1440 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001441
Gilles Peskinedcd14942018-07-12 00:30:52 +02001442/** Finish the calculation of the MAC of a message and compare it with
1443 * an expected value.
1444 *
1445 * The application must call psa_mac_verify_setup() before calling this function.
1446 * This function calculates the MAC of the message formed by concatenating
1447 * the inputs passed to preceding calls to psa_mac_update(). It then
1448 * compares the calculated MAC with the expected MAC passed as a
1449 * parameter to this function.
1450 *
1451 * When this function returns, the operation becomes inactive.
1452 *
1453 * \note Implementations shall make the best effort to ensure that the
1454 * comparison between the actual MAC and the expected MAC is performed
1455 * in constant time.
1456 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001457 * \param[in,out] operation Active MAC operation.
1458 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001459 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001460 *
1461 * \retval #PSA_SUCCESS
1462 * The expected MAC is identical to the actual MAC of the message.
1463 * \retval #PSA_ERROR_INVALID_SIGNATURE
1464 * The MAC of the message was calculated successfully, but it
1465 * differs from the expected MAC.
1466 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001467 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001468 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1469 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1470 * \retval #PSA_ERROR_HARDWARE_FAILURE
1471 * \retval #PSA_ERROR_TAMPERING_DETECTED
1472 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001473psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1474 const uint8_t *mac,
1475 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001476
Gilles Peskinedcd14942018-07-12 00:30:52 +02001477/** Abort a MAC operation.
1478 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001479 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001480 * \p operation structure itself. Once aborted, the operation object
1481 * can be reused for another operation by calling
1482 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001483 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001484 * You may call this function any time after the operation object has
1485 * been initialized by any of the following methods:
1486 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1487 * it succeeds or not.
1488 * - Initializing the \c struct to all-bits-zero.
1489 * - Initializing the \c struct to logical zeros, e.g.
1490 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001491 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001492 * In particular, calling psa_mac_abort() after the operation has been
1493 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1494 * psa_mac_verify_finish() is safe and has no effect.
1495 *
1496 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001497 *
1498 * \retval #PSA_SUCCESS
1499 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001500 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001501 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1502 * \retval #PSA_ERROR_HARDWARE_FAILURE
1503 * \retval #PSA_ERROR_TAMPERING_DETECTED
1504 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001505psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1506
1507/**@}*/
1508
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001509/** \defgroup cipher Symmetric ciphers
1510 * @{
1511 */
1512
Gilles Peskine69647a42019-01-14 20:18:12 +01001513/** Encrypt a message using a symmetric cipher.
1514 *
1515 * This function encrypts a message with a random IV (initialization
1516 * vector).
1517 *
1518 * \param handle Handle to the key to use for the operation.
1519 * It must remain valid until the operation
1520 * terminates.
1521 * \param alg The cipher algorithm to compute
1522 * (\c PSA_ALG_XXX value such that
1523 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1524 * \param[in] input Buffer containing the message to encrypt.
1525 * \param input_length Size of the \p input buffer in bytes.
1526 * \param[out] output Buffer where the output is to be written.
1527 * The output contains the IV followed by
1528 * the ciphertext proper.
1529 * \param output_size Size of the \p output buffer in bytes.
1530 * \param[out] output_length On success, the number of bytes
1531 * that make up the output.
1532 *
1533 * \retval #PSA_SUCCESS
1534 * Success.
1535 * \retval #PSA_ERROR_INVALID_HANDLE
1536 * \retval #PSA_ERROR_EMPTY_SLOT
1537 * \retval #PSA_ERROR_NOT_PERMITTED
1538 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001539 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001540 * \retval #PSA_ERROR_NOT_SUPPORTED
1541 * \p alg is not supported or is not a cipher algorithm.
1542 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1543 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1544 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1545 * \retval #PSA_ERROR_HARDWARE_FAILURE
1546 * \retval #PSA_ERROR_TAMPERING_DETECTED
1547 */
1548psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1549 psa_algorithm_t alg,
1550 const uint8_t *input,
1551 size_t input_length,
1552 uint8_t *output,
1553 size_t output_size,
1554 size_t *output_length);
1555
1556/** Decrypt a message using a symmetric cipher.
1557 *
1558 * This function decrypts a message encrypted with a symmetric cipher.
1559 *
1560 * \param handle Handle to the key to use for the operation.
1561 * It must remain valid until the operation
1562 * terminates.
1563 * \param alg The cipher algorithm to compute
1564 * (\c PSA_ALG_XXX value such that
1565 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1566 * \param[in] input Buffer containing the message to decrypt.
1567 * This consists of the IV followed by the
1568 * ciphertext proper.
1569 * \param input_length Size of the \p input buffer in bytes.
1570 * \param[out] output Buffer where the plaintext is to be written.
1571 * \param output_size Size of the \p output buffer in bytes.
1572 * \param[out] output_length On success, the number of bytes
1573 * that make up the output.
1574 *
1575 * \retval #PSA_SUCCESS
1576 * Success.
1577 * \retval #PSA_ERROR_INVALID_HANDLE
1578 * \retval #PSA_ERROR_EMPTY_SLOT
1579 * \retval #PSA_ERROR_NOT_PERMITTED
1580 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001581 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001582 * \retval #PSA_ERROR_NOT_SUPPORTED
1583 * \p alg is not supported or is not a cipher algorithm.
1584 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1585 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1586 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1587 * \retval #PSA_ERROR_HARDWARE_FAILURE
1588 * \retval #PSA_ERROR_TAMPERING_DETECTED
1589 */
1590psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1591 psa_algorithm_t alg,
1592 const uint8_t *input,
1593 size_t input_length,
1594 uint8_t *output,
1595 size_t output_size,
1596 size_t *output_length);
1597
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001598/** The type of the state data structure for multipart cipher operations.
1599 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001600 * Before calling any function on a cipher operation object, the application
1601 * must initialize it by any of the following means:
1602 * - Set the structure to all-bits-zero, for example:
1603 * \code
1604 * psa_cipher_operation_t operation;
1605 * memset(&operation, 0, sizeof(operation));
1606 * \endcode
1607 * - Initialize the structure to logical zero values, for example:
1608 * \code
1609 * psa_cipher_operation_t operation = {0};
1610 * \endcode
1611 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1612 * for example:
1613 * \code
1614 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1615 * \endcode
1616 * - Assign the result of the function psa_cipher_operation_init()
1617 * to the structure, for example:
1618 * \code
1619 * psa_cipher_operation_t operation;
1620 * operation = psa_cipher_operation_init();
1621 * \endcode
1622 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001623 * This is an implementation-defined \c struct. Applications should not
1624 * make any assumptions about the content of this structure except
1625 * as directed by the documentation of a specific implementation. */
1626typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1627
Jaeden Amero5bae2272019-01-04 11:48:27 +00001628/** \def PSA_CIPHER_OPERATION_INIT
1629 *
1630 * This macro returns a suitable initializer for a cipher operation object of
1631 * type #psa_cipher_operation_t.
1632 */
1633#ifdef __DOXYGEN_ONLY__
1634/* This is an example definition for documentation purposes.
1635 * Implementations should define a suitable value in `crypto_struct.h`.
1636 */
1637#define PSA_CIPHER_OPERATION_INIT {0}
1638#endif
1639
1640/** Return an initial value for a cipher operation object.
1641 */
1642static psa_cipher_operation_t psa_cipher_operation_init(void);
1643
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001644/** Set the key for a multipart symmetric encryption operation.
1645 *
1646 * The sequence of operations to encrypt a message with a symmetric cipher
1647 * is as follows:
1648 * -# Allocate an operation object which will be passed to all the functions
1649 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001650 * -# Initialize the operation object with one of the methods described in the
1651 * documentation for #psa_cipher_operation_t, e.g.
1652 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001653 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001654 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001655 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001656 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001657 * requires a specific IV value.
1658 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1659 * of the message each time.
1660 * -# Call psa_cipher_finish().
1661 *
1662 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001663 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001664 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001665 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001666 * eventually terminate the operation. The following events terminate an
1667 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001668 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001669 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001670 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001671 * \param[in,out] operation The operation object to set up. It must have
1672 * been initialized as per the documentation for
1673 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001674 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001675 * It must remain valid until the operation
1676 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001677 * \param alg The cipher algorithm to compute
1678 * (\c PSA_ALG_XXX value such that
1679 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001680 *
Gilles Peskine28538492018-07-11 17:34:00 +02001681 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001682 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001683 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001684 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001685 * \retval #PSA_ERROR_NOT_PERMITTED
1686 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001687 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001688 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001689 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001690 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1691 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1692 * \retval #PSA_ERROR_HARDWARE_FAILURE
1693 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001694 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001695 * The operation state is not valid (already set up and not
1696 * subsequently completed).
1697 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001698 * The library has not been previously initialized by psa_crypto_init().
1699 * It is implementation-dependent whether a failure to initialize
1700 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001701 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001702psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001703 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001704 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001705
1706/** Set the key for a multipart symmetric decryption operation.
1707 *
1708 * The sequence of operations to decrypt a message with a symmetric cipher
1709 * is as follows:
1710 * -# Allocate an operation object which will be passed to all the functions
1711 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001712 * -# Initialize the operation object with one of the methods described in the
1713 * documentation for #psa_cipher_operation_t, e.g.
1714 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001715 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001716 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001717 * decryption. If the IV is prepended to the ciphertext, you can call
1718 * psa_cipher_update() on a buffer containing the IV followed by the
1719 * beginning of the message.
1720 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1721 * of the message each time.
1722 * -# Call psa_cipher_finish().
1723 *
1724 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001725 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001726 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001727 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001728 * eventually terminate the operation. The following events terminate an
1729 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001730 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001731 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001732 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001733 * \param[in,out] operation The operation object to set up. It must have
1734 * been initialized as per the documentation for
1735 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001736 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001737 * It must remain valid until the operation
1738 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001739 * \param alg The cipher algorithm to compute
1740 * (\c PSA_ALG_XXX value such that
1741 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001742 *
Gilles Peskine28538492018-07-11 17:34:00 +02001743 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001744 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001745 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001746 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001747 * \retval #PSA_ERROR_NOT_PERMITTED
1748 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001749 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001750 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001751 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001752 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1753 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1754 * \retval #PSA_ERROR_HARDWARE_FAILURE
1755 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001756 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001757 * The operation state is not valid (already set up and not
1758 * subsequently completed).
1759 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001760 * The library has not been previously initialized by psa_crypto_init().
1761 * It is implementation-dependent whether a failure to initialize
1762 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001763 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001764psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001765 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001766 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001767
Gilles Peskinedcd14942018-07-12 00:30:52 +02001768/** Generate an IV for a symmetric encryption operation.
1769 *
1770 * This function generates a random IV (initialization vector), nonce
1771 * or initial counter value for the encryption operation as appropriate
1772 * for the chosen algorithm, key type and key size.
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 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001779 * \param[in,out] operation Active cipher operation.
1780 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001781 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001782 * \param[out] iv_length On success, the number of bytes of the
1783 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001784 *
1785 * \retval #PSA_SUCCESS
1786 * Success.
1787 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001788 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001789 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001790 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001791 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1792 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1793 * \retval #PSA_ERROR_HARDWARE_FAILURE
1794 * \retval #PSA_ERROR_TAMPERING_DETECTED
1795 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001796psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1797 unsigned char *iv,
1798 size_t iv_size,
1799 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001800
Gilles Peskinedcd14942018-07-12 00:30:52 +02001801/** Set the IV for a symmetric encryption or decryption operation.
1802 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001803 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001804 * or initial counter value for the encryption or decryption operation.
1805 *
1806 * The application must call psa_cipher_encrypt_setup() before
1807 * calling this function.
1808 *
1809 * If this function returns an error status, the operation becomes inactive.
1810 *
1811 * \note When encrypting, applications should use psa_cipher_generate_iv()
1812 * instead of this function, unless implementing a protocol that requires
1813 * a non-random IV.
1814 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001815 * \param[in,out] operation Active cipher operation.
1816 * \param[in] iv Buffer containing the IV to use.
1817 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001818 *
1819 * \retval #PSA_SUCCESS
1820 * Success.
1821 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001822 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001823 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001824 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001825 * or the chosen algorithm does not use an IV.
1826 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1827 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1828 * \retval #PSA_ERROR_HARDWARE_FAILURE
1829 * \retval #PSA_ERROR_TAMPERING_DETECTED
1830 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001831psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1832 const unsigned char *iv,
1833 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001834
Gilles Peskinedcd14942018-07-12 00:30:52 +02001835/** Encrypt or decrypt a message fragment in an active cipher operation.
1836 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001837 * Before calling this function, you must:
1838 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1839 * The choice of setup function determines whether this function
1840 * encrypts or decrypts its input.
1841 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1842 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001843 *
1844 * If this function returns an error status, the operation becomes inactive.
1845 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001846 * \param[in,out] operation Active cipher operation.
1847 * \param[in] input Buffer containing the message fragment to
1848 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001849 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001850 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001851 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001852 * \param[out] output_length On success, the number of bytes
1853 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001854 *
1855 * \retval #PSA_SUCCESS
1856 * Success.
1857 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001858 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001859 * not set, or already completed).
1860 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1861 * The size of the \p output buffer is too small.
1862 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1863 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1864 * \retval #PSA_ERROR_HARDWARE_FAILURE
1865 * \retval #PSA_ERROR_TAMPERING_DETECTED
1866 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001867psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1868 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001869 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001870 unsigned char *output,
1871 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001872 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001873
Gilles Peskinedcd14942018-07-12 00:30:52 +02001874/** Finish encrypting or decrypting a message in a cipher operation.
1875 *
1876 * The application must call psa_cipher_encrypt_setup() or
1877 * psa_cipher_decrypt_setup() before calling this function. The choice
1878 * of setup function determines whether this function encrypts or
1879 * decrypts its input.
1880 *
1881 * This function finishes the encryption or decryption of the message
1882 * formed by concatenating the inputs passed to preceding calls to
1883 * psa_cipher_update().
1884 *
1885 * When this function returns, the operation becomes inactive.
1886 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001887 * \param[in,out] operation Active cipher operation.
1888 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001889 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001890 * \param[out] output_length On success, the number of bytes
1891 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001892 *
1893 * \retval #PSA_SUCCESS
1894 * Success.
1895 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001896 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001897 * not set, or already completed).
1898 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1899 * The size of the \p output buffer is too small.
1900 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1901 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1902 * \retval #PSA_ERROR_HARDWARE_FAILURE
1903 * \retval #PSA_ERROR_TAMPERING_DETECTED
1904 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001905psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001906 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001907 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001908 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001909
Gilles Peskinedcd14942018-07-12 00:30:52 +02001910/** Abort a cipher operation.
1911 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001912 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001913 * \p operation structure itself. Once aborted, the operation object
1914 * can be reused for another operation by calling
1915 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001916 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001917 * You may call this function any time after the operation object has
1918 * been initialized by any of the following methods:
1919 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1920 * whether it succeeds or not.
1921 * - Initializing the \c struct to all-bits-zero.
1922 * - Initializing the \c struct to logical zeros, e.g.
1923 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001924 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001925 * In particular, calling psa_cipher_abort() after the operation has been
1926 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1927 * is safe and has no effect.
1928 *
1929 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001930 *
1931 * \retval #PSA_SUCCESS
1932 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001933 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001934 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1935 * \retval #PSA_ERROR_HARDWARE_FAILURE
1936 * \retval #PSA_ERROR_TAMPERING_DETECTED
1937 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001938psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1939
1940/**@}*/
1941
Gilles Peskine3b555712018-03-03 21:27:57 +01001942/** \defgroup aead Authenticated encryption with associated data (AEAD)
1943 * @{
1944 */
1945
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001946/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001947 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001948 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001949 * \param alg The AEAD algorithm to compute
1950 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001951 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001952 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001953 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001954 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001955 * but not encrypted.
1956 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001957 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001958 * encrypted.
1959 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001960 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001961 * encrypted data. The additional data is not
1962 * part of this output. For algorithms where the
1963 * encrypted data and the authentication tag
1964 * are defined as separate outputs, the
1965 * authentication tag is appended to the
1966 * encrypted data.
1967 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1968 * This must be at least
1969 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1970 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001971 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01001972 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001973 *
Gilles Peskine28538492018-07-11 17:34:00 +02001974 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001975 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001976 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001977 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001978 * \retval #PSA_ERROR_NOT_PERMITTED
1979 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001980 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001981 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001982 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001983 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1984 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1985 * \retval #PSA_ERROR_HARDWARE_FAILURE
1986 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001987 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001988 * The library has not been previously initialized by psa_crypto_init().
1989 * It is implementation-dependent whether a failure to initialize
1990 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001991 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001992psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001993 psa_algorithm_t alg,
1994 const uint8_t *nonce,
1995 size_t nonce_length,
1996 const uint8_t *additional_data,
1997 size_t additional_data_length,
1998 const uint8_t *plaintext,
1999 size_t plaintext_length,
2000 uint8_t *ciphertext,
2001 size_t ciphertext_size,
2002 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002003
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002004/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002005 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002006 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002007 * \param alg The AEAD algorithm to compute
2008 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002009 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002010 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002011 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002012 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002013 * but not encrypted.
2014 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002015 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002016 * encrypted. For algorithms where the
2017 * encrypted data and the authentication tag
2018 * are defined as separate inputs, the buffer
2019 * must contain the encrypted data followed
2020 * by the authentication tag.
2021 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002022 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002023 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2024 * This must be at least
2025 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2026 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002027 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002028 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002029 *
Gilles Peskine28538492018-07-11 17:34:00 +02002030 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002031 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002032 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002033 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002034 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002035 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002036 * \retval #PSA_ERROR_NOT_PERMITTED
2037 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002038 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002039 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002040 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002041 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2042 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2043 * \retval #PSA_ERROR_HARDWARE_FAILURE
2044 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002045 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002046 * The library has not been previously initialized by psa_crypto_init().
2047 * It is implementation-dependent whether a failure to initialize
2048 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002049 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002050psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002051 psa_algorithm_t alg,
2052 const uint8_t *nonce,
2053 size_t nonce_length,
2054 const uint8_t *additional_data,
2055 size_t additional_data_length,
2056 const uint8_t *ciphertext,
2057 size_t ciphertext_length,
2058 uint8_t *plaintext,
2059 size_t plaintext_size,
2060 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002061
Gilles Peskine30a9e412019-01-14 18:36:12 +01002062/** The type of the state data structure for multipart AEAD operations.
2063 *
2064 * Before calling any function on an AEAD operation object, the application
2065 * must initialize it by any of the following means:
2066 * - Set the structure to all-bits-zero, for example:
2067 * \code
2068 * psa_aead_operation_t operation;
2069 * memset(&operation, 0, sizeof(operation));
2070 * \endcode
2071 * - Initialize the structure to logical zero values, for example:
2072 * \code
2073 * psa_aead_operation_t operation = {0};
2074 * \endcode
2075 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2076 * for example:
2077 * \code
2078 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2079 * \endcode
2080 * - Assign the result of the function psa_aead_operation_init()
2081 * to the structure, for example:
2082 * \code
2083 * psa_aead_operation_t operation;
2084 * operation = psa_aead_operation_init();
2085 * \endcode
2086 *
2087 * This is an implementation-defined \c struct. Applications should not
2088 * make any assumptions about the content of this structure except
2089 * as directed by the documentation of a specific implementation. */
2090typedef struct psa_aead_operation_s psa_aead_operation_t;
2091
2092/** \def PSA_AEAD_OPERATION_INIT
2093 *
2094 * This macro returns a suitable initializer for an AEAD operation object of
2095 * type #psa_aead_operation_t.
2096 */
2097#ifdef __DOXYGEN_ONLY__
2098/* This is an example definition for documentation purposes.
2099 * Implementations should define a suitable value in `crypto_struct.h`.
2100 */
2101#define PSA_AEAD_OPERATION_INIT {0}
2102#endif
2103
2104/** Return an initial value for an AEAD operation object.
2105 */
2106static psa_aead_operation_t psa_aead_operation_init(void);
2107
2108/** Set the key for a multipart authenticated encryption operation.
2109 *
2110 * The sequence of operations to encrypt a message with authentication
2111 * is as follows:
2112 * -# Allocate an operation object which will be passed to all the functions
2113 * listed here.
2114 * -# Initialize the operation object with one of the methods described in the
2115 * documentation for #psa_aead_operation_t, e.g.
2116 * PSA_AEAD_OPERATION_INIT.
2117 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002118 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2119 * inputs to the subsequent calls to psa_aead_update_ad() and
2120 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2121 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002122 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2123 * generate or set the nonce. You should use
2124 * psa_aead_generate_nonce() unless the protocol you are implementing
2125 * requires a specific nonce value.
2126 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2127 * of the non-encrypted additional authenticated data each time.
2128 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002129 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002130 * -# Call psa_aead_finish().
2131 *
2132 * The application may call psa_aead_abort() at any time after the operation
2133 * has been initialized.
2134 *
2135 * After a successful call to psa_aead_encrypt_setup(), the application must
2136 * eventually terminate the operation. The following events terminate an
2137 * operation:
2138 * - A failed call to any of the \c psa_aead_xxx functions.
2139 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2140 *
2141 * \param[in,out] operation The operation object to set up. It must have
2142 * been initialized as per the documentation for
2143 * #psa_aead_operation_t and not yet in use.
2144 * \param handle Handle to the key to use for the operation.
2145 * It must remain valid until the operation
2146 * terminates.
2147 * \param alg The AEAD algorithm to compute
2148 * (\c PSA_ALG_XXX value such that
2149 * #PSA_ALG_IS_AEAD(\p alg) is true).
2150 *
2151 * \retval #PSA_SUCCESS
2152 * Success.
2153 * \retval #PSA_ERROR_INVALID_HANDLE
2154 * \retval #PSA_ERROR_EMPTY_SLOT
2155 * \retval #PSA_ERROR_NOT_PERMITTED
2156 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002157 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002158 * \retval #PSA_ERROR_NOT_SUPPORTED
2159 * \p alg is not supported or is not an AEAD algorithm.
2160 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2161 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2162 * \retval #PSA_ERROR_HARDWARE_FAILURE
2163 * \retval #PSA_ERROR_TAMPERING_DETECTED
2164 * \retval #PSA_ERROR_BAD_STATE
2165 * The library has not been previously initialized by psa_crypto_init().
2166 * It is implementation-dependent whether a failure to initialize
2167 * results in this error code.
2168 */
2169psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2170 psa_key_handle_t handle,
2171 psa_algorithm_t alg);
2172
2173/** Set the key for a multipart authenticated decryption operation.
2174 *
2175 * The sequence of operations to decrypt a message with authentication
2176 * is as follows:
2177 * -# Allocate an operation object which will be passed to all the functions
2178 * listed here.
2179 * -# Initialize the operation object with one of the methods described in the
2180 * documentation for #psa_aead_operation_t, e.g.
2181 * PSA_AEAD_OPERATION_INIT.
2182 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002183 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2184 * inputs to the subsequent calls to psa_aead_update_ad() and
2185 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2186 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002187 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2188 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2189 * of the non-encrypted additional authenticated data each time.
2190 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002191 * of the ciphertext to decrypt each time.
2192 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002193 *
2194 * The application may call psa_aead_abort() at any time after the operation
2195 * has been initialized.
2196 *
2197 * After a successful call to psa_aead_decrypt_setup(), the application must
2198 * eventually terminate the operation. The following events terminate an
2199 * operation:
2200 * - A failed call to any of the \c psa_aead_xxx functions.
2201 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2202 *
2203 * \param[in,out] operation The operation object to set up. It must have
2204 * been initialized as per the documentation for
2205 * #psa_aead_operation_t and not yet in use.
2206 * \param handle Handle to the key to use for the operation.
2207 * It must remain valid until the operation
2208 * terminates.
2209 * \param alg The AEAD algorithm to compute
2210 * (\c PSA_ALG_XXX value such that
2211 * #PSA_ALG_IS_AEAD(\p alg) is true).
2212 *
2213 * \retval #PSA_SUCCESS
2214 * Success.
2215 * \retval #PSA_ERROR_INVALID_HANDLE
2216 * \retval #PSA_ERROR_EMPTY_SLOT
2217 * \retval #PSA_ERROR_NOT_PERMITTED
2218 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002219 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002220 * \retval #PSA_ERROR_NOT_SUPPORTED
2221 * \p alg is not supported or is not an AEAD algorithm.
2222 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2223 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2224 * \retval #PSA_ERROR_HARDWARE_FAILURE
2225 * \retval #PSA_ERROR_TAMPERING_DETECTED
2226 * \retval #PSA_ERROR_BAD_STATE
2227 * The library has not been previously initialized by psa_crypto_init().
2228 * It is implementation-dependent whether a failure to initialize
2229 * results in this error code.
2230 */
2231psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2232 psa_key_handle_t handle,
2233 psa_algorithm_t alg);
2234
2235/** Generate a random nonce for an authenticated encryption operation.
2236 *
2237 * This function generates a random nonce for the authenticated encryption
2238 * operation with an appropriate size for the chosen algorithm, key type
2239 * and key size.
2240 *
2241 * The application must call psa_aead_encrypt_setup() before
2242 * calling this function.
2243 *
2244 * If this function returns an error status, the operation becomes inactive.
2245 *
2246 * \param[in,out] operation Active AEAD operation.
2247 * \param[out] nonce Buffer where the generated nonce is to be
2248 * written.
2249 * \param nonce_size Size of the \p nonce buffer in bytes.
2250 * \param[out] nonce_length On success, the number of bytes of the
2251 * generated nonce.
2252 *
2253 * \retval #PSA_SUCCESS
2254 * Success.
2255 * \retval #PSA_ERROR_BAD_STATE
2256 * The operation state is not valid (not set up, or nonce already set).
2257 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2258 * The size of the \p nonce buffer is too small.
2259 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2260 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2261 * \retval #PSA_ERROR_HARDWARE_FAILURE
2262 * \retval #PSA_ERROR_TAMPERING_DETECTED
2263 */
2264psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2265 unsigned char *nonce,
2266 size_t nonce_size,
2267 size_t *nonce_length);
2268
2269/** Set the nonce for an authenticated encryption or decryption operation.
2270 *
2271 * This function sets the nonce for the authenticated
2272 * encryption or decryption operation.
2273 *
2274 * The application must call psa_aead_encrypt_setup() before
2275 * calling this function.
2276 *
2277 * If this function returns an error status, the operation becomes inactive.
2278 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002279 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002280 * instead of this function, unless implementing a protocol that requires
2281 * a non-random IV.
2282 *
2283 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002284 * \param[in] nonce Buffer containing the nonce to use.
2285 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002286 *
2287 * \retval #PSA_SUCCESS
2288 * Success.
2289 * \retval #PSA_ERROR_BAD_STATE
2290 * The operation state is not valid (not set up, or nonce already set).
2291 * \retval #PSA_ERROR_INVALID_ARGUMENT
2292 * The size of \p nonce is not acceptable for the chosen algorithm.
2293 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2294 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2295 * \retval #PSA_ERROR_HARDWARE_FAILURE
2296 * \retval #PSA_ERROR_TAMPERING_DETECTED
2297 */
2298psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2299 const unsigned char *nonce,
2300 size_t nonce_length);
2301
Gilles Peskinebc59c852019-01-17 15:26:08 +01002302/** Declare the lengths of the message and additional data for AEAD.
2303 *
2304 * The application must call this function before calling
2305 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2306 * the operation requires it. If the algorithm does not require it,
2307 * calling this function is optional, but if this function is called
2308 * then the implementation must enforce the lengths.
2309 *
2310 * You may call this function before or after setting the nonce with
2311 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2312 *
2313 * - For #PSA_ALG_CCM, calling this function is required.
2314 * - For the other AEAD algorithms defined in this specification, calling
2315 * this function is not required.
2316 * - For vendor-defined algorithm, refer to the vendor documentation.
2317 *
2318 * \param[in,out] operation Active AEAD operation.
2319 * \param ad_length Size of the non-encrypted additional
2320 * authenticated data in bytes.
2321 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2322 *
2323 * \retval #PSA_SUCCESS
2324 * Success.
2325 * \retval #PSA_ERROR_BAD_STATE
2326 * The operation state is not valid (not set up, already completed,
2327 * or psa_aead_update_ad() or psa_aead_update() already called).
2328 * \retval #PSA_ERROR_INVALID_ARGUMENT
2329 * At least one of the lengths is not acceptable for the chosen
2330 * algorithm.
2331 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2332 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2333 * \retval #PSA_ERROR_HARDWARE_FAILURE
2334 * \retval #PSA_ERROR_TAMPERING_DETECTED
2335 */
2336psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2337 size_t ad_length,
2338 size_t plaintext_length);
2339
Gilles Peskine30a9e412019-01-14 18:36:12 +01002340/** Pass additional data to an active AEAD operation.
2341 *
2342 * Additional data is authenticated, but not encrypted.
2343 *
2344 * You may call this function multiple times to pass successive fragments
2345 * of the additional data. You may not call this function after passing
2346 * data to encrypt or decrypt with psa_aead_update().
2347 *
2348 * Before calling this function, you must:
2349 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2350 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2351 *
2352 * If this function returns an error status, the operation becomes inactive.
2353 *
2354 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2355 * there is no guarantee that the input is valid. Therefore, until
2356 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2357 * treat the input as untrusted and prepare to undo any action that
2358 * depends on the input if psa_aead_verify() returns an error status.
2359 *
2360 * \param[in,out] operation Active AEAD operation.
2361 * \param[in] input Buffer containing the fragment of
2362 * additional data.
2363 * \param input_length Size of the \p input buffer in bytes.
2364 *
2365 * \retval #PSA_SUCCESS
2366 * Success.
2367 * \retval #PSA_ERROR_BAD_STATE
2368 * The operation state is not valid (not set up, nonce not set,
2369 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002370 * \retval #PSA_ERROR_INVALID_ARGUMENT
2371 * The total input length overflows the additional data length that
2372 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002373 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2374 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2375 * \retval #PSA_ERROR_HARDWARE_FAILURE
2376 * \retval #PSA_ERROR_TAMPERING_DETECTED
2377 */
2378psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2379 const uint8_t *input,
2380 size_t input_length);
2381
2382/** Encrypt or decrypt a message fragment in an active AEAD operation.
2383 *
2384 * Before calling this function, you must:
2385 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2386 * The choice of setup function determines whether this function
2387 * encrypts or decrypts its input.
2388 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2389 * 3. Call psa_aead_update_ad() to pass all the additional data.
2390 *
2391 * If this function returns an error status, the operation becomes inactive.
2392 *
2393 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2394 * there is no guarantee that the input is valid. Therefore, until
2395 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2396 * - Do not use the output in any way other than storing it in a
2397 * confidential location. If you take any action that depends
2398 * on the tentative decrypted data, this action will need to be
2399 * undone if the input turns out not to be valid. Furthermore,
2400 * if an adversary can observe that this action took place
2401 * (for example through timing), they may be able to use this
2402 * fact as an oracle to decrypt any message encrypted with the
2403 * same key.
2404 * - In particular, do not copy the output anywhere but to a
2405 * memory or storage space that you have exclusive access to.
2406 *
2407 * \param[in,out] operation Active AEAD operation.
2408 * \param[in] input Buffer containing the message fragment to
2409 * encrypt or decrypt.
2410 * \param input_length Size of the \p input buffer in bytes.
2411 * \param[out] output Buffer where the output is to be written.
2412 * \param output_size Size of the \p output buffer in bytes.
2413 * \param[out] output_length On success, the number of bytes
2414 * that make up the returned output.
2415 *
2416 * \retval #PSA_SUCCESS
2417 * Success.
2418 * \retval #PSA_ERROR_BAD_STATE
2419 * The operation state is not valid (not set up, nonce not set
2420 * or already completed).
2421 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2422 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002423 * \retval #PSA_ERROR_INVALID_ARGUMENT
2424 * The total length of input to psa_aead_update_ad() so far is
2425 * less than the additional data length that was previously
2426 * specified with psa_aead_set_lengths().
2427 * \retval #PSA_ERROR_INVALID_ARGUMENT
2428 * The total input length overflows the plaintext length that
2429 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002430 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2431 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2432 * \retval #PSA_ERROR_HARDWARE_FAILURE
2433 * \retval #PSA_ERROR_TAMPERING_DETECTED
2434 */
2435psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2436 const uint8_t *input,
2437 size_t input_length,
2438 unsigned char *output,
2439 size_t output_size,
2440 size_t *output_length);
2441
2442/** Finish encrypting a message in an AEAD operation.
2443 *
2444 * The operation must have been set up with psa_aead_encrypt_setup().
2445 *
2446 * This function finishes the authentication of the additional data
2447 * formed by concatenating the inputs passed to preceding calls to
2448 * psa_aead_update_ad() with the plaintext formed by concatenating the
2449 * inputs passed to preceding calls to psa_aead_update().
2450 *
2451 * This function has two output buffers:
2452 * - \p ciphertext contains trailing ciphertext that was buffered from
2453 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2454 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2455 * will not contain any output and can be a 0-sized buffer.
2456 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002457 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002458 * that the operation performs.
2459 *
2460 * When this function returns, the operation becomes inactive.
2461 *
2462 * \param[in,out] operation Active AEAD operation.
2463 * \param[out] ciphertext Buffer where the last part of the ciphertext
2464 * is to be written.
2465 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2466 * \param[out] ciphertext_length On success, the number of bytes of
2467 * returned ciphertext.
2468 * \param[out] tag Buffer where the authentication tag is
2469 * to be written.
2470 * \param tag_size Size of the \p tag buffer in bytes.
2471 * \param[out] tag_length On success, the number of bytes
2472 * that make up the returned tag.
2473 *
2474 * \retval #PSA_SUCCESS
2475 * Success.
2476 * \retval #PSA_ERROR_BAD_STATE
2477 * The operation state is not valid (not set up, nonce not set,
2478 * decryption, or already completed).
2479 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002480 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002481 * \retval #PSA_ERROR_INVALID_ARGUMENT
2482 * The total length of input to psa_aead_update_ad() so far is
2483 * less than the additional data length that was previously
2484 * specified with psa_aead_set_lengths().
2485 * \retval #PSA_ERROR_INVALID_ARGUMENT
2486 * The total length of input to psa_aead_update() so far is
2487 * less than the plaintext length that was previously
2488 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002489 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2490 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2491 * \retval #PSA_ERROR_HARDWARE_FAILURE
2492 * \retval #PSA_ERROR_TAMPERING_DETECTED
2493 */
2494psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002495 uint8_t *ciphertext,
2496 size_t ciphertext_size,
2497 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002498 uint8_t *tag,
2499 size_t tag_size,
2500 size_t *tag_length);
2501
2502/** Finish authenticating and decrypting a message in an AEAD operation.
2503 *
2504 * The operation must have been set up with psa_aead_decrypt_setup().
2505 *
2506 * This function finishes the authentication of the additional data
2507 * formed by concatenating the inputs passed to preceding calls to
2508 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2509 * inputs passed to preceding calls to psa_aead_update().
2510 *
2511 * When this function returns, the operation becomes inactive.
2512 *
2513 * \param[in,out] operation Active AEAD operation.
2514 * \param[in] tag Buffer containing the authentication tag.
2515 * \param tag_length Size of the \p tag buffer in bytes.
2516 *
2517 * \retval #PSA_SUCCESS
2518 * Success.
2519 * \retval #PSA_ERROR_BAD_STATE
2520 * The operation state is not valid (not set up, nonce not set,
2521 * encryption, or already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002522 * \retval #PSA_ERROR_INVALID_ARGUMENT
2523 * The total length of input to psa_aead_update_ad() so far is
2524 * less than the additional data length that was previously
2525 * specified with psa_aead_set_lengths().
2526 * \retval #PSA_ERROR_INVALID_ARGUMENT
2527 * The total length of input to psa_aead_update() so far is
2528 * less than the plaintext length that was previously
2529 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002530 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2531 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2532 * \retval #PSA_ERROR_HARDWARE_FAILURE
2533 * \retval #PSA_ERROR_TAMPERING_DETECTED
2534 */
2535psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2536 const uint8_t *tag,
2537 size_t tag_length);
2538
2539/** Abort an AEAD operation.
2540 *
2541 * Aborting an operation frees all associated resources except for the
2542 * \p operation structure itself. Once aborted, the operation object
2543 * can be reused for another operation by calling
2544 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2545 *
2546 * You may call this function any time after the operation object has
2547 * been initialized by any of the following methods:
2548 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2549 * whether it succeeds or not.
2550 * - Initializing the \c struct to all-bits-zero.
2551 * - Initializing the \c struct to logical zeros, e.g.
2552 * `psa_aead_operation_t operation = {0}`.
2553 *
2554 * In particular, calling psa_aead_abort() after the operation has been
2555 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2556 * is safe and has no effect.
2557 *
2558 * \param[in,out] operation Initialized AEAD operation.
2559 *
2560 * \retval #PSA_SUCCESS
2561 * \retval #PSA_ERROR_BAD_STATE
2562 * \p operation is not an active AEAD operation.
2563 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2564 * \retval #PSA_ERROR_HARDWARE_FAILURE
2565 * \retval #PSA_ERROR_TAMPERING_DETECTED
2566 */
2567psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2568
Gilles Peskine3b555712018-03-03 21:27:57 +01002569/**@}*/
2570
Gilles Peskine20035e32018-02-03 22:44:14 +01002571/** \defgroup asymmetric Asymmetric cryptography
2572 * @{
2573 */
2574
2575/**
2576 * \brief Sign a hash or short message with a private key.
2577 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002578 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002579 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002580 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2581 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2582 * to determine the hash algorithm to use.
2583 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002584 * \param handle Handle to the key to use for the operation.
2585 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002586 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002587 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002588 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002589 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002590 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002591 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002592 * \param[out] signature_length On success, the number of bytes
2593 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002594 *
Gilles Peskine28538492018-07-11 17:34:00 +02002595 * \retval #PSA_SUCCESS
2596 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002597 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002598 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002599 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002600 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002601 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002602 * \retval #PSA_ERROR_NOT_SUPPORTED
2603 * \retval #PSA_ERROR_INVALID_ARGUMENT
2604 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2605 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2606 * \retval #PSA_ERROR_HARDWARE_FAILURE
2607 * \retval #PSA_ERROR_TAMPERING_DETECTED
2608 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002609 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002610 * The library has not been previously initialized by psa_crypto_init().
2611 * It is implementation-dependent whether a failure to initialize
2612 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002613 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002614psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002615 psa_algorithm_t alg,
2616 const uint8_t *hash,
2617 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002618 uint8_t *signature,
2619 size_t signature_size,
2620 size_t *signature_length);
2621
2622/**
2623 * \brief Verify the signature a hash or short message using a public key.
2624 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002625 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002626 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002627 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2628 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2629 * to determine the hash algorithm to use.
2630 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002631 * \param handle Handle to the key to use for the operation.
2632 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002633 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002634 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002635 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002636 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002637 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002638 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002639 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002640 *
Gilles Peskine28538492018-07-11 17:34:00 +02002641 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002642 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002643 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002644 * The calculation was perfomed successfully, but the passed
2645 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002646 * \retval #PSA_ERROR_NOT_SUPPORTED
2647 * \retval #PSA_ERROR_INVALID_ARGUMENT
2648 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2649 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2650 * \retval #PSA_ERROR_HARDWARE_FAILURE
2651 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002652 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002653 * The library has not been previously initialized by psa_crypto_init().
2654 * It is implementation-dependent whether a failure to initialize
2655 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002656 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002657psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002658 psa_algorithm_t alg,
2659 const uint8_t *hash,
2660 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002661 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002662 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002663
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002664/**
2665 * \brief Encrypt a short message with a public key.
2666 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002667 * \param handle Handle to the key to use for the operation.
2668 * It must be a public key or an asymmetric
2669 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002670 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002671 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002672 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002673 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002674 * \param[in] salt A salt or label, if supported by the
2675 * encryption algorithm.
2676 * If the algorithm does not support a
2677 * salt, pass \c NULL.
2678 * If the algorithm supports an optional
2679 * salt and you do not want to pass a salt,
2680 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002681 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002682 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2683 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002684 * \param salt_length Size of the \p salt buffer in bytes.
2685 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002686 * \param[out] output Buffer where the encrypted message is to
2687 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002688 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002689 * \param[out] output_length On success, the number of bytes
2690 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002691 *
Gilles Peskine28538492018-07-11 17:34:00 +02002692 * \retval #PSA_SUCCESS
2693 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002694 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002695 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002696 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002697 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002698 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002699 * \retval #PSA_ERROR_NOT_SUPPORTED
2700 * \retval #PSA_ERROR_INVALID_ARGUMENT
2701 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2702 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2703 * \retval #PSA_ERROR_HARDWARE_FAILURE
2704 * \retval #PSA_ERROR_TAMPERING_DETECTED
2705 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002706 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002707 * The library has not been previously initialized by psa_crypto_init().
2708 * It is implementation-dependent whether a failure to initialize
2709 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002710 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002711psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002712 psa_algorithm_t alg,
2713 const uint8_t *input,
2714 size_t input_length,
2715 const uint8_t *salt,
2716 size_t salt_length,
2717 uint8_t *output,
2718 size_t output_size,
2719 size_t *output_length);
2720
2721/**
2722 * \brief Decrypt a short message with a private key.
2723 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002724 * \param handle Handle to the key to use for the operation.
2725 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002726 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002727 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002728 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002729 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002730 * \param[in] salt A salt or label, if supported by the
2731 * encryption algorithm.
2732 * If the algorithm does not support a
2733 * salt, pass \c NULL.
2734 * If the algorithm supports an optional
2735 * salt and you do not want to pass a salt,
2736 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002737 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002738 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2739 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002740 * \param salt_length Size of the \p salt buffer in bytes.
2741 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002742 * \param[out] output Buffer where the decrypted message is to
2743 * be written.
2744 * \param output_size Size of the \c output buffer in bytes.
2745 * \param[out] output_length On success, the number of bytes
2746 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002747 *
Gilles Peskine28538492018-07-11 17:34:00 +02002748 * \retval #PSA_SUCCESS
2749 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002750 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002751 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002752 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002753 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002754 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002755 * \retval #PSA_ERROR_NOT_SUPPORTED
2756 * \retval #PSA_ERROR_INVALID_ARGUMENT
2757 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2758 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2759 * \retval #PSA_ERROR_HARDWARE_FAILURE
2760 * \retval #PSA_ERROR_TAMPERING_DETECTED
2761 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2762 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002763 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002764 * The library has not been previously initialized by psa_crypto_init().
2765 * It is implementation-dependent whether a failure to initialize
2766 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002767 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002768psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002769 psa_algorithm_t alg,
2770 const uint8_t *input,
2771 size_t input_length,
2772 const uint8_t *salt,
2773 size_t salt_length,
2774 uint8_t *output,
2775 size_t output_size,
2776 size_t *output_length);
2777
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002778/**@}*/
2779
Gilles Peskineedd76872018-07-20 17:42:05 +02002780/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002781 * @{
2782 */
2783
2784/** The type of the state data structure for generators.
2785 *
2786 * Before calling any function on a generator, the application must
2787 * initialize it by any of the following means:
2788 * - Set the structure to all-bits-zero, for example:
2789 * \code
2790 * psa_crypto_generator_t generator;
2791 * memset(&generator, 0, sizeof(generator));
2792 * \endcode
2793 * - Initialize the structure to logical zero values, for example:
2794 * \code
2795 * psa_crypto_generator_t generator = {0};
2796 * \endcode
2797 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2798 * for example:
2799 * \code
2800 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2801 * \endcode
2802 * - Assign the result of the function psa_crypto_generator_init()
2803 * to the structure, for example:
2804 * \code
2805 * psa_crypto_generator_t generator;
2806 * generator = psa_crypto_generator_init();
2807 * \endcode
2808 *
2809 * This is an implementation-defined \c struct. Applications should not
2810 * make any assumptions about the content of this structure except
2811 * as directed by the documentation of a specific implementation.
2812 */
2813typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2814
2815/** \def PSA_CRYPTO_GENERATOR_INIT
2816 *
2817 * This macro returns a suitable initializer for a generator object
2818 * of type #psa_crypto_generator_t.
2819 */
2820#ifdef __DOXYGEN_ONLY__
2821/* This is an example definition for documentation purposes.
2822 * Implementations should define a suitable value in `crypto_struct.h`.
2823 */
2824#define PSA_CRYPTO_GENERATOR_INIT {0}
2825#endif
2826
2827/** Return an initial value for a generator object.
2828 */
2829static psa_crypto_generator_t psa_crypto_generator_init(void);
2830
2831/** Retrieve the current capacity of a generator.
2832 *
2833 * The capacity of a generator is the maximum number of bytes that it can
2834 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2835 *
2836 * \param[in] generator The generator to query.
2837 * \param[out] capacity On success, the capacity of the generator.
2838 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002839 * \retval #PSA_SUCCESS
2840 * \retval #PSA_ERROR_BAD_STATE
2841 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002842 */
2843psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2844 size_t *capacity);
2845
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002846/** Set the maximum capacity of a generator.
2847 *
2848 * \param[in,out] generator The generator object to modify.
2849 * \param capacity The new capacity of the generator.
2850 * It must be less or equal to the generator's
2851 * current capacity.
2852 *
2853 * \retval #PSA_SUCCESS
2854 * \retval #PSA_ERROR_INVALID_ARGUMENT
2855 * \p capacity is larger than the generator's current capacity.
2856 * \retval #PSA_ERROR_BAD_STATE
2857 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2858 */
2859psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2860 size_t capacity);
2861
Gilles Peskineeab56e42018-07-12 17:12:33 +02002862/** Read some data from a generator.
2863 *
2864 * This function reads and returns a sequence of bytes from a generator.
2865 * The data that is read is discarded from the generator. The generator's
2866 * capacity is decreased by the number of bytes read.
2867 *
2868 * \param[in,out] generator The generator object to read from.
2869 * \param[out] output Buffer where the generator output will be
2870 * written.
2871 * \param output_length Number of bytes to output.
2872 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002873 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02002874 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02002875 * There were fewer than \p output_length bytes
2876 * in the generator. Note that in this case, no
2877 * output is written to the output buffer.
2878 * The generator's capacity is set to 0, thus
2879 * subsequent calls to this function will not
2880 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002881 * \retval #PSA_ERROR_BAD_STATE
2882 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2883 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2884 * \retval #PSA_ERROR_HARDWARE_FAILURE
2885 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002886 */
2887psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2888 uint8_t *output,
2889 size_t output_length);
2890
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002891/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002892 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002893 * This function uses the output of a generator to derive a key.
2894 * How much output it consumes and how the key is derived depends on the
2895 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002896 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002897 * - For key types for which the key is an arbitrary sequence of bytes
2898 * of a given size,
2899 * this function is functionally equivalent to calling #psa_generator_read
2900 * and passing the resulting output to #psa_import_key.
2901 * However, this function has a security benefit:
2902 * if the implementation provides an isolation boundary then
2903 * the key material is not exposed outside the isolation boundary.
2904 * As a consequence, for these key types, this function always consumes
2905 * exactly (\p bits / 8) bytes from the generator.
2906 * The following key types defined in this specification follow this scheme:
2907 *
2908 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002909 * - #PSA_KEY_TYPE_ARC4;
2910 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002911 * - #PSA_KEY_TYPE_DERIVE;
2912 * - #PSA_KEY_TYPE_HMAC.
2913 *
2914 * - For ECC keys on a Montgomery elliptic curve
2915 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
2916 * Montgomery curve), this function always draws a byte string whose
2917 * length is determined by the curve, and sets the mandatory bits
2918 * accordingly. That is:
2919 *
2920 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
2921 * and process it as specified in RFC 7748 &sect;5.
2922 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
2923 * and process it as specified in RFC 7748 &sect;5.
2924 *
2925 * - For key types for which the key is represented by a single sequence of
2926 * \p bits bits with constraints as to which bit sequences are acceptable,
2927 * this function draws a byte string of length (\p bits / 8) bytes rounded
2928 * up to the nearest whole number of bytes. If the resulting byte string
2929 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
2930 * This process is repeated until an acceptable byte string is drawn.
2931 * The byte string drawn from the generator is interpreted as specified
2932 * for the output produced by psa_export_key().
2933 * The following key types defined in this specification follow this scheme:
2934 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01002935 * - #PSA_KEY_TYPE_DES.
2936 * Force-set the parity bits, but discard forbidden weak keys.
2937 * For 2-key and 3-key triple-DES, the three keys are generated
2938 * successively (for example, for 3-key triple-DES,
2939 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
2940 * discard the first 8 bytes, use the next 8 bytes as the first key,
2941 * and continue reading output from the generator to derive the other
2942 * two keys).
2943 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
2944 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
2945 * ECC keys on a Weierstrass elliptic curve
2946 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
2947 * Weierstrass curve).
2948 * For these key types, interpret the byte string as integer
2949 * in big-endian order. Discard it if it is not in the range
2950 * [0, *N* - 2] where *N* is the boundary of the private key domain
2951 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01002952 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01002953 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01002954 * This method allows compliance to NIST standards, specifically
2955 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01002956 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
2957 * in FIPS 186-4 &sect;B.1.2 for DSA, and
2958 * in NIST SP 800-56A &sect;5.6.1.2.2 or
2959 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002960 *
2961 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
2962 * the way in which the generator output is consumed is
2963 * implementation-defined.
2964 *
2965 * In all cases, the data that is read is discarded from the generator.
2966 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002967 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002968 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002969 * It must have been obtained by calling
2970 * psa_allocate_key() or psa_create_key() and must
2971 * not contain key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002972 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskineee67dd62019-03-12 13:23:17 +01002973 * This must be a secret key type or a key pair type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002974 * \param bits Key size in bits.
2975 * \param[in,out] generator The generator object to read from.
2976 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002977 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002978 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002979 * If the key is persistent, the key material and the key's metadata
2980 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02002981 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002982 * There was not enough data to create the desired key.
2983 * Note that in this case, no output is written to the output buffer.
2984 * The generator's capacity is set to 0, thus subsequent calls to
2985 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002986 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002987 * The key type or key size is not supported, either by the
2988 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002989 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002990 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002991 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002992 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002993 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2994 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2995 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2996 * \retval #PSA_ERROR_HARDWARE_FAILURE
2997 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002998 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002999 * The library has not been previously initialized by psa_crypto_init().
3000 * It is implementation-dependent whether a failure to initialize
3001 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003002 */
Gilles Peskine87a5e562019-04-17 12:28:25 +02003003psa_status_t psa_generator_import_key(const psa_key_attributes_t *attributes,
3004 psa_key_handle_t *handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003005 size_t bits,
3006 psa_crypto_generator_t *generator);
3007
3008/** Abort a generator.
3009 *
3010 * Once a generator has been aborted, its capacity is zero.
3011 * Aborting a generator frees all associated resources except for the
3012 * \c generator structure itself.
3013 *
3014 * This function may be called at any time as long as the generator
3015 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3016 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3017 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3018 * on a generator that has not been set up.
3019 *
3020 * Once aborted, the generator object may be called.
3021 *
3022 * \param[in,out] generator The generator to abort.
3023 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003024 * \retval #PSA_SUCCESS
3025 * \retval #PSA_ERROR_BAD_STATE
3026 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3027 * \retval #PSA_ERROR_HARDWARE_FAILURE
3028 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003029 */
3030psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3031
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003032/** Use the maximum possible capacity for a generator.
3033 *
3034 * Use this value as the capacity argument when setting up a generator
3035 * to indicate that the generator should have the maximum possible capacity.
3036 * The value of the maximum possible capacity depends on the generator
3037 * algorithm.
3038 */
3039#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3040
Gilles Peskineeab56e42018-07-12 17:12:33 +02003041/**@}*/
3042
Gilles Peskineea0fb492018-07-12 17:17:20 +02003043/** \defgroup derivation Key derivation
3044 * @{
3045 */
3046
3047/** Set up a key derivation operation.
3048 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003049 * A key derivation algorithm takes some inputs and uses them to create
3050 * a byte generator which can be used to produce keys and other
3051 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003052 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003053 * To use a generator for key derivation:
3054 * - Start with an initialized object of type #psa_crypto_generator_t.
3055 * - Call psa_key_derivation_setup() to select the algorithm.
3056 * - Provide the inputs for the key derivation by calling
3057 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3058 * as appropriate. Which inputs are needed, in what order, and whether
3059 * they may be keys and if so of what type depends on the algorithm.
3060 * - Optionally set the generator's maximum capacity with
3061 * psa_set_generator_capacity(). You may do this before, in the middle of
3062 * or after providing inputs. For some algorithms, this step is mandatory
3063 * because the output depends on the maximum capacity.
3064 * - Generate output with psa_generator_read() or
3065 * psa_generator_import_key(). Successive calls to these functions
3066 * use successive output bytes from the generator.
3067 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003068 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003069 * \param[in,out] generator The generator object to set up. It must
3070 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003071 * \param alg The key derivation algorithm to compute
3072 * (\c PSA_ALG_XXX value such that
3073 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003074 *
3075 * \retval #PSA_SUCCESS
3076 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003077 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003078 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003079 * \retval #PSA_ERROR_NOT_SUPPORTED
3080 * \c alg is not supported or is not a key derivation algorithm.
3081 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3082 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3083 * \retval #PSA_ERROR_HARDWARE_FAILURE
3084 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003085 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003086 */
3087psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3088 psa_algorithm_t alg);
3089
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003090/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003091 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003092 * Which inputs are required and in what order depends on the algorithm.
3093 * Refer to the documentation of each key derivation or key agreement
3094 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003095 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003096 * This function passes direct inputs. Some inputs must be passed as keys
3097 * using psa_key_derivation_input_key() instead of this function. Refer to
3098 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003099 *
3100 * \param[in,out] generator The generator object to use. It must
3101 * have been set up with
3102 * psa_key_derivation_setup() and must not
3103 * have produced any output yet.
3104 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003105 * \param[in] data Input data to use.
3106 * \param data_length Size of the \p data buffer in bytes.
3107 *
3108 * \retval #PSA_SUCCESS
3109 * Success.
3110 * \retval #PSA_ERROR_INVALID_ARGUMENT
3111 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003112 * \retval #PSA_ERROR_INVALID_ARGUMENT
3113 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003114 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3115 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3116 * \retval #PSA_ERROR_HARDWARE_FAILURE
3117 * \retval #PSA_ERROR_TAMPERING_DETECTED
3118 * \retval #PSA_ERROR_BAD_STATE
3119 * The value of \p step is not valid given the state of \p generator.
3120 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003121 * The library has not been previously initialized by psa_crypto_init().
3122 * It is implementation-dependent whether a failure to initialize
3123 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003124 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003125psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3126 psa_key_derivation_step_t step,
3127 const uint8_t *data,
3128 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003129
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003130/** Provide an input for key derivation in the form of a key.
3131 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003132 * Which inputs are required and in what order depends on the algorithm.
3133 * Refer to the documentation of each key derivation or key agreement
3134 * algorithm for information.
3135 *
3136 * This function passes key inputs. Some inputs must be passed as keys
3137 * of the appropriate type using this function, while others must be
3138 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3139 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003140 *
3141 * \param[in,out] generator The generator object to use. It must
3142 * have been set up with
3143 * psa_key_derivation_setup() and must not
3144 * have produced any output yet.
3145 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003146 * \param handle Handle to the key. It must have an
3147 * appropriate type for \p step and must
3148 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003149 *
3150 * \retval #PSA_SUCCESS
3151 * Success.
3152 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003153 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003154 * \retval #PSA_ERROR_NOT_PERMITTED
3155 * \retval #PSA_ERROR_INVALID_ARGUMENT
3156 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003157 * \retval #PSA_ERROR_INVALID_ARGUMENT
3158 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003159 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3160 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3161 * \retval #PSA_ERROR_HARDWARE_FAILURE
3162 * \retval #PSA_ERROR_TAMPERING_DETECTED
3163 * \retval #PSA_ERROR_BAD_STATE
3164 * The value of \p step is not valid given the state of \p generator.
3165 * \retval #PSA_ERROR_BAD_STATE
3166 * The library has not been previously initialized by psa_crypto_init().
3167 * It is implementation-dependent whether a failure to initialize
3168 * results in this error code.
3169 */
3170psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3171 psa_key_derivation_step_t step,
3172 psa_key_handle_t handle);
3173
Gilles Peskine969c5d62019-01-16 15:53:06 +01003174/** Perform a key agreement and use the shared secret as input to a key
3175 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003176 *
3177 * A key agreement algorithm takes two inputs: a private key \p private_key
3178 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003179 * The result of this function is passed as input to a key derivation.
3180 * The output of this key derivation can be extracted by reading from the
3181 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003182 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003183 * \param[in,out] generator The generator object to use. It must
3184 * have been set up with
3185 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003186 * key agreement and derivation algorithm
3187 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003188 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3189 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003190 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003191 * The generator must be ready for an
3192 * input of the type given by \p step.
3193 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003194 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003195 * \param[in] peer_key Public key of the peer. The peer key must be in the
3196 * same format that psa_import_key() accepts for the
3197 * public key type corresponding to the type of
3198 * private_key. That is, this function performs the
3199 * equivalent of
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003200 * #psa_import_key(`internal_public_key_handle`,
3201 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(`private_key_type`),
3202 * `peer_key`, `peer_key_length`) where
Jaeden Amero8afbff82019-01-14 16:56:20 +00003203 * `private_key_type` is the type of `private_key`.
3204 * For example, for EC keys, this means that peer_key
3205 * is interpreted as a point on the curve that the
3206 * private key is on. The standard formats for public
3207 * keys are documented in the documentation of
3208 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003209 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003210 *
3211 * \retval #PSA_SUCCESS
3212 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003213 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003214 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003215 * \retval #PSA_ERROR_NOT_PERMITTED
3216 * \retval #PSA_ERROR_INVALID_ARGUMENT
3217 * \c private_key is not compatible with \c alg,
3218 * or \p peer_key is not valid for \c alg or not compatible with
3219 * \c private_key.
3220 * \retval #PSA_ERROR_NOT_SUPPORTED
3221 * \c alg is not supported or is not a key derivation algorithm.
3222 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3223 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3224 * \retval #PSA_ERROR_HARDWARE_FAILURE
3225 * \retval #PSA_ERROR_TAMPERING_DETECTED
3226 */
3227psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003228 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003229 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003230 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003231 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003232
Gilles Peskine769c7a62019-01-18 16:42:29 +01003233/** Perform a key agreement and use the shared secret as input to a key
3234 * derivation.
3235 *
3236 * A key agreement algorithm takes two inputs: a private key \p private_key
3237 * a public key \p peer_key.
3238 *
3239 * \warning The raw result of a key agreement algorithm such as finite-field
3240 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3241 * not be used directly as key material. It should instead be passed as
3242 * input to a key derivation algorithm. To chain a key agreement with
3243 * a key derivation, use psa_key_agreement() and other functions from
3244 * the key derivation and generator interface.
3245 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003246 * \param alg The key agreement algorithm to compute
3247 * (\c PSA_ALG_XXX value such that
3248 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3249 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003250 * \param private_key Handle to the private key to use.
3251 * \param[in] peer_key Public key of the peer. It must be
3252 * in the same format that psa_import_key()
3253 * accepts. The standard formats for public
3254 * keys are documented in the documentation
3255 * of psa_export_public_key().
3256 * \param peer_key_length Size of \p peer_key in bytes.
3257 * \param[out] output Buffer where the decrypted message is to
3258 * be written.
3259 * \param output_size Size of the \c output buffer in bytes.
3260 * \param[out] output_length On success, the number of bytes
3261 * that make up the returned output.
3262 *
3263 * \retval #PSA_SUCCESS
3264 * Success.
3265 * \retval #PSA_ERROR_INVALID_HANDLE
3266 * \retval #PSA_ERROR_EMPTY_SLOT
3267 * \retval #PSA_ERROR_NOT_PERMITTED
3268 * \retval #PSA_ERROR_INVALID_ARGUMENT
3269 * \p alg is not a key agreement algorithm
3270 * \retval #PSA_ERROR_INVALID_ARGUMENT
3271 * \p private_key is not compatible with \p alg,
3272 * or \p peer_key is not valid for \p alg or not compatible with
3273 * \p private_key.
3274 * \retval #PSA_ERROR_NOT_SUPPORTED
3275 * \p alg is not a supported key agreement algorithm.
3276 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3277 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3278 * \retval #PSA_ERROR_HARDWARE_FAILURE
3279 * \retval #PSA_ERROR_TAMPERING_DETECTED
3280 */
3281psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3282 psa_key_handle_t private_key,
3283 const uint8_t *peer_key,
3284 size_t peer_key_length,
3285 uint8_t *output,
3286 size_t output_size,
3287 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003288
3289/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003290
3291/** \defgroup random Random generation
3292 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003293 */
3294
3295/**
3296 * \brief Generate random bytes.
3297 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003298 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003299 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003300 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003301 *
3302 * \note To generate a key, use psa_generate_key() instead.
3303 *
3304 * \param[out] output Output buffer for the generated data.
3305 * \param output_size Number of bytes to generate and output.
3306 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003307 * \retval #PSA_SUCCESS
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003308 * \retval #PSA_ERROR_NOT_SUPPORTED
3309 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003310 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003311 * \retval #PSA_ERROR_HARDWARE_FAILURE
3312 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003313 * \retval #PSA_ERROR_BAD_STATE
3314 * The library has not been previously initialized by psa_crypto_init().
3315 * It is implementation-dependent whether a failure to initialize
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003316 * results in this error code.
3317 */
3318psa_status_t psa_generate_random(uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003319 size_t output_size);
3320
3321/** Extra parameters for RSA key generation.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003322 *
Gilles Peskine28538492018-07-11 17:34:00 +02003323 * You may pass a pointer to a structure of this type as the \c extra
3324 * parameter to psa_generate_key().
3325 */
3326typedef struct {
3327 uint32_t e; /**< Public exponent value. Default: 65537. */
3328} psa_generate_key_extra_rsa;
3329
3330/**
itayzafrir90d8c7a2018-09-12 11:44:52 +03003331 * \brief Generate a key or key pair.
itayzafrir18617092018-09-16 12:22:41 +03003332 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003333 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003334 * It must have been obtained by calling
3335 * psa_allocate_key() or psa_create_key() and must
3336 * not contain key material yet.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003337 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
3338 * \param bits Key size in bits.
3339 * \param[in] extra Extra parameters for key generation. The
3340 * interpretation of this parameter depends on
3341 * \p type. All types support \c NULL to use
3342 * default parameters. Implementation that support
3343 * the generation of vendor-specific key types
3344 * that allow extra parameters shall document
3345 * the format of these extra parameters and
3346 * the default values. For standard parameters,
3347 * the meaning of \p extra is as follows:
3348 * - For a symmetric key type (a type such
3349 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
Gilles Peskine53d991e2018-07-12 01:14:59 +02003350 * false), \p extra must be \c NULL.
3351 * - For an elliptic curve key type (a type
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003352 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
3353 * false), \p extra must be \c NULL.
3354 * - For an RSA key (\p type is
Gilles Peskinee59236f2018-01-27 23:32:46 +01003355 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3356 * optional #psa_generate_key_extra_rsa structure
3357 * specifying the public exponent. The
3358 * default public exponent used when \p extra
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003359 * is \c NULL is 65537.
Jaeden Amero1308fb52019-01-11 13:50:43 +00003360 * - For an DSA key (\p type is
3361 * #PSA_KEY_TYPE_DSA_KEYPAIR), \p extra is an
3362 * optional structure specifying the key domain
3363 * parameters. The key domain parameters can also be
3364 * provided by psa_set_key_domain_parameters(),
3365 * which documents the format of the structure.
Jaeden Amero8851c402019-01-11 14:20:03 +00003366 * - For a DH key (\p type is
3367 * #PSA_KEY_TYPE_DH_KEYPAIR), the \p extra is an
3368 * optional structure specifying the key domain
3369 * parameters. The key domain parameters can also be
3370 * provided by psa_set_key_domain_parameters(),
3371 * which documents the format of the structure.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003372 * \param extra_size Size of the buffer that \p extra
3373 * points to, in bytes. Note that if \p extra is
3374 * \c NULL then \p extra_size must be zero.
3375 *
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003376 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003377 * Success.
3378 * If the key is persistent, the key material and the key's metadata
3379 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003380 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003381 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskineae32aac2018-11-30 14:39:32 +01003382 * There is already a key in the specified slot.
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003383 * \retval #PSA_ERROR_NOT_SUPPORTED
3384 * \retval #PSA_ERROR_INVALID_ARGUMENT
3385 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003386 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3387 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3388 * \retval #PSA_ERROR_HARDWARE_FAILURE
3389 * \retval #PSA_ERROR_TAMPERING_DETECTED
3390 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003391 * The library has not been previously initialized by psa_crypto_init().
3392 * It is implementation-dependent whether a failure to initialize
3393 * results in this error code.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003394 */
Gilles Peskine87a5e562019-04-17 12:28:25 +02003395psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
3396 psa_key_handle_t *handle,
Gilles Peskinee59236f2018-01-27 23:32:46 +01003397 size_t bits,
3398 const void *extra,
3399 size_t extra_size);
3400
3401/**@}*/
3402
3403#ifdef __cplusplus
3404}
3405#endif
3406
3407/* The file "crypto_sizes.h" contains definitions for size calculation
3408 * macros whose definitions are implementation-specific. */
3409#include "crypto_sizes.h"
3410
3411/* The file "crypto_struct.h" contains definitions for
3412 * implementation-specific structs that are declared above. */
3413#include "crypto_struct.h"
3414
3415/* The file "crypto_extra.h" contains vendor-specific definitions. This
3416 * can include vendor-defined algorithms, extra functions, etc. */
3417#include "crypto_extra.h"
3418
3419#endif /* PSA_CRYPTO_H */