blob: acbcc3df01d408c1df60f1aedf0806c8b2a54f56 [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 *
127 * This function takes a key type and maximum size as arguments so that
128 * the implementation can reserve a corresponding amount of memory.
129 * Implementations are not required to enforce this limit: if the application
130 * later tries to create a larger key or a key of a different type, it
131 * is implementation-defined whether this may succeed.
132 *
133 * \param type The type of key that the slot will contain.
134 * \param max_bits The maximum key size that the slot will contain.
135 * \param[out] handle On success, a handle to a volatile key slot.
136 *
137 * \retval #PSA_SUCCESS
138 * Success. The application can now use the value of `*handle`
139 * to access the newly allocated key slot.
140 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
141 * There was not enough memory, or the maximum number of key slots
142 * has been reached.
143 * \retval #PSA_ERROR_INVALID_ARGUMENT
144 * This implementation does not support this key type.
145 */
146
147psa_status_t psa_allocate_key(psa_key_type_t type,
148 size_t max_bits,
149 psa_key_handle_t *handle);
150
151/** Open a handle to an existing persistent key.
152 *
153 * Open a handle to a key which was previously created with psa_create_key().
154 *
155 * \param lifetime The lifetime of the key. This designates a storage
156 * area where the key material is stored. This must not
157 * be #PSA_KEY_LIFETIME_VOLATILE.
158 * \param id The persistent identifier of the key.
159 * \param[out] handle On success, a handle to a key slot which contains
160 * the data and metadata loaded from the specified
161 * persistent location.
162 *
163 * \retval #PSA_SUCCESS
164 * Success. The application can now use the value of `*handle`
165 * to access the newly allocated key slot.
166 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
167 * \retval #PSA_ERROR_EMPTY_SLOT
168 * \retval #PSA_ERROR_INVALID_ARGUMENT
169 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
170 * \retval #PSA_ERROR_INVALID_ARGUMENT
171 * \p id is invalid for the specified lifetime.
172 * \retval #PSA_ERROR_NOT_SUPPORTED
173 * \p lifetime is not supported.
174 * \retval #PSA_ERROR_NOT_PERMITTED
175 * The specified key exists, but the application does not have the
176 * permission to access it. Note that this specification does not
177 * define any way to create such a key, but it may be possible
178 * through implementation-specific means.
179 */
180psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
181 psa_key_id_t id,
182 psa_key_handle_t *handle);
183
184/** Create a new persistent key slot.
185 *
186 * Create a new persistent key slot and return a handle to it. The handle
187 * remains valid until the application calls psa_close_key() or terminates.
188 * The application can open the key again with psa_open_key() until it
189 * removes the key by calling psa_destroy_key().
190 *
191 * \param lifetime The lifetime of the key. This designates a storage
192 * area where the key material is stored. This must not
193 * be #PSA_KEY_LIFETIME_VOLATILE.
194 * \param id The persistent identifier of the key.
195 * \param type The type of key that the slot will contain.
196 * \param max_bits The maximum key size that the slot will contain.
197 * \param[out] handle On success, a handle to the newly created key slot.
198 * When key material is later created in this key slot,
199 * it will be saved to the specified persistent location.
200 *
201 * \retval #PSA_SUCCESS
202 * Success. The application can now use the value of `*handle`
203 * to access the newly allocated key slot.
204 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
205 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
206 * \retval #PSA_ERROR_OCCUPIED_SLOT
207 * There is already a key with the identifier \p id in the storage
208 * area designated by \p lifetime.
209 * \retval #PSA_ERROR_INVALID_ARGUMENT
210 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
211 * \retval #PSA_ERROR_INVALID_ARGUMENT
212 * \p id is invalid for the specified lifetime.
213 * \retval #PSA_ERROR_NOT_SUPPORTED
214 * \p lifetime is not supported.
215 * \retval #PSA_ERROR_NOT_PERMITTED
216 * \p lifetime is valid, but the application does not have the
217 * permission to create a key there.
218 */
219psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
220 psa_key_id_t id,
221 psa_key_type_t type,
222 size_t max_bits,
223 psa_key_handle_t *handle);
224
225/** Close a key handle.
226 *
227 * If the handle designates a volatile key, destroy the key material and
228 * free all associated resources, just like psa_destroy_key().
229 *
230 * If the handle designates a persistent key, free all resources associated
231 * with the key in volatile memory. The key slot in persistent storage is
232 * not affected and can be opened again later with psa_open_key().
233 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100234 * If the key is currently in use in a multipart operation,
235 * the multipart operation is aborted.
236 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100237 * \param handle The key handle to close.
238 *
239 * \retval #PSA_SUCCESS
240 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100241 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100242 */
243psa_status_t psa_close_key(psa_key_handle_t handle);
244
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100245/**@}*/
246
247/** \defgroup import_export Key import and export
248 * @{
249 */
250
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100251/**
252 * \brief Import a key in binary format.
253 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100254 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100255 * documentation of psa_export_public_key() for the format of public keys
256 * and to the documentation of psa_export_key() for the format for
257 * other key types.
258 *
259 * This specification supports a single format for each key type.
260 * Implementations may support other formats as long as the standard
261 * format is supported. Implementations that support other formats
262 * should ensure that the formats are clearly unambiguous so as to
263 * minimize the risk that an invalid input is accidentally interpreted
264 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100265 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100266 * \param handle Handle to the slot where the key will be stored.
267 * This must be a valid slot for a key of the chosen
268 * type: it must have been obtained by calling
269 * psa_allocate_key() or psa_create_key() with the
270 * correct \p type and with a maximum size that is
271 * compatible with \p data.
Gilles Peskinef7933932018-10-31 14:07:52 +0100272 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
273 * import, the key slot will contain a key of this type.
274 * \param[in] data Buffer containing the key data. The content of this
275 * buffer is interpreted according to \p type. It must
276 * contain the format described in the documentation
277 * of psa_export_key() or psa_export_public_key() for
278 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200279 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100280 *
Gilles Peskine28538492018-07-11 17:34:00 +0200281 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100282 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100283 * If the key is persistent, the key material and the key's metadata
284 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100285 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200286 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200287 * The key type or key size is not supported, either by the
288 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200289 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100290 * The key slot is invalid,
291 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200292 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200293 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200294 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
295 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
296 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100297 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200298 * \retval #PSA_ERROR_HARDWARE_FAILURE
299 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300300 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300301 * The library has not been previously initialized by psa_crypto_init().
302 * It is implementation-dependent whether a failure to initialize
303 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100304 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100305psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 psa_key_type_t type,
307 const uint8_t *data,
308 size_t data_length);
309
310/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100311 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200312 *
313 * This function destroys the content of the key slot from both volatile
314 * memory and, if applicable, non-volatile storage. Implementations shall
315 * make a best effort to ensure that any previous content of the slot is
316 * unrecoverable.
317 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100318 * This function also erases any metadata such as policies and frees all
319 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200320 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100321 * If the key is currently in use in a multipart operation,
322 * the multipart operation is aborted.
323 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100324 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100325 *
Gilles Peskine28538492018-07-11 17:34:00 +0200326 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200327 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200328 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200329 * The slot holds content and cannot be erased because it is
330 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100331 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200332 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200333 * There was an failure in communication with the cryptoprocessor.
334 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200335 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200336 * The storage is corrupted. Implementations shall make a best effort
337 * to erase key material even in this stage, however applications
338 * should be aware that it may be impossible to guarantee that the
339 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200340 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200341 * An unexpected condition which is not a storage corruption or
342 * a communication failure occurred. The cryptoprocessor may have
343 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300344 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300345 * The library has not been previously initialized by psa_crypto_init().
346 * It is implementation-dependent whether a failure to initialize
347 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100348 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100349psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100350
351/**
352 * \brief Get basic metadata about a key.
353 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100354 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200355 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100356 * This may be a null pointer, in which case the key type
357 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200358 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100359 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100360 * is not written.
361 *
Gilles Peskine28538492018-07-11 17:34:00 +0200362 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100363 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200364 * \retval #PSA_ERROR_EMPTY_SLOT
Gilles Peskineae32aac2018-11-30 14:39:32 +0100365 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200366 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
367 * \retval #PSA_ERROR_HARDWARE_FAILURE
368 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300369 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300370 * The library has not been previously initialized by psa_crypto_init().
371 * It is implementation-dependent whether a failure to initialize
372 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100373 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100374psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100375 psa_key_type_t *type,
376 size_t *bits);
377
378/**
379 * \brief Export a key in binary format.
380 *
381 * The output of this function can be passed to psa_import_key() to
382 * create an equivalent object.
383 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100384 * If the implementation of psa_import_key() supports other formats
385 * beyond the format specified here, the output from psa_export_key()
386 * must use the representation specified here, not the original
387 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100388 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100389 * For standard key types, the output format is as follows:
390 *
391 * - For symmetric keys (including MAC keys), the format is the
392 * raw bytes of the key.
393 * - For DES, the key data consists of 8 bytes. The parity bits must be
394 * correct.
395 * - For Triple-DES, the format is the concatenation of the
396 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100397 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200398 * is the non-encrypted DER encoding of the representation defined by
399 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
400 * ```
401 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200402 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200403 * modulus INTEGER, -- n
404 * publicExponent INTEGER, -- e
405 * privateExponent INTEGER, -- d
406 * prime1 INTEGER, -- p
407 * prime2 INTEGER, -- q
408 * exponent1 INTEGER, -- d mod (p-1)
409 * exponent2 INTEGER, -- d mod (q-1)
410 * coefficient INTEGER, -- (inverse of q) mod p
411 * }
412 * ```
413 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
414 * is the non-encrypted DER encoding of the representation used by
Gilles Peskinec6290c02018-08-13 17:24:59 +0200415 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200416 * ```
417 * DSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200418 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200419 * prime INTEGER, -- p
420 * subprime INTEGER, -- q
421 * generator INTEGER, -- g
422 * public INTEGER, -- y
423 * private INTEGER, -- x
424 * }
425 * ```
426 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100427 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100428 * a representation of the private value as a `ceiling(m/8)`-byte string
429 * where `m` is the bit size associated with the curve, i.e. the bit size
430 * of the order of the curve's coordinate field. This byte string is
431 * in little-endian order for Montgomery curves (curve types
432 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
433 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
434 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100435 * This is the content of the `privateKey` field of the `ECPrivateKey`
436 * format defined by RFC 5915.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200437 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
438 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100439 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100440 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200441 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200442 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200443 * \param[out] data_length On success, the number of bytes
444 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100445 *
Gilles Peskine28538492018-07-11 17:34:00 +0200446 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100447 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200448 * \retval #PSA_ERROR_EMPTY_SLOT
449 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100450 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200451 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
452 * The size of the \p data buffer is too small. You can determine a
453 * sufficient buffer size by calling
454 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
455 * where \c type is the key type
456 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200457 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
458 * \retval #PSA_ERROR_HARDWARE_FAILURE
459 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300460 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300461 * The library has not been previously initialized by psa_crypto_init().
462 * It is implementation-dependent whether a failure to initialize
463 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100464 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100465psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100466 uint8_t *data,
467 size_t data_size,
468 size_t *data_length);
469
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100470/**
471 * \brief Export a public key or the public part of a key pair in binary format.
472 *
473 * The output of this function can be passed to psa_import_key() to
474 * create an object that is equivalent to the public key.
475 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200476 * The format is the DER representation defined by RFC 5280 as
477 * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
478 * specified below.
479 * ```
480 * SubjectPublicKeyInfo ::= SEQUENCE {
481 * algorithm AlgorithmIdentifier,
482 * subjectPublicKey BIT STRING }
483 * AlgorithmIdentifier ::= SEQUENCE {
484 * algorithm OBJECT IDENTIFIER,
485 * parameters ANY DEFINED BY algorithm OPTIONAL }
486 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100487 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200488 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY),
489 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.1 as
490 * `RSAPublicKey`,
491 * with the OID `rsaEncryption`,
492 * and with the parameters `NULL`.
493 * ```
494 * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
495 * rsadsi(113549) pkcs(1) 1 }
496 * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 }
497 *
498 * RSAPublicKey ::= SEQUENCE {
499 * modulus INTEGER, -- n
500 * publicExponent INTEGER } -- e
501 * ```
502 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
503 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
504 * `DSAPublicKey`,
505 * with the OID `id-dsa`,
506 * and with the parameters `DSS-Parms`.
507 * ```
508 * id-dsa OBJECT IDENTIFIER ::= {
509 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
510 *
511 * Dss-Parms ::= SEQUENCE {
512 * p INTEGER,
513 * q INTEGER,
514 * g INTEGER }
515 * DSAPublicKey ::= INTEGER -- public key, Y
516 * ```
517 * - For elliptic curve public keys (key types for which
518 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true),
519 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.5 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200520 * `ECPoint`, which contains the uncompressed
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200521 * representation defined by SEC1 &sect;2.3.3.
522 * The OID is `id-ecPublicKey`,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200523 * and the parameters must be given as a `namedCurve` OID as specified in
Gilles Peskinec6290c02018-08-13 17:24:59 +0200524 * RFC 5480 &sect;2.1.1.1 or other applicable standards.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200525 * ```
526 * ansi-X9-62 OBJECT IDENTIFIER ::=
527 * { iso(1) member-body(2) us(840) 10045 }
528 * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 }
529 * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 }
530 *
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200531 * ECPoint ::= ...
532 * -- first 8 bits: 0x04;
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100533 * -- then x_P as a `ceiling(m/8)`-byte string, big endian;
534 * -- then y_P as a `ceiling(m/8)`-byte string, big endian;
535 * -- where `m` is the bit size associated with the curve,
Gilles Peskine7b5b4a02018-11-14 21:05:10 +0100536 * -- i.e. the bit size of `q` for a curve over `F_q`.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200537 *
538 * EcpkParameters ::= CHOICE { -- other choices are not allowed
539 * namedCurve OBJECT IDENTIFIER }
540 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100541 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100542 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200543 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200544 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200545 * \param[out] data_length On success, the number of bytes
546 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100547 *
Gilles Peskine28538492018-07-11 17:34:00 +0200548 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100549 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200550 * \retval #PSA_ERROR_EMPTY_SLOT
551 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200552 * The key is neither a public key nor a key pair.
553 * \retval #PSA_ERROR_NOT_SUPPORTED
554 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
555 * The size of the \p data buffer is too small. You can determine a
556 * sufficient buffer size by calling
557 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
558 * where \c type is the key type
559 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200560 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
561 * \retval #PSA_ERROR_HARDWARE_FAILURE
562 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300563 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300564 * The library has not been previously initialized by psa_crypto_init().
565 * It is implementation-dependent whether a failure to initialize
566 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100567 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100568psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100569 uint8_t *data,
570 size_t data_size,
571 size_t *data_length);
572
573/**@}*/
574
575/** \defgroup policy Key policies
576 * @{
577 */
578
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100579/** The type of the key policy data structure.
580 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000581 * Before calling any function on a key policy, the application must initialize
582 * it by any of the following means:
583 * - Set the structure to all-bits-zero, for example:
584 * \code
585 * psa_key_policy_t policy;
586 * memset(&policy, 0, sizeof(policy));
587 * \endcode
588 * - Initialize the structure to logical zero values, for example:
589 * \code
590 * psa_key_policy_t policy = {0};
591 * \endcode
592 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
593 * for example:
594 * \code
595 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
596 * \endcode
597 * - Assign the result of the function psa_key_policy_init()
598 * to the structure, for example:
599 * \code
600 * psa_key_policy_t policy;
601 * policy = psa_key_policy_init();
602 * \endcode
603 *
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100604 * This is an implementation-defined \c struct. Applications should not
605 * make any assumptions about the content of this structure except
606 * as directed by the documentation of a specific implementation. */
607typedef struct psa_key_policy_s psa_key_policy_t;
608
Jaeden Amero70261c52019-01-04 11:47:20 +0000609/** \def PSA_KEY_POLICY_INIT
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200610 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000611 * This macro returns a suitable initializer for a key policy object of type
612 * #psa_key_policy_t.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200613 */
Jaeden Amero70261c52019-01-04 11:47:20 +0000614#ifdef __DOXYGEN_ONLY__
615/* This is an example definition for documentation purposes.
616 * Implementations should define a suitable value in `crypto_struct.h`.
617 */
618#define PSA_KEY_POLICY_INIT {0}
619#endif
620
621/** Return an initial value for a key policy that forbids all usage of the key.
622 */
623static psa_key_policy_t psa_key_policy_init(void);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100624
Gilles Peskine7e198532018-03-08 07:50:30 +0100625/** \brief Set the standard fields of a policy structure.
626 *
627 * Note that this function does not make any consistency check of the
628 * parameters. The values are only checked when applying the policy to
629 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200630 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000631 * \param[in,out] policy The key policy to modify. It must have been
632 * initialized as per the documentation for
633 * #psa_key_policy_t.
634 * \param usage The permitted uses for the key.
635 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100636 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100637void psa_key_policy_set_usage(psa_key_policy_t *policy,
638 psa_key_usage_t usage,
639 psa_algorithm_t alg);
640
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200641/** \brief Retrieve the usage field of a policy structure.
642 *
643 * \param[in] policy The policy object to query.
644 *
645 * \return The permitted uses for a key with this policy.
646 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200647psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100648
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200649/** \brief Retrieve the algorithm field of a policy structure.
650 *
651 * \param[in] policy The policy object to query.
652 *
653 * \return The permitted algorithm for a key with this policy.
654 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200655psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100656
657/** \brief Set the usage policy on a key slot.
658 *
659 * This function must be called on an empty key slot, before importing,
660 * generating or creating a key in the slot. Changing the policy of an
661 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100662 *
663 * Implementations may set restrictions on supported key policies
664 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200665 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100666 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200667 * \param[in] policy The policy object to query.
668 *
669 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100670 * Success.
671 * If the key is persistent, it is implementation-defined whether
672 * the policy has been saved to persistent storage. Implementations
673 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100674 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200675 * \retval #PSA_ERROR_OCCUPIED_SLOT
676 * \retval #PSA_ERROR_NOT_SUPPORTED
677 * \retval #PSA_ERROR_INVALID_ARGUMENT
678 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
679 * \retval #PSA_ERROR_HARDWARE_FAILURE
680 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300681 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300682 * The library has not been previously initialized by psa_crypto_init().
683 * It is implementation-dependent whether a failure to initialize
684 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100685 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100686psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100687 const psa_key_policy_t *policy);
688
Gilles Peskine7e198532018-03-08 07:50:30 +0100689/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200690 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100691 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200692 * \param[out] policy On success, the key's policy.
693 *
694 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100695 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200696 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
697 * \retval #PSA_ERROR_HARDWARE_FAILURE
698 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300699 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300700 * The library has not been previously initialized by psa_crypto_init().
701 * It is implementation-dependent whether a failure to initialize
702 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100703 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100704psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100705 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100706
707/**@}*/
708
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100709/** \defgroup hash Message digests
710 * @{
711 */
712
Gilles Peskine69647a42019-01-14 20:18:12 +0100713/** Calculate the hash (digest) of a message.
714 *
715 * \note To verify the hash of a message against an
716 * expected value, use psa_hash_compare() instead.
717 *
718 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
719 * such that #PSA_ALG_IS_HASH(\p alg) is true).
720 * \param[in] input Buffer containing the message to hash.
721 * \param input_length Size of the \p input buffer in bytes.
722 * \param[out] hash Buffer where the hash is to be written.
723 * \param hash_size Size of the \p hash buffer in bytes.
724 * \param[out] hash_length On success, the number of bytes
725 * that make up the hash value. This is always
726 * #PSA_HASH_SIZE(\c alg) where \c alg is the
727 * hash algorithm that is calculated.
728 *
729 * \retval #PSA_SUCCESS
730 * Success.
731 * \retval #PSA_ERROR_NOT_SUPPORTED
732 * \p alg is not supported or is not a hash algorithm.
733 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
734 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
735 * \retval #PSA_ERROR_HARDWARE_FAILURE
736 * \retval #PSA_ERROR_TAMPERING_DETECTED
737 */
738psa_status_t psa_hash_compute(psa_algorithm_t alg,
739 const uint8_t *input,
740 size_t input_length,
741 uint8_t *hash,
742 size_t hash_size,
743 size_t *hash_length);
744
745/** Calculate the hash (digest) of a message and compare it with a
746 * reference value.
747 *
748 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
749 * such that #PSA_ALG_IS_HASH(\p alg) is true).
750 * \param[in] input Buffer containing the message to hash.
751 * \param input_length Size of the \p input buffer in bytes.
752 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100753 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100754 *
755 * \retval #PSA_SUCCESS
756 * The expected hash is identical to the actual hash of the input.
757 * \retval #PSA_ERROR_INVALID_SIGNATURE
758 * The hash of the message was calculated successfully, but it
759 * differs from the expected hash.
760 * \retval #PSA_ERROR_NOT_SUPPORTED
761 * \p alg is not supported or is not a hash algorithm.
762 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
763 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
764 * \retval #PSA_ERROR_HARDWARE_FAILURE
765 * \retval #PSA_ERROR_TAMPERING_DETECTED
766 */
767psa_status_t psa_hash_compare(psa_algorithm_t alg,
768 const uint8_t *input,
769 size_t input_length,
770 const uint8_t *hash,
771 const size_t hash_length);
772
Gilles Peskine308b91d2018-02-08 09:47:44 +0100773/** The type of the state data structure for multipart hash operations.
774 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000775 * Before calling any function on a hash operation object, the application must
776 * initialize it by any of the following means:
777 * - Set the structure to all-bits-zero, for example:
778 * \code
779 * psa_hash_operation_t operation;
780 * memset(&operation, 0, sizeof(operation));
781 * \endcode
782 * - Initialize the structure to logical zero values, for example:
783 * \code
784 * psa_hash_operation_t operation = {0};
785 * \endcode
786 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
787 * for example:
788 * \code
789 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
790 * \endcode
791 * - Assign the result of the function psa_hash_operation_init()
792 * to the structure, for example:
793 * \code
794 * psa_hash_operation_t operation;
795 * operation = psa_hash_operation_init();
796 * \endcode
797 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100798 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100799 * make any assumptions about the content of this structure except
800 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100801typedef struct psa_hash_operation_s psa_hash_operation_t;
802
Jaeden Amero6a25b412019-01-04 11:47:44 +0000803/** \def PSA_HASH_OPERATION_INIT
804 *
805 * This macro returns a suitable initializer for a hash operation object
806 * of type #psa_hash_operation_t.
807 */
808#ifdef __DOXYGEN_ONLY__
809/* This is an example definition for documentation purposes.
810 * Implementations should define a suitable value in `crypto_struct.h`.
811 */
812#define PSA_HASH_OPERATION_INIT {0}
813#endif
814
815/** Return an initial value for a hash operation object.
816 */
817static psa_hash_operation_t psa_hash_operation_init(void);
818
Gilles Peskinef45adda2019-01-14 18:29:18 +0100819/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100820 *
821 * The sequence of operations to calculate a hash (message digest)
822 * is as follows:
823 * -# Allocate an operation object which will be passed to all the functions
824 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000825 * -# Initialize the operation object with one of the methods described in the
826 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200827 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100828 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100829 * of the message each time. The hash that is calculated is the hash
830 * of the concatenation of these messages in order.
831 * -# To calculate the hash, call psa_hash_finish().
832 * To compare the hash with an expected value, call psa_hash_verify().
833 *
834 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000835 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100836 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200837 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100838 * eventually terminate the operation. The following events terminate an
839 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100840 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100841 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100842 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000843 * \param[in,out] operation The operation object to set up. It must have
844 * been initialized as per the documentation for
845 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200846 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
847 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100848 *
Gilles Peskine28538492018-07-11 17:34:00 +0200849 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100850 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200851 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200852 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200853 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
854 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
855 * \retval #PSA_ERROR_HARDWARE_FAILURE
856 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100857 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200858psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100859 psa_algorithm_t alg);
860
Gilles Peskine308b91d2018-02-08 09:47:44 +0100861/** Add a message fragment to a multipart hash operation.
862 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200863 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100864 *
865 * If this function returns an error status, the operation becomes inactive.
866 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200867 * \param[in,out] operation Active hash operation.
868 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200869 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100870 *
Gilles Peskine28538492018-07-11 17:34:00 +0200871 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100872 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200873 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100874 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200875 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
876 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
877 * \retval #PSA_ERROR_HARDWARE_FAILURE
878 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100879 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100880psa_status_t psa_hash_update(psa_hash_operation_t *operation,
881 const uint8_t *input,
882 size_t input_length);
883
Gilles Peskine308b91d2018-02-08 09:47:44 +0100884/** Finish the calculation of the hash of a message.
885 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200886 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100887 * This function calculates the hash of the message formed by concatenating
888 * the inputs passed to preceding calls to psa_hash_update().
889 *
890 * When this function returns, the operation becomes inactive.
891 *
892 * \warning Applications should not call this function if they expect
893 * a specific value for the hash. Call psa_hash_verify() instead.
894 * Beware that comparing integrity or authenticity data such as
895 * hash values with a function such as \c memcmp is risky
896 * because the time taken by the comparison may leak information
897 * about the hashed data which could allow an attacker to guess
898 * a valid hash and thereby bypass security controls.
899 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200900 * \param[in,out] operation Active hash operation.
901 * \param[out] hash Buffer where the hash is to be written.
902 * \param hash_size Size of the \p hash buffer in bytes.
903 * \param[out] hash_length On success, the number of bytes
904 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200905 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200906 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100907 *
Gilles Peskine28538492018-07-11 17:34:00 +0200908 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100909 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200910 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100911 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200912 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200913 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200914 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100915 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200916 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
917 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
918 * \retval #PSA_ERROR_HARDWARE_FAILURE
919 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100920 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100921psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
922 uint8_t *hash,
923 size_t hash_size,
924 size_t *hash_length);
925
Gilles Peskine308b91d2018-02-08 09:47:44 +0100926/** Finish the calculation of the hash of a message and compare it with
927 * an expected value.
928 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200929 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100930 * This function calculates the hash of the message formed by concatenating
931 * the inputs passed to preceding calls to psa_hash_update(). It then
932 * compares the calculated hash with the expected hash passed as a
933 * parameter to this function.
934 *
935 * When this function returns, the operation becomes inactive.
936 *
Gilles Peskine19067982018-03-20 17:54:53 +0100937 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100938 * comparison between the actual hash and the expected hash is performed
939 * in constant time.
940 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200941 * \param[in,out] operation Active hash operation.
942 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200943 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100944 *
Gilles Peskine28538492018-07-11 17:34:00 +0200945 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100946 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200947 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100948 * The hash of the message was calculated successfully, but it
949 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200950 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100951 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200952 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
953 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
954 * \retval #PSA_ERROR_HARDWARE_FAILURE
955 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100956 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100957psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
958 const uint8_t *hash,
959 size_t hash_length);
960
Gilles Peskine308b91d2018-02-08 09:47:44 +0100961/** Abort a hash operation.
962 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100963 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200964 * \p operation structure itself. Once aborted, the operation object
965 * can be reused for another operation by calling
966 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100967 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200968 * You may call this function any time after the operation object has
969 * been initialized by any of the following methods:
970 * - A call to psa_hash_setup(), whether it succeeds or not.
971 * - Initializing the \c struct to all-bits-zero.
972 * - Initializing the \c struct to logical zeros, e.g.
973 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100974 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200975 * In particular, calling psa_hash_abort() after the operation has been
976 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
977 * psa_hash_verify() is safe and has no effect.
978 *
979 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100980 *
Gilles Peskine28538492018-07-11 17:34:00 +0200981 * \retval #PSA_SUCCESS
982 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200983 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +0200984 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
985 * \retval #PSA_ERROR_HARDWARE_FAILURE
986 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100987 */
988psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100989
990/**@}*/
991
Gilles Peskine8c9def32018-02-08 10:02:12 +0100992/** \defgroup MAC Message authentication codes
993 * @{
994 */
995
Gilles Peskine69647a42019-01-14 20:18:12 +0100996/** Calculate the MAC (message authentication code) of a message.
997 *
998 * \note To verify the MAC of a message against an
999 * expected value, use psa_mac_verify() instead.
1000 * Beware that comparing integrity or authenticity data such as
1001 * MAC values with a function such as \c memcmp is risky
1002 * because the time taken by the comparison may leak information
1003 * about the MAC value which could allow an attacker to guess
1004 * a valid MAC and thereby bypass security controls.
1005 *
1006 * \param handle Handle to the key to use for the operation.
1007 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1008 * such that #PSA_ALG_IS_MAC(alg) is true).
1009 * \param[in] input Buffer containing the input message.
1010 * \param input_length Size of the \p input buffer in bytes.
1011 * \param[out] mac Buffer where the MAC value is to be written.
1012 * \param mac_size Size of the \p mac buffer in bytes.
1013 * \param[out] mac_length On success, the number of bytes
1014 * that make up the mac value. This is always
1015 * #PSA_HASH_SIZE(\c alg) where \c alg is the
1016 * hash algorithm that is calculated.
1017 *
1018 * \retval #PSA_SUCCESS
1019 * Success.
1020 * \retval #PSA_ERROR_INVALID_HANDLE
1021 * \retval #PSA_ERROR_EMPTY_SLOT
1022 * \retval #PSA_ERROR_NOT_PERMITTED
1023 * \retval #PSA_ERROR_INVALID_ARGUMENT
1024 * \p key is not compatible with \p alg.
1025 * \retval #PSA_ERROR_NOT_SUPPORTED
1026 * \p alg is not supported or is not a MAC algorithm.
1027 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1028 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1029 * \retval #PSA_ERROR_HARDWARE_FAILURE
1030 * \retval #PSA_ERROR_TAMPERING_DETECTED
1031 * \retval #PSA_ERROR_BAD_STATE
1032 * The library has not been previously initialized by psa_crypto_init().
1033 * It is implementation-dependent whether a failure to initialize
1034 * results in this error code.
1035 */
1036psa_status_t psa_mac_compute(psa_key_handle_t handle,
1037 psa_algorithm_t alg,
1038 const uint8_t *input,
1039 size_t input_length,
1040 uint8_t *mac,
1041 size_t mac_size,
1042 size_t *mac_length);
1043
1044/** Calculate the MAC of a message and compare it with a reference value.
1045 *
1046 * \param handle Handle to the key to use for the operation.
1047 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1048 * such that #PSA_ALG_IS_MAC(alg) is true).
1049 * \param[in] input Buffer containing the input message.
1050 * \param input_length Size of the \p input buffer in bytes.
1051 * \param[out] mac Buffer containing the expected MAC value.
1052 * \param mac_length Size of the \p mac buffer in bytes.
1053 *
1054 * \retval #PSA_SUCCESS
1055 * The expected MAC is identical to the actual MAC of the input.
1056 * \retval #PSA_ERROR_INVALID_SIGNATURE
1057 * The MAC of the message was calculated successfully, but it
1058 * differs from the expected value.
1059 * \retval #PSA_ERROR_INVALID_HANDLE
1060 * \retval #PSA_ERROR_EMPTY_SLOT
1061 * \retval #PSA_ERROR_NOT_PERMITTED
1062 * \retval #PSA_ERROR_INVALID_ARGUMENT
1063 * \p key is not compatible with \p alg.
1064 * \retval #PSA_ERROR_NOT_SUPPORTED
1065 * \p alg is not supported or is not a MAC algorithm.
1066 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1067 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1068 * \retval #PSA_ERROR_HARDWARE_FAILURE
1069 * \retval #PSA_ERROR_TAMPERING_DETECTED
1070 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001071psa_status_t psa_mac_verify(psa_key_handle_t handle,
1072 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001073 const uint8_t *input,
1074 size_t input_length,
1075 const uint8_t *mac,
1076 const size_t mac_length);
1077
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001078/** The type of the state data structure for multipart MAC operations.
1079 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001080 * Before calling any function on a MAC operation object, the application must
1081 * initialize it by any of the following means:
1082 * - Set the structure to all-bits-zero, for example:
1083 * \code
1084 * psa_mac_operation_t operation;
1085 * memset(&operation, 0, sizeof(operation));
1086 * \endcode
1087 * - Initialize the structure to logical zero values, for example:
1088 * \code
1089 * psa_mac_operation_t operation = {0};
1090 * \endcode
1091 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1092 * for example:
1093 * \code
1094 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1095 * \endcode
1096 * - Assign the result of the function psa_mac_operation_init()
1097 * to the structure, for example:
1098 * \code
1099 * psa_mac_operation_t operation;
1100 * operation = psa_mac_operation_init();
1101 * \endcode
1102 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001103 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001104 * make any assumptions about the content of this structure except
1105 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001106typedef struct psa_mac_operation_s psa_mac_operation_t;
1107
Jaeden Amero769ce272019-01-04 11:48:03 +00001108/** \def PSA_MAC_OPERATION_INIT
1109 *
1110 * This macro returns a suitable initializer for a MAC operation object of type
1111 * #psa_mac_operation_t.
1112 */
1113#ifdef __DOXYGEN_ONLY__
1114/* This is an example definition for documentation purposes.
1115 * Implementations should define a suitable value in `crypto_struct.h`.
1116 */
1117#define PSA_MAC_OPERATION_INIT {0}
1118#endif
1119
1120/** Return an initial value for a MAC operation object.
1121 */
1122static psa_mac_operation_t psa_mac_operation_init(void);
1123
Gilles Peskinef45adda2019-01-14 18:29:18 +01001124/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001125 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001126 * This function sets up the calculation of the MAC
1127 * (message authentication code) of a byte string.
1128 * To verify the MAC of a message against an
1129 * expected value, use psa_mac_verify_setup() instead.
1130 *
1131 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001132 * -# Allocate an operation object which will be passed to all the functions
1133 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001134 * -# Initialize the operation object with one of the methods described in the
1135 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001136 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001137 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1138 * of the message each time. The MAC that is calculated is the MAC
1139 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001140 * -# At the end of the message, call psa_mac_sign_finish() to finish
1141 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001142 *
1143 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001144 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001145 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001146 * After a successful call to psa_mac_sign_setup(), the application must
1147 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001148 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001149 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001150 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001151 * \param[in,out] operation The operation object to set up. It must have
1152 * been initialized as per the documentation for
1153 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001154 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001155 * It must remain valid until the operation
1156 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001157 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1158 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001159 *
Gilles Peskine28538492018-07-11 17:34:00 +02001160 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001161 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001162 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001163 * \retval #PSA_ERROR_EMPTY_SLOT
1164 * \retval #PSA_ERROR_NOT_PERMITTED
1165 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001166 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001167 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001168 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001169 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1170 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1171 * \retval #PSA_ERROR_HARDWARE_FAILURE
1172 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001173 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001174 * The library has not been previously initialized by psa_crypto_init().
1175 * It is implementation-dependent whether a failure to initialize
1176 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001177 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001178psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001179 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001180 psa_algorithm_t alg);
1181
Gilles Peskinef45adda2019-01-14 18:29:18 +01001182/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001183 *
1184 * This function sets up the verification of the MAC
1185 * (message authentication code) of a byte string against an expected value.
1186 *
1187 * The sequence of operations to verify a MAC is as follows:
1188 * -# Allocate an operation object which will be passed to all the functions
1189 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001190 * -# Initialize the operation object with one of the methods described in the
1191 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001192 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001193 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1194 * of the message each time. The MAC that is calculated is the MAC
1195 * of the concatenation of these messages in order.
1196 * -# At the end of the message, call psa_mac_verify_finish() to finish
1197 * calculating the actual MAC of the message and verify it against
1198 * the expected value.
1199 *
1200 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001201 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001202 *
1203 * After a successful call to psa_mac_verify_setup(), the application must
1204 * eventually terminate the operation through one of the following methods:
1205 * - A failed call to psa_mac_update().
1206 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1207 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001208 * \param[in,out] operation The operation object to set up. It must have
1209 * been initialized as per the documentation for
1210 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001211 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001212 * It must remain valid until the operation
1213 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001214 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1215 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001216 *
Gilles Peskine28538492018-07-11 17:34:00 +02001217 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001218 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001219 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001220 * \retval #PSA_ERROR_EMPTY_SLOT
1221 * \retval #PSA_ERROR_NOT_PERMITTED
1222 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001223 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001224 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001225 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001226 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1227 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1228 * \retval #PSA_ERROR_HARDWARE_FAILURE
1229 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001230 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001231 * The library has not been previously initialized by psa_crypto_init().
1232 * It is implementation-dependent whether a failure to initialize
1233 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001234 */
1235psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001236 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001237 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001238
Gilles Peskinedcd14942018-07-12 00:30:52 +02001239/** Add a message fragment to a multipart MAC operation.
1240 *
1241 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1242 * before calling this function.
1243 *
1244 * If this function returns an error status, the operation becomes inactive.
1245 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001246 * \param[in,out] operation Active MAC operation.
1247 * \param[in] input Buffer containing the message fragment to add to
1248 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001249 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001250 *
1251 * \retval #PSA_SUCCESS
1252 * Success.
1253 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001254 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001255 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1256 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1257 * \retval #PSA_ERROR_HARDWARE_FAILURE
1258 * \retval #PSA_ERROR_TAMPERING_DETECTED
1259 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001260psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1261 const uint8_t *input,
1262 size_t input_length);
1263
Gilles Peskinedcd14942018-07-12 00:30:52 +02001264/** Finish the calculation of the MAC of a message.
1265 *
1266 * The application must call psa_mac_sign_setup() before calling this function.
1267 * This function calculates the MAC of the message formed by concatenating
1268 * the inputs passed to preceding calls to psa_mac_update().
1269 *
1270 * When this function returns, the operation becomes inactive.
1271 *
1272 * \warning Applications should not call this function if they expect
1273 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1274 * Beware that comparing integrity or authenticity data such as
1275 * MAC values with a function such as \c memcmp is risky
1276 * because the time taken by the comparison may leak information
1277 * about the MAC value which could allow an attacker to guess
1278 * a valid MAC and thereby bypass security controls.
1279 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001280 * \param[in,out] operation Active MAC operation.
1281 * \param[out] mac Buffer where the MAC value is to be written.
1282 * \param mac_size Size of the \p mac buffer in bytes.
1283 * \param[out] mac_length On success, the number of bytes
1284 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001285 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001286 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001287 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001288 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001289 *
1290 * \retval #PSA_SUCCESS
1291 * Success.
1292 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001293 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001294 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001295 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001296 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1297 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1298 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1299 * \retval #PSA_ERROR_HARDWARE_FAILURE
1300 * \retval #PSA_ERROR_TAMPERING_DETECTED
1301 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001302psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1303 uint8_t *mac,
1304 size_t mac_size,
1305 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001306
Gilles Peskinedcd14942018-07-12 00:30:52 +02001307/** Finish the calculation of the MAC of a message and compare it with
1308 * an expected value.
1309 *
1310 * The application must call psa_mac_verify_setup() before calling this function.
1311 * This function calculates the MAC of the message formed by concatenating
1312 * the inputs passed to preceding calls to psa_mac_update(). It then
1313 * compares the calculated MAC with the expected MAC passed as a
1314 * parameter to this function.
1315 *
1316 * When this function returns, the operation becomes inactive.
1317 *
1318 * \note Implementations shall make the best effort to ensure that the
1319 * comparison between the actual MAC and the expected MAC is performed
1320 * in constant time.
1321 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001322 * \param[in,out] operation Active MAC operation.
1323 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001324 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001325 *
1326 * \retval #PSA_SUCCESS
1327 * The expected MAC is identical to the actual MAC of the message.
1328 * \retval #PSA_ERROR_INVALID_SIGNATURE
1329 * The MAC of the message was calculated successfully, but it
1330 * differs from the expected MAC.
1331 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001332 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001333 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1334 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1335 * \retval #PSA_ERROR_HARDWARE_FAILURE
1336 * \retval #PSA_ERROR_TAMPERING_DETECTED
1337 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001338psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1339 const uint8_t *mac,
1340 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001341
Gilles Peskinedcd14942018-07-12 00:30:52 +02001342/** Abort a MAC operation.
1343 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001344 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001345 * \p operation structure itself. Once aborted, the operation object
1346 * can be reused for another operation by calling
1347 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001348 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001349 * You may call this function any time after the operation object has
1350 * been initialized by any of the following methods:
1351 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1352 * it succeeds or not.
1353 * - Initializing the \c struct to all-bits-zero.
1354 * - Initializing the \c struct to logical zeros, e.g.
1355 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001356 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001357 * In particular, calling psa_mac_abort() after the operation has been
1358 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1359 * psa_mac_verify_finish() is safe and has no effect.
1360 *
1361 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001362 *
1363 * \retval #PSA_SUCCESS
1364 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001365 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001366 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1367 * \retval #PSA_ERROR_HARDWARE_FAILURE
1368 * \retval #PSA_ERROR_TAMPERING_DETECTED
1369 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001370psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1371
1372/**@}*/
1373
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001374/** \defgroup cipher Symmetric ciphers
1375 * @{
1376 */
1377
Gilles Peskine69647a42019-01-14 20:18:12 +01001378/** Encrypt a message using a symmetric cipher.
1379 *
1380 * This function encrypts a message with a random IV (initialization
1381 * vector).
1382 *
1383 * \param handle Handle to the key to use for the operation.
1384 * It must remain valid until the operation
1385 * terminates.
1386 * \param alg The cipher algorithm to compute
1387 * (\c PSA_ALG_XXX value such that
1388 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1389 * \param[in] input Buffer containing the message to encrypt.
1390 * \param input_length Size of the \p input buffer in bytes.
1391 * \param[out] output Buffer where the output is to be written.
1392 * The output contains the IV followed by
1393 * the ciphertext proper.
1394 * \param output_size Size of the \p output buffer in bytes.
1395 * \param[out] output_length On success, the number of bytes
1396 * that make up the output.
1397 *
1398 * \retval #PSA_SUCCESS
1399 * Success.
1400 * \retval #PSA_ERROR_INVALID_HANDLE
1401 * \retval #PSA_ERROR_EMPTY_SLOT
1402 * \retval #PSA_ERROR_NOT_PERMITTED
1403 * \retval #PSA_ERROR_INVALID_ARGUMENT
1404 * \p key is not compatible with \p alg.
1405 * \retval #PSA_ERROR_NOT_SUPPORTED
1406 * \p alg is not supported or is not a cipher algorithm.
1407 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1408 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1409 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1410 * \retval #PSA_ERROR_HARDWARE_FAILURE
1411 * \retval #PSA_ERROR_TAMPERING_DETECTED
1412 */
1413psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1414 psa_algorithm_t alg,
1415 const uint8_t *input,
1416 size_t input_length,
1417 uint8_t *output,
1418 size_t output_size,
1419 size_t *output_length);
1420
1421/** Decrypt a message using a symmetric cipher.
1422 *
1423 * This function decrypts a message encrypted with a symmetric cipher.
1424 *
1425 * \param handle Handle to the key to use for the operation.
1426 * It must remain valid until the operation
1427 * terminates.
1428 * \param alg The cipher algorithm to compute
1429 * (\c PSA_ALG_XXX value such that
1430 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1431 * \param[in] input Buffer containing the message to decrypt.
1432 * This consists of the IV followed by the
1433 * ciphertext proper.
1434 * \param input_length Size of the \p input buffer in bytes.
1435 * \param[out] output Buffer where the plaintext is to be written.
1436 * \param output_size Size of the \p output buffer in bytes.
1437 * \param[out] output_length On success, the number of bytes
1438 * that make up the output.
1439 *
1440 * \retval #PSA_SUCCESS
1441 * Success.
1442 * \retval #PSA_ERROR_INVALID_HANDLE
1443 * \retval #PSA_ERROR_EMPTY_SLOT
1444 * \retval #PSA_ERROR_NOT_PERMITTED
1445 * \retval #PSA_ERROR_INVALID_ARGUMENT
1446 * \p key is not compatible with \p alg.
1447 * \retval #PSA_ERROR_NOT_SUPPORTED
1448 * \p alg is not supported or is not a cipher algorithm.
1449 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1450 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1451 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1452 * \retval #PSA_ERROR_HARDWARE_FAILURE
1453 * \retval #PSA_ERROR_TAMPERING_DETECTED
1454 */
1455psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1456 psa_algorithm_t alg,
1457 const uint8_t *input,
1458 size_t input_length,
1459 uint8_t *output,
1460 size_t output_size,
1461 size_t *output_length);
1462
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001463/** The type of the state data structure for multipart cipher operations.
1464 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001465 * Before calling any function on a cipher operation object, the application
1466 * must initialize it by any of the following means:
1467 * - Set the structure to all-bits-zero, for example:
1468 * \code
1469 * psa_cipher_operation_t operation;
1470 * memset(&operation, 0, sizeof(operation));
1471 * \endcode
1472 * - Initialize the structure to logical zero values, for example:
1473 * \code
1474 * psa_cipher_operation_t operation = {0};
1475 * \endcode
1476 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1477 * for example:
1478 * \code
1479 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1480 * \endcode
1481 * - Assign the result of the function psa_cipher_operation_init()
1482 * to the structure, for example:
1483 * \code
1484 * psa_cipher_operation_t operation;
1485 * operation = psa_cipher_operation_init();
1486 * \endcode
1487 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001488 * This is an implementation-defined \c struct. Applications should not
1489 * make any assumptions about the content of this structure except
1490 * as directed by the documentation of a specific implementation. */
1491typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1492
Jaeden Amero5bae2272019-01-04 11:48:27 +00001493/** \def PSA_CIPHER_OPERATION_INIT
1494 *
1495 * This macro returns a suitable initializer for a cipher operation object of
1496 * type #psa_cipher_operation_t.
1497 */
1498#ifdef __DOXYGEN_ONLY__
1499/* This is an example definition for documentation purposes.
1500 * Implementations should define a suitable value in `crypto_struct.h`.
1501 */
1502#define PSA_CIPHER_OPERATION_INIT {0}
1503#endif
1504
1505/** Return an initial value for a cipher operation object.
1506 */
1507static psa_cipher_operation_t psa_cipher_operation_init(void);
1508
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001509/** Set the key for a multipart symmetric encryption operation.
1510 *
1511 * The sequence of operations to encrypt a message with a symmetric cipher
1512 * is as follows:
1513 * -# Allocate an operation object which will be passed to all the functions
1514 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001515 * -# Initialize the operation object with one of the methods described in the
1516 * documentation for #psa_cipher_operation_t, e.g.
1517 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001518 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001519 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001520 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001521 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001522 * requires a specific IV value.
1523 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1524 * of the message each time.
1525 * -# Call psa_cipher_finish().
1526 *
1527 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001528 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001529 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001530 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001531 * eventually terminate the operation. The following events terminate an
1532 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001533 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001534 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001535 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001536 * \param[in,out] operation The operation object to set up. It must have
1537 * been initialized as per the documentation for
1538 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001539 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001540 * It must remain valid until the operation
1541 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001542 * \param alg The cipher algorithm to compute
1543 * (\c PSA_ALG_XXX value such that
1544 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001545 *
Gilles Peskine28538492018-07-11 17:34:00 +02001546 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001547 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001548 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001549 * \retval #PSA_ERROR_EMPTY_SLOT
1550 * \retval #PSA_ERROR_NOT_PERMITTED
1551 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001552 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001553 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001554 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001555 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1556 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1557 * \retval #PSA_ERROR_HARDWARE_FAILURE
1558 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001559 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001560 * The library has not been previously initialized by psa_crypto_init().
1561 * It is implementation-dependent whether a failure to initialize
1562 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001563 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001564psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001565 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001566 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001567
1568/** Set the key for a multipart symmetric decryption operation.
1569 *
1570 * The sequence of operations to decrypt a message with a symmetric cipher
1571 * is as follows:
1572 * -# Allocate an operation object which will be passed to all the functions
1573 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001574 * -# Initialize the operation object with one of the methods described in the
1575 * documentation for #psa_cipher_operation_t, e.g.
1576 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001577 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001578 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001579 * decryption. If the IV is prepended to the ciphertext, you can call
1580 * psa_cipher_update() on a buffer containing the IV followed by the
1581 * beginning of the message.
1582 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1583 * of the message each time.
1584 * -# Call psa_cipher_finish().
1585 *
1586 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001587 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001588 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001589 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001590 * eventually terminate the operation. The following events terminate an
1591 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001592 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001593 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001594 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001595 * \param[in,out] operation The operation object to set up. It must have
1596 * been initialized as per the documentation for
1597 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001598 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001599 * It must remain valid until the operation
1600 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001601 * \param alg The cipher algorithm to compute
1602 * (\c PSA_ALG_XXX value such that
1603 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001604 *
Gilles Peskine28538492018-07-11 17:34:00 +02001605 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001606 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001607 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001608 * \retval #PSA_ERROR_EMPTY_SLOT
1609 * \retval #PSA_ERROR_NOT_PERMITTED
1610 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001611 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001612 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001613 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001614 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1615 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1616 * \retval #PSA_ERROR_HARDWARE_FAILURE
1617 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001618 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001619 * The library has not been previously initialized by psa_crypto_init().
1620 * It is implementation-dependent whether a failure to initialize
1621 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001622 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001623psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001624 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001625 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001626
Gilles Peskinedcd14942018-07-12 00:30:52 +02001627/** Generate an IV for a symmetric encryption operation.
1628 *
1629 * This function generates a random IV (initialization vector), nonce
1630 * or initial counter value for the encryption operation as appropriate
1631 * for the chosen algorithm, key type and key size.
1632 *
1633 * The application must call psa_cipher_encrypt_setup() before
1634 * calling this function.
1635 *
1636 * If this function returns an error status, the operation becomes inactive.
1637 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001638 * \param[in,out] operation Active cipher operation.
1639 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001640 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001641 * \param[out] iv_length On success, the number of bytes of the
1642 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001643 *
1644 * \retval #PSA_SUCCESS
1645 * Success.
1646 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001647 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001648 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001649 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001650 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1651 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1652 * \retval #PSA_ERROR_HARDWARE_FAILURE
1653 * \retval #PSA_ERROR_TAMPERING_DETECTED
1654 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001655psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1656 unsigned char *iv,
1657 size_t iv_size,
1658 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001659
Gilles Peskinedcd14942018-07-12 00:30:52 +02001660/** Set the IV for a symmetric encryption or decryption operation.
1661 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001662 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001663 * or initial counter value for the encryption or decryption operation.
1664 *
1665 * The application must call psa_cipher_encrypt_setup() before
1666 * calling this function.
1667 *
1668 * If this function returns an error status, the operation becomes inactive.
1669 *
1670 * \note When encrypting, applications should use psa_cipher_generate_iv()
1671 * instead of this function, unless implementing a protocol that requires
1672 * a non-random IV.
1673 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001674 * \param[in,out] operation Active cipher operation.
1675 * \param[in] iv Buffer containing the IV to use.
1676 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001677 *
1678 * \retval #PSA_SUCCESS
1679 * Success.
1680 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001681 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001682 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001683 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001684 * or the chosen algorithm does not use an IV.
1685 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1686 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1687 * \retval #PSA_ERROR_HARDWARE_FAILURE
1688 * \retval #PSA_ERROR_TAMPERING_DETECTED
1689 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001690psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1691 const unsigned char *iv,
1692 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001693
Gilles Peskinedcd14942018-07-12 00:30:52 +02001694/** Encrypt or decrypt a message fragment in an active cipher operation.
1695 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001696 * Before calling this function, you must:
1697 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1698 * The choice of setup function determines whether this function
1699 * encrypts or decrypts its input.
1700 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1701 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001702 *
1703 * If this function returns an error status, the operation becomes inactive.
1704 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001705 * \param[in,out] operation Active cipher operation.
1706 * \param[in] input Buffer containing the message fragment to
1707 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001708 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001709 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001710 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001711 * \param[out] output_length On success, the number of bytes
1712 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001713 *
1714 * \retval #PSA_SUCCESS
1715 * Success.
1716 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001717 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001718 * not set, or already completed).
1719 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1720 * The size of the \p output buffer is too small.
1721 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1722 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1723 * \retval #PSA_ERROR_HARDWARE_FAILURE
1724 * \retval #PSA_ERROR_TAMPERING_DETECTED
1725 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001726psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1727 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001728 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001729 unsigned char *output,
1730 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001731 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001732
Gilles Peskinedcd14942018-07-12 00:30:52 +02001733/** Finish encrypting or decrypting a message in a cipher operation.
1734 *
1735 * The application must call psa_cipher_encrypt_setup() or
1736 * psa_cipher_decrypt_setup() before calling this function. The choice
1737 * of setup function determines whether this function encrypts or
1738 * decrypts its input.
1739 *
1740 * This function finishes the encryption or decryption of the message
1741 * formed by concatenating the inputs passed to preceding calls to
1742 * psa_cipher_update().
1743 *
1744 * When this function returns, the operation becomes inactive.
1745 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001746 * \param[in,out] operation Active cipher operation.
1747 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001748 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001749 * \param[out] output_length On success, the number of bytes
1750 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001751 *
1752 * \retval #PSA_SUCCESS
1753 * Success.
1754 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001755 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001756 * not set, or already completed).
1757 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1758 * The size of the \p output buffer is too small.
1759 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1760 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1761 * \retval #PSA_ERROR_HARDWARE_FAILURE
1762 * \retval #PSA_ERROR_TAMPERING_DETECTED
1763 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001764psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001765 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001766 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001767 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001768
Gilles Peskinedcd14942018-07-12 00:30:52 +02001769/** Abort a cipher operation.
1770 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001771 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001772 * \p operation structure itself. Once aborted, the operation object
1773 * can be reused for another operation by calling
1774 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001775 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001776 * You may call this function any time after the operation object has
1777 * been initialized by any of the following methods:
1778 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1779 * whether it succeeds or not.
1780 * - Initializing the \c struct to all-bits-zero.
1781 * - Initializing the \c struct to logical zeros, e.g.
1782 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001783 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001784 * In particular, calling psa_cipher_abort() after the operation has been
1785 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1786 * is safe and has no effect.
1787 *
1788 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001789 *
1790 * \retval #PSA_SUCCESS
1791 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001792 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001793 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1794 * \retval #PSA_ERROR_HARDWARE_FAILURE
1795 * \retval #PSA_ERROR_TAMPERING_DETECTED
1796 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001797psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1798
1799/**@}*/
1800
Gilles Peskine3b555712018-03-03 21:27:57 +01001801/** \defgroup aead Authenticated encryption with associated data (AEAD)
1802 * @{
1803 */
1804
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001805/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001806 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001807 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001808 * \param alg The AEAD algorithm to compute
1809 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001810 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001811 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001812 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001813 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001814 * but not encrypted.
1815 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001816 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001817 * encrypted.
1818 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001819 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001820 * encrypted data. The additional data is not
1821 * part of this output. For algorithms where the
1822 * encrypted data and the authentication tag
1823 * are defined as separate outputs, the
1824 * authentication tag is appended to the
1825 * encrypted data.
1826 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1827 * This must be at least
1828 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1829 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001830 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001831 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001832 *
Gilles Peskine28538492018-07-11 17:34:00 +02001833 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001834 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001835 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001836 * \retval #PSA_ERROR_EMPTY_SLOT
1837 * \retval #PSA_ERROR_NOT_PERMITTED
1838 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001839 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001840 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001841 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001842 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1843 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1844 * \retval #PSA_ERROR_HARDWARE_FAILURE
1845 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001846 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001847 * The library has not been previously initialized by psa_crypto_init().
1848 * It is implementation-dependent whether a failure to initialize
1849 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001850 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001851psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001852 psa_algorithm_t alg,
1853 const uint8_t *nonce,
1854 size_t nonce_length,
1855 const uint8_t *additional_data,
1856 size_t additional_data_length,
1857 const uint8_t *plaintext,
1858 size_t plaintext_length,
1859 uint8_t *ciphertext,
1860 size_t ciphertext_size,
1861 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001862
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001863/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001864 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001865 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001866 * \param alg The AEAD algorithm to compute
1867 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001868 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001869 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001870 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001871 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001872 * but not encrypted.
1873 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001874 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001875 * encrypted. For algorithms where the
1876 * encrypted data and the authentication tag
1877 * are defined as separate inputs, the buffer
1878 * must contain the encrypted data followed
1879 * by the authentication tag.
1880 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001881 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001882 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1883 * This must be at least
1884 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1885 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001886 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001887 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001888 *
Gilles Peskine28538492018-07-11 17:34:00 +02001889 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001890 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001891 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001892 * \retval #PSA_ERROR_EMPTY_SLOT
1893 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001894 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001895 * \retval #PSA_ERROR_NOT_PERMITTED
1896 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001897 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001898 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001899 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001900 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1901 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1902 * \retval #PSA_ERROR_HARDWARE_FAILURE
1903 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001904 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001905 * The library has not been previously initialized by psa_crypto_init().
1906 * It is implementation-dependent whether a failure to initialize
1907 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001908 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001909psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001910 psa_algorithm_t alg,
1911 const uint8_t *nonce,
1912 size_t nonce_length,
1913 const uint8_t *additional_data,
1914 size_t additional_data_length,
1915 const uint8_t *ciphertext,
1916 size_t ciphertext_length,
1917 uint8_t *plaintext,
1918 size_t plaintext_size,
1919 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001920
Gilles Peskine30a9e412019-01-14 18:36:12 +01001921/** The type of the state data structure for multipart AEAD operations.
1922 *
1923 * Before calling any function on an AEAD operation object, the application
1924 * must initialize it by any of the following means:
1925 * - Set the structure to all-bits-zero, for example:
1926 * \code
1927 * psa_aead_operation_t operation;
1928 * memset(&operation, 0, sizeof(operation));
1929 * \endcode
1930 * - Initialize the structure to logical zero values, for example:
1931 * \code
1932 * psa_aead_operation_t operation = {0};
1933 * \endcode
1934 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
1935 * for example:
1936 * \code
1937 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
1938 * \endcode
1939 * - Assign the result of the function psa_aead_operation_init()
1940 * to the structure, for example:
1941 * \code
1942 * psa_aead_operation_t operation;
1943 * operation = psa_aead_operation_init();
1944 * \endcode
1945 *
1946 * This is an implementation-defined \c struct. Applications should not
1947 * make any assumptions about the content of this structure except
1948 * as directed by the documentation of a specific implementation. */
1949typedef struct psa_aead_operation_s psa_aead_operation_t;
1950
1951/** \def PSA_AEAD_OPERATION_INIT
1952 *
1953 * This macro returns a suitable initializer for an AEAD operation object of
1954 * type #psa_aead_operation_t.
1955 */
1956#ifdef __DOXYGEN_ONLY__
1957/* This is an example definition for documentation purposes.
1958 * Implementations should define a suitable value in `crypto_struct.h`.
1959 */
1960#define PSA_AEAD_OPERATION_INIT {0}
1961#endif
1962
1963/** Return an initial value for an AEAD operation object.
1964 */
1965static psa_aead_operation_t psa_aead_operation_init(void);
1966
1967/** Set the key for a multipart authenticated encryption operation.
1968 *
1969 * The sequence of operations to encrypt a message with authentication
1970 * is as follows:
1971 * -# Allocate an operation object which will be passed to all the functions
1972 * listed here.
1973 * -# Initialize the operation object with one of the methods described in the
1974 * documentation for #psa_aead_operation_t, e.g.
1975 * PSA_AEAD_OPERATION_INIT.
1976 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01001977 * -# If needed, call psa_aead_set_lengths() to specify the length of the
1978 * inputs to the subsequent calls to psa_aead_update_ad() and
1979 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
1980 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01001981 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
1982 * generate or set the nonce. You should use
1983 * psa_aead_generate_nonce() unless the protocol you are implementing
1984 * requires a specific nonce value.
1985 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
1986 * of the non-encrypted additional authenticated data each time.
1987 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01001988 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01001989 * -# Call psa_aead_finish().
1990 *
1991 * The application may call psa_aead_abort() at any time after the operation
1992 * has been initialized.
1993 *
1994 * After a successful call to psa_aead_encrypt_setup(), the application must
1995 * eventually terminate the operation. The following events terminate an
1996 * operation:
1997 * - A failed call to any of the \c psa_aead_xxx functions.
1998 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
1999 *
2000 * \param[in,out] operation The operation object to set up. It must have
2001 * been initialized as per the documentation for
2002 * #psa_aead_operation_t and not yet in use.
2003 * \param handle Handle to the key to use for the operation.
2004 * It must remain valid until the operation
2005 * terminates.
2006 * \param alg The AEAD algorithm to compute
2007 * (\c PSA_ALG_XXX value such that
2008 * #PSA_ALG_IS_AEAD(\p alg) is true).
2009 *
2010 * \retval #PSA_SUCCESS
2011 * Success.
2012 * \retval #PSA_ERROR_INVALID_HANDLE
2013 * \retval #PSA_ERROR_EMPTY_SLOT
2014 * \retval #PSA_ERROR_NOT_PERMITTED
2015 * \retval #PSA_ERROR_INVALID_ARGUMENT
2016 * \p key is not compatible with \p alg.
2017 * \retval #PSA_ERROR_NOT_SUPPORTED
2018 * \p alg is not supported or is not an AEAD algorithm.
2019 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2020 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2021 * \retval #PSA_ERROR_HARDWARE_FAILURE
2022 * \retval #PSA_ERROR_TAMPERING_DETECTED
2023 * \retval #PSA_ERROR_BAD_STATE
2024 * The library has not been previously initialized by psa_crypto_init().
2025 * It is implementation-dependent whether a failure to initialize
2026 * results in this error code.
2027 */
2028psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2029 psa_key_handle_t handle,
2030 psa_algorithm_t alg);
2031
2032/** Set the key for a multipart authenticated decryption operation.
2033 *
2034 * The sequence of operations to decrypt a message with authentication
2035 * is as follows:
2036 * -# Allocate an operation object which will be passed to all the functions
2037 * listed here.
2038 * -# Initialize the operation object with one of the methods described in the
2039 * documentation for #psa_aead_operation_t, e.g.
2040 * PSA_AEAD_OPERATION_INIT.
2041 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002042 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2043 * inputs to the subsequent calls to psa_aead_update_ad() and
2044 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2045 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002046 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2047 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2048 * of the non-encrypted additional authenticated data each time.
2049 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002050 * of the ciphertext to decrypt each time.
2051 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002052 *
2053 * The application may call psa_aead_abort() at any time after the operation
2054 * has been initialized.
2055 *
2056 * After a successful call to psa_aead_decrypt_setup(), the application must
2057 * eventually terminate the operation. The following events terminate an
2058 * operation:
2059 * - A failed call to any of the \c psa_aead_xxx functions.
2060 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2061 *
2062 * \param[in,out] operation The operation object to set up. It must have
2063 * been initialized as per the documentation for
2064 * #psa_aead_operation_t and not yet in use.
2065 * \param handle Handle to the key to use for the operation.
2066 * It must remain valid until the operation
2067 * terminates.
2068 * \param alg The AEAD algorithm to compute
2069 * (\c PSA_ALG_XXX value such that
2070 * #PSA_ALG_IS_AEAD(\p alg) is true).
2071 *
2072 * \retval #PSA_SUCCESS
2073 * Success.
2074 * \retval #PSA_ERROR_INVALID_HANDLE
2075 * \retval #PSA_ERROR_EMPTY_SLOT
2076 * \retval #PSA_ERROR_NOT_PERMITTED
2077 * \retval #PSA_ERROR_INVALID_ARGUMENT
2078 * \p key is not compatible with \p alg.
2079 * \retval #PSA_ERROR_NOT_SUPPORTED
2080 * \p alg is not supported or is not an AEAD algorithm.
2081 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2082 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2083 * \retval #PSA_ERROR_HARDWARE_FAILURE
2084 * \retval #PSA_ERROR_TAMPERING_DETECTED
2085 * \retval #PSA_ERROR_BAD_STATE
2086 * The library has not been previously initialized by psa_crypto_init().
2087 * It is implementation-dependent whether a failure to initialize
2088 * results in this error code.
2089 */
2090psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2091 psa_key_handle_t handle,
2092 psa_algorithm_t alg);
2093
2094/** Generate a random nonce for an authenticated encryption operation.
2095 *
2096 * This function generates a random nonce for the authenticated encryption
2097 * operation with an appropriate size for the chosen algorithm, key type
2098 * and key size.
2099 *
2100 * The application must call psa_aead_encrypt_setup() before
2101 * calling this function.
2102 *
2103 * If this function returns an error status, the operation becomes inactive.
2104 *
2105 * \param[in,out] operation Active AEAD operation.
2106 * \param[out] nonce Buffer where the generated nonce is to be
2107 * written.
2108 * \param nonce_size Size of the \p nonce buffer in bytes.
2109 * \param[out] nonce_length On success, the number of bytes of the
2110 * generated nonce.
2111 *
2112 * \retval #PSA_SUCCESS
2113 * Success.
2114 * \retval #PSA_ERROR_BAD_STATE
2115 * The operation state is not valid (not set up, or nonce already set).
2116 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2117 * The size of the \p nonce buffer is too small.
2118 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2119 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2120 * \retval #PSA_ERROR_HARDWARE_FAILURE
2121 * \retval #PSA_ERROR_TAMPERING_DETECTED
2122 */
2123psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2124 unsigned char *nonce,
2125 size_t nonce_size,
2126 size_t *nonce_length);
2127
2128/** Set the nonce for an authenticated encryption or decryption operation.
2129 *
2130 * This function sets the nonce for the authenticated
2131 * encryption or decryption operation.
2132 *
2133 * The application must call psa_aead_encrypt_setup() before
2134 * calling this function.
2135 *
2136 * If this function returns an error status, the operation becomes inactive.
2137 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002138 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002139 * instead of this function, unless implementing a protocol that requires
2140 * a non-random IV.
2141 *
2142 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002143 * \param[in] nonce Buffer containing the nonce to use.
2144 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002145 *
2146 * \retval #PSA_SUCCESS
2147 * Success.
2148 * \retval #PSA_ERROR_BAD_STATE
2149 * The operation state is not valid (not set up, or nonce already set).
2150 * \retval #PSA_ERROR_INVALID_ARGUMENT
2151 * The size of \p nonce is not acceptable for the chosen algorithm.
2152 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2153 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2154 * \retval #PSA_ERROR_HARDWARE_FAILURE
2155 * \retval #PSA_ERROR_TAMPERING_DETECTED
2156 */
2157psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2158 const unsigned char *nonce,
2159 size_t nonce_length);
2160
Gilles Peskinebc59c852019-01-17 15:26:08 +01002161/** Declare the lengths of the message and additional data for AEAD.
2162 *
2163 * The application must call this function before calling
2164 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2165 * the operation requires it. If the algorithm does not require it,
2166 * calling this function is optional, but if this function is called
2167 * then the implementation must enforce the lengths.
2168 *
2169 * You may call this function before or after setting the nonce with
2170 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2171 *
2172 * - For #PSA_ALG_CCM, calling this function is required.
2173 * - For the other AEAD algorithms defined in this specification, calling
2174 * this function is not required.
2175 * - For vendor-defined algorithm, refer to the vendor documentation.
2176 *
2177 * \param[in,out] operation Active AEAD operation.
2178 * \param ad_length Size of the non-encrypted additional
2179 * authenticated data in bytes.
2180 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2181 *
2182 * \retval #PSA_SUCCESS
2183 * Success.
2184 * \retval #PSA_ERROR_BAD_STATE
2185 * The operation state is not valid (not set up, already completed,
2186 * or psa_aead_update_ad() or psa_aead_update() already called).
2187 * \retval #PSA_ERROR_INVALID_ARGUMENT
2188 * At least one of the lengths is not acceptable for the chosen
2189 * algorithm.
2190 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2191 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2192 * \retval #PSA_ERROR_HARDWARE_FAILURE
2193 * \retval #PSA_ERROR_TAMPERING_DETECTED
2194 */
2195psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2196 size_t ad_length,
2197 size_t plaintext_length);
2198
Gilles Peskine30a9e412019-01-14 18:36:12 +01002199/** Pass additional data to an active AEAD operation.
2200 *
2201 * Additional data is authenticated, but not encrypted.
2202 *
2203 * You may call this function multiple times to pass successive fragments
2204 * of the additional data. You may not call this function after passing
2205 * data to encrypt or decrypt with psa_aead_update().
2206 *
2207 * Before calling this function, you must:
2208 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2209 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2210 *
2211 * If this function returns an error status, the operation becomes inactive.
2212 *
2213 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2214 * there is no guarantee that the input is valid. Therefore, until
2215 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2216 * treat the input as untrusted and prepare to undo any action that
2217 * depends on the input if psa_aead_verify() returns an error status.
2218 *
2219 * \param[in,out] operation Active AEAD operation.
2220 * \param[in] input Buffer containing the fragment of
2221 * additional data.
2222 * \param input_length Size of the \p input buffer in bytes.
2223 *
2224 * \retval #PSA_SUCCESS
2225 * Success.
2226 * \retval #PSA_ERROR_BAD_STATE
2227 * The operation state is not valid (not set up, nonce not set,
2228 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002229 * \retval #PSA_ERROR_INVALID_ARGUMENT
2230 * The total input length overflows the additional data length that
2231 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002232 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2233 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2234 * \retval #PSA_ERROR_HARDWARE_FAILURE
2235 * \retval #PSA_ERROR_TAMPERING_DETECTED
2236 */
2237psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2238 const uint8_t *input,
2239 size_t input_length);
2240
2241/** Encrypt or decrypt a message fragment in an active AEAD operation.
2242 *
2243 * Before calling this function, you must:
2244 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2245 * The choice of setup function determines whether this function
2246 * encrypts or decrypts its input.
2247 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2248 * 3. Call psa_aead_update_ad() to pass all the additional data.
2249 *
2250 * If this function returns an error status, the operation becomes inactive.
2251 *
2252 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2253 * there is no guarantee that the input is valid. Therefore, until
2254 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2255 * - Do not use the output in any way other than storing it in a
2256 * confidential location. If you take any action that depends
2257 * on the tentative decrypted data, this action will need to be
2258 * undone if the input turns out not to be valid. Furthermore,
2259 * if an adversary can observe that this action took place
2260 * (for example through timing), they may be able to use this
2261 * fact as an oracle to decrypt any message encrypted with the
2262 * same key.
2263 * - In particular, do not copy the output anywhere but to a
2264 * memory or storage space that you have exclusive access to.
2265 *
2266 * \param[in,out] operation Active AEAD operation.
2267 * \param[in] input Buffer containing the message fragment to
2268 * encrypt or decrypt.
2269 * \param input_length Size of the \p input buffer in bytes.
2270 * \param[out] output Buffer where the output is to be written.
2271 * \param output_size Size of the \p output buffer in bytes.
2272 * \param[out] output_length On success, the number of bytes
2273 * that make up the returned output.
2274 *
2275 * \retval #PSA_SUCCESS
2276 * Success.
2277 * \retval #PSA_ERROR_BAD_STATE
2278 * The operation state is not valid (not set up, nonce not set
2279 * or already completed).
2280 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2281 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002282 * \retval #PSA_ERROR_INVALID_ARGUMENT
2283 * The total length of input to psa_aead_update_ad() so far is
2284 * less than the additional data length that was previously
2285 * specified with psa_aead_set_lengths().
2286 * \retval #PSA_ERROR_INVALID_ARGUMENT
2287 * The total input length overflows the plaintext length that
2288 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002289 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2290 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2291 * \retval #PSA_ERROR_HARDWARE_FAILURE
2292 * \retval #PSA_ERROR_TAMPERING_DETECTED
2293 */
2294psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2295 const uint8_t *input,
2296 size_t input_length,
2297 unsigned char *output,
2298 size_t output_size,
2299 size_t *output_length);
2300
2301/** Finish encrypting a message in an AEAD operation.
2302 *
2303 * The operation must have been set up with psa_aead_encrypt_setup().
2304 *
2305 * This function finishes the authentication of the additional data
2306 * formed by concatenating the inputs passed to preceding calls to
2307 * psa_aead_update_ad() with the plaintext formed by concatenating the
2308 * inputs passed to preceding calls to psa_aead_update().
2309 *
2310 * This function has two output buffers:
2311 * - \p ciphertext contains trailing ciphertext that was buffered from
2312 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2313 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2314 * will not contain any output and can be a 0-sized buffer.
2315 * - \p tag contains the authentication tag. Its length is always
2316 * #PSA_AEAD_TAG_LENGTH(\p alg) where \p alg is the AEAD algorithm
2317 * that the operation performs.
2318 *
2319 * When this function returns, the operation becomes inactive.
2320 *
2321 * \param[in,out] operation Active AEAD operation.
2322 * \param[out] ciphertext Buffer where the last part of the ciphertext
2323 * is to be written.
2324 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2325 * \param[out] ciphertext_length On success, the number of bytes of
2326 * returned ciphertext.
2327 * \param[out] tag Buffer where the authentication tag is
2328 * to be written.
2329 * \param tag_size Size of the \p tag buffer in bytes.
2330 * \param[out] tag_length On success, the number of bytes
2331 * that make up the returned tag.
2332 *
2333 * \retval #PSA_SUCCESS
2334 * Success.
2335 * \retval #PSA_ERROR_BAD_STATE
2336 * The operation state is not valid (not set up, nonce not set,
2337 * decryption, or already completed).
2338 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2339 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002340 * \retval #PSA_ERROR_INVALID_ARGUMENT
2341 * The total length of input to psa_aead_update_ad() so far is
2342 * less than the additional data length that was previously
2343 * specified with psa_aead_set_lengths().
2344 * \retval #PSA_ERROR_INVALID_ARGUMENT
2345 * The total length of input to psa_aead_update() so far is
2346 * less than the plaintext length that was previously
2347 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002348 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2349 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2350 * \retval #PSA_ERROR_HARDWARE_FAILURE
2351 * \retval #PSA_ERROR_TAMPERING_DETECTED
2352 */
2353psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002354 uint8_t *ciphertext,
2355 size_t ciphertext_size,
2356 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002357 uint8_t *tag,
2358 size_t tag_size,
2359 size_t *tag_length);
2360
2361/** Finish authenticating and decrypting a message in an AEAD operation.
2362 *
2363 * The operation must have been set up with psa_aead_decrypt_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 ciphertext formed by concatenating the
2368 * inputs passed to preceding calls to psa_aead_update().
2369 *
2370 * When this function returns, the operation becomes inactive.
2371 *
2372 * \param[in,out] operation Active AEAD operation.
2373 * \param[in] tag Buffer containing the authentication tag.
2374 * \param tag_length Size of the \p tag buffer in bytes.
2375 *
2376 * \retval #PSA_SUCCESS
2377 * Success.
2378 * \retval #PSA_ERROR_BAD_STATE
2379 * The operation state is not valid (not set up, nonce not set,
2380 * encryption, or already completed).
2381 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2382 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002383 * \retval #PSA_ERROR_INVALID_ARGUMENT
2384 * The total length of input to psa_aead_update_ad() so far is
2385 * less than the additional data length that was previously
2386 * specified with psa_aead_set_lengths().
2387 * \retval #PSA_ERROR_INVALID_ARGUMENT
2388 * The total length of input to psa_aead_update() so far is
2389 * less than the plaintext length that was previously
2390 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002391 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2392 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2393 * \retval #PSA_ERROR_HARDWARE_FAILURE
2394 * \retval #PSA_ERROR_TAMPERING_DETECTED
2395 */
2396psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2397 const uint8_t *tag,
2398 size_t tag_length);
2399
2400/** Abort an AEAD operation.
2401 *
2402 * Aborting an operation frees all associated resources except for the
2403 * \p operation structure itself. Once aborted, the operation object
2404 * can be reused for another operation by calling
2405 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2406 *
2407 * You may call this function any time after the operation object has
2408 * been initialized by any of the following methods:
2409 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2410 * whether it succeeds or not.
2411 * - Initializing the \c struct to all-bits-zero.
2412 * - Initializing the \c struct to logical zeros, e.g.
2413 * `psa_aead_operation_t operation = {0}`.
2414 *
2415 * In particular, calling psa_aead_abort() after the operation has been
2416 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2417 * is safe and has no effect.
2418 *
2419 * \param[in,out] operation Initialized AEAD operation.
2420 *
2421 * \retval #PSA_SUCCESS
2422 * \retval #PSA_ERROR_BAD_STATE
2423 * \p operation is not an active AEAD operation.
2424 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2425 * \retval #PSA_ERROR_HARDWARE_FAILURE
2426 * \retval #PSA_ERROR_TAMPERING_DETECTED
2427 */
2428psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2429
Gilles Peskine3b555712018-03-03 21:27:57 +01002430/**@}*/
2431
Gilles Peskine20035e32018-02-03 22:44:14 +01002432/** \defgroup asymmetric Asymmetric cryptography
2433 * @{
2434 */
2435
2436/**
2437 * \brief Sign a hash or short message with a private key.
2438 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002439 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002440 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002441 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2442 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2443 * to determine the hash algorithm to use.
2444 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002445 * \param handle Handle to the key to use for the operation.
2446 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002447 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002448 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002449 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002450 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002451 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002452 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002453 * \param[out] signature_length On success, the number of bytes
2454 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002455 *
Gilles Peskine28538492018-07-11 17:34:00 +02002456 * \retval #PSA_SUCCESS
2457 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002458 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002459 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002460 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002461 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002462 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002463 * \retval #PSA_ERROR_NOT_SUPPORTED
2464 * \retval #PSA_ERROR_INVALID_ARGUMENT
2465 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2466 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2467 * \retval #PSA_ERROR_HARDWARE_FAILURE
2468 * \retval #PSA_ERROR_TAMPERING_DETECTED
2469 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002470 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002471 * The library has not been previously initialized by psa_crypto_init().
2472 * It is implementation-dependent whether a failure to initialize
2473 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002474 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002475psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002476 psa_algorithm_t alg,
2477 const uint8_t *hash,
2478 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002479 uint8_t *signature,
2480 size_t signature_size,
2481 size_t *signature_length);
2482
2483/**
2484 * \brief Verify the signature a hash or short message using a public key.
2485 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002486 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002487 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002488 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2489 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2490 * to determine the hash algorithm to use.
2491 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002492 * \param handle Handle to the key to use for the operation.
2493 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002494 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002495 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002496 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002497 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002498 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002499 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002500 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002501 *
Gilles Peskine28538492018-07-11 17:34:00 +02002502 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002503 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002504 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002505 * The calculation was perfomed successfully, but the passed
2506 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002507 * \retval #PSA_ERROR_NOT_SUPPORTED
2508 * \retval #PSA_ERROR_INVALID_ARGUMENT
2509 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2510 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2511 * \retval #PSA_ERROR_HARDWARE_FAILURE
2512 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002513 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002514 * The library has not been previously initialized by psa_crypto_init().
2515 * It is implementation-dependent whether a failure to initialize
2516 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002517 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002518psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002519 psa_algorithm_t alg,
2520 const uint8_t *hash,
2521 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002522 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002523 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002524
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002525/**
2526 * \brief Encrypt a short message with a public key.
2527 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002528 * \param handle Handle to the key to use for the operation.
2529 * It must be a public key or an asymmetric
2530 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002531 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002532 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002533 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002534 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002535 * \param[in] salt A salt or label, if supported by the
2536 * encryption algorithm.
2537 * If the algorithm does not support a
2538 * salt, pass \c NULL.
2539 * If the algorithm supports an optional
2540 * salt and you do not want to pass a salt,
2541 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002542 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002543 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2544 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002545 * \param salt_length Size of the \p salt buffer in bytes.
2546 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002547 * \param[out] output Buffer where the encrypted message is to
2548 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002549 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002550 * \param[out] output_length On success, the number of bytes
2551 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002552 *
Gilles Peskine28538492018-07-11 17:34:00 +02002553 * \retval #PSA_SUCCESS
2554 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002555 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002556 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002557 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002558 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002559 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002560 * \retval #PSA_ERROR_NOT_SUPPORTED
2561 * \retval #PSA_ERROR_INVALID_ARGUMENT
2562 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2563 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2564 * \retval #PSA_ERROR_HARDWARE_FAILURE
2565 * \retval #PSA_ERROR_TAMPERING_DETECTED
2566 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002567 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002568 * The library has not been previously initialized by psa_crypto_init().
2569 * It is implementation-dependent whether a failure to initialize
2570 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002571 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002572psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002573 psa_algorithm_t alg,
2574 const uint8_t *input,
2575 size_t input_length,
2576 const uint8_t *salt,
2577 size_t salt_length,
2578 uint8_t *output,
2579 size_t output_size,
2580 size_t *output_length);
2581
2582/**
2583 * \brief Decrypt a short message with a private key.
2584 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002585 * \param handle Handle to the key to use for the operation.
2586 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002587 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002588 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002589 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002590 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002591 * \param[in] salt A salt or label, if supported by the
2592 * encryption algorithm.
2593 * If the algorithm does not support a
2594 * salt, pass \c NULL.
2595 * If the algorithm supports an optional
2596 * salt and you do not want to pass a salt,
2597 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002598 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002599 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2600 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002601 * \param salt_length Size of the \p salt buffer in bytes.
2602 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002603 * \param[out] output Buffer where the decrypted message is to
2604 * be written.
2605 * \param output_size Size of the \c output buffer in bytes.
2606 * \param[out] output_length On success, the number of bytes
2607 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002608 *
Gilles Peskine28538492018-07-11 17:34:00 +02002609 * \retval #PSA_SUCCESS
2610 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002611 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002612 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002613 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002614 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002615 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002616 * \retval #PSA_ERROR_NOT_SUPPORTED
2617 * \retval #PSA_ERROR_INVALID_ARGUMENT
2618 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2619 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2620 * \retval #PSA_ERROR_HARDWARE_FAILURE
2621 * \retval #PSA_ERROR_TAMPERING_DETECTED
2622 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2623 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002624 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002625 * The library has not been previously initialized by psa_crypto_init().
2626 * It is implementation-dependent whether a failure to initialize
2627 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002628 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002629psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002630 psa_algorithm_t alg,
2631 const uint8_t *input,
2632 size_t input_length,
2633 const uint8_t *salt,
2634 size_t salt_length,
2635 uint8_t *output,
2636 size_t output_size,
2637 size_t *output_length);
2638
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002639/**@}*/
2640
Gilles Peskineedd76872018-07-20 17:42:05 +02002641/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002642 * @{
2643 */
2644
2645/** The type of the state data structure for generators.
2646 *
2647 * Before calling any function on a generator, the application must
2648 * initialize it by any of the following means:
2649 * - Set the structure to all-bits-zero, for example:
2650 * \code
2651 * psa_crypto_generator_t generator;
2652 * memset(&generator, 0, sizeof(generator));
2653 * \endcode
2654 * - Initialize the structure to logical zero values, for example:
2655 * \code
2656 * psa_crypto_generator_t generator = {0};
2657 * \endcode
2658 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2659 * for example:
2660 * \code
2661 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2662 * \endcode
2663 * - Assign the result of the function psa_crypto_generator_init()
2664 * to the structure, for example:
2665 * \code
2666 * psa_crypto_generator_t generator;
2667 * generator = psa_crypto_generator_init();
2668 * \endcode
2669 *
2670 * This is an implementation-defined \c struct. Applications should not
2671 * make any assumptions about the content of this structure except
2672 * as directed by the documentation of a specific implementation.
2673 */
2674typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2675
2676/** \def PSA_CRYPTO_GENERATOR_INIT
2677 *
2678 * This macro returns a suitable initializer for a generator object
2679 * of type #psa_crypto_generator_t.
2680 */
2681#ifdef __DOXYGEN_ONLY__
2682/* This is an example definition for documentation purposes.
2683 * Implementations should define a suitable value in `crypto_struct.h`.
2684 */
2685#define PSA_CRYPTO_GENERATOR_INIT {0}
2686#endif
2687
2688/** Return an initial value for a generator object.
2689 */
2690static psa_crypto_generator_t psa_crypto_generator_init(void);
2691
2692/** Retrieve the current capacity of a generator.
2693 *
2694 * The capacity of a generator is the maximum number of bytes that it can
2695 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2696 *
2697 * \param[in] generator The generator to query.
2698 * \param[out] capacity On success, the capacity of the generator.
2699 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002700 * \retval #PSA_SUCCESS
2701 * \retval #PSA_ERROR_BAD_STATE
2702 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002703 */
2704psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2705 size_t *capacity);
2706
2707/** Read some data from a generator.
2708 *
2709 * This function reads and returns a sequence of bytes from a generator.
2710 * The data that is read is discarded from the generator. The generator's
2711 * capacity is decreased by the number of bytes read.
2712 *
2713 * \param[in,out] generator The generator object to read from.
2714 * \param[out] output Buffer where the generator output will be
2715 * written.
2716 * \param output_length Number of bytes to output.
2717 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002718 * \retval #PSA_SUCCESS
2719 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002720 * There were fewer than \p output_length bytes
2721 * in the generator. Note that in this case, no
2722 * output is written to the output buffer.
2723 * The generator's capacity is set to 0, thus
2724 * subsequent calls to this function will not
2725 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002726 * \retval #PSA_ERROR_BAD_STATE
2727 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2728 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2729 * \retval #PSA_ERROR_HARDWARE_FAILURE
2730 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002731 */
2732psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2733 uint8_t *output,
2734 size_t output_length);
2735
2736/** Create a symmetric key from data read from a generator.
2737 *
2738 * This function reads a sequence of bytes from a generator and imports
2739 * these bytes as a key.
2740 * The data that is read is discarded from the generator. The generator's
2741 * capacity is decreased by the number of bytes read.
2742 *
2743 * This function is equivalent to calling #psa_generator_read and
2744 * passing the resulting output to #psa_import_key, but
2745 * if the implementation provides an isolation boundary then
2746 * the key material is not exposed outside the isolation boundary.
2747 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002748 * \param handle Handle to the slot where the key will be stored.
2749 * This must be a valid slot for a key of the chosen
2750 * type: it must have been obtained by calling
2751 * psa_allocate_key() or psa_create_key() with the
2752 * correct \p type and with a maximum size that is
2753 * compatible with \p bits.
2754 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002755 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2756 * This must be a symmetric key type.
2757 * \param bits Key size in bits.
2758 * \param[in,out] generator The generator object to read from.
2759 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002760 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002761 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002762 * If the key is persistent, the key material and the key's metadata
2763 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002764 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002765 * There were fewer than \p output_length bytes
2766 * in the generator. Note that in this case, no
2767 * output is written to the output buffer.
2768 * The generator's capacity is set to 0, thus
2769 * subsequent calls to this function will not
2770 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002771 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002772 * The key type or key size is not supported, either by the
2773 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002774 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002775 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002776 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002777 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002778 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2779 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2780 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2781 * \retval #PSA_ERROR_HARDWARE_FAILURE
2782 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002783 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002784 * The library has not been previously initialized by psa_crypto_init().
2785 * It is implementation-dependent whether a failure to initialize
2786 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002787 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002788psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002789 psa_key_type_t type,
2790 size_t bits,
2791 psa_crypto_generator_t *generator);
2792
2793/** Abort a generator.
2794 *
2795 * Once a generator has been aborted, its capacity is zero.
2796 * Aborting a generator frees all associated resources except for the
2797 * \c generator structure itself.
2798 *
2799 * This function may be called at any time as long as the generator
2800 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2801 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2802 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2803 * on a generator that has not been set up.
2804 *
2805 * Once aborted, the generator object may be called.
2806 *
2807 * \param[in,out] generator The generator to abort.
2808 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002809 * \retval #PSA_SUCCESS
2810 * \retval #PSA_ERROR_BAD_STATE
2811 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2812 * \retval #PSA_ERROR_HARDWARE_FAILURE
2813 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002814 */
2815psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2816
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002817/** Use the maximum possible capacity for a generator.
2818 *
2819 * Use this value as the capacity argument when setting up a generator
2820 * to indicate that the generator should have the maximum possible capacity.
2821 * The value of the maximum possible capacity depends on the generator
2822 * algorithm.
2823 */
2824#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2825
Gilles Peskineeab56e42018-07-12 17:12:33 +02002826/**@}*/
2827
Gilles Peskineea0fb492018-07-12 17:17:20 +02002828/** \defgroup derivation Key derivation
2829 * @{
2830 */
2831
2832/** Set up a key derivation operation.
2833 *
2834 * A key derivation algorithm takes three inputs: a secret input \p key and
2835 * two non-secret inputs \p label and p salt.
2836 * The result of this function is a byte generator which can
2837 * be used to produce keys and other cryptographic material.
2838 *
2839 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002840 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2841 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002842 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002843 * \param[in,out] generator The generator object to set up. It must have
2844 * been initialized as per the documentation for
2845 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002846 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002847 * \param alg The key derivation algorithm to compute
2848 * (\c PSA_ALG_XXX value such that
2849 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2850 * \param[in] salt Salt to use.
2851 * \param salt_length Size of the \p salt buffer in bytes.
2852 * \param[in] label Label to use.
2853 * \param label_length Size of the \p label buffer in bytes.
2854 * \param capacity The maximum number of bytes that the
2855 * generator will be able to provide.
2856 *
2857 * \retval #PSA_SUCCESS
2858 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002859 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02002860 * \retval #PSA_ERROR_EMPTY_SLOT
2861 * \retval #PSA_ERROR_NOT_PERMITTED
2862 * \retval #PSA_ERROR_INVALID_ARGUMENT
2863 * \c key is not compatible with \c alg,
2864 * or \p capacity is too large for the specified algorithm and key.
2865 * \retval #PSA_ERROR_NOT_SUPPORTED
2866 * \c alg is not supported or is not a key derivation algorithm.
2867 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2868 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2869 * \retval #PSA_ERROR_HARDWARE_FAILURE
2870 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002871 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002872 * The library has not been previously initialized by psa_crypto_init().
2873 * It is implementation-dependent whether a failure to initialize
2874 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002875 */
2876psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002877 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002878 psa_algorithm_t alg,
2879 const uint8_t *salt,
2880 size_t salt_length,
2881 const uint8_t *label,
2882 size_t label_length,
2883 size_t capacity);
2884
Gilles Peskine01d718c2018-09-18 12:01:02 +02002885/** Set up a key agreement operation.
2886 *
2887 * A key agreement algorithm takes two inputs: a private key \p private_key
2888 * a public key \p peer_key.
2889 * The result of this function is a byte generator which can
2890 * be used to produce keys and other cryptographic material.
2891 *
Gilles Peskine211a4362018-10-25 22:22:31 +02002892 * The resulting generator always has the maximum capacity permitted by
2893 * the algorithm.
2894 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002895 * \param[in,out] generator The generator object to set up. It must have
2896 * been initialized as per the documentation for
2897 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002898 * \param private_key Handle to the private key to use.
Gilles Peskined171e782018-11-15 17:46:21 +01002899 * \param[in] peer_key Public key of the peer. It must be
2900 * in the same format that psa_import_key()
2901 * accepts. The standard formats for public
2902 * keys are documented in the documentation
2903 * of psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02002904 * \param peer_key_length Size of \p peer_key in bytes.
2905 * \param alg The key agreement algorithm to compute
2906 * (\c PSA_ALG_XXX value such that
2907 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
2908 *
2909 * \retval #PSA_SUCCESS
2910 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002911 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02002912 * \retval #PSA_ERROR_EMPTY_SLOT
2913 * \retval #PSA_ERROR_NOT_PERMITTED
2914 * \retval #PSA_ERROR_INVALID_ARGUMENT
2915 * \c private_key is not compatible with \c alg,
2916 * or \p peer_key is not valid for \c alg or not compatible with
2917 * \c private_key.
2918 * \retval #PSA_ERROR_NOT_SUPPORTED
2919 * \c alg is not supported or is not a key derivation algorithm.
2920 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2921 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2922 * \retval #PSA_ERROR_HARDWARE_FAILURE
2923 * \retval #PSA_ERROR_TAMPERING_DETECTED
2924 */
2925psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002926 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02002927 const uint8_t *peer_key,
2928 size_t peer_key_length,
2929 psa_algorithm_t alg);
2930
Gilles Peskineea0fb492018-07-12 17:17:20 +02002931/**@}*/
2932
Gilles Peskineedd76872018-07-20 17:42:05 +02002933/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002934 * @{
2935 */
2936
2937/**
2938 * \brief Generate random bytes.
2939 *
2940 * \warning This function **can** fail! Callers MUST check the return status
2941 * and MUST NOT use the content of the output buffer if the return
2942 * status is not #PSA_SUCCESS.
2943 *
2944 * \note To generate a key, use psa_generate_key() instead.
2945 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002946 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002947 * \param output_size Number of bytes to generate and output.
2948 *
Gilles Peskine28538492018-07-11 17:34:00 +02002949 * \retval #PSA_SUCCESS
2950 * \retval #PSA_ERROR_NOT_SUPPORTED
2951 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2952 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2953 * \retval #PSA_ERROR_HARDWARE_FAILURE
2954 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002955 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002956 * The library has not been previously initialized by psa_crypto_init().
2957 * It is implementation-dependent whether a failure to initialize
2958 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002959 */
2960psa_status_t psa_generate_random(uint8_t *output,
2961 size_t output_size);
2962
Gilles Peskine4c317f42018-07-12 01:24:09 +02002963/** Extra parameters for RSA key generation.
2964 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002965 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002966 * parameter to psa_generate_key().
2967 */
2968typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002969 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002970} psa_generate_key_extra_rsa;
2971
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002972/**
2973 * \brief Generate a key or key pair.
2974 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002975 * \param handle Handle to the slot where the key will be stored.
2976 * This must be a valid slot for a key of the chosen
2977 * type: it must have been obtained by calling
2978 * psa_allocate_key() or psa_create_key() with the
2979 * correct \p type and with a maximum size that is
2980 * compatible with \p bits.
2981 * It must not contain any key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002982 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2983 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002984 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002985 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002986 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002987 * default parameters. Implementation that support
2988 * the generation of vendor-specific key types
2989 * that allow extra parameters shall document
2990 * the format of these extra parameters and
2991 * the default values. For standard parameters,
2992 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002993 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002994 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2995 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002996 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002997 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2998 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002999 * - For an RSA key (\p type is
3000 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3001 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003002 * specifying the public exponent. The
3003 * default public exponent used when \p extra
3004 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02003005 * \param extra_size Size of the buffer that \p extra
3006 * points to, in bytes. Note that if \p extra is
3007 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003008 *
Gilles Peskine28538492018-07-11 17:34:00 +02003009 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003010 * Success.
3011 * If the key is persistent, the key material and the key's metadata
3012 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003013 * \retval #PSA_ERROR_INVALID_HANDLE
3014 * \retval #PSA_ERROR_OCCUPIED_SLOT
3015 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02003016 * \retval #PSA_ERROR_NOT_SUPPORTED
3017 * \retval #PSA_ERROR_INVALID_ARGUMENT
3018 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3019 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3020 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3021 * \retval #PSA_ERROR_HARDWARE_FAILURE
3022 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003023 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003024 * 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.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003027 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003028psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003029 psa_key_type_t type,
3030 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003031 const void *extra,
3032 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003033
3034/**@}*/
3035
Gilles Peskinee59236f2018-01-27 23:32:46 +01003036#ifdef __cplusplus
3037}
3038#endif
3039
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003040/* The file "crypto_sizes.h" contains definitions for size calculation
3041 * macros whose definitions are implementation-specific. */
3042#include "crypto_sizes.h"
3043
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003044/* The file "crypto_struct.h" contains definitions for
3045 * implementation-specific structs that are declared above. */
3046#include "crypto_struct.h"
3047
3048/* The file "crypto_extra.h" contains vendor-specific definitions. This
3049 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003050#include "crypto_extra.h"
3051
3052#endif /* PSA_CRYPTO_H */