blob: 76d56ede53ec653aeea6d8d1181d8d86432fd568 [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
96/** \defgroup key_management Key management
97 * @{
98 */
99
Gilles Peskineae32aac2018-11-30 14:39:32 +0100100/** \brief Retrieve the lifetime of an open key.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100101 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100102 * \param handle Handle to query.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100103 * \param[out] lifetime On success, the lifetime value.
104 *
105 * \retval #PSA_SUCCESS
106 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100107 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100108 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
109 * \retval #PSA_ERROR_HARDWARE_FAILURE
110 * \retval #PSA_ERROR_TAMPERING_DETECTED
111 * \retval #PSA_ERROR_BAD_STATE
112 * The library has not been previously initialized by psa_crypto_init().
113 * It is implementation-dependent whether a failure to initialize
114 * results in this error code.
115 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100116psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100117 psa_key_lifetime_t *lifetime);
118
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100119
Gilles Peskinef535eb22018-11-30 14:08:36 +0100120/** Allocate a key slot for a transient key, i.e. a key which is only stored
121 * in volatile memory.
122 *
123 * The allocated key slot and its handle remain valid until the
124 * application calls psa_close_key() or psa_destroy_key() or until the
125 * application terminates.
126 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100127 * \param[out] handle On success, a handle to a volatile key slot.
128 *
129 * \retval #PSA_SUCCESS
130 * Success. The application can now use the value of `*handle`
131 * to access the newly allocated key slot.
132 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
133 * There was not enough memory, or the maximum number of key slots
134 * has been reached.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100135 */
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100136psa_status_t psa_allocate_key(psa_key_handle_t *handle);
Gilles Peskinef535eb22018-11-30 14:08:36 +0100137
138/** Open a handle to an existing persistent key.
139 *
140 * Open a handle to a key which was previously created with psa_create_key().
141 *
142 * \param lifetime The lifetime of the key. This designates a storage
143 * area where the key material is stored. This must not
144 * be #PSA_KEY_LIFETIME_VOLATILE.
145 * \param id The persistent identifier of the key.
146 * \param[out] handle On success, a handle to a key slot which contains
147 * the data and metadata loaded from the specified
148 * persistent location.
149 *
150 * \retval #PSA_SUCCESS
151 * Success. The application can now use the value of `*handle`
152 * to access the newly allocated key slot.
153 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
154 * \retval #PSA_ERROR_EMPTY_SLOT
155 * \retval #PSA_ERROR_INVALID_ARGUMENT
156 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
157 * \retval #PSA_ERROR_INVALID_ARGUMENT
158 * \p id is invalid for the specified lifetime.
159 * \retval #PSA_ERROR_NOT_SUPPORTED
160 * \p lifetime is not supported.
161 * \retval #PSA_ERROR_NOT_PERMITTED
162 * The specified key exists, but the application does not have the
163 * permission to access it. Note that this specification does not
164 * define any way to create such a key, but it may be possible
165 * through implementation-specific means.
166 */
167psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
168 psa_key_id_t id,
169 psa_key_handle_t *handle);
170
171/** Create a new persistent key slot.
172 *
173 * Create a new persistent key slot and return a handle to it. The handle
174 * remains valid until the application calls psa_close_key() or terminates.
175 * The application can open the key again with psa_open_key() until it
176 * removes the key by calling psa_destroy_key().
177 *
178 * \param lifetime The lifetime of the key. This designates a storage
179 * area where the key material is stored. This must not
180 * be #PSA_KEY_LIFETIME_VOLATILE.
181 * \param id The persistent identifier of the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100182 * \param[out] handle On success, a handle to the newly created key slot.
183 * When key material is later created in this key slot,
184 * it will be saved to the specified persistent location.
185 *
186 * \retval #PSA_SUCCESS
187 * Success. The application can now use the value of `*handle`
188 * to access the newly allocated key slot.
189 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
190 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
191 * \retval #PSA_ERROR_OCCUPIED_SLOT
192 * There is already a key with the identifier \p id in the storage
193 * area designated by \p lifetime.
194 * \retval #PSA_ERROR_INVALID_ARGUMENT
195 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
196 * \retval #PSA_ERROR_INVALID_ARGUMENT
197 * \p id is invalid for the specified lifetime.
198 * \retval #PSA_ERROR_NOT_SUPPORTED
199 * \p lifetime is not supported.
200 * \retval #PSA_ERROR_NOT_PERMITTED
201 * \p lifetime is valid, but the application does not have the
202 * permission to create a key there.
203 */
204psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
205 psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100206 psa_key_handle_t *handle);
207
208/** Close a key handle.
209 *
210 * If the handle designates a volatile key, destroy the key material and
211 * free all associated resources, just like psa_destroy_key().
212 *
213 * If the handle designates a persistent key, free all resources associated
214 * with the key in volatile memory. The key slot in persistent storage is
215 * not affected and can be opened again later with psa_open_key().
216 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100217 * If the key is currently in use in a multipart operation,
218 * the multipart operation is aborted.
219 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100220 * \param handle The key handle to close.
221 *
222 * \retval #PSA_SUCCESS
223 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100224 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100225 */
226psa_status_t psa_close_key(psa_key_handle_t handle);
227
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100228/**@}*/
229
230/** \defgroup import_export Key import and export
231 * @{
232 */
233
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100234/**
235 * \brief Import a key in binary format.
236 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100237 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100238 * documentation of psa_export_public_key() for the format of public keys
239 * and to the documentation of psa_export_key() for the format for
240 * other key types.
241 *
242 * This specification supports a single format for each key type.
243 * Implementations may support other formats as long as the standard
244 * format is supported. Implementations that support other formats
245 * should ensure that the formats are clearly unambiguous so as to
246 * minimize the risk that an invalid input is accidentally interpreted
247 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100248 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100249 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100250 * It must have been obtained by calling
251 * psa_allocate_key() or psa_create_key() and must
252 * not contain key material yet.
Gilles Peskinef7933932018-10-31 14:07:52 +0100253 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
254 * import, the key slot will contain a key of this type.
255 * \param[in] data Buffer containing the key data. The content of this
256 * buffer is interpreted according to \p type. It must
257 * contain the format described in the documentation
258 * of psa_export_key() or psa_export_public_key() for
259 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200260 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100261 *
Gilles Peskine28538492018-07-11 17:34:00 +0200262 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100263 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100264 * If the key is persistent, the key material and the key's metadata
265 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100266 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200267 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200268 * The key type or key size is not supported, either by the
269 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200270 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100271 * The key slot is invalid,
272 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200273 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200274 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200275 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
276 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
277 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100278 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200279 * \retval #PSA_ERROR_HARDWARE_FAILURE
280 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300281 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300282 * The library has not been previously initialized by psa_crypto_init().
283 * It is implementation-dependent whether a failure to initialize
284 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100285 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100286psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100287 psa_key_type_t type,
288 const uint8_t *data,
289 size_t data_length);
290
291/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100292 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200293 *
294 * This function destroys the content of the key slot from both volatile
295 * memory and, if applicable, non-volatile storage. Implementations shall
296 * make a best effort to ensure that any previous content of the slot is
297 * unrecoverable.
298 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100299 * This function also erases any metadata such as policies and frees all
300 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200301 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100302 * If the key is currently in use in a multipart operation,
303 * the multipart operation is aborted.
304 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100305 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 *
Gilles Peskine28538492018-07-11 17:34:00 +0200307 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200308 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200309 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200310 * The slot holds content and cannot be erased because it is
311 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100312 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200313 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200314 * There was an failure in communication with the cryptoprocessor.
315 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200316 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200317 * The storage is corrupted. Implementations shall make a best effort
318 * to erase key material even in this stage, however applications
319 * should be aware that it may be impossible to guarantee that the
320 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200321 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200322 * An unexpected condition which is not a storage corruption or
323 * a communication failure occurred. The cryptoprocessor may have
324 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300325 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300326 * The library has not been previously initialized by psa_crypto_init().
327 * It is implementation-dependent whether a failure to initialize
328 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100329 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100330psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100331
332/**
333 * \brief Get basic metadata about a key.
334 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100335 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200336 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100337 * This may be a null pointer, in which case the key type
338 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200339 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100340 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100341 * is not written.
342 *
Gilles Peskine28538492018-07-11 17:34:00 +0200343 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100344 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200345 * \retval #PSA_ERROR_EMPTY_SLOT
Gilles Peskineae32aac2018-11-30 14:39:32 +0100346 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200347 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
348 * \retval #PSA_ERROR_HARDWARE_FAILURE
349 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300350 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300351 * The library has not been previously initialized by psa_crypto_init().
352 * It is implementation-dependent whether a failure to initialize
353 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100354 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100355psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356 psa_key_type_t *type,
357 size_t *bits);
358
359/**
Jaeden Amero283dfd12019-01-11 12:06:22 +0000360 * \brief Set domain parameters for a key.
361 *
362 * Some key types require additional domain parameters to be set before import
363 * or generation of the key. The domain parameters can be set with this
364 * function or, for key generation, through the \c extra parameter of
365 * psa_generate_key().
366 *
367 * The format for the required domain parameters varies by the key type.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000368 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
369 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
370 * ```
371 * Dss-Parms ::= SEQUENCE {
372 * p INTEGER,
373 * q INTEGER,
374 * g INTEGER
375 * }
376 * ```
Jaeden Amero8851c402019-01-11 14:20:03 +0000377 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY), the
378 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
379 * ```
380 * DomainParameters ::= SEQUENCE {
381 * p INTEGER, -- odd prime, p=jq +1
382 * g INTEGER, -- generator, g
383 * q INTEGER, -- factor of p-1
384 * j INTEGER OPTIONAL, -- subgroup factor
385 * validationParms ValidationParms OPTIONAL
386 * }
387 * ValidationParms ::= SEQUENCE {
388 * seed BIT STRING,
389 * pgenCounter INTEGER
390 * }
391 * ```
Jaeden Amero283dfd12019-01-11 12:06:22 +0000392 *
Gilles Peskine3a74e002019-01-18 17:11:25 +0100393 * \param handle Handle to the slot where the key will be stored.
394 * This must be a valid slot for a key of the chosen
395 * type: it must have been obtained by calling
396 * psa_allocate_key() or psa_create_key() with the
397 * correct \p type and with a maximum size that is
398 * compatible with \p data. It must not contain
399 * key material yet.
400 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). When
401 * subsequently creating key material into \p handle,
402 * the type must be compatible.
Jaeden Amero283dfd12019-01-11 12:06:22 +0000403 * \param[in] data Buffer containing the key domain parameters. The content
404 * of this buffer is interpreted according to \p type. of
405 * psa_export_key() or psa_export_public_key() for the
406 * chosen type.
407 * \param data_length Size of the \p data buffer in bytes.
408 *
409 * \retval #PSA_SUCCESS
410 * \retval #PSA_ERROR_INVALID_HANDLE
411 * \retval #PSA_ERROR_OCCUPIED_SLOT
412 * There is already a key in the specified slot.
413 * \retval #PSA_ERROR_INVALID_ARGUMENT
414 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
415 * \retval #PSA_ERROR_HARDWARE_FAILURE
416 * \retval #PSA_ERROR_TAMPERING_DETECTED
417 * \retval #PSA_ERROR_BAD_STATE
418 * The library has not been previously initialized by psa_crypto_init().
419 * It is implementation-dependent whether a failure to initialize
420 * results in this error code.
421 */
422psa_status_t psa_set_key_domain_parameters(psa_key_handle_t handle,
Gilles Peskine3a74e002019-01-18 17:11:25 +0100423 psa_key_type_t type,
Jaeden Amero283dfd12019-01-11 12:06:22 +0000424 const uint8_t *data,
425 size_t data_length);
426
427/**
428 * \brief Get domain parameters for a key.
429 *
430 * Get the domain parameters for a key with this function, if any. The format
431 * of the domain parameters written to \p data is specified in the
432 * documentation for psa_set_key_domain_parameters().
433 *
434 * \param handle Handle to the key to get domain parameters from.
435 * \param[out] data On success, the key domain parameters.
436 * \param data_size Size of the \p data buffer in bytes.
437 * \param[out] data_length On success, the number of bytes
438 * that make up the key domain parameters data.
439 *
440 * \retval #PSA_SUCCESS
441 * \retval #PSA_ERROR_INVALID_HANDLE
442 * \retval #PSA_ERROR_EMPTY_SLOT
443 * There is no key in the specified slot.
444 * \retval #PSA_ERROR_INVALID_ARGUMENT
445 * \retval #PSA_ERROR_NOT_SUPPORTED
446 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
447 * \retval #PSA_ERROR_HARDWARE_FAILURE
448 * \retval #PSA_ERROR_TAMPERING_DETECTED
449 * \retval #PSA_ERROR_BAD_STATE
450 * The library has not been previously initialized by psa_crypto_init().
451 * It is implementation-dependent whether a failure to initialize
452 * results in this error code.
453 */
454psa_status_t psa_get_key_domain_parameters(psa_key_handle_t handle,
455 uint8_t *data,
456 size_t data_size,
457 size_t *data_length);
458
459/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100460 * \brief Export a key in binary format.
461 *
462 * The output of this function can be passed to psa_import_key() to
463 * create an equivalent object.
464 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100465 * If the implementation of psa_import_key() supports other formats
466 * beyond the format specified here, the output from psa_export_key()
467 * must use the representation specified here, not the original
468 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100469 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100470 * For standard key types, the output format is as follows:
471 *
472 * - For symmetric keys (including MAC keys), the format is the
473 * raw bytes of the key.
474 * - For DES, the key data consists of 8 bytes. The parity bits must be
475 * correct.
476 * - For Triple-DES, the format is the concatenation of the
477 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100478 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200479 * is the non-encrypted DER encoding of the representation defined by
480 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
481 * ```
482 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200483 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200484 * modulus INTEGER, -- n
485 * publicExponent INTEGER, -- e
486 * privateExponent INTEGER, -- d
487 * prime1 INTEGER, -- p
488 * prime2 INTEGER, -- q
489 * exponent1 INTEGER, -- d mod (p-1)
490 * exponent2 INTEGER, -- d mod (q-1)
491 * coefficient INTEGER, -- (inverse of q) mod p
492 * }
493 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000494 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
495 * representation of the private key `x` as a big-endian byte string. The
496 * length of the byte string is the private key size in bytes (leading zeroes
497 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200498 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100499 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100500 * a representation of the private value as a `ceiling(m/8)`-byte string
501 * where `m` is the bit size associated with the curve, i.e. the bit size
502 * of the order of the curve's coordinate field. This byte string is
503 * in little-endian order for Montgomery curves (curve types
504 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
505 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
506 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100507 * This is the content of the `privateKey` field of the `ECPrivateKey`
508 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000509 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
510 * format is the representation of the private key `x` as a big-endian byte
511 * string. The length of the byte string is the private key size in bytes
512 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200513 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
514 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100515 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100516 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200517 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200518 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200519 * \param[out] data_length On success, the number of bytes
520 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100521 *
Gilles Peskine28538492018-07-11 17:34:00 +0200522 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100523 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200524 * \retval #PSA_ERROR_EMPTY_SLOT
525 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100526 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200527 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
528 * The size of the \p data buffer is too small. You can determine a
529 * sufficient buffer size by calling
530 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
531 * where \c type is the key type
532 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200533 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
534 * \retval #PSA_ERROR_HARDWARE_FAILURE
535 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300536 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300537 * The library has not been previously initialized by psa_crypto_init().
538 * It is implementation-dependent whether a failure to initialize
539 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100540 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100541psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100542 uint8_t *data,
543 size_t data_size,
544 size_t *data_length);
545
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100546/**
547 * \brief Export a public key or the public part of a key pair in binary format.
548 *
549 * The output of this function can be passed to psa_import_key() to
550 * create an object that is equivalent to the public key.
551 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000552 * This specification supports a single format for each key type.
553 * Implementations may support other formats as long as the standard
554 * format is supported. Implementations that support other formats
555 * should ensure that the formats are clearly unambiguous so as to
556 * minimize the risk that an invalid input is accidentally interpreted
557 * according to a different format.
558 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000559 * For standard key types, the output format is as follows:
560 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
561 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
562 * ```
563 * RSAPublicKey ::= SEQUENCE {
564 * modulus INTEGER, -- n
565 * publicExponent INTEGER } -- e
566 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000567 * - For elliptic curve public keys (key types for which
568 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
569 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
570 * Let `m` be the bit size associated with the curve, i.e. the bit size of
571 * `q` for a curve over `F_q`. The representation consists of:
572 * - The byte 0x04;
573 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
574 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000575 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
576 * representation of the public key `y = g^x mod p` as a big-endian byte
577 * string. The length of the byte string is the length of the base prime `p`
578 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000579 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
580 * the format is the representation of the public key `y = g^x mod p` as a
581 * big-endian byte string. The length of the byte string is the length of the
582 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100583 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100584 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200585 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200586 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200587 * \param[out] data_length On success, the number of bytes
588 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100589 *
Gilles Peskine28538492018-07-11 17:34:00 +0200590 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100591 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200592 * \retval #PSA_ERROR_EMPTY_SLOT
593 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200594 * The key is neither a public key nor a key pair.
595 * \retval #PSA_ERROR_NOT_SUPPORTED
596 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
597 * The size of the \p data buffer is too small. You can determine a
598 * sufficient buffer size by calling
599 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
600 * where \c type is the key type
601 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200602 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
603 * \retval #PSA_ERROR_HARDWARE_FAILURE
604 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300605 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300606 * The library has not been previously initialized by psa_crypto_init().
607 * It is implementation-dependent whether a failure to initialize
608 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100609 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100610psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100611 uint8_t *data,
612 size_t data_size,
613 size_t *data_length);
614
615/**@}*/
616
617/** \defgroup policy Key policies
618 * @{
619 */
620
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100621/** The type of the key policy data structure.
622 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000623 * Before calling any function on a key policy, the application must initialize
624 * it by any of the following means:
625 * - Set the structure to all-bits-zero, for example:
626 * \code
627 * psa_key_policy_t policy;
628 * memset(&policy, 0, sizeof(policy));
629 * \endcode
630 * - Initialize the structure to logical zero values, for example:
631 * \code
632 * psa_key_policy_t policy = {0};
633 * \endcode
634 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
635 * for example:
636 * \code
637 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
638 * \endcode
639 * - Assign the result of the function psa_key_policy_init()
640 * to the structure, for example:
641 * \code
642 * psa_key_policy_t policy;
643 * policy = psa_key_policy_init();
644 * \endcode
645 *
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100646 * This is an implementation-defined \c struct. Applications should not
647 * make any assumptions about the content of this structure except
648 * as directed by the documentation of a specific implementation. */
649typedef struct psa_key_policy_s psa_key_policy_t;
650
Jaeden Amero70261c52019-01-04 11:47:20 +0000651/** \def PSA_KEY_POLICY_INIT
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200652 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000653 * This macro returns a suitable initializer for a key policy object of type
654 * #psa_key_policy_t.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200655 */
Jaeden Amero70261c52019-01-04 11:47:20 +0000656#ifdef __DOXYGEN_ONLY__
657/* This is an example definition for documentation purposes.
658 * Implementations should define a suitable value in `crypto_struct.h`.
659 */
660#define PSA_KEY_POLICY_INIT {0}
661#endif
662
663/** Return an initial value for a key policy that forbids all usage of the key.
664 */
665static psa_key_policy_t psa_key_policy_init(void);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100666
Gilles Peskine7e198532018-03-08 07:50:30 +0100667/** \brief Set the standard fields of a policy structure.
668 *
669 * Note that this function does not make any consistency check of the
670 * parameters. The values are only checked when applying the policy to
671 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200672 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000673 * \param[in,out] policy The key policy to modify. It must have been
674 * initialized as per the documentation for
675 * #psa_key_policy_t.
676 * \param usage The permitted uses for the key.
677 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100678 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100679void psa_key_policy_set_usage(psa_key_policy_t *policy,
680 psa_key_usage_t usage,
681 psa_algorithm_t alg);
682
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200683/** \brief Retrieve the usage field of a policy structure.
684 *
685 * \param[in] policy The policy object to query.
686 *
687 * \return The permitted uses for a key with this policy.
688 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200689psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100690
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200691/** \brief Retrieve the algorithm field of a policy structure.
692 *
693 * \param[in] policy The policy object to query.
694 *
695 * \return The permitted algorithm for a key with this policy.
696 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200697psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100698
699/** \brief Set the usage policy on a key slot.
700 *
701 * This function must be called on an empty key slot, before importing,
702 * generating or creating a key in the slot. Changing the policy of an
703 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100704 *
705 * Implementations may set restrictions on supported key policies
706 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200707 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100708 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200709 * \param[in] policy The policy object to query.
710 *
711 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100712 * Success.
713 * If the key is persistent, it is implementation-defined whether
714 * the policy has been saved to persistent storage. Implementations
715 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100716 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200717 * \retval #PSA_ERROR_OCCUPIED_SLOT
718 * \retval #PSA_ERROR_NOT_SUPPORTED
719 * \retval #PSA_ERROR_INVALID_ARGUMENT
720 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
721 * \retval #PSA_ERROR_HARDWARE_FAILURE
722 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300723 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300724 * The library has not been previously initialized by psa_crypto_init().
725 * It is implementation-dependent whether a failure to initialize
726 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100727 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100728psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100729 const psa_key_policy_t *policy);
730
Gilles Peskine7e198532018-03-08 07:50:30 +0100731/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200732 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100733 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200734 * \param[out] policy On success, the key's policy.
735 *
736 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100737 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200738 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
739 * \retval #PSA_ERROR_HARDWARE_FAILURE
740 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300741 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300742 * The library has not been previously initialized by psa_crypto_init().
743 * It is implementation-dependent whether a failure to initialize
744 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100745 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100746psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100747 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100748
749/**@}*/
750
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100751/** \defgroup hash Message digests
752 * @{
753 */
754
Gilles Peskine69647a42019-01-14 20:18:12 +0100755/** Calculate the hash (digest) of a message.
756 *
757 * \note To verify the hash of a message against an
758 * expected value, use psa_hash_compare() instead.
759 *
760 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
761 * such that #PSA_ALG_IS_HASH(\p alg) is true).
762 * \param[in] input Buffer containing the message to hash.
763 * \param input_length Size of the \p input buffer in bytes.
764 * \param[out] hash Buffer where the hash is to be written.
765 * \param hash_size Size of the \p hash buffer in bytes.
766 * \param[out] hash_length On success, the number of bytes
767 * that make up the hash value. This is always
768 * #PSA_HASH_SIZE(\c alg) where \c alg is the
769 * hash algorithm that is calculated.
770 *
771 * \retval #PSA_SUCCESS
772 * Success.
773 * \retval #PSA_ERROR_NOT_SUPPORTED
774 * \p alg is not supported or is not a hash algorithm.
775 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
776 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
777 * \retval #PSA_ERROR_HARDWARE_FAILURE
778 * \retval #PSA_ERROR_TAMPERING_DETECTED
779 */
780psa_status_t psa_hash_compute(psa_algorithm_t alg,
781 const uint8_t *input,
782 size_t input_length,
783 uint8_t *hash,
784 size_t hash_size,
785 size_t *hash_length);
786
787/** Calculate the hash (digest) of a message and compare it with a
788 * reference value.
789 *
790 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
791 * such that #PSA_ALG_IS_HASH(\p alg) is true).
792 * \param[in] input Buffer containing the message to hash.
793 * \param input_length Size of the \p input buffer in bytes.
794 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100795 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100796 *
797 * \retval #PSA_SUCCESS
798 * The expected hash is identical to the actual hash of the input.
799 * \retval #PSA_ERROR_INVALID_SIGNATURE
800 * The hash of the message was calculated successfully, but it
801 * differs from the expected hash.
802 * \retval #PSA_ERROR_NOT_SUPPORTED
803 * \p alg is not supported or is not a hash algorithm.
804 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
805 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
806 * \retval #PSA_ERROR_HARDWARE_FAILURE
807 * \retval #PSA_ERROR_TAMPERING_DETECTED
808 */
809psa_status_t psa_hash_compare(psa_algorithm_t alg,
810 const uint8_t *input,
811 size_t input_length,
812 const uint8_t *hash,
813 const size_t hash_length);
814
Gilles Peskine308b91d2018-02-08 09:47:44 +0100815/** The type of the state data structure for multipart hash operations.
816 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000817 * Before calling any function on a hash operation object, the application must
818 * initialize it by any of the following means:
819 * - Set the structure to all-bits-zero, for example:
820 * \code
821 * psa_hash_operation_t operation;
822 * memset(&operation, 0, sizeof(operation));
823 * \endcode
824 * - Initialize the structure to logical zero values, for example:
825 * \code
826 * psa_hash_operation_t operation = {0};
827 * \endcode
828 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
829 * for example:
830 * \code
831 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
832 * \endcode
833 * - Assign the result of the function psa_hash_operation_init()
834 * to the structure, for example:
835 * \code
836 * psa_hash_operation_t operation;
837 * operation = psa_hash_operation_init();
838 * \endcode
839 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100840 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100841 * make any assumptions about the content of this structure except
842 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100843typedef struct psa_hash_operation_s psa_hash_operation_t;
844
Jaeden Amero6a25b412019-01-04 11:47:44 +0000845/** \def PSA_HASH_OPERATION_INIT
846 *
847 * This macro returns a suitable initializer for a hash operation object
848 * of type #psa_hash_operation_t.
849 */
850#ifdef __DOXYGEN_ONLY__
851/* This is an example definition for documentation purposes.
852 * Implementations should define a suitable value in `crypto_struct.h`.
853 */
854#define PSA_HASH_OPERATION_INIT {0}
855#endif
856
857/** Return an initial value for a hash operation object.
858 */
859static psa_hash_operation_t psa_hash_operation_init(void);
860
Gilles Peskinef45adda2019-01-14 18:29:18 +0100861/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100862 *
863 * The sequence of operations to calculate a hash (message digest)
864 * is as follows:
865 * -# Allocate an operation object which will be passed to all the functions
866 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000867 * -# Initialize the operation object with one of the methods described in the
868 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200869 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100870 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100871 * of the message each time. The hash that is calculated is the hash
872 * of the concatenation of these messages in order.
873 * -# To calculate the hash, call psa_hash_finish().
874 * To compare the hash with an expected value, call psa_hash_verify().
875 *
876 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000877 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100878 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200879 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100880 * eventually terminate the operation. The following events terminate an
881 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100882 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100883 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100884 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000885 * \param[in,out] operation The operation object to set up. It must have
886 * been initialized as per the documentation for
887 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200888 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
889 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100890 *
Gilles Peskine28538492018-07-11 17:34:00 +0200891 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100892 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200893 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200894 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200895 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
896 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
897 * \retval #PSA_ERROR_HARDWARE_FAILURE
898 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100899 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200900psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100901 psa_algorithm_t alg);
902
Gilles Peskine308b91d2018-02-08 09:47:44 +0100903/** Add a message fragment to a multipart hash operation.
904 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200905 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100906 *
907 * If this function returns an error status, the operation becomes inactive.
908 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200909 * \param[in,out] operation Active hash operation.
910 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200911 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100912 *
Gilles Peskine28538492018-07-11 17:34:00 +0200913 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100914 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200915 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100916 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200917 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
918 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
919 * \retval #PSA_ERROR_HARDWARE_FAILURE
920 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100921 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100922psa_status_t psa_hash_update(psa_hash_operation_t *operation,
923 const uint8_t *input,
924 size_t input_length);
925
Gilles Peskine308b91d2018-02-08 09:47:44 +0100926/** Finish the calculation of the hash of a message.
927 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200928 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100929 * This function calculates the hash of the message formed by concatenating
930 * the inputs passed to preceding calls to psa_hash_update().
931 *
932 * When this function returns, the operation becomes inactive.
933 *
934 * \warning Applications should not call this function if they expect
935 * a specific value for the hash. Call psa_hash_verify() instead.
936 * Beware that comparing integrity or authenticity data such as
937 * hash values with a function such as \c memcmp is risky
938 * because the time taken by the comparison may leak information
939 * about the hashed data which could allow an attacker to guess
940 * a valid hash and thereby bypass security controls.
941 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200942 * \param[in,out] operation Active hash operation.
943 * \param[out] hash Buffer where the hash is to be written.
944 * \param hash_size Size of the \p hash buffer in bytes.
945 * \param[out] hash_length On success, the number of bytes
946 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200947 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200948 * hash algorithm that is calculated.
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_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100953 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200954 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200955 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200956 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100957 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200958 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
959 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
960 * \retval #PSA_ERROR_HARDWARE_FAILURE
961 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100962 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100963psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
964 uint8_t *hash,
965 size_t hash_size,
966 size_t *hash_length);
967
Gilles Peskine308b91d2018-02-08 09:47:44 +0100968/** Finish the calculation of the hash of a message and compare it with
969 * an expected value.
970 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200971 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100972 * This function calculates the hash of the message formed by concatenating
973 * the inputs passed to preceding calls to psa_hash_update(). It then
974 * compares the calculated hash with the expected hash passed as a
975 * parameter to this function.
976 *
977 * When this function returns, the operation becomes inactive.
978 *
Gilles Peskine19067982018-03-20 17:54:53 +0100979 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100980 * comparison between the actual hash and the expected hash is performed
981 * in constant time.
982 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200983 * \param[in,out] operation Active hash operation.
984 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200985 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100986 *
Gilles Peskine28538492018-07-11 17:34:00 +0200987 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100988 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200989 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100990 * The hash of the message was calculated successfully, but it
991 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200992 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100993 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200994 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
995 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
996 * \retval #PSA_ERROR_HARDWARE_FAILURE
997 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100998 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100999psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1000 const uint8_t *hash,
1001 size_t hash_length);
1002
Gilles Peskine308b91d2018-02-08 09:47:44 +01001003/** Abort a hash operation.
1004 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001005 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001006 * \p operation structure itself. Once aborted, the operation object
1007 * can be reused for another operation by calling
1008 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001009 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001010 * You may call this function any time after the operation object has
1011 * been initialized by any of the following methods:
1012 * - A call to psa_hash_setup(), whether it succeeds or not.
1013 * - Initializing the \c struct to all-bits-zero.
1014 * - Initializing the \c struct to logical zeros, e.g.
1015 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001016 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001017 * In particular, calling psa_hash_abort() after the operation has been
1018 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1019 * psa_hash_verify() is safe and has no effect.
1020 *
1021 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001022 *
Gilles Peskine28538492018-07-11 17:34:00 +02001023 * \retval #PSA_SUCCESS
1024 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001025 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001026 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1027 * \retval #PSA_ERROR_HARDWARE_FAILURE
1028 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001029 */
1030psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001031
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001032/** Clone a hash operation.
1033 *
1034 * \param[in] source_operation The active hash operation to clone.
1035 * \param[in,out] target_operation The operation object to set up.
1036 * It must be initialized but not active.
1037 *
1038 * \retval #PSA_SUCCESS
1039 * \retval #PSA_ERROR_BAD_STATE
1040 * \p source_operation is not an active hash operation.
1041 * \retval #PSA_ERROR_BAD_STATE
1042 * \p source_operation is active.
1043 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1044 * \retval #PSA_ERROR_HARDWARE_FAILURE
1045 * \retval #PSA_ERROR_TAMPERING_DETECTED
1046 */
1047psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1048 psa_hash_operation_t *target_operation);
1049
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001050/**@}*/
1051
Gilles Peskine8c9def32018-02-08 10:02:12 +01001052/** \defgroup MAC Message authentication codes
1053 * @{
1054 */
1055
Gilles Peskine69647a42019-01-14 20:18:12 +01001056/** Calculate the MAC (message authentication code) of a message.
1057 *
1058 * \note To verify the MAC of a message against an
1059 * expected value, use psa_mac_verify() instead.
1060 * Beware that comparing integrity or authenticity data such as
1061 * MAC values with a function such as \c memcmp is risky
1062 * because the time taken by the comparison may leak information
1063 * about the MAC value which could allow an attacker to guess
1064 * a valid MAC and thereby bypass security controls.
1065 *
1066 * \param handle Handle to the key to use for the operation.
1067 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1068 * such that #PSA_ALG_IS_MAC(alg) is true).
1069 * \param[in] input Buffer containing the input message.
1070 * \param input_length Size of the \p input buffer in bytes.
1071 * \param[out] mac Buffer where the MAC value is to be written.
1072 * \param mac_size Size of the \p mac buffer in bytes.
1073 * \param[out] mac_length On success, the number of bytes
1074 * that make up the mac value. This is always
1075 * #PSA_HASH_SIZE(\c alg) where \c alg is the
1076 * hash algorithm that is calculated.
1077 *
1078 * \retval #PSA_SUCCESS
1079 * Success.
1080 * \retval #PSA_ERROR_INVALID_HANDLE
1081 * \retval #PSA_ERROR_EMPTY_SLOT
1082 * \retval #PSA_ERROR_NOT_PERMITTED
1083 * \retval #PSA_ERROR_INVALID_ARGUMENT
1084 * \p key is not compatible with \p alg.
1085 * \retval #PSA_ERROR_NOT_SUPPORTED
1086 * \p alg is not supported or is not a MAC algorithm.
1087 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1088 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1089 * \retval #PSA_ERROR_HARDWARE_FAILURE
1090 * \retval #PSA_ERROR_TAMPERING_DETECTED
1091 * \retval #PSA_ERROR_BAD_STATE
1092 * The library has not been previously initialized by psa_crypto_init().
1093 * It is implementation-dependent whether a failure to initialize
1094 * results in this error code.
1095 */
1096psa_status_t psa_mac_compute(psa_key_handle_t handle,
1097 psa_algorithm_t alg,
1098 const uint8_t *input,
1099 size_t input_length,
1100 uint8_t *mac,
1101 size_t mac_size,
1102 size_t *mac_length);
1103
1104/** Calculate the MAC of a message and compare it with a reference value.
1105 *
1106 * \param handle Handle to the key to use for the operation.
1107 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1108 * such that #PSA_ALG_IS_MAC(alg) is true).
1109 * \param[in] input Buffer containing the input message.
1110 * \param input_length Size of the \p input buffer in bytes.
1111 * \param[out] mac Buffer containing the expected MAC value.
1112 * \param mac_length Size of the \p mac buffer in bytes.
1113 *
1114 * \retval #PSA_SUCCESS
1115 * The expected MAC is identical to the actual MAC of the input.
1116 * \retval #PSA_ERROR_INVALID_SIGNATURE
1117 * The MAC of the message was calculated successfully, but it
1118 * differs from the expected value.
1119 * \retval #PSA_ERROR_INVALID_HANDLE
1120 * \retval #PSA_ERROR_EMPTY_SLOT
1121 * \retval #PSA_ERROR_NOT_PERMITTED
1122 * \retval #PSA_ERROR_INVALID_ARGUMENT
1123 * \p key is not compatible with \p alg.
1124 * \retval #PSA_ERROR_NOT_SUPPORTED
1125 * \p alg is not supported or is not a MAC algorithm.
1126 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1127 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1128 * \retval #PSA_ERROR_HARDWARE_FAILURE
1129 * \retval #PSA_ERROR_TAMPERING_DETECTED
1130 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001131psa_status_t psa_mac_verify(psa_key_handle_t handle,
1132 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001133 const uint8_t *input,
1134 size_t input_length,
1135 const uint8_t *mac,
1136 const size_t mac_length);
1137
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001138/** The type of the state data structure for multipart MAC operations.
1139 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001140 * Before calling any function on a MAC operation object, the application must
1141 * initialize it by any of the following means:
1142 * - Set the structure to all-bits-zero, for example:
1143 * \code
1144 * psa_mac_operation_t operation;
1145 * memset(&operation, 0, sizeof(operation));
1146 * \endcode
1147 * - Initialize the structure to logical zero values, for example:
1148 * \code
1149 * psa_mac_operation_t operation = {0};
1150 * \endcode
1151 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1152 * for example:
1153 * \code
1154 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1155 * \endcode
1156 * - Assign the result of the function psa_mac_operation_init()
1157 * to the structure, for example:
1158 * \code
1159 * psa_mac_operation_t operation;
1160 * operation = psa_mac_operation_init();
1161 * \endcode
1162 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001163 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001164 * make any assumptions about the content of this structure except
1165 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001166typedef struct psa_mac_operation_s psa_mac_operation_t;
1167
Jaeden Amero769ce272019-01-04 11:48:03 +00001168/** \def PSA_MAC_OPERATION_INIT
1169 *
1170 * This macro returns a suitable initializer for a MAC operation object of type
1171 * #psa_mac_operation_t.
1172 */
1173#ifdef __DOXYGEN_ONLY__
1174/* This is an example definition for documentation purposes.
1175 * Implementations should define a suitable value in `crypto_struct.h`.
1176 */
1177#define PSA_MAC_OPERATION_INIT {0}
1178#endif
1179
1180/** Return an initial value for a MAC operation object.
1181 */
1182static psa_mac_operation_t psa_mac_operation_init(void);
1183
Gilles Peskinef45adda2019-01-14 18:29:18 +01001184/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001185 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001186 * This function sets up the calculation of the MAC
1187 * (message authentication code) of a byte string.
1188 * To verify the MAC of a message against an
1189 * expected value, use psa_mac_verify_setup() instead.
1190 *
1191 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001192 * -# Allocate an operation object which will be passed to all the functions
1193 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001194 * -# Initialize the operation object with one of the methods described in the
1195 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001196 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001197 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1198 * of the message each time. The MAC that is calculated is the MAC
1199 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001200 * -# At the end of the message, call psa_mac_sign_finish() to finish
1201 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001202 *
1203 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001204 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001205 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001206 * After a successful call to psa_mac_sign_setup(), the application must
1207 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001208 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001209 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001210 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001211 * \param[in,out] operation The operation object to set up. It must have
1212 * been initialized as per the documentation for
1213 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001214 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001215 * It must remain valid until the operation
1216 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001217 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1218 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001219 *
Gilles Peskine28538492018-07-11 17:34:00 +02001220 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001221 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001222 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001223 * \retval #PSA_ERROR_EMPTY_SLOT
1224 * \retval #PSA_ERROR_NOT_PERMITTED
1225 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001226 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001227 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001228 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001229 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1230 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1231 * \retval #PSA_ERROR_HARDWARE_FAILURE
1232 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001233 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001234 * The library has not been previously initialized by psa_crypto_init().
1235 * It is implementation-dependent whether a failure to initialize
1236 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001237 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001238psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001239 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001240 psa_algorithm_t alg);
1241
Gilles Peskinef45adda2019-01-14 18:29:18 +01001242/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001243 *
1244 * This function sets up the verification of the MAC
1245 * (message authentication code) of a byte string against an expected value.
1246 *
1247 * The sequence of operations to verify a MAC is as follows:
1248 * -# Allocate an operation object which will be passed to all the functions
1249 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001250 * -# Initialize the operation object with one of the methods described in the
1251 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001252 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001253 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1254 * of the message each time. The MAC that is calculated is the MAC
1255 * of the concatenation of these messages in order.
1256 * -# At the end of the message, call psa_mac_verify_finish() to finish
1257 * calculating the actual MAC of the message and verify it against
1258 * the expected value.
1259 *
1260 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001261 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001262 *
1263 * After a successful call to psa_mac_verify_setup(), the application must
1264 * eventually terminate the operation through one of the following methods:
1265 * - A failed call to psa_mac_update().
1266 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1267 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001268 * \param[in,out] operation The operation object to set up. It must have
1269 * been initialized as per the documentation for
1270 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001271 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001272 * It must remain valid until the operation
1273 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001274 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1275 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001276 *
Gilles Peskine28538492018-07-11 17:34:00 +02001277 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001278 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001279 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001280 * \retval #PSA_ERROR_EMPTY_SLOT
1281 * \retval #PSA_ERROR_NOT_PERMITTED
1282 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001283 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001284 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001285 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001286 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1287 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1288 * \retval #PSA_ERROR_HARDWARE_FAILURE
1289 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001290 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001291 * The library has not been previously initialized by psa_crypto_init().
1292 * It is implementation-dependent whether a failure to initialize
1293 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001294 */
1295psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001296 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001297 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001298
Gilles Peskinedcd14942018-07-12 00:30:52 +02001299/** Add a message fragment to a multipart MAC operation.
1300 *
1301 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1302 * before calling this function.
1303 *
1304 * If this function returns an error status, the operation becomes inactive.
1305 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001306 * \param[in,out] operation Active MAC operation.
1307 * \param[in] input Buffer containing the message fragment to add to
1308 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001309 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001310 *
1311 * \retval #PSA_SUCCESS
1312 * Success.
1313 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001314 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001315 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1316 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1317 * \retval #PSA_ERROR_HARDWARE_FAILURE
1318 * \retval #PSA_ERROR_TAMPERING_DETECTED
1319 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001320psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1321 const uint8_t *input,
1322 size_t input_length);
1323
Gilles Peskinedcd14942018-07-12 00:30:52 +02001324/** Finish the calculation of the MAC of a message.
1325 *
1326 * The application must call psa_mac_sign_setup() before calling this function.
1327 * This function calculates the MAC of the message formed by concatenating
1328 * the inputs passed to preceding calls to psa_mac_update().
1329 *
1330 * When this function returns, the operation becomes inactive.
1331 *
1332 * \warning Applications should not call this function if they expect
1333 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1334 * Beware that comparing integrity or authenticity data such as
1335 * MAC values with a function such as \c memcmp is risky
1336 * because the time taken by the comparison may leak information
1337 * about the MAC value which could allow an attacker to guess
1338 * a valid MAC and thereby bypass security controls.
1339 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001340 * \param[in,out] operation Active MAC operation.
1341 * \param[out] mac Buffer where the MAC value is to be written.
1342 * \param mac_size Size of the \p mac buffer in bytes.
1343 * \param[out] mac_length On success, the number of bytes
1344 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001345 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001346 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001347 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001348 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001349 *
1350 * \retval #PSA_SUCCESS
1351 * Success.
1352 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001353 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001354 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001355 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001356 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1357 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1358 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1359 * \retval #PSA_ERROR_HARDWARE_FAILURE
1360 * \retval #PSA_ERROR_TAMPERING_DETECTED
1361 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001362psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1363 uint8_t *mac,
1364 size_t mac_size,
1365 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001366
Gilles Peskinedcd14942018-07-12 00:30:52 +02001367/** Finish the calculation of the MAC of a message and compare it with
1368 * an expected value.
1369 *
1370 * The application must call psa_mac_verify_setup() before calling this function.
1371 * This function calculates the MAC of the message formed by concatenating
1372 * the inputs passed to preceding calls to psa_mac_update(). It then
1373 * compares the calculated MAC with the expected MAC passed as a
1374 * parameter to this function.
1375 *
1376 * When this function returns, the operation becomes inactive.
1377 *
1378 * \note Implementations shall make the best effort to ensure that the
1379 * comparison between the actual MAC and the expected MAC is performed
1380 * in constant time.
1381 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001382 * \param[in,out] operation Active MAC operation.
1383 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001384 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001385 *
1386 * \retval #PSA_SUCCESS
1387 * The expected MAC is identical to the actual MAC of the message.
1388 * \retval #PSA_ERROR_INVALID_SIGNATURE
1389 * The MAC of the message was calculated successfully, but it
1390 * differs from the expected MAC.
1391 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001392 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001393 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1394 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1395 * \retval #PSA_ERROR_HARDWARE_FAILURE
1396 * \retval #PSA_ERROR_TAMPERING_DETECTED
1397 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001398psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1399 const uint8_t *mac,
1400 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001401
Gilles Peskinedcd14942018-07-12 00:30:52 +02001402/** Abort a MAC operation.
1403 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001404 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001405 * \p operation structure itself. Once aborted, the operation object
1406 * can be reused for another operation by calling
1407 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001408 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001409 * You may call this function any time after the operation object has
1410 * been initialized by any of the following methods:
1411 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1412 * it succeeds or not.
1413 * - Initializing the \c struct to all-bits-zero.
1414 * - Initializing the \c struct to logical zeros, e.g.
1415 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001416 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001417 * In particular, calling psa_mac_abort() after the operation has been
1418 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1419 * psa_mac_verify_finish() is safe and has no effect.
1420 *
1421 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001422 *
1423 * \retval #PSA_SUCCESS
1424 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001425 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001426 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1427 * \retval #PSA_ERROR_HARDWARE_FAILURE
1428 * \retval #PSA_ERROR_TAMPERING_DETECTED
1429 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001430psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1431
1432/**@}*/
1433
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001434/** \defgroup cipher Symmetric ciphers
1435 * @{
1436 */
1437
Gilles Peskine69647a42019-01-14 20:18:12 +01001438/** Encrypt a message using a symmetric cipher.
1439 *
1440 * This function encrypts a message with a random IV (initialization
1441 * vector).
1442 *
1443 * \param handle Handle to the key to use for the operation.
1444 * It must remain valid until the operation
1445 * terminates.
1446 * \param alg The cipher algorithm to compute
1447 * (\c PSA_ALG_XXX value such that
1448 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1449 * \param[in] input Buffer containing the message to encrypt.
1450 * \param input_length Size of the \p input buffer in bytes.
1451 * \param[out] output Buffer where the output is to be written.
1452 * The output contains the IV followed by
1453 * the ciphertext proper.
1454 * \param output_size Size of the \p output buffer in bytes.
1455 * \param[out] output_length On success, the number of bytes
1456 * that make up the output.
1457 *
1458 * \retval #PSA_SUCCESS
1459 * Success.
1460 * \retval #PSA_ERROR_INVALID_HANDLE
1461 * \retval #PSA_ERROR_EMPTY_SLOT
1462 * \retval #PSA_ERROR_NOT_PERMITTED
1463 * \retval #PSA_ERROR_INVALID_ARGUMENT
1464 * \p key is not compatible with \p alg.
1465 * \retval #PSA_ERROR_NOT_SUPPORTED
1466 * \p alg is not supported or is not a cipher algorithm.
1467 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1468 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1469 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1470 * \retval #PSA_ERROR_HARDWARE_FAILURE
1471 * \retval #PSA_ERROR_TAMPERING_DETECTED
1472 */
1473psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1474 psa_algorithm_t alg,
1475 const uint8_t *input,
1476 size_t input_length,
1477 uint8_t *output,
1478 size_t output_size,
1479 size_t *output_length);
1480
1481/** Decrypt a message using a symmetric cipher.
1482 *
1483 * This function decrypts a message encrypted with a symmetric cipher.
1484 *
1485 * \param handle Handle to the key to use for the operation.
1486 * It must remain valid until the operation
1487 * terminates.
1488 * \param alg The cipher algorithm to compute
1489 * (\c PSA_ALG_XXX value such that
1490 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1491 * \param[in] input Buffer containing the message to decrypt.
1492 * This consists of the IV followed by the
1493 * ciphertext proper.
1494 * \param input_length Size of the \p input buffer in bytes.
1495 * \param[out] output Buffer where the plaintext is to be written.
1496 * \param output_size Size of the \p output buffer in bytes.
1497 * \param[out] output_length On success, the number of bytes
1498 * that make up the output.
1499 *
1500 * \retval #PSA_SUCCESS
1501 * Success.
1502 * \retval #PSA_ERROR_INVALID_HANDLE
1503 * \retval #PSA_ERROR_EMPTY_SLOT
1504 * \retval #PSA_ERROR_NOT_PERMITTED
1505 * \retval #PSA_ERROR_INVALID_ARGUMENT
1506 * \p key is not compatible with \p alg.
1507 * \retval #PSA_ERROR_NOT_SUPPORTED
1508 * \p alg is not supported or is not a cipher algorithm.
1509 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1510 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1511 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1512 * \retval #PSA_ERROR_HARDWARE_FAILURE
1513 * \retval #PSA_ERROR_TAMPERING_DETECTED
1514 */
1515psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1516 psa_algorithm_t alg,
1517 const uint8_t *input,
1518 size_t input_length,
1519 uint8_t *output,
1520 size_t output_size,
1521 size_t *output_length);
1522
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001523/** The type of the state data structure for multipart cipher operations.
1524 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001525 * Before calling any function on a cipher operation object, the application
1526 * must initialize it by any of the following means:
1527 * - Set the structure to all-bits-zero, for example:
1528 * \code
1529 * psa_cipher_operation_t operation;
1530 * memset(&operation, 0, sizeof(operation));
1531 * \endcode
1532 * - Initialize the structure to logical zero values, for example:
1533 * \code
1534 * psa_cipher_operation_t operation = {0};
1535 * \endcode
1536 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1537 * for example:
1538 * \code
1539 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1540 * \endcode
1541 * - Assign the result of the function psa_cipher_operation_init()
1542 * to the structure, for example:
1543 * \code
1544 * psa_cipher_operation_t operation;
1545 * operation = psa_cipher_operation_init();
1546 * \endcode
1547 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001548 * This is an implementation-defined \c struct. Applications should not
1549 * make any assumptions about the content of this structure except
1550 * as directed by the documentation of a specific implementation. */
1551typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1552
Jaeden Amero5bae2272019-01-04 11:48:27 +00001553/** \def PSA_CIPHER_OPERATION_INIT
1554 *
1555 * This macro returns a suitable initializer for a cipher operation object of
1556 * type #psa_cipher_operation_t.
1557 */
1558#ifdef __DOXYGEN_ONLY__
1559/* This is an example definition for documentation purposes.
1560 * Implementations should define a suitable value in `crypto_struct.h`.
1561 */
1562#define PSA_CIPHER_OPERATION_INIT {0}
1563#endif
1564
1565/** Return an initial value for a cipher operation object.
1566 */
1567static psa_cipher_operation_t psa_cipher_operation_init(void);
1568
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001569/** Set the key for a multipart symmetric encryption operation.
1570 *
1571 * The sequence of operations to encrypt a message with a symmetric cipher
1572 * is as follows:
1573 * -# Allocate an operation object which will be passed to all the functions
1574 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001575 * -# Initialize the operation object with one of the methods described in the
1576 * documentation for #psa_cipher_operation_t, e.g.
1577 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001578 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001579 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001580 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001581 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001582 * requires a specific IV value.
1583 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1584 * of the message each time.
1585 * -# Call psa_cipher_finish().
1586 *
1587 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001588 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001589 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001590 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001591 * eventually terminate the operation. The following events terminate an
1592 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001593 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001594 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001595 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001596 * \param[in,out] operation The operation object to set up. It must have
1597 * been initialized as per the documentation for
1598 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001599 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001600 * It must remain valid until the operation
1601 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001602 * \param alg The cipher algorithm to compute
1603 * (\c PSA_ALG_XXX value such that
1604 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001605 *
Gilles Peskine28538492018-07-11 17:34:00 +02001606 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001607 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001608 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001609 * \retval #PSA_ERROR_EMPTY_SLOT
1610 * \retval #PSA_ERROR_NOT_PERMITTED
1611 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001612 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001613 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001614 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001615 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1616 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1617 * \retval #PSA_ERROR_HARDWARE_FAILURE
1618 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001619 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001620 * The library has not been previously initialized by psa_crypto_init().
1621 * It is implementation-dependent whether a failure to initialize
1622 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001623 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001624psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001625 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001626 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001627
1628/** Set the key for a multipart symmetric decryption operation.
1629 *
1630 * The sequence of operations to decrypt a message with a symmetric cipher
1631 * is as follows:
1632 * -# Allocate an operation object which will be passed to all the functions
1633 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001634 * -# Initialize the operation object with one of the methods described in the
1635 * documentation for #psa_cipher_operation_t, e.g.
1636 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001637 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001638 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001639 * decryption. If the IV is prepended to the ciphertext, you can call
1640 * psa_cipher_update() on a buffer containing the IV followed by the
1641 * beginning of the message.
1642 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1643 * of the message each time.
1644 * -# Call psa_cipher_finish().
1645 *
1646 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001647 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001648 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001649 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001650 * eventually terminate the operation. The following events terminate an
1651 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001652 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001653 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001654 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001655 * \param[in,out] operation The operation object to set up. It must have
1656 * been initialized as per the documentation for
1657 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001658 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001659 * It must remain valid until the operation
1660 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001661 * \param alg The cipher algorithm to compute
1662 * (\c PSA_ALG_XXX value such that
1663 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001664 *
Gilles Peskine28538492018-07-11 17:34:00 +02001665 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001666 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001667 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001668 * \retval #PSA_ERROR_EMPTY_SLOT
1669 * \retval #PSA_ERROR_NOT_PERMITTED
1670 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001671 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001672 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001673 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001674 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1675 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1676 * \retval #PSA_ERROR_HARDWARE_FAILURE
1677 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001678 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001679 * The library has not been previously initialized by psa_crypto_init().
1680 * It is implementation-dependent whether a failure to initialize
1681 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001682 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001683psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001684 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001685 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001686
Gilles Peskinedcd14942018-07-12 00:30:52 +02001687/** Generate an IV for a symmetric encryption operation.
1688 *
1689 * This function generates a random IV (initialization vector), nonce
1690 * or initial counter value for the encryption operation as appropriate
1691 * for the chosen algorithm, key type and key size.
1692 *
1693 * The application must call psa_cipher_encrypt_setup() before
1694 * calling this function.
1695 *
1696 * If this function returns an error status, the operation becomes inactive.
1697 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001698 * \param[in,out] operation Active cipher operation.
1699 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001700 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001701 * \param[out] iv_length On success, the number of bytes of the
1702 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001703 *
1704 * \retval #PSA_SUCCESS
1705 * Success.
1706 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001707 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001708 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001709 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001710 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1711 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1712 * \retval #PSA_ERROR_HARDWARE_FAILURE
1713 * \retval #PSA_ERROR_TAMPERING_DETECTED
1714 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001715psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1716 unsigned char *iv,
1717 size_t iv_size,
1718 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001719
Gilles Peskinedcd14942018-07-12 00:30:52 +02001720/** Set the IV for a symmetric encryption or decryption operation.
1721 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001722 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001723 * or initial counter value for the encryption or decryption operation.
1724 *
1725 * The application must call psa_cipher_encrypt_setup() before
1726 * calling this function.
1727 *
1728 * If this function returns an error status, the operation becomes inactive.
1729 *
1730 * \note When encrypting, applications should use psa_cipher_generate_iv()
1731 * instead of this function, unless implementing a protocol that requires
1732 * a non-random IV.
1733 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001734 * \param[in,out] operation Active cipher operation.
1735 * \param[in] iv Buffer containing the IV to use.
1736 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001737 *
1738 * \retval #PSA_SUCCESS
1739 * Success.
1740 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001741 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001742 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001743 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001744 * or the chosen algorithm does not use an IV.
1745 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1746 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1747 * \retval #PSA_ERROR_HARDWARE_FAILURE
1748 * \retval #PSA_ERROR_TAMPERING_DETECTED
1749 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001750psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1751 const unsigned char *iv,
1752 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001753
Gilles Peskinedcd14942018-07-12 00:30:52 +02001754/** Encrypt or decrypt a message fragment in an active cipher operation.
1755 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001756 * Before calling this function, you must:
1757 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1758 * The choice of setup function determines whether this function
1759 * encrypts or decrypts its input.
1760 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1761 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001762 *
1763 * If this function returns an error status, the operation becomes inactive.
1764 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001765 * \param[in,out] operation Active cipher operation.
1766 * \param[in] input Buffer containing the message fragment to
1767 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001768 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001769 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001770 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001771 * \param[out] output_length On success, the number of bytes
1772 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001773 *
1774 * \retval #PSA_SUCCESS
1775 * Success.
1776 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001777 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001778 * not set, or already completed).
1779 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1780 * The size of the \p output buffer is too small.
1781 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1782 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1783 * \retval #PSA_ERROR_HARDWARE_FAILURE
1784 * \retval #PSA_ERROR_TAMPERING_DETECTED
1785 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001786psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1787 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001788 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001789 unsigned char *output,
1790 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001791 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001792
Gilles Peskinedcd14942018-07-12 00:30:52 +02001793/** Finish encrypting or decrypting a message in a cipher operation.
1794 *
1795 * The application must call psa_cipher_encrypt_setup() or
1796 * psa_cipher_decrypt_setup() before calling this function. The choice
1797 * of setup function determines whether this function encrypts or
1798 * decrypts its input.
1799 *
1800 * This function finishes the encryption or decryption of the message
1801 * formed by concatenating the inputs passed to preceding calls to
1802 * psa_cipher_update().
1803 *
1804 * When this function returns, the operation becomes inactive.
1805 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001806 * \param[in,out] operation Active cipher operation.
1807 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001808 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001809 * \param[out] output_length On success, the number of bytes
1810 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001811 *
1812 * \retval #PSA_SUCCESS
1813 * Success.
1814 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001815 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001816 * not set, or already completed).
1817 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1818 * The size of the \p output buffer is too small.
1819 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1820 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1821 * \retval #PSA_ERROR_HARDWARE_FAILURE
1822 * \retval #PSA_ERROR_TAMPERING_DETECTED
1823 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001824psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001825 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001826 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001827 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001828
Gilles Peskinedcd14942018-07-12 00:30:52 +02001829/** Abort a cipher operation.
1830 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001831 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001832 * \p operation structure itself. Once aborted, the operation object
1833 * can be reused for another operation by calling
1834 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001835 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001836 * You may call this function any time after the operation object has
1837 * been initialized by any of the following methods:
1838 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1839 * whether it succeeds or not.
1840 * - Initializing the \c struct to all-bits-zero.
1841 * - Initializing the \c struct to logical zeros, e.g.
1842 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001843 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001844 * In particular, calling psa_cipher_abort() after the operation has been
1845 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1846 * is safe and has no effect.
1847 *
1848 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001849 *
1850 * \retval #PSA_SUCCESS
1851 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001852 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001853 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1854 * \retval #PSA_ERROR_HARDWARE_FAILURE
1855 * \retval #PSA_ERROR_TAMPERING_DETECTED
1856 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001857psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1858
1859/**@}*/
1860
Gilles Peskine3b555712018-03-03 21:27:57 +01001861/** \defgroup aead Authenticated encryption with associated data (AEAD)
1862 * @{
1863 */
1864
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001865/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001866 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001867 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001868 * \param alg The AEAD algorithm to compute
1869 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001870 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001871 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001872 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001873 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001874 * but not encrypted.
1875 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001876 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001877 * encrypted.
1878 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001879 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001880 * encrypted data. The additional data is not
1881 * part of this output. For algorithms where the
1882 * encrypted data and the authentication tag
1883 * are defined as separate outputs, the
1884 * authentication tag is appended to the
1885 * encrypted data.
1886 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1887 * This must be at least
1888 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1889 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001890 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001891 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001892 *
Gilles Peskine28538492018-07-11 17:34:00 +02001893 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001894 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001895 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001896 * \retval #PSA_ERROR_EMPTY_SLOT
1897 * \retval #PSA_ERROR_NOT_PERMITTED
1898 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001899 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001900 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001901 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001902 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1903 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1904 * \retval #PSA_ERROR_HARDWARE_FAILURE
1905 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001906 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001907 * The library has not been previously initialized by psa_crypto_init().
1908 * It is implementation-dependent whether a failure to initialize
1909 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001910 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001911psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001912 psa_algorithm_t alg,
1913 const uint8_t *nonce,
1914 size_t nonce_length,
1915 const uint8_t *additional_data,
1916 size_t additional_data_length,
1917 const uint8_t *plaintext,
1918 size_t plaintext_length,
1919 uint8_t *ciphertext,
1920 size_t ciphertext_size,
1921 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001922
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001923/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001924 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001925 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001926 * \param alg The AEAD algorithm to compute
1927 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001928 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001929 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001930 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001931 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001932 * but not encrypted.
1933 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001934 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001935 * encrypted. For algorithms where the
1936 * encrypted data and the authentication tag
1937 * are defined as separate inputs, the buffer
1938 * must contain the encrypted data followed
1939 * by the authentication tag.
1940 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001941 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001942 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1943 * This must be at least
1944 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1945 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001946 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001947 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001948 *
Gilles Peskine28538492018-07-11 17:34:00 +02001949 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001950 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001951 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001952 * \retval #PSA_ERROR_EMPTY_SLOT
1953 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001954 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001955 * \retval #PSA_ERROR_NOT_PERMITTED
1956 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001957 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001958 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001959 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001960 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1961 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1962 * \retval #PSA_ERROR_HARDWARE_FAILURE
1963 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001964 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001965 * The library has not been previously initialized by psa_crypto_init().
1966 * It is implementation-dependent whether a failure to initialize
1967 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001968 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001969psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001970 psa_algorithm_t alg,
1971 const uint8_t *nonce,
1972 size_t nonce_length,
1973 const uint8_t *additional_data,
1974 size_t additional_data_length,
1975 const uint8_t *ciphertext,
1976 size_t ciphertext_length,
1977 uint8_t *plaintext,
1978 size_t plaintext_size,
1979 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001980
Gilles Peskine30a9e412019-01-14 18:36:12 +01001981/** The type of the state data structure for multipart AEAD operations.
1982 *
1983 * Before calling any function on an AEAD operation object, the application
1984 * must initialize it by any of the following means:
1985 * - Set the structure to all-bits-zero, for example:
1986 * \code
1987 * psa_aead_operation_t operation;
1988 * memset(&operation, 0, sizeof(operation));
1989 * \endcode
1990 * - Initialize the structure to logical zero values, for example:
1991 * \code
1992 * psa_aead_operation_t operation = {0};
1993 * \endcode
1994 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
1995 * for example:
1996 * \code
1997 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
1998 * \endcode
1999 * - Assign the result of the function psa_aead_operation_init()
2000 * to the structure, for example:
2001 * \code
2002 * psa_aead_operation_t operation;
2003 * operation = psa_aead_operation_init();
2004 * \endcode
2005 *
2006 * This is an implementation-defined \c struct. Applications should not
2007 * make any assumptions about the content of this structure except
2008 * as directed by the documentation of a specific implementation. */
2009typedef struct psa_aead_operation_s psa_aead_operation_t;
2010
2011/** \def PSA_AEAD_OPERATION_INIT
2012 *
2013 * This macro returns a suitable initializer for an AEAD operation object of
2014 * type #psa_aead_operation_t.
2015 */
2016#ifdef __DOXYGEN_ONLY__
2017/* This is an example definition for documentation purposes.
2018 * Implementations should define a suitable value in `crypto_struct.h`.
2019 */
2020#define PSA_AEAD_OPERATION_INIT {0}
2021#endif
2022
2023/** Return an initial value for an AEAD operation object.
2024 */
2025static psa_aead_operation_t psa_aead_operation_init(void);
2026
2027/** Set the key for a multipart authenticated encryption operation.
2028 *
2029 * The sequence of operations to encrypt a message with authentication
2030 * is as follows:
2031 * -# Allocate an operation object which will be passed to all the functions
2032 * listed here.
2033 * -# Initialize the operation object with one of the methods described in the
2034 * documentation for #psa_aead_operation_t, e.g.
2035 * PSA_AEAD_OPERATION_INIT.
2036 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002037 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2038 * inputs to the subsequent calls to psa_aead_update_ad() and
2039 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2040 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002041 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2042 * generate or set the nonce. You should use
2043 * psa_aead_generate_nonce() unless the protocol you are implementing
2044 * requires a specific nonce value.
2045 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2046 * of the non-encrypted additional authenticated data each time.
2047 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002048 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002049 * -# Call psa_aead_finish().
2050 *
2051 * The application may call psa_aead_abort() at any time after the operation
2052 * has been initialized.
2053 *
2054 * After a successful call to psa_aead_encrypt_setup(), the application must
2055 * eventually terminate the operation. The following events terminate an
2056 * operation:
2057 * - A failed call to any of the \c psa_aead_xxx functions.
2058 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2059 *
2060 * \param[in,out] operation The operation object to set up. It must have
2061 * been initialized as per the documentation for
2062 * #psa_aead_operation_t and not yet in use.
2063 * \param handle Handle to the key to use for the operation.
2064 * It must remain valid until the operation
2065 * terminates.
2066 * \param alg The AEAD algorithm to compute
2067 * (\c PSA_ALG_XXX value such that
2068 * #PSA_ALG_IS_AEAD(\p alg) is true).
2069 *
2070 * \retval #PSA_SUCCESS
2071 * Success.
2072 * \retval #PSA_ERROR_INVALID_HANDLE
2073 * \retval #PSA_ERROR_EMPTY_SLOT
2074 * \retval #PSA_ERROR_NOT_PERMITTED
2075 * \retval #PSA_ERROR_INVALID_ARGUMENT
2076 * \p key is not compatible with \p alg.
2077 * \retval #PSA_ERROR_NOT_SUPPORTED
2078 * \p alg is not supported or is not an AEAD algorithm.
2079 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2080 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2081 * \retval #PSA_ERROR_HARDWARE_FAILURE
2082 * \retval #PSA_ERROR_TAMPERING_DETECTED
2083 * \retval #PSA_ERROR_BAD_STATE
2084 * The library has not been previously initialized by psa_crypto_init().
2085 * It is implementation-dependent whether a failure to initialize
2086 * results in this error code.
2087 */
2088psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2089 psa_key_handle_t handle,
2090 psa_algorithm_t alg);
2091
2092/** Set the key for a multipart authenticated decryption operation.
2093 *
2094 * The sequence of operations to decrypt a message with authentication
2095 * is as follows:
2096 * -# Allocate an operation object which will be passed to all the functions
2097 * listed here.
2098 * -# Initialize the operation object with one of the methods described in the
2099 * documentation for #psa_aead_operation_t, e.g.
2100 * PSA_AEAD_OPERATION_INIT.
2101 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002102 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2103 * inputs to the subsequent calls to psa_aead_update_ad() and
2104 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2105 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002106 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2107 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2108 * of the non-encrypted additional authenticated data each time.
2109 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002110 * of the ciphertext to decrypt each time.
2111 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002112 *
2113 * The application may call psa_aead_abort() at any time after the operation
2114 * has been initialized.
2115 *
2116 * After a successful call to psa_aead_decrypt_setup(), the application must
2117 * eventually terminate the operation. The following events terminate an
2118 * operation:
2119 * - A failed call to any of the \c psa_aead_xxx functions.
2120 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2121 *
2122 * \param[in,out] operation The operation object to set up. It must have
2123 * been initialized as per the documentation for
2124 * #psa_aead_operation_t and not yet in use.
2125 * \param handle Handle to the key to use for the operation.
2126 * It must remain valid until the operation
2127 * terminates.
2128 * \param alg The AEAD algorithm to compute
2129 * (\c PSA_ALG_XXX value such that
2130 * #PSA_ALG_IS_AEAD(\p alg) is true).
2131 *
2132 * \retval #PSA_SUCCESS
2133 * Success.
2134 * \retval #PSA_ERROR_INVALID_HANDLE
2135 * \retval #PSA_ERROR_EMPTY_SLOT
2136 * \retval #PSA_ERROR_NOT_PERMITTED
2137 * \retval #PSA_ERROR_INVALID_ARGUMENT
2138 * \p key is not compatible with \p alg.
2139 * \retval #PSA_ERROR_NOT_SUPPORTED
2140 * \p alg is not supported or is not an AEAD algorithm.
2141 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2142 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2143 * \retval #PSA_ERROR_HARDWARE_FAILURE
2144 * \retval #PSA_ERROR_TAMPERING_DETECTED
2145 * \retval #PSA_ERROR_BAD_STATE
2146 * The library has not been previously initialized by psa_crypto_init().
2147 * It is implementation-dependent whether a failure to initialize
2148 * results in this error code.
2149 */
2150psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2151 psa_key_handle_t handle,
2152 psa_algorithm_t alg);
2153
2154/** Generate a random nonce for an authenticated encryption operation.
2155 *
2156 * This function generates a random nonce for the authenticated encryption
2157 * operation with an appropriate size for the chosen algorithm, key type
2158 * and key size.
2159 *
2160 * The application must call psa_aead_encrypt_setup() before
2161 * calling this function.
2162 *
2163 * If this function returns an error status, the operation becomes inactive.
2164 *
2165 * \param[in,out] operation Active AEAD operation.
2166 * \param[out] nonce Buffer where the generated nonce is to be
2167 * written.
2168 * \param nonce_size Size of the \p nonce buffer in bytes.
2169 * \param[out] nonce_length On success, the number of bytes of the
2170 * generated nonce.
2171 *
2172 * \retval #PSA_SUCCESS
2173 * Success.
2174 * \retval #PSA_ERROR_BAD_STATE
2175 * The operation state is not valid (not set up, or nonce already set).
2176 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2177 * The size of the \p nonce buffer is too small.
2178 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2179 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2180 * \retval #PSA_ERROR_HARDWARE_FAILURE
2181 * \retval #PSA_ERROR_TAMPERING_DETECTED
2182 */
2183psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2184 unsigned char *nonce,
2185 size_t nonce_size,
2186 size_t *nonce_length);
2187
2188/** Set the nonce for an authenticated encryption or decryption operation.
2189 *
2190 * This function sets the nonce for the authenticated
2191 * encryption or decryption operation.
2192 *
2193 * The application must call psa_aead_encrypt_setup() before
2194 * calling this function.
2195 *
2196 * If this function returns an error status, the operation becomes inactive.
2197 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002198 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002199 * instead of this function, unless implementing a protocol that requires
2200 * a non-random IV.
2201 *
2202 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002203 * \param[in] nonce Buffer containing the nonce to use.
2204 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002205 *
2206 * \retval #PSA_SUCCESS
2207 * Success.
2208 * \retval #PSA_ERROR_BAD_STATE
2209 * The operation state is not valid (not set up, or nonce already set).
2210 * \retval #PSA_ERROR_INVALID_ARGUMENT
2211 * The size of \p nonce is not acceptable for the chosen algorithm.
2212 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2213 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2214 * \retval #PSA_ERROR_HARDWARE_FAILURE
2215 * \retval #PSA_ERROR_TAMPERING_DETECTED
2216 */
2217psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2218 const unsigned char *nonce,
2219 size_t nonce_length);
2220
Gilles Peskinebc59c852019-01-17 15:26:08 +01002221/** Declare the lengths of the message and additional data for AEAD.
2222 *
2223 * The application must call this function before calling
2224 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2225 * the operation requires it. If the algorithm does not require it,
2226 * calling this function is optional, but if this function is called
2227 * then the implementation must enforce the lengths.
2228 *
2229 * You may call this function before or after setting the nonce with
2230 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2231 *
2232 * - For #PSA_ALG_CCM, calling this function is required.
2233 * - For the other AEAD algorithms defined in this specification, calling
2234 * this function is not required.
2235 * - For vendor-defined algorithm, refer to the vendor documentation.
2236 *
2237 * \param[in,out] operation Active AEAD operation.
2238 * \param ad_length Size of the non-encrypted additional
2239 * authenticated data in bytes.
2240 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2241 *
2242 * \retval #PSA_SUCCESS
2243 * Success.
2244 * \retval #PSA_ERROR_BAD_STATE
2245 * The operation state is not valid (not set up, already completed,
2246 * or psa_aead_update_ad() or psa_aead_update() already called).
2247 * \retval #PSA_ERROR_INVALID_ARGUMENT
2248 * At least one of the lengths is not acceptable for the chosen
2249 * algorithm.
2250 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2251 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2252 * \retval #PSA_ERROR_HARDWARE_FAILURE
2253 * \retval #PSA_ERROR_TAMPERING_DETECTED
2254 */
2255psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2256 size_t ad_length,
2257 size_t plaintext_length);
2258
Gilles Peskine30a9e412019-01-14 18:36:12 +01002259/** Pass additional data to an active AEAD operation.
2260 *
2261 * Additional data is authenticated, but not encrypted.
2262 *
2263 * You may call this function multiple times to pass successive fragments
2264 * of the additional data. You may not call this function after passing
2265 * data to encrypt or decrypt with psa_aead_update().
2266 *
2267 * Before calling this function, you must:
2268 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2269 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2270 *
2271 * If this function returns an error status, the operation becomes inactive.
2272 *
2273 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2274 * there is no guarantee that the input is valid. Therefore, until
2275 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2276 * treat the input as untrusted and prepare to undo any action that
2277 * depends on the input if psa_aead_verify() returns an error status.
2278 *
2279 * \param[in,out] operation Active AEAD operation.
2280 * \param[in] input Buffer containing the fragment of
2281 * additional data.
2282 * \param input_length Size of the \p input buffer in bytes.
2283 *
2284 * \retval #PSA_SUCCESS
2285 * Success.
2286 * \retval #PSA_ERROR_BAD_STATE
2287 * The operation state is not valid (not set up, nonce not set,
2288 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002289 * \retval #PSA_ERROR_INVALID_ARGUMENT
2290 * The total input length overflows the additional data length that
2291 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002292 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2293 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2294 * \retval #PSA_ERROR_HARDWARE_FAILURE
2295 * \retval #PSA_ERROR_TAMPERING_DETECTED
2296 */
2297psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2298 const uint8_t *input,
2299 size_t input_length);
2300
2301/** Encrypt or decrypt a message fragment in an active AEAD operation.
2302 *
2303 * Before calling this function, you must:
2304 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2305 * The choice of setup function determines whether this function
2306 * encrypts or decrypts its input.
2307 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2308 * 3. Call psa_aead_update_ad() to pass all the additional data.
2309 *
2310 * If this function returns an error status, the operation becomes inactive.
2311 *
2312 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2313 * there is no guarantee that the input is valid. Therefore, until
2314 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2315 * - Do not use the output in any way other than storing it in a
2316 * confidential location. If you take any action that depends
2317 * on the tentative decrypted data, this action will need to be
2318 * undone if the input turns out not to be valid. Furthermore,
2319 * if an adversary can observe that this action took place
2320 * (for example through timing), they may be able to use this
2321 * fact as an oracle to decrypt any message encrypted with the
2322 * same key.
2323 * - In particular, do not copy the output anywhere but to a
2324 * memory or storage space that you have exclusive access to.
2325 *
2326 * \param[in,out] operation Active AEAD operation.
2327 * \param[in] input Buffer containing the message fragment to
2328 * encrypt or decrypt.
2329 * \param input_length Size of the \p input buffer in bytes.
2330 * \param[out] output Buffer where the output is to be written.
2331 * \param output_size Size of the \p output buffer in bytes.
2332 * \param[out] output_length On success, the number of bytes
2333 * that make up the returned output.
2334 *
2335 * \retval #PSA_SUCCESS
2336 * Success.
2337 * \retval #PSA_ERROR_BAD_STATE
2338 * The operation state is not valid (not set up, nonce not set
2339 * or already completed).
2340 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2341 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002342 * \retval #PSA_ERROR_INVALID_ARGUMENT
2343 * The total length of input to psa_aead_update_ad() so far is
2344 * less than the additional data length that was previously
2345 * specified with psa_aead_set_lengths().
2346 * \retval #PSA_ERROR_INVALID_ARGUMENT
2347 * The total input length overflows the plaintext length that
2348 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002349 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2350 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2351 * \retval #PSA_ERROR_HARDWARE_FAILURE
2352 * \retval #PSA_ERROR_TAMPERING_DETECTED
2353 */
2354psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2355 const uint8_t *input,
2356 size_t input_length,
2357 unsigned char *output,
2358 size_t output_size,
2359 size_t *output_length);
2360
2361/** Finish encrypting a message in an AEAD operation.
2362 *
2363 * The operation must have been set up with psa_aead_encrypt_setup().
2364 *
2365 * This function finishes the authentication of the additional data
2366 * formed by concatenating the inputs passed to preceding calls to
2367 * psa_aead_update_ad() with the plaintext formed by concatenating the
2368 * inputs passed to preceding calls to psa_aead_update().
2369 *
2370 * This function has two output buffers:
2371 * - \p ciphertext contains trailing ciphertext that was buffered from
2372 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2373 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2374 * will not contain any output and can be a 0-sized buffer.
2375 * - \p tag contains the authentication tag. Its length is always
2376 * #PSA_AEAD_TAG_LENGTH(\p alg) where \p alg is the AEAD algorithm
2377 * that the operation performs.
2378 *
2379 * When this function returns, the operation becomes inactive.
2380 *
2381 * \param[in,out] operation Active AEAD operation.
2382 * \param[out] ciphertext Buffer where the last part of the ciphertext
2383 * is to be written.
2384 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2385 * \param[out] ciphertext_length On success, the number of bytes of
2386 * returned ciphertext.
2387 * \param[out] tag Buffer where the authentication tag is
2388 * to be written.
2389 * \param tag_size Size of the \p tag buffer in bytes.
2390 * \param[out] tag_length On success, the number of bytes
2391 * that make up the returned tag.
2392 *
2393 * \retval #PSA_SUCCESS
2394 * Success.
2395 * \retval #PSA_ERROR_BAD_STATE
2396 * The operation state is not valid (not set up, nonce not set,
2397 * decryption, or already completed).
2398 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2399 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002400 * \retval #PSA_ERROR_INVALID_ARGUMENT
2401 * The total length of input to psa_aead_update_ad() so far is
2402 * less than the additional data length that was previously
2403 * specified with psa_aead_set_lengths().
2404 * \retval #PSA_ERROR_INVALID_ARGUMENT
2405 * The total length of input to psa_aead_update() so far is
2406 * less than the plaintext length that was previously
2407 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002408 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2409 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2410 * \retval #PSA_ERROR_HARDWARE_FAILURE
2411 * \retval #PSA_ERROR_TAMPERING_DETECTED
2412 */
2413psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002414 uint8_t *ciphertext,
2415 size_t ciphertext_size,
2416 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002417 uint8_t *tag,
2418 size_t tag_size,
2419 size_t *tag_length);
2420
2421/** Finish authenticating and decrypting a message in an AEAD operation.
2422 *
2423 * The operation must have been set up with psa_aead_decrypt_setup().
2424 *
2425 * This function finishes the authentication of the additional data
2426 * formed by concatenating the inputs passed to preceding calls to
2427 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2428 * inputs passed to preceding calls to psa_aead_update().
2429 *
2430 * When this function returns, the operation becomes inactive.
2431 *
2432 * \param[in,out] operation Active AEAD operation.
2433 * \param[in] tag Buffer containing the authentication tag.
2434 * \param tag_length Size of the \p tag buffer in bytes.
2435 *
2436 * \retval #PSA_SUCCESS
2437 * Success.
2438 * \retval #PSA_ERROR_BAD_STATE
2439 * The operation state is not valid (not set up, nonce not set,
2440 * encryption, or already completed).
2441 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2442 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002443 * \retval #PSA_ERROR_INVALID_ARGUMENT
2444 * The total length of input to psa_aead_update_ad() so far is
2445 * less than the additional data length that was previously
2446 * specified with psa_aead_set_lengths().
2447 * \retval #PSA_ERROR_INVALID_ARGUMENT
2448 * The total length of input to psa_aead_update() so far is
2449 * less than the plaintext length that was previously
2450 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002451 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2452 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2453 * \retval #PSA_ERROR_HARDWARE_FAILURE
2454 * \retval #PSA_ERROR_TAMPERING_DETECTED
2455 */
2456psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2457 const uint8_t *tag,
2458 size_t tag_length);
2459
2460/** Abort an AEAD operation.
2461 *
2462 * Aborting an operation frees all associated resources except for the
2463 * \p operation structure itself. Once aborted, the operation object
2464 * can be reused for another operation by calling
2465 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2466 *
2467 * You may call this function any time after the operation object has
2468 * been initialized by any of the following methods:
2469 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2470 * whether it succeeds or not.
2471 * - Initializing the \c struct to all-bits-zero.
2472 * - Initializing the \c struct to logical zeros, e.g.
2473 * `psa_aead_operation_t operation = {0}`.
2474 *
2475 * In particular, calling psa_aead_abort() after the operation has been
2476 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2477 * is safe and has no effect.
2478 *
2479 * \param[in,out] operation Initialized AEAD operation.
2480 *
2481 * \retval #PSA_SUCCESS
2482 * \retval #PSA_ERROR_BAD_STATE
2483 * \p operation is not an active AEAD operation.
2484 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2485 * \retval #PSA_ERROR_HARDWARE_FAILURE
2486 * \retval #PSA_ERROR_TAMPERING_DETECTED
2487 */
2488psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2489
Gilles Peskine3b555712018-03-03 21:27:57 +01002490/**@}*/
2491
Gilles Peskine20035e32018-02-03 22:44:14 +01002492/** \defgroup asymmetric Asymmetric cryptography
2493 * @{
2494 */
2495
2496/**
2497 * \brief Sign a hash or short message with a private key.
2498 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002499 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002500 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002501 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2502 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2503 * to determine the hash algorithm to use.
2504 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002505 * \param handle Handle to the key to use for the operation.
2506 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002507 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002508 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002509 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002510 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002511 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002512 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002513 * \param[out] signature_length On success, the number of bytes
2514 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002515 *
Gilles Peskine28538492018-07-11 17:34:00 +02002516 * \retval #PSA_SUCCESS
2517 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002518 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002519 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002520 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002521 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002522 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002523 * \retval #PSA_ERROR_NOT_SUPPORTED
2524 * \retval #PSA_ERROR_INVALID_ARGUMENT
2525 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2526 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2527 * \retval #PSA_ERROR_HARDWARE_FAILURE
2528 * \retval #PSA_ERROR_TAMPERING_DETECTED
2529 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002530 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002531 * The library has not been previously initialized by psa_crypto_init().
2532 * It is implementation-dependent whether a failure to initialize
2533 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002534 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002535psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002536 psa_algorithm_t alg,
2537 const uint8_t *hash,
2538 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002539 uint8_t *signature,
2540 size_t signature_size,
2541 size_t *signature_length);
2542
2543/**
2544 * \brief Verify the signature a hash or short message using a public key.
2545 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002546 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002547 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002548 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2549 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2550 * to determine the hash algorithm to use.
2551 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002552 * \param handle Handle to the key to use for the operation.
2553 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002554 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002555 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002556 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002557 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002558 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002559 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002560 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002561 *
Gilles Peskine28538492018-07-11 17:34:00 +02002562 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002563 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002564 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002565 * The calculation was perfomed successfully, but the passed
2566 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002567 * \retval #PSA_ERROR_NOT_SUPPORTED
2568 * \retval #PSA_ERROR_INVALID_ARGUMENT
2569 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2570 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2571 * \retval #PSA_ERROR_HARDWARE_FAILURE
2572 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002573 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002574 * The library has not been previously initialized by psa_crypto_init().
2575 * It is implementation-dependent whether a failure to initialize
2576 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002577 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002578psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002579 psa_algorithm_t alg,
2580 const uint8_t *hash,
2581 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002582 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002583 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002584
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002585/**
2586 * \brief Encrypt a short message with a public key.
2587 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002588 * \param handle Handle to the key to use for the operation.
2589 * It must be a public key or an asymmetric
2590 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002591 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002592 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002593 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002594 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002595 * \param[in] salt A salt or label, if supported by the
2596 * encryption algorithm.
2597 * If the algorithm does not support a
2598 * salt, pass \c NULL.
2599 * If the algorithm supports an optional
2600 * salt and you do not want to pass a salt,
2601 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002602 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002603 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2604 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002605 * \param salt_length Size of the \p salt buffer in bytes.
2606 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002607 * \param[out] output Buffer where the encrypted message is to
2608 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002609 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002610 * \param[out] output_length On success, the number of bytes
2611 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002612 *
Gilles Peskine28538492018-07-11 17:34:00 +02002613 * \retval #PSA_SUCCESS
2614 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002615 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002616 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002617 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002618 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002619 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002620 * \retval #PSA_ERROR_NOT_SUPPORTED
2621 * \retval #PSA_ERROR_INVALID_ARGUMENT
2622 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2623 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2624 * \retval #PSA_ERROR_HARDWARE_FAILURE
2625 * \retval #PSA_ERROR_TAMPERING_DETECTED
2626 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002627 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002628 * The library has not been previously initialized by psa_crypto_init().
2629 * It is implementation-dependent whether a failure to initialize
2630 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002631 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002632psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002633 psa_algorithm_t alg,
2634 const uint8_t *input,
2635 size_t input_length,
2636 const uint8_t *salt,
2637 size_t salt_length,
2638 uint8_t *output,
2639 size_t output_size,
2640 size_t *output_length);
2641
2642/**
2643 * \brief Decrypt a short message with a private key.
2644 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002645 * \param handle Handle to the key to use for the operation.
2646 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002647 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002648 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002649 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002650 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002651 * \param[in] salt A salt or label, if supported by the
2652 * encryption algorithm.
2653 * If the algorithm does not support a
2654 * salt, pass \c NULL.
2655 * If the algorithm supports an optional
2656 * salt and you do not want to pass a salt,
2657 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002658 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002659 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2660 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002661 * \param salt_length Size of the \p salt buffer in bytes.
2662 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002663 * \param[out] output Buffer where the decrypted message is to
2664 * be written.
2665 * \param output_size Size of the \c output buffer in bytes.
2666 * \param[out] output_length On success, the number of bytes
2667 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002668 *
Gilles Peskine28538492018-07-11 17:34:00 +02002669 * \retval #PSA_SUCCESS
2670 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002671 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002672 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002673 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002674 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002675 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002676 * \retval #PSA_ERROR_NOT_SUPPORTED
2677 * \retval #PSA_ERROR_INVALID_ARGUMENT
2678 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2679 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2680 * \retval #PSA_ERROR_HARDWARE_FAILURE
2681 * \retval #PSA_ERROR_TAMPERING_DETECTED
2682 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2683 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002684 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002685 * The library has not been previously initialized by psa_crypto_init().
2686 * It is implementation-dependent whether a failure to initialize
2687 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002688 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002689psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002690 psa_algorithm_t alg,
2691 const uint8_t *input,
2692 size_t input_length,
2693 const uint8_t *salt,
2694 size_t salt_length,
2695 uint8_t *output,
2696 size_t output_size,
2697 size_t *output_length);
2698
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002699/**@}*/
2700
Gilles Peskineedd76872018-07-20 17:42:05 +02002701/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002702 * @{
2703 */
2704
2705/** The type of the state data structure for generators.
2706 *
2707 * Before calling any function on a generator, the application must
2708 * initialize it by any of the following means:
2709 * - Set the structure to all-bits-zero, for example:
2710 * \code
2711 * psa_crypto_generator_t generator;
2712 * memset(&generator, 0, sizeof(generator));
2713 * \endcode
2714 * - Initialize the structure to logical zero values, for example:
2715 * \code
2716 * psa_crypto_generator_t generator = {0};
2717 * \endcode
2718 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2719 * for example:
2720 * \code
2721 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2722 * \endcode
2723 * - Assign the result of the function psa_crypto_generator_init()
2724 * to the structure, for example:
2725 * \code
2726 * psa_crypto_generator_t generator;
2727 * generator = psa_crypto_generator_init();
2728 * \endcode
2729 *
2730 * This is an implementation-defined \c struct. Applications should not
2731 * make any assumptions about the content of this structure except
2732 * as directed by the documentation of a specific implementation.
2733 */
2734typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2735
2736/** \def PSA_CRYPTO_GENERATOR_INIT
2737 *
2738 * This macro returns a suitable initializer for a generator object
2739 * of type #psa_crypto_generator_t.
2740 */
2741#ifdef __DOXYGEN_ONLY__
2742/* This is an example definition for documentation purposes.
2743 * Implementations should define a suitable value in `crypto_struct.h`.
2744 */
2745#define PSA_CRYPTO_GENERATOR_INIT {0}
2746#endif
2747
2748/** Return an initial value for a generator object.
2749 */
2750static psa_crypto_generator_t psa_crypto_generator_init(void);
2751
2752/** Retrieve the current capacity of a generator.
2753 *
2754 * The capacity of a generator is the maximum number of bytes that it can
2755 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2756 *
2757 * \param[in] generator The generator to query.
2758 * \param[out] capacity On success, the capacity of the generator.
2759 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002760 * \retval #PSA_SUCCESS
2761 * \retval #PSA_ERROR_BAD_STATE
2762 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002763 */
2764psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2765 size_t *capacity);
2766
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002767/** Set the maximum capacity of a generator.
2768 *
2769 * \param[in,out] generator The generator object to modify.
2770 * \param capacity The new capacity of the generator.
2771 * It must be less or equal to the generator's
2772 * current capacity.
2773 *
2774 * \retval #PSA_SUCCESS
2775 * \retval #PSA_ERROR_INVALID_ARGUMENT
2776 * \p capacity is larger than the generator's current capacity.
2777 * \retval #PSA_ERROR_BAD_STATE
2778 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2779 */
2780psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2781 size_t capacity);
2782
Gilles Peskineeab56e42018-07-12 17:12:33 +02002783/** Read some data from a generator.
2784 *
2785 * This function reads and returns a sequence of bytes from a generator.
2786 * The data that is read is discarded from the generator. The generator's
2787 * capacity is decreased by the number of bytes read.
2788 *
2789 * \param[in,out] generator The generator object to read from.
2790 * \param[out] output Buffer where the generator output will be
2791 * written.
2792 * \param output_length Number of bytes to output.
2793 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002794 * \retval #PSA_SUCCESS
2795 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002796 * There were fewer than \p output_length bytes
2797 * in the generator. Note that in this case, no
2798 * output is written to the output buffer.
2799 * The generator's capacity is set to 0, thus
2800 * subsequent calls to this function will not
2801 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002802 * \retval #PSA_ERROR_BAD_STATE
2803 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2804 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2805 * \retval #PSA_ERROR_HARDWARE_FAILURE
2806 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002807 */
2808psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2809 uint8_t *output,
2810 size_t output_length);
2811
2812/** Create a symmetric key from data read from a generator.
2813 *
2814 * This function reads a sequence of bytes from a generator and imports
2815 * these bytes as a key.
2816 * The data that is read is discarded from the generator. The generator's
2817 * capacity is decreased by the number of bytes read.
2818 *
2819 * This function is equivalent to calling #psa_generator_read and
2820 * passing the resulting output to #psa_import_key, but
2821 * if the implementation provides an isolation boundary then
2822 * the key material is not exposed outside the isolation boundary.
2823 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002824 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002825 * It must have been obtained by calling
2826 * psa_allocate_key() or psa_create_key() and must
2827 * not contain key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002828 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2829 * This must be a symmetric key type.
2830 * \param bits Key size in bits.
2831 * \param[in,out] generator The generator object to read from.
2832 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002833 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002834 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002835 * If the key is persistent, the key material and the key's metadata
2836 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002837 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002838 * There were fewer than \p output_length bytes
2839 * in the generator. Note that in this case, no
2840 * output is written to the output buffer.
2841 * The generator's capacity is set to 0, thus
2842 * subsequent calls to this function will not
2843 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002844 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002845 * The key type or key size is not supported, either by the
2846 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002847 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002848 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002849 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002850 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002851 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2852 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2853 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2854 * \retval #PSA_ERROR_HARDWARE_FAILURE
2855 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002856 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002857 * The library has not been previously initialized by psa_crypto_init().
2858 * It is implementation-dependent whether a failure to initialize
2859 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002860 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002861psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002862 psa_key_type_t type,
2863 size_t bits,
2864 psa_crypto_generator_t *generator);
2865
2866/** Abort a generator.
2867 *
2868 * Once a generator has been aborted, its capacity is zero.
2869 * Aborting a generator frees all associated resources except for the
2870 * \c generator structure itself.
2871 *
2872 * This function may be called at any time as long as the generator
2873 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2874 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2875 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2876 * on a generator that has not been set up.
2877 *
2878 * Once aborted, the generator object may be called.
2879 *
2880 * \param[in,out] generator The generator to abort.
2881 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002882 * \retval #PSA_SUCCESS
2883 * \retval #PSA_ERROR_BAD_STATE
2884 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2885 * \retval #PSA_ERROR_HARDWARE_FAILURE
2886 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002887 */
2888psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2889
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002890/** Use the maximum possible capacity for a generator.
2891 *
2892 * Use this value as the capacity argument when setting up a generator
2893 * to indicate that the generator should have the maximum possible capacity.
2894 * The value of the maximum possible capacity depends on the generator
2895 * algorithm.
2896 */
2897#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2898
Gilles Peskineeab56e42018-07-12 17:12:33 +02002899/**@}*/
2900
Gilles Peskineea0fb492018-07-12 17:17:20 +02002901/** \defgroup derivation Key derivation
2902 * @{
2903 */
2904
2905/** Set up a key derivation operation.
2906 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002907 * A key derivation algorithm takes some inputs and uses them to create
2908 * a byte generator which can be used to produce keys and other
2909 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002910 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002911 * To use a generator for key derivation:
2912 * - Start with an initialized object of type #psa_crypto_generator_t.
2913 * - Call psa_key_derivation_setup() to select the algorithm.
2914 * - Provide the inputs for the key derivation by calling
2915 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2916 * as appropriate. Which inputs are needed, in what order, and whether
2917 * they may be keys and if so of what type depends on the algorithm.
2918 * - Optionally set the generator's maximum capacity with
2919 * psa_set_generator_capacity(). You may do this before, in the middle of
2920 * or after providing inputs. For some algorithms, this step is mandatory
2921 * because the output depends on the maximum capacity.
2922 * - Generate output with psa_generator_read() or
2923 * psa_generator_import_key(). Successive calls to these functions
2924 * use successive output bytes from the generator.
2925 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02002926 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002927 * \param[in,out] generator The generator object to set up. It must
2928 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002929 * \param alg The key derivation algorithm to compute
2930 * (\c PSA_ALG_XXX value such that
2931 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02002932 *
2933 * \retval #PSA_SUCCESS
2934 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002935 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002936 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002937 * \retval #PSA_ERROR_NOT_SUPPORTED
2938 * \c alg is not supported or is not a key derivation algorithm.
2939 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2940 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2941 * \retval #PSA_ERROR_HARDWARE_FAILURE
2942 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002943 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002944 */
2945psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
2946 psa_algorithm_t alg);
2947
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002948/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002949 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002950 * Which inputs are required and in what order depends on the algorithm.
2951 * Refer to the documentation of each key derivation or key agreement
2952 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002953 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002954 * This function passes direct inputs. Some inputs must be passed as keys
2955 * using psa_key_derivation_input_key() instead of this function. Refer to
2956 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002957 *
2958 * \param[in,out] generator The generator object to use. It must
2959 * have been set up with
2960 * psa_key_derivation_setup() and must not
2961 * have produced any output yet.
2962 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002963 * \param[in] data Input data to use.
2964 * \param data_length Size of the \p data buffer in bytes.
2965 *
2966 * \retval #PSA_SUCCESS
2967 * Success.
2968 * \retval #PSA_ERROR_INVALID_ARGUMENT
2969 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002970 * \retval #PSA_ERROR_INVALID_ARGUMENT
2971 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002972 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2973 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2974 * \retval #PSA_ERROR_HARDWARE_FAILURE
2975 * \retval #PSA_ERROR_TAMPERING_DETECTED
2976 * \retval #PSA_ERROR_BAD_STATE
2977 * The value of \p step is not valid given the state of \p generator.
2978 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002979 * The library has not been previously initialized by psa_crypto_init().
2980 * It is implementation-dependent whether a failure to initialize
2981 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002982 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002983psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
2984 psa_key_derivation_step_t step,
2985 const uint8_t *data,
2986 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02002987
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002988/** Provide an input for key derivation in the form of a key.
2989 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002990 * Which inputs are required and in what order depends on the algorithm.
2991 * Refer to the documentation of each key derivation or key agreement
2992 * algorithm for information.
2993 *
2994 * This function passes key inputs. Some inputs must be passed as keys
2995 * of the appropriate type using this function, while others must be
2996 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
2997 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002998 *
2999 * \param[in,out] generator The generator object to use. It must
3000 * have been set up with
3001 * psa_key_derivation_setup() and must not
3002 * have produced any output yet.
3003 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003004 * \param handle Handle to the key. It must have an
3005 * appropriate type for \p step and must
3006 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003007 *
3008 * \retval #PSA_SUCCESS
3009 * Success.
3010 * \retval #PSA_ERROR_INVALID_HANDLE
3011 * \retval #PSA_ERROR_EMPTY_SLOT
3012 * \retval #PSA_ERROR_NOT_PERMITTED
3013 * \retval #PSA_ERROR_INVALID_ARGUMENT
3014 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003015 * \retval #PSA_ERROR_INVALID_ARGUMENT
3016 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003017 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3018 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3019 * \retval #PSA_ERROR_HARDWARE_FAILURE
3020 * \retval #PSA_ERROR_TAMPERING_DETECTED
3021 * \retval #PSA_ERROR_BAD_STATE
3022 * The value of \p step is not valid given the state of \p generator.
3023 * \retval #PSA_ERROR_BAD_STATE
3024 * The library has not been previously initialized by psa_crypto_init().
3025 * It is implementation-dependent whether a failure to initialize
3026 * results in this error code.
3027 */
3028psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3029 psa_key_derivation_step_t step,
3030 psa_key_handle_t handle);
3031
Gilles Peskine969c5d62019-01-16 15:53:06 +01003032/** Perform a key agreement and use the shared secret as input to a key
3033 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003034 *
3035 * A key agreement algorithm takes two inputs: a private key \p private_key
3036 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003037 * The result of this function is passed as input to a key derivation.
3038 * The output of this key derivation can be extracted by reading from the
3039 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003040 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003041 * \param[in,out] generator The generator object to use. It must
3042 * have been set up with
3043 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003044 * key agreement and derivation algorithm
3045 * \c alg (\c PSA_ALG_XXX value such that
3046 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true
3047 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3048 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003049 * The generator must be ready for an
3050 * input of the type given by \p step.
3051 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003052 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003053 * \param[in] peer_key Public key of the peer. The peer key must be in the
3054 * same format that psa_import_key() accepts for the
3055 * public key type corresponding to the type of
3056 * private_key. That is, this function performs the
3057 * equivalent of
3058 * `psa_import_key(internal_public_key_handle,
3059 * PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(private_key_type),
3060 * peer_key, peer_key_length)` where
3061 * `private_key_type` is the type of `private_key`.
3062 * For example, for EC keys, this means that peer_key
3063 * is interpreted as a point on the curve that the
3064 * private key is on. The standard formats for public
3065 * keys are documented in the documentation of
3066 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003067 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003068 *
3069 * \retval #PSA_SUCCESS
3070 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003071 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02003072 * \retval #PSA_ERROR_EMPTY_SLOT
3073 * \retval #PSA_ERROR_NOT_PERMITTED
3074 * \retval #PSA_ERROR_INVALID_ARGUMENT
3075 * \c private_key is not compatible with \c alg,
3076 * or \p peer_key is not valid for \c alg or not compatible with
3077 * \c private_key.
3078 * \retval #PSA_ERROR_NOT_SUPPORTED
3079 * \c alg is not supported or is not a key derivation algorithm.
3080 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3081 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3082 * \retval #PSA_ERROR_HARDWARE_FAILURE
3083 * \retval #PSA_ERROR_TAMPERING_DETECTED
3084 */
3085psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003086 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003087 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003088 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003089 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003090
Gilles Peskine769c7a62019-01-18 16:42:29 +01003091/** Perform a key agreement and use the shared secret as input to a key
3092 * derivation.
3093 *
3094 * A key agreement algorithm takes two inputs: a private key \p private_key
3095 * a public key \p peer_key.
3096 *
3097 * \warning The raw result of a key agreement algorithm such as finite-field
3098 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3099 * not be used directly as key material. It should instead be passed as
3100 * input to a key derivation algorithm. To chain a key agreement with
3101 * a key derivation, use psa_key_agreement() and other functions from
3102 * the key derivation and generator interface.
3103 *
3104 * \param private_key Handle to the private key to use.
3105 * \param[in] peer_key Public key of the peer. It must be
3106 * in the same format that psa_import_key()
3107 * accepts. The standard formats for public
3108 * keys are documented in the documentation
3109 * of psa_export_public_key().
3110 * \param peer_key_length Size of \p peer_key in bytes.
3111 * \param[out] output Buffer where the decrypted message is to
3112 * be written.
3113 * \param output_size Size of the \c output buffer in bytes.
3114 * \param[out] output_length On success, the number of bytes
3115 * that make up the returned output.
3116 *
3117 * \retval #PSA_SUCCESS
3118 * Success.
3119 * \retval #PSA_ERROR_INVALID_HANDLE
3120 * \retval #PSA_ERROR_EMPTY_SLOT
3121 * \retval #PSA_ERROR_NOT_PERMITTED
3122 * \retval #PSA_ERROR_INVALID_ARGUMENT
3123 * \p alg is not a key agreement algorithm
3124 * \retval #PSA_ERROR_INVALID_ARGUMENT
3125 * \p private_key is not compatible with \p alg,
3126 * or \p peer_key is not valid for \p alg or not compatible with
3127 * \p private_key.
3128 * \retval #PSA_ERROR_NOT_SUPPORTED
3129 * \p alg is not a supported key agreement algorithm.
3130 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3131 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3132 * \retval #PSA_ERROR_HARDWARE_FAILURE
3133 * \retval #PSA_ERROR_TAMPERING_DETECTED
3134 */
3135psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3136 psa_key_handle_t private_key,
3137 const uint8_t *peer_key,
3138 size_t peer_key_length,
3139 uint8_t *output,
3140 size_t output_size,
3141 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003142
3143/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003144
3145/** \defgroup random Random generation
3146 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003147 */
3148
3149/**
3150 * \brief Generate random bytes.
3151 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003152 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003153 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003154 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003155 *
3156 * \note To generate a key, use psa_generate_key() instead.
3157 *
3158 * \param[out] output Output buffer for the generated data.
3159 * \param output_size Number of bytes to generate and output.
3160 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003161 * \retval #PSA_SUCCESS
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003162 * \retval #PSA_ERROR_NOT_SUPPORTED
3163 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003164 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003165 * \retval #PSA_ERROR_HARDWARE_FAILURE
3166 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003167 * \retval #PSA_ERROR_BAD_STATE
3168 * The library has not been previously initialized by psa_crypto_init().
3169 * It is implementation-dependent whether a failure to initialize
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003170 * results in this error code.
3171 */
3172psa_status_t psa_generate_random(uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003173 size_t output_size);
3174
3175/** Extra parameters for RSA key generation.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003176 *
Gilles Peskine28538492018-07-11 17:34:00 +02003177 * You may pass a pointer to a structure of this type as the \c extra
3178 * parameter to psa_generate_key().
3179 */
3180typedef struct {
3181 uint32_t e; /**< Public exponent value. Default: 65537. */
3182} psa_generate_key_extra_rsa;
3183
3184/**
itayzafrir90d8c7a2018-09-12 11:44:52 +03003185 * \brief Generate a key or key pair.
itayzafrir18617092018-09-16 12:22:41 +03003186 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003187 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003188 * It must have been obtained by calling
3189 * psa_allocate_key() or psa_create_key() and must
3190 * not contain key material yet.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003191 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
3192 * \param bits Key size in bits.
3193 * \param[in] extra Extra parameters for key generation. The
3194 * interpretation of this parameter depends on
3195 * \p type. All types support \c NULL to use
3196 * default parameters. Implementation that support
3197 * the generation of vendor-specific key types
3198 * that allow extra parameters shall document
3199 * the format of these extra parameters and
3200 * the default values. For standard parameters,
3201 * the meaning of \p extra is as follows:
3202 * - For a symmetric key type (a type such
3203 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
Gilles Peskine53d991e2018-07-12 01:14:59 +02003204 * false), \p extra must be \c NULL.
3205 * - For an elliptic curve key type (a type
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003206 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
3207 * false), \p extra must be \c NULL.
3208 * - For an RSA key (\p type is
Gilles Peskinee59236f2018-01-27 23:32:46 +01003209 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3210 * optional #psa_generate_key_extra_rsa structure
3211 * specifying the public exponent. The
3212 * default public exponent used when \p extra
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003213 * is \c NULL is 65537.
Jaeden Amero1308fb52019-01-11 13:50:43 +00003214 * - For an DSA key (\p type is
3215 * #PSA_KEY_TYPE_DSA_KEYPAIR), \p extra is an
3216 * optional structure specifying the key domain
3217 * parameters. The key domain parameters can also be
3218 * provided by psa_set_key_domain_parameters(),
3219 * which documents the format of the structure.
Jaeden Amero8851c402019-01-11 14:20:03 +00003220 * - For a DH key (\p type is
3221 * #PSA_KEY_TYPE_DH_KEYPAIR), the \p extra is an
3222 * optional structure specifying the key domain
3223 * parameters. The key domain parameters can also be
3224 * provided by psa_set_key_domain_parameters(),
3225 * which documents the format of the structure.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003226 * \param extra_size Size of the buffer that \p extra
3227 * points to, in bytes. Note that if \p extra is
3228 * \c NULL then \p extra_size must be zero.
3229 *
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003230 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003231 * Success.
3232 * If the key is persistent, the key material and the key's metadata
3233 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003234 * \retval #PSA_ERROR_INVALID_HANDLE
3235 * \retval #PSA_ERROR_OCCUPIED_SLOT
3236 * There is already a key in the specified slot.
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003237 * \retval #PSA_ERROR_NOT_SUPPORTED
3238 * \retval #PSA_ERROR_INVALID_ARGUMENT
3239 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003240 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3241 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3242 * \retval #PSA_ERROR_HARDWARE_FAILURE
3243 * \retval #PSA_ERROR_TAMPERING_DETECTED
3244 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003245 * The library has not been previously initialized by psa_crypto_init().
3246 * It is implementation-dependent whether a failure to initialize
3247 * results in this error code.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003248 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003249psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskinee59236f2018-01-27 23:32:46 +01003250 psa_key_type_t type,
3251 size_t bits,
3252 const void *extra,
3253 size_t extra_size);
3254
3255/**@}*/
3256
3257#ifdef __cplusplus
3258}
3259#endif
3260
3261/* The file "crypto_sizes.h" contains definitions for size calculation
3262 * macros whose definitions are implementation-specific. */
3263#include "crypto_sizes.h"
3264
3265/* The file "crypto_struct.h" contains definitions for
3266 * implementation-specific structs that are declared above. */
3267#include "crypto_struct.h"
3268
3269/* The file "crypto_extra.h" contains vendor-specific definitions. This
3270 * can include vendor-defined algorithms, extra functions, etc. */
3271#include "crypto_extra.h"
3272
3273#endif /* PSA_CRYPTO_H */