blob: 28211f2d03bf1801330bb525b20981764b323447 [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/**
Jaeden Amero283dfd12019-01-11 12:06:22 +0000379 * \brief Set domain parameters for a key.
380 *
381 * Some key types require additional domain parameters to be set before import
382 * or generation of the key. The domain parameters can be set with this
383 * function or, for key generation, through the \c extra parameter of
384 * psa_generate_key().
385 *
386 * The format for the required domain parameters varies by the key type.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000387 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
388 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
389 * ```
390 * Dss-Parms ::= SEQUENCE {
391 * p INTEGER,
392 * q INTEGER,
393 * g INTEGER
394 * }
395 * ```
Jaeden Amero8851c402019-01-11 14:20:03 +0000396 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY), the
397 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
398 * ```
399 * DomainParameters ::= SEQUENCE {
400 * p INTEGER, -- odd prime, p=jq +1
401 * g INTEGER, -- generator, g
402 * q INTEGER, -- factor of p-1
403 * j INTEGER OPTIONAL, -- subgroup factor
404 * validationParms ValidationParms OPTIONAL
405 * }
406 * ValidationParms ::= SEQUENCE {
407 * seed BIT STRING,
408 * pgenCounter INTEGER
409 * }
410 * ```
Jaeden Amero283dfd12019-01-11 12:06:22 +0000411 *
Gilles Peskine3a74e002019-01-18 17:11:25 +0100412 * \param handle Handle to the slot where the key will be stored.
413 * This must be a valid slot for a key of the chosen
414 * type: it must have been obtained by calling
415 * psa_allocate_key() or psa_create_key() with the
416 * correct \p type and with a maximum size that is
417 * compatible with \p data. It must not contain
418 * key material yet.
419 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). When
420 * subsequently creating key material into \p handle,
421 * the type must be compatible.
Jaeden Amero283dfd12019-01-11 12:06:22 +0000422 * \param[in] data Buffer containing the key domain parameters. The content
423 * of this buffer is interpreted according to \p type. of
424 * psa_export_key() or psa_export_public_key() for the
425 * chosen type.
426 * \param data_length Size of the \p data buffer in bytes.
427 *
428 * \retval #PSA_SUCCESS
429 * \retval #PSA_ERROR_INVALID_HANDLE
430 * \retval #PSA_ERROR_OCCUPIED_SLOT
431 * There is already a key in the specified slot.
432 * \retval #PSA_ERROR_INVALID_ARGUMENT
433 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
434 * \retval #PSA_ERROR_HARDWARE_FAILURE
435 * \retval #PSA_ERROR_TAMPERING_DETECTED
436 * \retval #PSA_ERROR_BAD_STATE
437 * The library has not been previously initialized by psa_crypto_init().
438 * It is implementation-dependent whether a failure to initialize
439 * results in this error code.
440 */
441psa_status_t psa_set_key_domain_parameters(psa_key_handle_t handle,
Gilles Peskine3a74e002019-01-18 17:11:25 +0100442 psa_key_type_t type,
Jaeden Amero283dfd12019-01-11 12:06:22 +0000443 const uint8_t *data,
444 size_t data_length);
445
446/**
447 * \brief Get domain parameters for a key.
448 *
449 * Get the domain parameters for a key with this function, if any. The format
450 * of the domain parameters written to \p data is specified in the
451 * documentation for psa_set_key_domain_parameters().
452 *
453 * \param handle Handle to the key to get domain parameters from.
454 * \param[out] data On success, the key domain parameters.
455 * \param data_size Size of the \p data buffer in bytes.
456 * \param[out] data_length On success, the number of bytes
457 * that make up the key domain parameters data.
458 *
459 * \retval #PSA_SUCCESS
460 * \retval #PSA_ERROR_INVALID_HANDLE
461 * \retval #PSA_ERROR_EMPTY_SLOT
462 * There is no key in the specified slot.
463 * \retval #PSA_ERROR_INVALID_ARGUMENT
464 * \retval #PSA_ERROR_NOT_SUPPORTED
465 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
466 * \retval #PSA_ERROR_HARDWARE_FAILURE
467 * \retval #PSA_ERROR_TAMPERING_DETECTED
468 * \retval #PSA_ERROR_BAD_STATE
469 * The library has not been previously initialized by psa_crypto_init().
470 * It is implementation-dependent whether a failure to initialize
471 * results in this error code.
472 */
473psa_status_t psa_get_key_domain_parameters(psa_key_handle_t handle,
474 uint8_t *data,
475 size_t data_size,
476 size_t *data_length);
477
478/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100479 * \brief Export a key in binary format.
480 *
481 * The output of this function can be passed to psa_import_key() to
482 * create an equivalent object.
483 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100484 * If the implementation of psa_import_key() supports other formats
485 * beyond the format specified here, the output from psa_export_key()
486 * must use the representation specified here, not the original
487 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100488 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100489 * For standard key types, the output format is as follows:
490 *
491 * - For symmetric keys (including MAC keys), the format is the
492 * raw bytes of the key.
493 * - For DES, the key data consists of 8 bytes. The parity bits must be
494 * correct.
495 * - For Triple-DES, the format is the concatenation of the
496 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100497 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200498 * is the non-encrypted DER encoding of the representation defined by
499 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
500 * ```
501 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200502 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200503 * modulus INTEGER, -- n
504 * publicExponent INTEGER, -- e
505 * privateExponent INTEGER, -- d
506 * prime1 INTEGER, -- p
507 * prime2 INTEGER, -- q
508 * exponent1 INTEGER, -- d mod (p-1)
509 * exponent2 INTEGER, -- d mod (q-1)
510 * coefficient INTEGER, -- (inverse of q) mod p
511 * }
512 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000513 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
514 * representation of the private key `x` as a big-endian byte string. The
515 * length of the byte string is the private key size in bytes (leading zeroes
516 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200517 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100518 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100519 * a representation of the private value as a `ceiling(m/8)`-byte string
520 * where `m` is the bit size associated with the curve, i.e. the bit size
521 * of the order of the curve's coordinate field. This byte string is
522 * in little-endian order for Montgomery curves (curve types
523 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
524 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
525 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100526 * This is the content of the `privateKey` field of the `ECPrivateKey`
527 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000528 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
529 * format is the representation of the private key `x` as a big-endian byte
530 * string. The length of the byte string is the private key size in bytes
531 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200532 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
533 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100534 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100535 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200536 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200537 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200538 * \param[out] data_length On success, the number of bytes
539 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100540 *
Gilles Peskine28538492018-07-11 17:34:00 +0200541 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100542 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200543 * \retval #PSA_ERROR_EMPTY_SLOT
544 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100545 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200546 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
547 * The size of the \p data buffer is too small. You can determine a
548 * sufficient buffer size by calling
549 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
550 * where \c type is the key type
551 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200552 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
553 * \retval #PSA_ERROR_HARDWARE_FAILURE
554 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300555 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300556 * The library has not been previously initialized by psa_crypto_init().
557 * It is implementation-dependent whether a failure to initialize
558 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100560psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100561 uint8_t *data,
562 size_t data_size,
563 size_t *data_length);
564
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100565/**
566 * \brief Export a public key or the public part of a key pair in binary format.
567 *
568 * The output of this function can be passed to psa_import_key() to
569 * create an object that is equivalent to the public key.
570 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000571 * This specification supports a single format for each key type.
572 * Implementations may support other formats as long as the standard
573 * format is supported. Implementations that support other formats
574 * should ensure that the formats are clearly unambiguous so as to
575 * minimize the risk that an invalid input is accidentally interpreted
576 * according to a different format.
577 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000578 * For standard key types, the output format is as follows:
579 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
580 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
581 * ```
582 * RSAPublicKey ::= SEQUENCE {
583 * modulus INTEGER, -- n
584 * publicExponent INTEGER } -- e
585 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000586 * - For elliptic curve public keys (key types for which
587 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
588 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
589 * Let `m` be the bit size associated with the curve, i.e. the bit size of
590 * `q` for a curve over `F_q`. The representation consists of:
591 * - The byte 0x04;
592 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
593 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000594 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
595 * representation of the public key `y = g^x mod p` as a big-endian byte
596 * string. The length of the byte string is the length of the base prime `p`
597 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000598 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
599 * the format is the representation of the public key `y = g^x mod p` as a
600 * big-endian byte string. The length of the byte string is the length of the
601 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100602 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100603 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200604 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200605 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200606 * \param[out] data_length On success, the number of bytes
607 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100608 *
Gilles Peskine28538492018-07-11 17:34:00 +0200609 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100610 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200611 * \retval #PSA_ERROR_EMPTY_SLOT
612 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200613 * The key is neither a public key nor a key pair.
614 * \retval #PSA_ERROR_NOT_SUPPORTED
615 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
616 * The size of the \p data buffer is too small. You can determine a
617 * sufficient buffer size by calling
618 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
619 * where \c type is the key type
620 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200621 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
622 * \retval #PSA_ERROR_HARDWARE_FAILURE
623 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300624 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300625 * The library has not been previously initialized by psa_crypto_init().
626 * It is implementation-dependent whether a failure to initialize
627 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100628 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100629psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100630 uint8_t *data,
631 size_t data_size,
632 size_t *data_length);
633
634/**@}*/
635
636/** \defgroup policy Key policies
637 * @{
638 */
639
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100640/** The type of the key policy data structure.
641 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000642 * Before calling any function on a key policy, the application must initialize
643 * it by any of the following means:
644 * - Set the structure to all-bits-zero, for example:
645 * \code
646 * psa_key_policy_t policy;
647 * memset(&policy, 0, sizeof(policy));
648 * \endcode
649 * - Initialize the structure to logical zero values, for example:
650 * \code
651 * psa_key_policy_t policy = {0};
652 * \endcode
653 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
654 * for example:
655 * \code
656 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
657 * \endcode
658 * - Assign the result of the function psa_key_policy_init()
659 * to the structure, for example:
660 * \code
661 * psa_key_policy_t policy;
662 * policy = psa_key_policy_init();
663 * \endcode
664 *
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100665 * This is an implementation-defined \c struct. Applications should not
666 * make any assumptions about the content of this structure except
667 * as directed by the documentation of a specific implementation. */
668typedef struct psa_key_policy_s psa_key_policy_t;
669
Jaeden Amero70261c52019-01-04 11:47:20 +0000670/** \def PSA_KEY_POLICY_INIT
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200671 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000672 * This macro returns a suitable initializer for a key policy object of type
673 * #psa_key_policy_t.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200674 */
Jaeden Amero70261c52019-01-04 11:47:20 +0000675#ifdef __DOXYGEN_ONLY__
676/* This is an example definition for documentation purposes.
677 * Implementations should define a suitable value in `crypto_struct.h`.
678 */
679#define PSA_KEY_POLICY_INIT {0}
680#endif
681
682/** Return an initial value for a key policy that forbids all usage of the key.
683 */
684static psa_key_policy_t psa_key_policy_init(void);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100685
Gilles Peskine7e198532018-03-08 07:50:30 +0100686/** \brief Set the standard fields of a policy structure.
687 *
688 * Note that this function does not make any consistency check of the
689 * parameters. The values are only checked when applying the policy to
690 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200691 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000692 * \param[in,out] policy The key policy to modify. It must have been
693 * initialized as per the documentation for
694 * #psa_key_policy_t.
695 * \param usage The permitted uses for the key.
696 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100697 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100698void psa_key_policy_set_usage(psa_key_policy_t *policy,
699 psa_key_usage_t usage,
700 psa_algorithm_t alg);
701
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200702/** \brief Retrieve the usage field of a policy structure.
703 *
704 * \param[in] policy The policy object to query.
705 *
706 * \return The permitted uses for a key with this policy.
707 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200708psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100709
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200710/** \brief Retrieve the algorithm field of a policy structure.
711 *
712 * \param[in] policy The policy object to query.
713 *
714 * \return The permitted algorithm for a key with this policy.
715 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200716psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100717
718/** \brief Set the usage policy on a key slot.
719 *
720 * This function must be called on an empty key slot, before importing,
721 * generating or creating a key in the slot. Changing the policy of an
722 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100723 *
724 * Implementations may set restrictions on supported key policies
725 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200726 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100727 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200728 * \param[in] policy The policy object to query.
729 *
730 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100731 * Success.
732 * If the key is persistent, it is implementation-defined whether
733 * the policy has been saved to persistent storage. Implementations
734 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100735 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200736 * \retval #PSA_ERROR_OCCUPIED_SLOT
737 * \retval #PSA_ERROR_NOT_SUPPORTED
738 * \retval #PSA_ERROR_INVALID_ARGUMENT
739 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
740 * \retval #PSA_ERROR_HARDWARE_FAILURE
741 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300742 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300743 * The library has not been previously initialized by psa_crypto_init().
744 * It is implementation-dependent whether a failure to initialize
745 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100746 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100747psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100748 const psa_key_policy_t *policy);
749
Gilles Peskine7e198532018-03-08 07:50:30 +0100750/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200751 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100752 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200753 * \param[out] policy On success, the key's policy.
754 *
755 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100756 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200757 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
758 * \retval #PSA_ERROR_HARDWARE_FAILURE
759 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300760 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300761 * The library has not been previously initialized by psa_crypto_init().
762 * It is implementation-dependent whether a failure to initialize
763 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100764 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100765psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100766 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100767
768/**@}*/
769
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100770/** \defgroup hash Message digests
771 * @{
772 */
773
Gilles Peskine69647a42019-01-14 20:18:12 +0100774/** Calculate the hash (digest) of a message.
775 *
776 * \note To verify the hash of a message against an
777 * expected value, use psa_hash_compare() instead.
778 *
779 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
780 * such that #PSA_ALG_IS_HASH(\p alg) is true).
781 * \param[in] input Buffer containing the message to hash.
782 * \param input_length Size of the \p input buffer in bytes.
783 * \param[out] hash Buffer where the hash is to be written.
784 * \param hash_size Size of the \p hash buffer in bytes.
785 * \param[out] hash_length On success, the number of bytes
786 * that make up the hash value. This is always
787 * #PSA_HASH_SIZE(\c alg) where \c alg is the
788 * hash algorithm that is calculated.
789 *
790 * \retval #PSA_SUCCESS
791 * Success.
792 * \retval #PSA_ERROR_NOT_SUPPORTED
793 * \p alg is not supported or is not a hash algorithm.
794 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
795 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
796 * \retval #PSA_ERROR_HARDWARE_FAILURE
797 * \retval #PSA_ERROR_TAMPERING_DETECTED
798 */
799psa_status_t psa_hash_compute(psa_algorithm_t alg,
800 const uint8_t *input,
801 size_t input_length,
802 uint8_t *hash,
803 size_t hash_size,
804 size_t *hash_length);
805
806/** Calculate the hash (digest) of a message and compare it with a
807 * reference value.
808 *
809 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
810 * such that #PSA_ALG_IS_HASH(\p alg) is true).
811 * \param[in] input Buffer containing the message to hash.
812 * \param input_length Size of the \p input buffer in bytes.
813 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100814 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100815 *
816 * \retval #PSA_SUCCESS
817 * The expected hash is identical to the actual hash of the input.
818 * \retval #PSA_ERROR_INVALID_SIGNATURE
819 * The hash of the message was calculated successfully, but it
820 * differs from the expected hash.
821 * \retval #PSA_ERROR_NOT_SUPPORTED
822 * \p alg is not supported or is not a hash algorithm.
823 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
824 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
825 * \retval #PSA_ERROR_HARDWARE_FAILURE
826 * \retval #PSA_ERROR_TAMPERING_DETECTED
827 */
828psa_status_t psa_hash_compare(psa_algorithm_t alg,
829 const uint8_t *input,
830 size_t input_length,
831 const uint8_t *hash,
832 const size_t hash_length);
833
Gilles Peskine308b91d2018-02-08 09:47:44 +0100834/** The type of the state data structure for multipart hash operations.
835 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000836 * Before calling any function on a hash operation object, the application must
837 * initialize it by any of the following means:
838 * - Set the structure to all-bits-zero, for example:
839 * \code
840 * psa_hash_operation_t operation;
841 * memset(&operation, 0, sizeof(operation));
842 * \endcode
843 * - Initialize the structure to logical zero values, for example:
844 * \code
845 * psa_hash_operation_t operation = {0};
846 * \endcode
847 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
848 * for example:
849 * \code
850 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
851 * \endcode
852 * - Assign the result of the function psa_hash_operation_init()
853 * to the structure, for example:
854 * \code
855 * psa_hash_operation_t operation;
856 * operation = psa_hash_operation_init();
857 * \endcode
858 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100859 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100860 * make any assumptions about the content of this structure except
861 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100862typedef struct psa_hash_operation_s psa_hash_operation_t;
863
Jaeden Amero6a25b412019-01-04 11:47:44 +0000864/** \def PSA_HASH_OPERATION_INIT
865 *
866 * This macro returns a suitable initializer for a hash operation object
867 * of type #psa_hash_operation_t.
868 */
869#ifdef __DOXYGEN_ONLY__
870/* This is an example definition for documentation purposes.
871 * Implementations should define a suitable value in `crypto_struct.h`.
872 */
873#define PSA_HASH_OPERATION_INIT {0}
874#endif
875
876/** Return an initial value for a hash operation object.
877 */
878static psa_hash_operation_t psa_hash_operation_init(void);
879
Gilles Peskinef45adda2019-01-14 18:29:18 +0100880/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100881 *
882 * The sequence of operations to calculate a hash (message digest)
883 * is as follows:
884 * -# Allocate an operation object which will be passed to all the functions
885 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000886 * -# Initialize the operation object with one of the methods described in the
887 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200888 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100889 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100890 * of the message each time. The hash that is calculated is the hash
891 * of the concatenation of these messages in order.
892 * -# To calculate the hash, call psa_hash_finish().
893 * To compare the hash with an expected value, call psa_hash_verify().
894 *
895 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000896 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100897 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200898 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100899 * eventually terminate the operation. The following events terminate an
900 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100901 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100902 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100903 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000904 * \param[in,out] operation The operation object to set up. It must have
905 * been initialized as per the documentation for
906 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200907 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
908 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100909 *
Gilles Peskine28538492018-07-11 17:34:00 +0200910 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100911 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200912 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200913 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200914 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
915 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
916 * \retval #PSA_ERROR_HARDWARE_FAILURE
917 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100918 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200919psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100920 psa_algorithm_t alg);
921
Gilles Peskine308b91d2018-02-08 09:47:44 +0100922/** Add a message fragment to a multipart hash operation.
923 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200924 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100925 *
926 * If this function returns an error status, the operation becomes inactive.
927 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200928 * \param[in,out] operation Active hash operation.
929 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200930 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100931 *
Gilles Peskine28538492018-07-11 17:34:00 +0200932 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100933 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200934 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100935 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200936 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
937 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
938 * \retval #PSA_ERROR_HARDWARE_FAILURE
939 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100940 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100941psa_status_t psa_hash_update(psa_hash_operation_t *operation,
942 const uint8_t *input,
943 size_t input_length);
944
Gilles Peskine308b91d2018-02-08 09:47:44 +0100945/** Finish the calculation of the hash of a message.
946 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200947 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100948 * This function calculates the hash of the message formed by concatenating
949 * the inputs passed to preceding calls to psa_hash_update().
950 *
951 * When this function returns, the operation becomes inactive.
952 *
953 * \warning Applications should not call this function if they expect
954 * a specific value for the hash. Call psa_hash_verify() instead.
955 * Beware that comparing integrity or authenticity data such as
956 * hash values with a function such as \c memcmp is risky
957 * because the time taken by the comparison may leak information
958 * about the hashed data which could allow an attacker to guess
959 * a valid hash and thereby bypass security controls.
960 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200961 * \param[in,out] operation Active hash operation.
962 * \param[out] hash Buffer where the hash is to be written.
963 * \param hash_size Size of the \p hash buffer in bytes.
964 * \param[out] hash_length On success, the number of bytes
965 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200966 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200967 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100968 *
Gilles Peskine28538492018-07-11 17:34:00 +0200969 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100970 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200971 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100972 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200973 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200974 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200975 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100976 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200977 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
978 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
979 * \retval #PSA_ERROR_HARDWARE_FAILURE
980 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100981 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100982psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
983 uint8_t *hash,
984 size_t hash_size,
985 size_t *hash_length);
986
Gilles Peskine308b91d2018-02-08 09:47:44 +0100987/** Finish the calculation of the hash of a message and compare it with
988 * an expected value.
989 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200990 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100991 * This function calculates the hash of the message formed by concatenating
992 * the inputs passed to preceding calls to psa_hash_update(). It then
993 * compares the calculated hash with the expected hash passed as a
994 * parameter to this function.
995 *
996 * When this function returns, the operation becomes inactive.
997 *
Gilles Peskine19067982018-03-20 17:54:53 +0100998 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100999 * comparison between the actual hash and the expected hash is performed
1000 * in constant time.
1001 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001002 * \param[in,out] operation Active hash operation.
1003 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001004 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001005 *
Gilles Peskine28538492018-07-11 17:34:00 +02001006 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001007 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001008 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001009 * The hash of the message was calculated successfully, but it
1010 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001011 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001012 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001013 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1014 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1015 * \retval #PSA_ERROR_HARDWARE_FAILURE
1016 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001017 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001018psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1019 const uint8_t *hash,
1020 size_t hash_length);
1021
Gilles Peskine308b91d2018-02-08 09:47:44 +01001022/** Abort a hash operation.
1023 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001024 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001025 * \p operation structure itself. Once aborted, the operation object
1026 * can be reused for another operation by calling
1027 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001028 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001029 * You may call this function any time after the operation object has
1030 * been initialized by any of the following methods:
1031 * - A call to psa_hash_setup(), whether it succeeds or not.
1032 * - Initializing the \c struct to all-bits-zero.
1033 * - Initializing the \c struct to logical zeros, e.g.
1034 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001035 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001036 * In particular, calling psa_hash_abort() after the operation has been
1037 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1038 * psa_hash_verify() is safe and has no effect.
1039 *
1040 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001041 *
Gilles Peskine28538492018-07-11 17:34:00 +02001042 * \retval #PSA_SUCCESS
1043 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001044 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001045 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1046 * \retval #PSA_ERROR_HARDWARE_FAILURE
1047 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001048 */
1049psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001050
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001051/** Clone a hash operation.
1052 *
1053 * \param[in] source_operation The active hash operation to clone.
1054 * \param[in,out] target_operation The operation object to set up.
1055 * It must be initialized but not active.
1056 *
1057 * \retval #PSA_SUCCESS
1058 * \retval #PSA_ERROR_BAD_STATE
1059 * \p source_operation is not an active hash operation.
1060 * \retval #PSA_ERROR_BAD_STATE
1061 * \p source_operation is active.
1062 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1063 * \retval #PSA_ERROR_HARDWARE_FAILURE
1064 * \retval #PSA_ERROR_TAMPERING_DETECTED
1065 */
1066psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1067 psa_hash_operation_t *target_operation);
1068
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001069/**@}*/
1070
Gilles Peskine8c9def32018-02-08 10:02:12 +01001071/** \defgroup MAC Message authentication codes
1072 * @{
1073 */
1074
Gilles Peskine69647a42019-01-14 20:18:12 +01001075/** Calculate the MAC (message authentication code) of a message.
1076 *
1077 * \note To verify the MAC of a message against an
1078 * expected value, use psa_mac_verify() instead.
1079 * Beware that comparing integrity or authenticity data such as
1080 * MAC values with a function such as \c memcmp is risky
1081 * because the time taken by the comparison may leak information
1082 * about the MAC value which could allow an attacker to guess
1083 * a valid MAC and thereby bypass security controls.
1084 *
1085 * \param handle Handle to the key to use for the operation.
1086 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1087 * such that #PSA_ALG_IS_MAC(alg) is true).
1088 * \param[in] input Buffer containing the input message.
1089 * \param input_length Size of the \p input buffer in bytes.
1090 * \param[out] mac Buffer where the MAC value is to be written.
1091 * \param mac_size Size of the \p mac buffer in bytes.
1092 * \param[out] mac_length On success, the number of bytes
1093 * that make up the mac value. This is always
1094 * #PSA_HASH_SIZE(\c alg) where \c alg is the
1095 * hash algorithm that is calculated.
1096 *
1097 * \retval #PSA_SUCCESS
1098 * Success.
1099 * \retval #PSA_ERROR_INVALID_HANDLE
1100 * \retval #PSA_ERROR_EMPTY_SLOT
1101 * \retval #PSA_ERROR_NOT_PERMITTED
1102 * \retval #PSA_ERROR_INVALID_ARGUMENT
1103 * \p key is not compatible with \p alg.
1104 * \retval #PSA_ERROR_NOT_SUPPORTED
1105 * \p alg is not supported or is not a MAC algorithm.
1106 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1107 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1108 * \retval #PSA_ERROR_HARDWARE_FAILURE
1109 * \retval #PSA_ERROR_TAMPERING_DETECTED
1110 * \retval #PSA_ERROR_BAD_STATE
1111 * The library has not been previously initialized by psa_crypto_init().
1112 * It is implementation-dependent whether a failure to initialize
1113 * results in this error code.
1114 */
1115psa_status_t psa_mac_compute(psa_key_handle_t handle,
1116 psa_algorithm_t alg,
1117 const uint8_t *input,
1118 size_t input_length,
1119 uint8_t *mac,
1120 size_t mac_size,
1121 size_t *mac_length);
1122
1123/** Calculate the MAC of a message and compare it with a reference value.
1124 *
1125 * \param handle Handle to the key to use for the operation.
1126 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1127 * such that #PSA_ALG_IS_MAC(alg) is true).
1128 * \param[in] input Buffer containing the input message.
1129 * \param input_length Size of the \p input buffer in bytes.
1130 * \param[out] mac Buffer containing the expected MAC value.
1131 * \param mac_length Size of the \p mac buffer in bytes.
1132 *
1133 * \retval #PSA_SUCCESS
1134 * The expected MAC is identical to the actual MAC of the input.
1135 * \retval #PSA_ERROR_INVALID_SIGNATURE
1136 * The MAC of the message was calculated successfully, but it
1137 * differs from the expected value.
1138 * \retval #PSA_ERROR_INVALID_HANDLE
1139 * \retval #PSA_ERROR_EMPTY_SLOT
1140 * \retval #PSA_ERROR_NOT_PERMITTED
1141 * \retval #PSA_ERROR_INVALID_ARGUMENT
1142 * \p key is not compatible with \p alg.
1143 * \retval #PSA_ERROR_NOT_SUPPORTED
1144 * \p alg is not supported or is not a MAC algorithm.
1145 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1146 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1147 * \retval #PSA_ERROR_HARDWARE_FAILURE
1148 * \retval #PSA_ERROR_TAMPERING_DETECTED
1149 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001150psa_status_t psa_mac_verify(psa_key_handle_t handle,
1151 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001152 const uint8_t *input,
1153 size_t input_length,
1154 const uint8_t *mac,
1155 const size_t mac_length);
1156
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001157/** The type of the state data structure for multipart MAC operations.
1158 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001159 * Before calling any function on a MAC operation object, the application must
1160 * initialize it by any of the following means:
1161 * - Set the structure to all-bits-zero, for example:
1162 * \code
1163 * psa_mac_operation_t operation;
1164 * memset(&operation, 0, sizeof(operation));
1165 * \endcode
1166 * - Initialize the structure to logical zero values, for example:
1167 * \code
1168 * psa_mac_operation_t operation = {0};
1169 * \endcode
1170 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1171 * for example:
1172 * \code
1173 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1174 * \endcode
1175 * - Assign the result of the function psa_mac_operation_init()
1176 * to the structure, for example:
1177 * \code
1178 * psa_mac_operation_t operation;
1179 * operation = psa_mac_operation_init();
1180 * \endcode
1181 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001182 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001183 * make any assumptions about the content of this structure except
1184 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001185typedef struct psa_mac_operation_s psa_mac_operation_t;
1186
Jaeden Amero769ce272019-01-04 11:48:03 +00001187/** \def PSA_MAC_OPERATION_INIT
1188 *
1189 * This macro returns a suitable initializer for a MAC operation object of type
1190 * #psa_mac_operation_t.
1191 */
1192#ifdef __DOXYGEN_ONLY__
1193/* This is an example definition for documentation purposes.
1194 * Implementations should define a suitable value in `crypto_struct.h`.
1195 */
1196#define PSA_MAC_OPERATION_INIT {0}
1197#endif
1198
1199/** Return an initial value for a MAC operation object.
1200 */
1201static psa_mac_operation_t psa_mac_operation_init(void);
1202
Gilles Peskinef45adda2019-01-14 18:29:18 +01001203/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001204 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001205 * This function sets up the calculation of the MAC
1206 * (message authentication code) of a byte string.
1207 * To verify the MAC of a message against an
1208 * expected value, use psa_mac_verify_setup() instead.
1209 *
1210 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001211 * -# Allocate an operation object which will be passed to all the functions
1212 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001213 * -# Initialize the operation object with one of the methods described in the
1214 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001215 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001216 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1217 * of the message each time. The MAC that is calculated is the MAC
1218 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001219 * -# At the end of the message, call psa_mac_sign_finish() to finish
1220 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001221 *
1222 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001223 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001224 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001225 * After a successful call to psa_mac_sign_setup(), the application must
1226 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001227 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001228 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001229 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001230 * \param[in,out] operation The operation object to set up. It must have
1231 * been initialized as per the documentation for
1232 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001233 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001234 * It must remain valid until the operation
1235 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001236 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1237 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001238 *
Gilles Peskine28538492018-07-11 17:34:00 +02001239 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001240 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001241 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001242 * \retval #PSA_ERROR_EMPTY_SLOT
1243 * \retval #PSA_ERROR_NOT_PERMITTED
1244 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001245 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001246 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001247 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001248 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1249 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1250 * \retval #PSA_ERROR_HARDWARE_FAILURE
1251 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001252 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001253 * The library has not been previously initialized by psa_crypto_init().
1254 * It is implementation-dependent whether a failure to initialize
1255 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001256 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001257psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001258 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001259 psa_algorithm_t alg);
1260
Gilles Peskinef45adda2019-01-14 18:29:18 +01001261/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001262 *
1263 * This function sets up the verification of the MAC
1264 * (message authentication code) of a byte string against an expected value.
1265 *
1266 * The sequence of operations to verify a MAC is as follows:
1267 * -# Allocate an operation object which will be passed to all the functions
1268 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001269 * -# Initialize the operation object with one of the methods described in the
1270 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001271 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001272 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1273 * of the message each time. The MAC that is calculated is the MAC
1274 * of the concatenation of these messages in order.
1275 * -# At the end of the message, call psa_mac_verify_finish() to finish
1276 * calculating the actual MAC of the message and verify it against
1277 * the expected value.
1278 *
1279 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001280 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001281 *
1282 * After a successful call to psa_mac_verify_setup(), the application must
1283 * eventually terminate the operation through one of the following methods:
1284 * - A failed call to psa_mac_update().
1285 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1286 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001287 * \param[in,out] operation The operation object to set up. It must have
1288 * been initialized as per the documentation for
1289 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001290 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001291 * It must remain valid until the operation
1292 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001293 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1294 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001295 *
Gilles Peskine28538492018-07-11 17:34:00 +02001296 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001297 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001298 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001299 * \retval #PSA_ERROR_EMPTY_SLOT
1300 * \retval #PSA_ERROR_NOT_PERMITTED
1301 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001302 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001303 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001304 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001305 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1306 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1307 * \retval #PSA_ERROR_HARDWARE_FAILURE
1308 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001309 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001310 * The library has not been previously initialized by psa_crypto_init().
1311 * It is implementation-dependent whether a failure to initialize
1312 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001313 */
1314psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001315 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001316 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001317
Gilles Peskinedcd14942018-07-12 00:30:52 +02001318/** Add a message fragment to a multipart MAC operation.
1319 *
1320 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1321 * before calling this function.
1322 *
1323 * If this function returns an error status, the operation becomes inactive.
1324 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001325 * \param[in,out] operation Active MAC operation.
1326 * \param[in] input Buffer containing the message fragment to add to
1327 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001328 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001329 *
1330 * \retval #PSA_SUCCESS
1331 * Success.
1332 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001333 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001334 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1335 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1336 * \retval #PSA_ERROR_HARDWARE_FAILURE
1337 * \retval #PSA_ERROR_TAMPERING_DETECTED
1338 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001339psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1340 const uint8_t *input,
1341 size_t input_length);
1342
Gilles Peskinedcd14942018-07-12 00:30:52 +02001343/** Finish the calculation of the MAC of a message.
1344 *
1345 * The application must call psa_mac_sign_setup() before calling this function.
1346 * This function calculates the MAC of the message formed by concatenating
1347 * the inputs passed to preceding calls to psa_mac_update().
1348 *
1349 * When this function returns, the operation becomes inactive.
1350 *
1351 * \warning Applications should not call this function if they expect
1352 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1353 * Beware that comparing integrity or authenticity data such as
1354 * MAC values with a function such as \c memcmp is risky
1355 * because the time taken by the comparison may leak information
1356 * about the MAC value which could allow an attacker to guess
1357 * a valid MAC and thereby bypass security controls.
1358 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001359 * \param[in,out] operation Active MAC operation.
1360 * \param[out] mac Buffer where the MAC value is to be written.
1361 * \param mac_size Size of the \p mac buffer in bytes.
1362 * \param[out] mac_length On success, the number of bytes
1363 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001364 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001365 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001366 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001367 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001368 *
1369 * \retval #PSA_SUCCESS
1370 * Success.
1371 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001372 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001373 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001374 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001375 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1376 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1377 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1378 * \retval #PSA_ERROR_HARDWARE_FAILURE
1379 * \retval #PSA_ERROR_TAMPERING_DETECTED
1380 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001381psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1382 uint8_t *mac,
1383 size_t mac_size,
1384 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001385
Gilles Peskinedcd14942018-07-12 00:30:52 +02001386/** Finish the calculation of the MAC of a message and compare it with
1387 * an expected value.
1388 *
1389 * The application must call psa_mac_verify_setup() before calling this function.
1390 * This function calculates the MAC of the message formed by concatenating
1391 * the inputs passed to preceding calls to psa_mac_update(). It then
1392 * compares the calculated MAC with the expected MAC passed as a
1393 * parameter to this function.
1394 *
1395 * When this function returns, the operation becomes inactive.
1396 *
1397 * \note Implementations shall make the best effort to ensure that the
1398 * comparison between the actual MAC and the expected MAC is performed
1399 * in constant time.
1400 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001401 * \param[in,out] operation Active MAC operation.
1402 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001403 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001404 *
1405 * \retval #PSA_SUCCESS
1406 * The expected MAC is identical to the actual MAC of the message.
1407 * \retval #PSA_ERROR_INVALID_SIGNATURE
1408 * The MAC of the message was calculated successfully, but it
1409 * differs from the expected MAC.
1410 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001411 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001412 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1413 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1414 * \retval #PSA_ERROR_HARDWARE_FAILURE
1415 * \retval #PSA_ERROR_TAMPERING_DETECTED
1416 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001417psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1418 const uint8_t *mac,
1419 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001420
Gilles Peskinedcd14942018-07-12 00:30:52 +02001421/** Abort a MAC operation.
1422 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001423 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001424 * \p operation structure itself. Once aborted, the operation object
1425 * can be reused for another operation by calling
1426 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001427 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001428 * You may call this function any time after the operation object has
1429 * been initialized by any of the following methods:
1430 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1431 * it succeeds or not.
1432 * - Initializing the \c struct to all-bits-zero.
1433 * - Initializing the \c struct to logical zeros, e.g.
1434 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001435 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001436 * In particular, calling psa_mac_abort() after the operation has been
1437 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1438 * psa_mac_verify_finish() is safe and has no effect.
1439 *
1440 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001441 *
1442 * \retval #PSA_SUCCESS
1443 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001444 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001445 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1446 * \retval #PSA_ERROR_HARDWARE_FAILURE
1447 * \retval #PSA_ERROR_TAMPERING_DETECTED
1448 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001449psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1450
1451/**@}*/
1452
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001453/** \defgroup cipher Symmetric ciphers
1454 * @{
1455 */
1456
Gilles Peskine69647a42019-01-14 20:18:12 +01001457/** Encrypt a message using a symmetric cipher.
1458 *
1459 * This function encrypts a message with a random IV (initialization
1460 * vector).
1461 *
1462 * \param handle Handle to the key to use for the operation.
1463 * It must remain valid until the operation
1464 * terminates.
1465 * \param alg The cipher algorithm to compute
1466 * (\c PSA_ALG_XXX value such that
1467 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1468 * \param[in] input Buffer containing the message to encrypt.
1469 * \param input_length Size of the \p input buffer in bytes.
1470 * \param[out] output Buffer where the output is to be written.
1471 * The output contains the IV followed by
1472 * the ciphertext proper.
1473 * \param output_size Size of the \p output buffer in bytes.
1474 * \param[out] output_length On success, the number of bytes
1475 * that make up the output.
1476 *
1477 * \retval #PSA_SUCCESS
1478 * Success.
1479 * \retval #PSA_ERROR_INVALID_HANDLE
1480 * \retval #PSA_ERROR_EMPTY_SLOT
1481 * \retval #PSA_ERROR_NOT_PERMITTED
1482 * \retval #PSA_ERROR_INVALID_ARGUMENT
1483 * \p key is not compatible with \p alg.
1484 * \retval #PSA_ERROR_NOT_SUPPORTED
1485 * \p alg is not supported or is not a cipher algorithm.
1486 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1487 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1488 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1489 * \retval #PSA_ERROR_HARDWARE_FAILURE
1490 * \retval #PSA_ERROR_TAMPERING_DETECTED
1491 */
1492psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1493 psa_algorithm_t alg,
1494 const uint8_t *input,
1495 size_t input_length,
1496 uint8_t *output,
1497 size_t output_size,
1498 size_t *output_length);
1499
1500/** Decrypt a message using a symmetric cipher.
1501 *
1502 * This function decrypts a message encrypted with a symmetric cipher.
1503 *
1504 * \param handle Handle to the key to use for the operation.
1505 * It must remain valid until the operation
1506 * terminates.
1507 * \param alg The cipher algorithm to compute
1508 * (\c PSA_ALG_XXX value such that
1509 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1510 * \param[in] input Buffer containing the message to decrypt.
1511 * This consists of the IV followed by the
1512 * ciphertext proper.
1513 * \param input_length Size of the \p input buffer in bytes.
1514 * \param[out] output Buffer where the plaintext is to be written.
1515 * \param output_size Size of the \p output buffer in bytes.
1516 * \param[out] output_length On success, the number of bytes
1517 * that make up the output.
1518 *
1519 * \retval #PSA_SUCCESS
1520 * Success.
1521 * \retval #PSA_ERROR_INVALID_HANDLE
1522 * \retval #PSA_ERROR_EMPTY_SLOT
1523 * \retval #PSA_ERROR_NOT_PERMITTED
1524 * \retval #PSA_ERROR_INVALID_ARGUMENT
1525 * \p key is not compatible with \p alg.
1526 * \retval #PSA_ERROR_NOT_SUPPORTED
1527 * \p alg is not supported or is not a cipher algorithm.
1528 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1529 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1530 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1531 * \retval #PSA_ERROR_HARDWARE_FAILURE
1532 * \retval #PSA_ERROR_TAMPERING_DETECTED
1533 */
1534psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1535 psa_algorithm_t alg,
1536 const uint8_t *input,
1537 size_t input_length,
1538 uint8_t *output,
1539 size_t output_size,
1540 size_t *output_length);
1541
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001542/** The type of the state data structure for multipart cipher operations.
1543 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001544 * Before calling any function on a cipher operation object, the application
1545 * must initialize it by any of the following means:
1546 * - Set the structure to all-bits-zero, for example:
1547 * \code
1548 * psa_cipher_operation_t operation;
1549 * memset(&operation, 0, sizeof(operation));
1550 * \endcode
1551 * - Initialize the structure to logical zero values, for example:
1552 * \code
1553 * psa_cipher_operation_t operation = {0};
1554 * \endcode
1555 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1556 * for example:
1557 * \code
1558 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1559 * \endcode
1560 * - Assign the result of the function psa_cipher_operation_init()
1561 * to the structure, for example:
1562 * \code
1563 * psa_cipher_operation_t operation;
1564 * operation = psa_cipher_operation_init();
1565 * \endcode
1566 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001567 * This is an implementation-defined \c struct. Applications should not
1568 * make any assumptions about the content of this structure except
1569 * as directed by the documentation of a specific implementation. */
1570typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1571
Jaeden Amero5bae2272019-01-04 11:48:27 +00001572/** \def PSA_CIPHER_OPERATION_INIT
1573 *
1574 * This macro returns a suitable initializer for a cipher operation object of
1575 * type #psa_cipher_operation_t.
1576 */
1577#ifdef __DOXYGEN_ONLY__
1578/* This is an example definition for documentation purposes.
1579 * Implementations should define a suitable value in `crypto_struct.h`.
1580 */
1581#define PSA_CIPHER_OPERATION_INIT {0}
1582#endif
1583
1584/** Return an initial value for a cipher operation object.
1585 */
1586static psa_cipher_operation_t psa_cipher_operation_init(void);
1587
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001588/** Set the key for a multipart symmetric encryption operation.
1589 *
1590 * The sequence of operations to encrypt a message with a symmetric cipher
1591 * is as follows:
1592 * -# Allocate an operation object which will be passed to all the functions
1593 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001594 * -# Initialize the operation object with one of the methods described in the
1595 * documentation for #psa_cipher_operation_t, e.g.
1596 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001597 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001598 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001599 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001600 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001601 * requires a specific IV value.
1602 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1603 * of the message each time.
1604 * -# Call psa_cipher_finish().
1605 *
1606 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001607 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001608 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001609 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001610 * eventually terminate the operation. The following events terminate an
1611 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001612 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001613 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001614 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001615 * \param[in,out] operation The operation object to set up. It must have
1616 * been initialized as per the documentation for
1617 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001618 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001619 * It must remain valid until the operation
1620 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001621 * \param alg The cipher algorithm to compute
1622 * (\c PSA_ALG_XXX value such that
1623 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001624 *
Gilles Peskine28538492018-07-11 17:34:00 +02001625 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001626 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001627 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001628 * \retval #PSA_ERROR_EMPTY_SLOT
1629 * \retval #PSA_ERROR_NOT_PERMITTED
1630 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001631 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001632 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001633 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001634 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1635 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1636 * \retval #PSA_ERROR_HARDWARE_FAILURE
1637 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001638 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001639 * The library has not been previously initialized by psa_crypto_init().
1640 * It is implementation-dependent whether a failure to initialize
1641 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001642 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001643psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001644 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001645 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001646
1647/** Set the key for a multipart symmetric decryption operation.
1648 *
1649 * The sequence of operations to decrypt a message with a symmetric cipher
1650 * is as follows:
1651 * -# Allocate an operation object which will be passed to all the functions
1652 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001653 * -# Initialize the operation object with one of the methods described in the
1654 * documentation for #psa_cipher_operation_t, e.g.
1655 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001656 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001657 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001658 * decryption. If the IV is prepended to the ciphertext, you can call
1659 * psa_cipher_update() on a buffer containing the IV followed by the
1660 * beginning of the message.
1661 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1662 * of the message each time.
1663 * -# Call psa_cipher_finish().
1664 *
1665 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001666 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001667 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001668 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001669 * eventually terminate the operation. The following events terminate an
1670 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001671 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001672 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001673 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001674 * \param[in,out] operation The operation object to set up. It must have
1675 * been initialized as per the documentation for
1676 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001677 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001678 * It must remain valid until the operation
1679 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001680 * \param alg The cipher algorithm to compute
1681 * (\c PSA_ALG_XXX value such that
1682 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001683 *
Gilles Peskine28538492018-07-11 17:34:00 +02001684 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001685 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001686 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001687 * \retval #PSA_ERROR_EMPTY_SLOT
1688 * \retval #PSA_ERROR_NOT_PERMITTED
1689 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001690 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001691 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001692 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001693 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1694 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1695 * \retval #PSA_ERROR_HARDWARE_FAILURE
1696 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001697 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001698 * The library has not been previously initialized by psa_crypto_init().
1699 * It is implementation-dependent whether a failure to initialize
1700 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001701 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001702psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001703 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001704 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001705
Gilles Peskinedcd14942018-07-12 00:30:52 +02001706/** Generate an IV for a symmetric encryption operation.
1707 *
1708 * This function generates a random IV (initialization vector), nonce
1709 * or initial counter value for the encryption operation as appropriate
1710 * for the chosen algorithm, key type and key size.
1711 *
1712 * The application must call psa_cipher_encrypt_setup() before
1713 * calling this function.
1714 *
1715 * If this function returns an error status, the operation becomes inactive.
1716 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001717 * \param[in,out] operation Active cipher operation.
1718 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001719 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001720 * \param[out] iv_length On success, the number of bytes of the
1721 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001722 *
1723 * \retval #PSA_SUCCESS
1724 * Success.
1725 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001726 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001727 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001728 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001729 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1730 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1731 * \retval #PSA_ERROR_HARDWARE_FAILURE
1732 * \retval #PSA_ERROR_TAMPERING_DETECTED
1733 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001734psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1735 unsigned char *iv,
1736 size_t iv_size,
1737 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001738
Gilles Peskinedcd14942018-07-12 00:30:52 +02001739/** Set the IV for a symmetric encryption or decryption operation.
1740 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001741 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001742 * or initial counter value for the encryption or decryption operation.
1743 *
1744 * The application must call psa_cipher_encrypt_setup() before
1745 * calling this function.
1746 *
1747 * If this function returns an error status, the operation becomes inactive.
1748 *
1749 * \note When encrypting, applications should use psa_cipher_generate_iv()
1750 * instead of this function, unless implementing a protocol that requires
1751 * a non-random IV.
1752 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001753 * \param[in,out] operation Active cipher operation.
1754 * \param[in] iv Buffer containing the IV to use.
1755 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001756 *
1757 * \retval #PSA_SUCCESS
1758 * Success.
1759 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001760 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001761 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001762 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001763 * or the chosen algorithm does not use an IV.
1764 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1765 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1766 * \retval #PSA_ERROR_HARDWARE_FAILURE
1767 * \retval #PSA_ERROR_TAMPERING_DETECTED
1768 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001769psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1770 const unsigned char *iv,
1771 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001772
Gilles Peskinedcd14942018-07-12 00:30:52 +02001773/** Encrypt or decrypt a message fragment in an active cipher operation.
1774 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001775 * Before calling this function, you must:
1776 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1777 * The choice of setup function determines whether this function
1778 * encrypts or decrypts its input.
1779 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1780 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001781 *
1782 * If this function returns an error status, the operation becomes inactive.
1783 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001784 * \param[in,out] operation Active cipher operation.
1785 * \param[in] input Buffer containing the message fragment to
1786 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001787 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001788 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001789 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001790 * \param[out] output_length On success, the number of bytes
1791 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001792 *
1793 * \retval #PSA_SUCCESS
1794 * Success.
1795 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001796 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001797 * not set, or already completed).
1798 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1799 * The size of the \p output buffer is too small.
1800 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1801 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1802 * \retval #PSA_ERROR_HARDWARE_FAILURE
1803 * \retval #PSA_ERROR_TAMPERING_DETECTED
1804 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001805psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1806 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001807 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001808 unsigned char *output,
1809 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001810 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001811
Gilles Peskinedcd14942018-07-12 00:30:52 +02001812/** Finish encrypting or decrypting a message in a cipher operation.
1813 *
1814 * The application must call psa_cipher_encrypt_setup() or
1815 * psa_cipher_decrypt_setup() before calling this function. The choice
1816 * of setup function determines whether this function encrypts or
1817 * decrypts its input.
1818 *
1819 * This function finishes the encryption or decryption of the message
1820 * formed by concatenating the inputs passed to preceding calls to
1821 * psa_cipher_update().
1822 *
1823 * When this function returns, the operation becomes inactive.
1824 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001825 * \param[in,out] operation Active cipher operation.
1826 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001827 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001828 * \param[out] output_length On success, the number of bytes
1829 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001830 *
1831 * \retval #PSA_SUCCESS
1832 * Success.
1833 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001834 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001835 * not set, or already completed).
1836 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1837 * The size of the \p output buffer is too small.
1838 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1839 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1840 * \retval #PSA_ERROR_HARDWARE_FAILURE
1841 * \retval #PSA_ERROR_TAMPERING_DETECTED
1842 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001843psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001844 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001845 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001846 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001847
Gilles Peskinedcd14942018-07-12 00:30:52 +02001848/** Abort a cipher operation.
1849 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001850 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001851 * \p operation structure itself. Once aborted, the operation object
1852 * can be reused for another operation by calling
1853 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001854 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001855 * You may call this function any time after the operation object has
1856 * been initialized by any of the following methods:
1857 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1858 * whether it succeeds or not.
1859 * - Initializing the \c struct to all-bits-zero.
1860 * - Initializing the \c struct to logical zeros, e.g.
1861 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001862 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001863 * In particular, calling psa_cipher_abort() after the operation has been
1864 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1865 * is safe and has no effect.
1866 *
1867 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001868 *
1869 * \retval #PSA_SUCCESS
1870 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001871 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001872 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1873 * \retval #PSA_ERROR_HARDWARE_FAILURE
1874 * \retval #PSA_ERROR_TAMPERING_DETECTED
1875 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001876psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1877
1878/**@}*/
1879
Gilles Peskine3b555712018-03-03 21:27:57 +01001880/** \defgroup aead Authenticated encryption with associated data (AEAD)
1881 * @{
1882 */
1883
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001884/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001885 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001886 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001887 * \param alg The AEAD algorithm to compute
1888 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001889 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001890 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001891 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001892 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001893 * but not encrypted.
1894 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001895 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001896 * encrypted.
1897 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001898 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001899 * encrypted data. The additional data is not
1900 * part of this output. For algorithms where the
1901 * encrypted data and the authentication tag
1902 * are defined as separate outputs, the
1903 * authentication tag is appended to the
1904 * encrypted data.
1905 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1906 * This must be at least
1907 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1908 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001909 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001910 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001911 *
Gilles Peskine28538492018-07-11 17:34:00 +02001912 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001913 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001914 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001915 * \retval #PSA_ERROR_EMPTY_SLOT
1916 * \retval #PSA_ERROR_NOT_PERMITTED
1917 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001918 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001919 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001920 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001921 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1922 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1923 * \retval #PSA_ERROR_HARDWARE_FAILURE
1924 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001925 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001926 * The library has not been previously initialized by psa_crypto_init().
1927 * It is implementation-dependent whether a failure to initialize
1928 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001929 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001930psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001931 psa_algorithm_t alg,
1932 const uint8_t *nonce,
1933 size_t nonce_length,
1934 const uint8_t *additional_data,
1935 size_t additional_data_length,
1936 const uint8_t *plaintext,
1937 size_t plaintext_length,
1938 uint8_t *ciphertext,
1939 size_t ciphertext_size,
1940 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001941
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001942/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001943 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001944 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001945 * \param alg The AEAD algorithm to compute
1946 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001947 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001948 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001949 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001950 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001951 * but not encrypted.
1952 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001953 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001954 * encrypted. For algorithms where the
1955 * encrypted data and the authentication tag
1956 * are defined as separate inputs, the buffer
1957 * must contain the encrypted data followed
1958 * by the authentication tag.
1959 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001960 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001961 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1962 * This must be at least
1963 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1964 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001965 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001966 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001967 *
Gilles Peskine28538492018-07-11 17:34:00 +02001968 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001969 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001970 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001971 * \retval #PSA_ERROR_EMPTY_SLOT
1972 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001973 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001974 * \retval #PSA_ERROR_NOT_PERMITTED
1975 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001976 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001977 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001978 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001979 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1980 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1981 * \retval #PSA_ERROR_HARDWARE_FAILURE
1982 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001983 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001984 * The library has not been previously initialized by psa_crypto_init().
1985 * It is implementation-dependent whether a failure to initialize
1986 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001987 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001988psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001989 psa_algorithm_t alg,
1990 const uint8_t *nonce,
1991 size_t nonce_length,
1992 const uint8_t *additional_data,
1993 size_t additional_data_length,
1994 const uint8_t *ciphertext,
1995 size_t ciphertext_length,
1996 uint8_t *plaintext,
1997 size_t plaintext_size,
1998 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001999
Gilles Peskine30a9e412019-01-14 18:36:12 +01002000/** The type of the state data structure for multipart AEAD operations.
2001 *
2002 * Before calling any function on an AEAD operation object, the application
2003 * must initialize it by any of the following means:
2004 * - Set the structure to all-bits-zero, for example:
2005 * \code
2006 * psa_aead_operation_t operation;
2007 * memset(&operation, 0, sizeof(operation));
2008 * \endcode
2009 * - Initialize the structure to logical zero values, for example:
2010 * \code
2011 * psa_aead_operation_t operation = {0};
2012 * \endcode
2013 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2014 * for example:
2015 * \code
2016 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2017 * \endcode
2018 * - Assign the result of the function psa_aead_operation_init()
2019 * to the structure, for example:
2020 * \code
2021 * psa_aead_operation_t operation;
2022 * operation = psa_aead_operation_init();
2023 * \endcode
2024 *
2025 * This is an implementation-defined \c struct. Applications should not
2026 * make any assumptions about the content of this structure except
2027 * as directed by the documentation of a specific implementation. */
2028typedef struct psa_aead_operation_s psa_aead_operation_t;
2029
2030/** \def PSA_AEAD_OPERATION_INIT
2031 *
2032 * This macro returns a suitable initializer for an AEAD operation object of
2033 * type #psa_aead_operation_t.
2034 */
2035#ifdef __DOXYGEN_ONLY__
2036/* This is an example definition for documentation purposes.
2037 * Implementations should define a suitable value in `crypto_struct.h`.
2038 */
2039#define PSA_AEAD_OPERATION_INIT {0}
2040#endif
2041
2042/** Return an initial value for an AEAD operation object.
2043 */
2044static psa_aead_operation_t psa_aead_operation_init(void);
2045
2046/** Set the key for a multipart authenticated encryption operation.
2047 *
2048 * The sequence of operations to encrypt a message with authentication
2049 * is as follows:
2050 * -# Allocate an operation object which will be passed to all the functions
2051 * listed here.
2052 * -# Initialize the operation object with one of the methods described in the
2053 * documentation for #psa_aead_operation_t, e.g.
2054 * PSA_AEAD_OPERATION_INIT.
2055 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002056 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2057 * inputs to the subsequent calls to psa_aead_update_ad() and
2058 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2059 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002060 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2061 * generate or set the nonce. You should use
2062 * psa_aead_generate_nonce() unless the protocol you are implementing
2063 * requires a specific nonce value.
2064 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2065 * of the non-encrypted additional authenticated data each time.
2066 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002067 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002068 * -# Call psa_aead_finish().
2069 *
2070 * The application may call psa_aead_abort() at any time after the operation
2071 * has been initialized.
2072 *
2073 * After a successful call to psa_aead_encrypt_setup(), the application must
2074 * eventually terminate the operation. The following events terminate an
2075 * operation:
2076 * - A failed call to any of the \c psa_aead_xxx functions.
2077 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2078 *
2079 * \param[in,out] operation The operation object to set up. It must have
2080 * been initialized as per the documentation for
2081 * #psa_aead_operation_t and not yet in use.
2082 * \param handle Handle to the key to use for the operation.
2083 * It must remain valid until the operation
2084 * terminates.
2085 * \param alg The AEAD algorithm to compute
2086 * (\c PSA_ALG_XXX value such that
2087 * #PSA_ALG_IS_AEAD(\p alg) is true).
2088 *
2089 * \retval #PSA_SUCCESS
2090 * Success.
2091 * \retval #PSA_ERROR_INVALID_HANDLE
2092 * \retval #PSA_ERROR_EMPTY_SLOT
2093 * \retval #PSA_ERROR_NOT_PERMITTED
2094 * \retval #PSA_ERROR_INVALID_ARGUMENT
2095 * \p key is not compatible with \p alg.
2096 * \retval #PSA_ERROR_NOT_SUPPORTED
2097 * \p alg is not supported or is not an AEAD algorithm.
2098 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2099 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2100 * \retval #PSA_ERROR_HARDWARE_FAILURE
2101 * \retval #PSA_ERROR_TAMPERING_DETECTED
2102 * \retval #PSA_ERROR_BAD_STATE
2103 * The library has not been previously initialized by psa_crypto_init().
2104 * It is implementation-dependent whether a failure to initialize
2105 * results in this error code.
2106 */
2107psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2108 psa_key_handle_t handle,
2109 psa_algorithm_t alg);
2110
2111/** Set the key for a multipart authenticated decryption operation.
2112 *
2113 * The sequence of operations to decrypt a message with authentication
2114 * is as follows:
2115 * -# Allocate an operation object which will be passed to all the functions
2116 * listed here.
2117 * -# Initialize the operation object with one of the methods described in the
2118 * documentation for #psa_aead_operation_t, e.g.
2119 * PSA_AEAD_OPERATION_INIT.
2120 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002121 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2122 * inputs to the subsequent calls to psa_aead_update_ad() and
2123 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2124 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002125 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2126 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2127 * of the non-encrypted additional authenticated data each time.
2128 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002129 * of the ciphertext to decrypt each time.
2130 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002131 *
2132 * The application may call psa_aead_abort() at any time after the operation
2133 * has been initialized.
2134 *
2135 * After a successful call to psa_aead_decrypt_setup(), the application must
2136 * eventually terminate the operation. The following events terminate an
2137 * operation:
2138 * - A failed call to any of the \c psa_aead_xxx functions.
2139 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2140 *
2141 * \param[in,out] operation The operation object to set up. It must have
2142 * been initialized as per the documentation for
2143 * #psa_aead_operation_t and not yet in use.
2144 * \param handle Handle to the key to use for the operation.
2145 * It must remain valid until the operation
2146 * terminates.
2147 * \param alg The AEAD algorithm to compute
2148 * (\c PSA_ALG_XXX value such that
2149 * #PSA_ALG_IS_AEAD(\p alg) is true).
2150 *
2151 * \retval #PSA_SUCCESS
2152 * Success.
2153 * \retval #PSA_ERROR_INVALID_HANDLE
2154 * \retval #PSA_ERROR_EMPTY_SLOT
2155 * \retval #PSA_ERROR_NOT_PERMITTED
2156 * \retval #PSA_ERROR_INVALID_ARGUMENT
2157 * \p key is not compatible with \p alg.
2158 * \retval #PSA_ERROR_NOT_SUPPORTED
2159 * \p alg is not supported or is not an AEAD algorithm.
2160 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2161 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2162 * \retval #PSA_ERROR_HARDWARE_FAILURE
2163 * \retval #PSA_ERROR_TAMPERING_DETECTED
2164 * \retval #PSA_ERROR_BAD_STATE
2165 * The library has not been previously initialized by psa_crypto_init().
2166 * It is implementation-dependent whether a failure to initialize
2167 * results in this error code.
2168 */
2169psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2170 psa_key_handle_t handle,
2171 psa_algorithm_t alg);
2172
2173/** Generate a random nonce for an authenticated encryption operation.
2174 *
2175 * This function generates a random nonce for the authenticated encryption
2176 * operation with an appropriate size for the chosen algorithm, key type
2177 * and key size.
2178 *
2179 * The application must call psa_aead_encrypt_setup() before
2180 * calling this function.
2181 *
2182 * If this function returns an error status, the operation becomes inactive.
2183 *
2184 * \param[in,out] operation Active AEAD operation.
2185 * \param[out] nonce Buffer where the generated nonce is to be
2186 * written.
2187 * \param nonce_size Size of the \p nonce buffer in bytes.
2188 * \param[out] nonce_length On success, the number of bytes of the
2189 * generated nonce.
2190 *
2191 * \retval #PSA_SUCCESS
2192 * Success.
2193 * \retval #PSA_ERROR_BAD_STATE
2194 * The operation state is not valid (not set up, or nonce already set).
2195 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2196 * The size of the \p nonce buffer is too small.
2197 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2198 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2199 * \retval #PSA_ERROR_HARDWARE_FAILURE
2200 * \retval #PSA_ERROR_TAMPERING_DETECTED
2201 */
2202psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2203 unsigned char *nonce,
2204 size_t nonce_size,
2205 size_t *nonce_length);
2206
2207/** Set the nonce for an authenticated encryption or decryption operation.
2208 *
2209 * This function sets the nonce for the authenticated
2210 * encryption or decryption operation.
2211 *
2212 * The application must call psa_aead_encrypt_setup() before
2213 * calling this function.
2214 *
2215 * If this function returns an error status, the operation becomes inactive.
2216 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002217 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002218 * instead of this function, unless implementing a protocol that requires
2219 * a non-random IV.
2220 *
2221 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002222 * \param[in] nonce Buffer containing the nonce to use.
2223 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002224 *
2225 * \retval #PSA_SUCCESS
2226 * Success.
2227 * \retval #PSA_ERROR_BAD_STATE
2228 * The operation state is not valid (not set up, or nonce already set).
2229 * \retval #PSA_ERROR_INVALID_ARGUMENT
2230 * The size of \p nonce is not acceptable for the chosen algorithm.
2231 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2232 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2233 * \retval #PSA_ERROR_HARDWARE_FAILURE
2234 * \retval #PSA_ERROR_TAMPERING_DETECTED
2235 */
2236psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2237 const unsigned char *nonce,
2238 size_t nonce_length);
2239
Gilles Peskinebc59c852019-01-17 15:26:08 +01002240/** Declare the lengths of the message and additional data for AEAD.
2241 *
2242 * The application must call this function before calling
2243 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2244 * the operation requires it. If the algorithm does not require it,
2245 * calling this function is optional, but if this function is called
2246 * then the implementation must enforce the lengths.
2247 *
2248 * You may call this function before or after setting the nonce with
2249 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2250 *
2251 * - For #PSA_ALG_CCM, calling this function is required.
2252 * - For the other AEAD algorithms defined in this specification, calling
2253 * this function is not required.
2254 * - For vendor-defined algorithm, refer to the vendor documentation.
2255 *
2256 * \param[in,out] operation Active AEAD operation.
2257 * \param ad_length Size of the non-encrypted additional
2258 * authenticated data in bytes.
2259 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2260 *
2261 * \retval #PSA_SUCCESS
2262 * Success.
2263 * \retval #PSA_ERROR_BAD_STATE
2264 * The operation state is not valid (not set up, already completed,
2265 * or psa_aead_update_ad() or psa_aead_update() already called).
2266 * \retval #PSA_ERROR_INVALID_ARGUMENT
2267 * At least one of the lengths is not acceptable for the chosen
2268 * algorithm.
2269 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2270 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2271 * \retval #PSA_ERROR_HARDWARE_FAILURE
2272 * \retval #PSA_ERROR_TAMPERING_DETECTED
2273 */
2274psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2275 size_t ad_length,
2276 size_t plaintext_length);
2277
Gilles Peskine30a9e412019-01-14 18:36:12 +01002278/** Pass additional data to an active AEAD operation.
2279 *
2280 * Additional data is authenticated, but not encrypted.
2281 *
2282 * You may call this function multiple times to pass successive fragments
2283 * of the additional data. You may not call this function after passing
2284 * data to encrypt or decrypt with psa_aead_update().
2285 *
2286 * Before calling this function, you must:
2287 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2288 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2289 *
2290 * If this function returns an error status, the operation becomes inactive.
2291 *
2292 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2293 * there is no guarantee that the input is valid. Therefore, until
2294 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2295 * treat the input as untrusted and prepare to undo any action that
2296 * depends on the input if psa_aead_verify() returns an error status.
2297 *
2298 * \param[in,out] operation Active AEAD operation.
2299 * \param[in] input Buffer containing the fragment of
2300 * additional data.
2301 * \param input_length Size of the \p input buffer in bytes.
2302 *
2303 * \retval #PSA_SUCCESS
2304 * Success.
2305 * \retval #PSA_ERROR_BAD_STATE
2306 * The operation state is not valid (not set up, nonce not set,
2307 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002308 * \retval #PSA_ERROR_INVALID_ARGUMENT
2309 * The total input length overflows the additional data length that
2310 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002311 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2312 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2313 * \retval #PSA_ERROR_HARDWARE_FAILURE
2314 * \retval #PSA_ERROR_TAMPERING_DETECTED
2315 */
2316psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2317 const uint8_t *input,
2318 size_t input_length);
2319
2320/** Encrypt or decrypt a message fragment in an active AEAD operation.
2321 *
2322 * Before calling this function, you must:
2323 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2324 * The choice of setup function determines whether this function
2325 * encrypts or decrypts its input.
2326 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2327 * 3. Call psa_aead_update_ad() to pass all the additional data.
2328 *
2329 * If this function returns an error status, the operation becomes inactive.
2330 *
2331 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2332 * there is no guarantee that the input is valid. Therefore, until
2333 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2334 * - Do not use the output in any way other than storing it in a
2335 * confidential location. If you take any action that depends
2336 * on the tentative decrypted data, this action will need to be
2337 * undone if the input turns out not to be valid. Furthermore,
2338 * if an adversary can observe that this action took place
2339 * (for example through timing), they may be able to use this
2340 * fact as an oracle to decrypt any message encrypted with the
2341 * same key.
2342 * - In particular, do not copy the output anywhere but to a
2343 * memory or storage space that you have exclusive access to.
2344 *
2345 * \param[in,out] operation Active AEAD operation.
2346 * \param[in] input Buffer containing the message fragment to
2347 * encrypt or decrypt.
2348 * \param input_length Size of the \p input buffer in bytes.
2349 * \param[out] output Buffer where the output is to be written.
2350 * \param output_size Size of the \p output buffer in bytes.
2351 * \param[out] output_length On success, the number of bytes
2352 * that make up the returned output.
2353 *
2354 * \retval #PSA_SUCCESS
2355 * Success.
2356 * \retval #PSA_ERROR_BAD_STATE
2357 * The operation state is not valid (not set up, nonce not set
2358 * or already completed).
2359 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2360 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002361 * \retval #PSA_ERROR_INVALID_ARGUMENT
2362 * The total length of input to psa_aead_update_ad() so far is
2363 * less than the additional data length that was previously
2364 * specified with psa_aead_set_lengths().
2365 * \retval #PSA_ERROR_INVALID_ARGUMENT
2366 * The total input length overflows the plaintext length that
2367 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002368 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2369 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2370 * \retval #PSA_ERROR_HARDWARE_FAILURE
2371 * \retval #PSA_ERROR_TAMPERING_DETECTED
2372 */
2373psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2374 const uint8_t *input,
2375 size_t input_length,
2376 unsigned char *output,
2377 size_t output_size,
2378 size_t *output_length);
2379
2380/** Finish encrypting a message in an AEAD operation.
2381 *
2382 * The operation must have been set up with psa_aead_encrypt_setup().
2383 *
2384 * This function finishes the authentication of the additional data
2385 * formed by concatenating the inputs passed to preceding calls to
2386 * psa_aead_update_ad() with the plaintext formed by concatenating the
2387 * inputs passed to preceding calls to psa_aead_update().
2388 *
2389 * This function has two output buffers:
2390 * - \p ciphertext contains trailing ciphertext that was buffered from
2391 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2392 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2393 * will not contain any output and can be a 0-sized buffer.
2394 * - \p tag contains the authentication tag. Its length is always
2395 * #PSA_AEAD_TAG_LENGTH(\p alg) where \p alg is the AEAD algorithm
2396 * that the operation performs.
2397 *
2398 * When this function returns, the operation becomes inactive.
2399 *
2400 * \param[in,out] operation Active AEAD operation.
2401 * \param[out] ciphertext Buffer where the last part of the ciphertext
2402 * is to be written.
2403 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2404 * \param[out] ciphertext_length On success, the number of bytes of
2405 * returned ciphertext.
2406 * \param[out] tag Buffer where the authentication tag is
2407 * to be written.
2408 * \param tag_size Size of the \p tag buffer in bytes.
2409 * \param[out] tag_length On success, the number of bytes
2410 * that make up the returned tag.
2411 *
2412 * \retval #PSA_SUCCESS
2413 * Success.
2414 * \retval #PSA_ERROR_BAD_STATE
2415 * The operation state is not valid (not set up, nonce not set,
2416 * decryption, or already completed).
2417 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2418 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002419 * \retval #PSA_ERROR_INVALID_ARGUMENT
2420 * The total length of input to psa_aead_update_ad() so far is
2421 * less than the additional data length that was previously
2422 * specified with psa_aead_set_lengths().
2423 * \retval #PSA_ERROR_INVALID_ARGUMENT
2424 * The total length of input to psa_aead_update() so far is
2425 * less than the plaintext length that was previously
2426 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002427 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2428 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2429 * \retval #PSA_ERROR_HARDWARE_FAILURE
2430 * \retval #PSA_ERROR_TAMPERING_DETECTED
2431 */
2432psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002433 uint8_t *ciphertext,
2434 size_t ciphertext_size,
2435 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002436 uint8_t *tag,
2437 size_t tag_size,
2438 size_t *tag_length);
2439
2440/** Finish authenticating and decrypting a message in an AEAD operation.
2441 *
2442 * The operation must have been set up with psa_aead_decrypt_setup().
2443 *
2444 * This function finishes the authentication of the additional data
2445 * formed by concatenating the inputs passed to preceding calls to
2446 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2447 * inputs passed to preceding calls to psa_aead_update().
2448 *
2449 * When this function returns, the operation becomes inactive.
2450 *
2451 * \param[in,out] operation Active AEAD operation.
2452 * \param[in] tag Buffer containing the authentication tag.
2453 * \param tag_length Size of the \p tag buffer in bytes.
2454 *
2455 * \retval #PSA_SUCCESS
2456 * Success.
2457 * \retval #PSA_ERROR_BAD_STATE
2458 * The operation state is not valid (not set up, nonce not set,
2459 * encryption, or already completed).
2460 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2461 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002462 * \retval #PSA_ERROR_INVALID_ARGUMENT
2463 * The total length of input to psa_aead_update_ad() so far is
2464 * less than the additional data length that was previously
2465 * specified with psa_aead_set_lengths().
2466 * \retval #PSA_ERROR_INVALID_ARGUMENT
2467 * The total length of input to psa_aead_update() so far is
2468 * less than the plaintext length that was previously
2469 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002470 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2471 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2472 * \retval #PSA_ERROR_HARDWARE_FAILURE
2473 * \retval #PSA_ERROR_TAMPERING_DETECTED
2474 */
2475psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2476 const uint8_t *tag,
2477 size_t tag_length);
2478
2479/** Abort an AEAD operation.
2480 *
2481 * Aborting an operation frees all associated resources except for the
2482 * \p operation structure itself. Once aborted, the operation object
2483 * can be reused for another operation by calling
2484 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2485 *
2486 * You may call this function any time after the operation object has
2487 * been initialized by any of the following methods:
2488 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2489 * whether it succeeds or not.
2490 * - Initializing the \c struct to all-bits-zero.
2491 * - Initializing the \c struct to logical zeros, e.g.
2492 * `psa_aead_operation_t operation = {0}`.
2493 *
2494 * In particular, calling psa_aead_abort() after the operation has been
2495 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2496 * is safe and has no effect.
2497 *
2498 * \param[in,out] operation Initialized AEAD operation.
2499 *
2500 * \retval #PSA_SUCCESS
2501 * \retval #PSA_ERROR_BAD_STATE
2502 * \p operation is not an active AEAD operation.
2503 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2504 * \retval #PSA_ERROR_HARDWARE_FAILURE
2505 * \retval #PSA_ERROR_TAMPERING_DETECTED
2506 */
2507psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2508
Gilles Peskine3b555712018-03-03 21:27:57 +01002509/**@}*/
2510
Gilles Peskine20035e32018-02-03 22:44:14 +01002511/** \defgroup asymmetric Asymmetric cryptography
2512 * @{
2513 */
2514
2515/**
2516 * \brief Sign a hash or short message with a private key.
2517 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002518 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002519 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002520 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2521 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2522 * to determine the hash algorithm to use.
2523 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002524 * \param handle Handle to the key to use for the operation.
2525 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002526 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002527 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002528 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002529 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002530 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002531 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002532 * \param[out] signature_length On success, the number of bytes
2533 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002534 *
Gilles Peskine28538492018-07-11 17:34:00 +02002535 * \retval #PSA_SUCCESS
2536 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002537 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002538 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002539 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002540 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002541 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002542 * \retval #PSA_ERROR_NOT_SUPPORTED
2543 * \retval #PSA_ERROR_INVALID_ARGUMENT
2544 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2545 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2546 * \retval #PSA_ERROR_HARDWARE_FAILURE
2547 * \retval #PSA_ERROR_TAMPERING_DETECTED
2548 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002549 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002550 * The library has not been previously initialized by psa_crypto_init().
2551 * It is implementation-dependent whether a failure to initialize
2552 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002553 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002554psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002555 psa_algorithm_t alg,
2556 const uint8_t *hash,
2557 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002558 uint8_t *signature,
2559 size_t signature_size,
2560 size_t *signature_length);
2561
2562/**
2563 * \brief Verify the signature a hash or short message using a public key.
2564 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002565 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002566 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002567 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2568 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2569 * to determine the hash algorithm to use.
2570 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002571 * \param handle Handle to the key to use for the operation.
2572 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002573 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002574 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002575 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002576 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002577 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002578 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002579 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002580 *
Gilles Peskine28538492018-07-11 17:34:00 +02002581 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002582 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002583 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002584 * The calculation was perfomed successfully, but the passed
2585 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002586 * \retval #PSA_ERROR_NOT_SUPPORTED
2587 * \retval #PSA_ERROR_INVALID_ARGUMENT
2588 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2589 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2590 * \retval #PSA_ERROR_HARDWARE_FAILURE
2591 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002592 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002593 * The library has not been previously initialized by psa_crypto_init().
2594 * It is implementation-dependent whether a failure to initialize
2595 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002596 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002597psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002598 psa_algorithm_t alg,
2599 const uint8_t *hash,
2600 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002601 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002602 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002603
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002604/**
2605 * \brief Encrypt a short message with a public key.
2606 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002607 * \param handle Handle to the key to use for the operation.
2608 * It must be a public key or an asymmetric
2609 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002610 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002611 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002612 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002613 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002614 * \param[in] salt A salt or label, if supported by the
2615 * encryption algorithm.
2616 * If the algorithm does not support a
2617 * salt, pass \c NULL.
2618 * If the algorithm supports an optional
2619 * salt and you do not want to pass a salt,
2620 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002621 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002622 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2623 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002624 * \param salt_length Size of the \p salt buffer in bytes.
2625 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002626 * \param[out] output Buffer where the encrypted message is to
2627 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002628 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002629 * \param[out] output_length On success, the number of bytes
2630 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002631 *
Gilles Peskine28538492018-07-11 17:34:00 +02002632 * \retval #PSA_SUCCESS
2633 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002634 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002635 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002636 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002637 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002638 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002639 * \retval #PSA_ERROR_NOT_SUPPORTED
2640 * \retval #PSA_ERROR_INVALID_ARGUMENT
2641 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2642 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2643 * \retval #PSA_ERROR_HARDWARE_FAILURE
2644 * \retval #PSA_ERROR_TAMPERING_DETECTED
2645 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002646 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002647 * The library has not been previously initialized by psa_crypto_init().
2648 * It is implementation-dependent whether a failure to initialize
2649 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002650 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002651psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002652 psa_algorithm_t alg,
2653 const uint8_t *input,
2654 size_t input_length,
2655 const uint8_t *salt,
2656 size_t salt_length,
2657 uint8_t *output,
2658 size_t output_size,
2659 size_t *output_length);
2660
2661/**
2662 * \brief Decrypt a short message with a private key.
2663 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002664 * \param handle Handle to the key to use for the operation.
2665 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002666 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002667 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002668 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002669 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002670 * \param[in] salt A salt or label, if supported by the
2671 * encryption algorithm.
2672 * If the algorithm does not support a
2673 * salt, pass \c NULL.
2674 * If the algorithm supports an optional
2675 * salt and you do not want to pass a salt,
2676 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002677 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002678 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2679 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002680 * \param salt_length Size of the \p salt buffer in bytes.
2681 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002682 * \param[out] output Buffer where the decrypted message is to
2683 * be written.
2684 * \param output_size Size of the \c output buffer in bytes.
2685 * \param[out] output_length On success, the number of bytes
2686 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002687 *
Gilles Peskine28538492018-07-11 17:34:00 +02002688 * \retval #PSA_SUCCESS
2689 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002690 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002691 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002692 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002693 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002694 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002695 * \retval #PSA_ERROR_NOT_SUPPORTED
2696 * \retval #PSA_ERROR_INVALID_ARGUMENT
2697 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2698 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2699 * \retval #PSA_ERROR_HARDWARE_FAILURE
2700 * \retval #PSA_ERROR_TAMPERING_DETECTED
2701 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2702 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002703 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002704 * The library has not been previously initialized by psa_crypto_init().
2705 * It is implementation-dependent whether a failure to initialize
2706 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002707 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002708psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002709 psa_algorithm_t alg,
2710 const uint8_t *input,
2711 size_t input_length,
2712 const uint8_t *salt,
2713 size_t salt_length,
2714 uint8_t *output,
2715 size_t output_size,
2716 size_t *output_length);
2717
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002718/**@}*/
2719
Gilles Peskineedd76872018-07-20 17:42:05 +02002720/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002721 * @{
2722 */
2723
2724/** The type of the state data structure for generators.
2725 *
2726 * Before calling any function on a generator, the application must
2727 * initialize it by any of the following means:
2728 * - Set the structure to all-bits-zero, for example:
2729 * \code
2730 * psa_crypto_generator_t generator;
2731 * memset(&generator, 0, sizeof(generator));
2732 * \endcode
2733 * - Initialize the structure to logical zero values, for example:
2734 * \code
2735 * psa_crypto_generator_t generator = {0};
2736 * \endcode
2737 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2738 * for example:
2739 * \code
2740 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2741 * \endcode
2742 * - Assign the result of the function psa_crypto_generator_init()
2743 * to the structure, for example:
2744 * \code
2745 * psa_crypto_generator_t generator;
2746 * generator = psa_crypto_generator_init();
2747 * \endcode
2748 *
2749 * This is an implementation-defined \c struct. Applications should not
2750 * make any assumptions about the content of this structure except
2751 * as directed by the documentation of a specific implementation.
2752 */
2753typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2754
2755/** \def PSA_CRYPTO_GENERATOR_INIT
2756 *
2757 * This macro returns a suitable initializer for a generator object
2758 * of type #psa_crypto_generator_t.
2759 */
2760#ifdef __DOXYGEN_ONLY__
2761/* This is an example definition for documentation purposes.
2762 * Implementations should define a suitable value in `crypto_struct.h`.
2763 */
2764#define PSA_CRYPTO_GENERATOR_INIT {0}
2765#endif
2766
2767/** Return an initial value for a generator object.
2768 */
2769static psa_crypto_generator_t psa_crypto_generator_init(void);
2770
2771/** Retrieve the current capacity of a generator.
2772 *
2773 * The capacity of a generator is the maximum number of bytes that it can
2774 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2775 *
2776 * \param[in] generator The generator to query.
2777 * \param[out] capacity On success, the capacity of the generator.
2778 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002779 * \retval #PSA_SUCCESS
2780 * \retval #PSA_ERROR_BAD_STATE
2781 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002782 */
2783psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2784 size_t *capacity);
2785
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002786/** Set the maximum capacity of a generator.
2787 *
2788 * \param[in,out] generator The generator object to modify.
2789 * \param capacity The new capacity of the generator.
2790 * It must be less or equal to the generator's
2791 * current capacity.
2792 *
2793 * \retval #PSA_SUCCESS
2794 * \retval #PSA_ERROR_INVALID_ARGUMENT
2795 * \p capacity is larger than the generator's current capacity.
2796 * \retval #PSA_ERROR_BAD_STATE
2797 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2798 */
2799psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2800 size_t capacity);
2801
Gilles Peskineeab56e42018-07-12 17:12:33 +02002802/** Read some data from a generator.
2803 *
2804 * This function reads and returns a sequence of bytes from a generator.
2805 * The data that is read is discarded from the generator. The generator's
2806 * capacity is decreased by the number of bytes read.
2807 *
2808 * \param[in,out] generator The generator object to read from.
2809 * \param[out] output Buffer where the generator output will be
2810 * written.
2811 * \param output_length Number of bytes to output.
2812 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002813 * \retval #PSA_SUCCESS
2814 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002815 * There were fewer than \p output_length bytes
2816 * in the generator. Note that in this case, no
2817 * output is written to the output buffer.
2818 * The generator's capacity is set to 0, thus
2819 * subsequent calls to this function will not
2820 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002821 * \retval #PSA_ERROR_BAD_STATE
2822 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2823 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2824 * \retval #PSA_ERROR_HARDWARE_FAILURE
2825 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002826 */
2827psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2828 uint8_t *output,
2829 size_t output_length);
2830
2831/** Create a symmetric key from data read from a generator.
2832 *
2833 * This function reads a sequence of bytes from a generator and imports
2834 * these bytes as a key.
2835 * The data that is read is discarded from the generator. The generator's
2836 * capacity is decreased by the number of bytes read.
2837 *
2838 * This function is equivalent to calling #psa_generator_read and
2839 * passing the resulting output to #psa_import_key, but
2840 * if the implementation provides an isolation boundary then
2841 * the key material is not exposed outside the isolation boundary.
2842 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002843 * \param handle Handle to the slot where the key will be stored.
2844 * This must be a valid slot for a key of the chosen
2845 * type: it must have been obtained by calling
2846 * psa_allocate_key() or psa_create_key() with the
2847 * correct \p type and with a maximum size that is
2848 * compatible with \p bits.
2849 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002850 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2851 * This must be a symmetric key type.
2852 * \param bits Key size in bits.
2853 * \param[in,out] generator The generator object to read from.
2854 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002855 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002856 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002857 * If the key is persistent, the key material and the key's metadata
2858 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002859 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002860 * There were fewer than \p output_length bytes
2861 * in the generator. Note that in this case, no
2862 * output is written to the output buffer.
2863 * The generator's capacity is set to 0, thus
2864 * subsequent calls to this function will not
2865 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002866 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002867 * The key type or key size is not supported, either by the
2868 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002869 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002870 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002871 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002872 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002873 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2874 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2875 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2876 * \retval #PSA_ERROR_HARDWARE_FAILURE
2877 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002878 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002879 * The library has not been previously initialized by psa_crypto_init().
2880 * It is implementation-dependent whether a failure to initialize
2881 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002882 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002883psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002884 psa_key_type_t type,
2885 size_t bits,
2886 psa_crypto_generator_t *generator);
2887
2888/** Abort a generator.
2889 *
2890 * Once a generator has been aborted, its capacity is zero.
2891 * Aborting a generator frees all associated resources except for the
2892 * \c generator structure itself.
2893 *
2894 * This function may be called at any time as long as the generator
2895 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2896 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2897 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2898 * on a generator that has not been set up.
2899 *
2900 * Once aborted, the generator object may be called.
2901 *
2902 * \param[in,out] generator The generator to abort.
2903 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002904 * \retval #PSA_SUCCESS
2905 * \retval #PSA_ERROR_BAD_STATE
2906 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2907 * \retval #PSA_ERROR_HARDWARE_FAILURE
2908 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002909 */
2910psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2911
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002912/** Use the maximum possible capacity for a generator.
2913 *
2914 * Use this value as the capacity argument when setting up a generator
2915 * to indicate that the generator should have the maximum possible capacity.
2916 * The value of the maximum possible capacity depends on the generator
2917 * algorithm.
2918 */
2919#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2920
Gilles Peskineeab56e42018-07-12 17:12:33 +02002921/**@}*/
2922
Gilles Peskineea0fb492018-07-12 17:17:20 +02002923/** \defgroup derivation Key derivation
2924 * @{
2925 */
2926
2927/** Set up a key derivation operation.
2928 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002929 * A key derivation algorithm takes some inputs and uses them to create
2930 * a byte generator which can be used to produce keys and other
2931 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002932 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002933 * To use a generator for key derivation:
2934 * - Start with an initialized object of type #psa_crypto_generator_t.
2935 * - Call psa_key_derivation_setup() to select the algorithm.
2936 * - Provide the inputs for the key derivation by calling
2937 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
2938 * as appropriate. Which inputs are needed, in what order, and whether
2939 * they may be keys and if so of what type depends on the algorithm.
2940 * - Optionally set the generator's maximum capacity with
2941 * psa_set_generator_capacity(). You may do this before, in the middle of
2942 * or after providing inputs. For some algorithms, this step is mandatory
2943 * because the output depends on the maximum capacity.
2944 * - Generate output with psa_generator_read() or
2945 * psa_generator_import_key(). Successive calls to these functions
2946 * use successive output bytes from the generator.
2947 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02002948 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002949 * \param[in,out] generator The generator object to set up. It must
2950 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002951 * \param alg The key derivation algorithm to compute
2952 * (\c PSA_ALG_XXX value such that
2953 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02002954 *
2955 * \retval #PSA_SUCCESS
2956 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002957 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002958 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002959 * \retval #PSA_ERROR_NOT_SUPPORTED
2960 * \c alg is not supported or is not a key derivation algorithm.
2961 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2962 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2963 * \retval #PSA_ERROR_HARDWARE_FAILURE
2964 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002965 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002966 */
2967psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
2968 psa_algorithm_t alg);
2969
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002970/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002971 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002972 * Which inputs are required and in what order depends on the algorithm.
2973 * Refer to the documentation of each key derivation or key agreement
2974 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002975 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002976 * This function passes direct inputs. Some inputs must be passed as keys
2977 * using psa_key_derivation_input_key() instead of this function. Refer to
2978 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002979 *
2980 * \param[in,out] generator The generator object to use. It must
2981 * have been set up with
2982 * psa_key_derivation_setup() and must not
2983 * have produced any output yet.
2984 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002985 * \param[in] data Input data to use.
2986 * \param data_length Size of the \p data buffer in bytes.
2987 *
2988 * \retval #PSA_SUCCESS
2989 * Success.
2990 * \retval #PSA_ERROR_INVALID_ARGUMENT
2991 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01002992 * \retval #PSA_ERROR_INVALID_ARGUMENT
2993 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002994 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2995 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2996 * \retval #PSA_ERROR_HARDWARE_FAILURE
2997 * \retval #PSA_ERROR_TAMPERING_DETECTED
2998 * \retval #PSA_ERROR_BAD_STATE
2999 * The value of \p step is not valid given the state of \p generator.
3000 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003001 * The library has not been previously initialized by psa_crypto_init().
3002 * It is implementation-dependent whether a failure to initialize
3003 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003004 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003005psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3006 psa_key_derivation_step_t step,
3007 const uint8_t *data,
3008 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003009
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003010/** Provide an input for key derivation in the form of a key.
3011 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003012 * Which inputs are required and in what order depends on the algorithm.
3013 * Refer to the documentation of each key derivation or key agreement
3014 * algorithm for information.
3015 *
3016 * This function passes key inputs. Some inputs must be passed as keys
3017 * of the appropriate type using this function, while others must be
3018 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3019 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003020 *
3021 * \param[in,out] generator The generator object to use. It must
3022 * have been set up with
3023 * psa_key_derivation_setup() and must not
3024 * have produced any output yet.
3025 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003026 * \param handle Handle to the key. It must have an
3027 * appropriate type for \p step and must
3028 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003029 *
3030 * \retval #PSA_SUCCESS
3031 * Success.
3032 * \retval #PSA_ERROR_INVALID_HANDLE
3033 * \retval #PSA_ERROR_EMPTY_SLOT
3034 * \retval #PSA_ERROR_NOT_PERMITTED
3035 * \retval #PSA_ERROR_INVALID_ARGUMENT
3036 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003037 * \retval #PSA_ERROR_INVALID_ARGUMENT
3038 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003039 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3040 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3041 * \retval #PSA_ERROR_HARDWARE_FAILURE
3042 * \retval #PSA_ERROR_TAMPERING_DETECTED
3043 * \retval #PSA_ERROR_BAD_STATE
3044 * The value of \p step is not valid given the state of \p generator.
3045 * \retval #PSA_ERROR_BAD_STATE
3046 * The library has not been previously initialized by psa_crypto_init().
3047 * It is implementation-dependent whether a failure to initialize
3048 * results in this error code.
3049 */
3050psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3051 psa_key_derivation_step_t step,
3052 psa_key_handle_t handle);
3053
Gilles Peskine969c5d62019-01-16 15:53:06 +01003054/** Perform a key agreement and use the shared secret as input to a key
3055 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003056 *
3057 * A key agreement algorithm takes two inputs: a private key \p private_key
3058 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003059 * The result of this function is passed as input to a key derivation.
3060 * The output of this key derivation can be extracted by reading from the
3061 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003062 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003063 * \param[in,out] generator The generator object to use. It must
3064 * have been set up with
3065 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003066 * key agreement and derivation algorithm
3067 * \c alg (\c PSA_ALG_XXX value such that
3068 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true
3069 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3070 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003071 * The generator must be ready for an
3072 * input of the type given by \p step.
3073 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003074 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003075 * \param[in] peer_key Public key of the peer. The peer key must be in the
3076 * same format that psa_import_key() accepts for the
3077 * public key type corresponding to the type of
3078 * private_key. That is, this function performs the
3079 * equivalent of
3080 * `psa_import_key(internal_public_key_handle,
3081 * PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(private_key_type),
3082 * peer_key, peer_key_length)` where
3083 * `private_key_type` is the type of `private_key`.
3084 * For example, for EC keys, this means that peer_key
3085 * is interpreted as a point on the curve that the
3086 * private key is on. The standard formats for public
3087 * keys are documented in the documentation of
3088 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003089 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003090 *
3091 * \retval #PSA_SUCCESS
3092 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003093 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02003094 * \retval #PSA_ERROR_EMPTY_SLOT
3095 * \retval #PSA_ERROR_NOT_PERMITTED
3096 * \retval #PSA_ERROR_INVALID_ARGUMENT
3097 * \c private_key is not compatible with \c alg,
3098 * or \p peer_key is not valid for \c alg or not compatible with
3099 * \c private_key.
3100 * \retval #PSA_ERROR_NOT_SUPPORTED
3101 * \c alg is not supported or is not a key derivation algorithm.
3102 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3103 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3104 * \retval #PSA_ERROR_HARDWARE_FAILURE
3105 * \retval #PSA_ERROR_TAMPERING_DETECTED
3106 */
3107psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003108 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003109 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003110 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003111 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003112
Gilles Peskine769c7a62019-01-18 16:42:29 +01003113/** Perform a key agreement and use the shared secret as input to a key
3114 * derivation.
3115 *
3116 * A key agreement algorithm takes two inputs: a private key \p private_key
3117 * a public key \p peer_key.
3118 *
3119 * \warning The raw result of a key agreement algorithm such as finite-field
3120 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3121 * not be used directly as key material. It should instead be passed as
3122 * input to a key derivation algorithm. To chain a key agreement with
3123 * a key derivation, use psa_key_agreement() and other functions from
3124 * the key derivation and generator interface.
3125 *
3126 * \param private_key Handle to the private key to use.
3127 * \param[in] peer_key Public key of the peer. It must be
3128 * in the same format that psa_import_key()
3129 * accepts. The standard formats for public
3130 * keys are documented in the documentation
3131 * of psa_export_public_key().
3132 * \param peer_key_length Size of \p peer_key in bytes.
3133 * \param[out] output Buffer where the decrypted message is to
3134 * be written.
3135 * \param output_size Size of the \c output buffer in bytes.
3136 * \param[out] output_length On success, the number of bytes
3137 * that make up the returned output.
3138 *
3139 * \retval #PSA_SUCCESS
3140 * Success.
3141 * \retval #PSA_ERROR_INVALID_HANDLE
3142 * \retval #PSA_ERROR_EMPTY_SLOT
3143 * \retval #PSA_ERROR_NOT_PERMITTED
3144 * \retval #PSA_ERROR_INVALID_ARGUMENT
3145 * \p alg is not a key agreement algorithm
3146 * \retval #PSA_ERROR_INVALID_ARGUMENT
3147 * \p private_key is not compatible with \p alg,
3148 * or \p peer_key is not valid for \p alg or not compatible with
3149 * \p private_key.
3150 * \retval #PSA_ERROR_NOT_SUPPORTED
3151 * \p alg is not a supported key agreement algorithm.
3152 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3153 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3154 * \retval #PSA_ERROR_HARDWARE_FAILURE
3155 * \retval #PSA_ERROR_TAMPERING_DETECTED
3156 */
3157psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3158 psa_key_handle_t private_key,
3159 const uint8_t *peer_key,
3160 size_t peer_key_length,
3161 uint8_t *output,
3162 size_t output_size,
3163 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003164
3165/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003166
3167/** \defgroup random Random generation
3168 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003169 */
3170
3171/**
3172 * \brief Generate random bytes.
3173 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003174 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003175 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003176 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003177 *
3178 * \note To generate a key, use psa_generate_key() instead.
3179 *
3180 * \param[out] output Output buffer for the generated data.
3181 * \param output_size Number of bytes to generate and output.
3182 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003183 * \retval #PSA_SUCCESS
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003184 * \retval #PSA_ERROR_NOT_SUPPORTED
3185 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003186 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003187 * \retval #PSA_ERROR_HARDWARE_FAILURE
3188 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003189 * \retval #PSA_ERROR_BAD_STATE
3190 * The library has not been previously initialized by psa_crypto_init().
3191 * It is implementation-dependent whether a failure to initialize
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003192 * results in this error code.
3193 */
3194psa_status_t psa_generate_random(uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003195 size_t output_size);
3196
3197/** Extra parameters for RSA key generation.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003198 *
Gilles Peskine28538492018-07-11 17:34:00 +02003199 * You may pass a pointer to a structure of this type as the \c extra
3200 * parameter to psa_generate_key().
3201 */
3202typedef struct {
3203 uint32_t e; /**< Public exponent value. Default: 65537. */
3204} psa_generate_key_extra_rsa;
3205
3206/**
itayzafrir90d8c7a2018-09-12 11:44:52 +03003207 * \brief Generate a key or key pair.
itayzafrir18617092018-09-16 12:22:41 +03003208 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003209 * \param handle Handle to the slot where the key will be stored.
3210 * This must be a valid slot for a key of the chosen
3211 * type: it must have been obtained by calling
3212 * psa_allocate_key() or psa_create_key() with the
3213 * correct \p type and with a maximum size that is
3214 * compatible with \p bits.
3215 * It must not contain any key material yet.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003216 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
3217 * \param bits Key size in bits.
3218 * \param[in] extra Extra parameters for key generation. The
3219 * interpretation of this parameter depends on
3220 * \p type. All types support \c NULL to use
3221 * default parameters. Implementation that support
3222 * the generation of vendor-specific key types
3223 * that allow extra parameters shall document
3224 * the format of these extra parameters and
3225 * the default values. For standard parameters,
3226 * the meaning of \p extra is as follows:
3227 * - For a symmetric key type (a type such
3228 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
Gilles Peskine53d991e2018-07-12 01:14:59 +02003229 * false), \p extra must be \c NULL.
3230 * - For an elliptic curve key type (a type
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003231 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
3232 * false), \p extra must be \c NULL.
3233 * - For an RSA key (\p type is
Gilles Peskinee59236f2018-01-27 23:32:46 +01003234 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3235 * optional #psa_generate_key_extra_rsa structure
3236 * specifying the public exponent. The
3237 * default public exponent used when \p extra
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003238 * is \c NULL is 65537.
Jaeden Amero1308fb52019-01-11 13:50:43 +00003239 * - For an DSA key (\p type is
3240 * #PSA_KEY_TYPE_DSA_KEYPAIR), \p extra is an
3241 * optional structure specifying the key domain
3242 * parameters. The key domain parameters can also be
3243 * provided by psa_set_key_domain_parameters(),
3244 * which documents the format of the structure.
Jaeden Amero8851c402019-01-11 14:20:03 +00003245 * - For a DH key (\p type is
3246 * #PSA_KEY_TYPE_DH_KEYPAIR), the \p extra is an
3247 * optional structure specifying the key domain
3248 * parameters. The key domain parameters can also be
3249 * provided by psa_set_key_domain_parameters(),
3250 * which documents the format of the structure.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003251 * \param extra_size Size of the buffer that \p extra
3252 * points to, in bytes. Note that if \p extra is
3253 * \c NULL then \p extra_size must be zero.
3254 *
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003255 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003256 * Success.
3257 * If the key is persistent, the key material and the key's metadata
3258 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003259 * \retval #PSA_ERROR_INVALID_HANDLE
3260 * \retval #PSA_ERROR_OCCUPIED_SLOT
3261 * There is already a key in the specified slot.
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003262 * \retval #PSA_ERROR_NOT_SUPPORTED
3263 * \retval #PSA_ERROR_INVALID_ARGUMENT
3264 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003265 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3266 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3267 * \retval #PSA_ERROR_HARDWARE_FAILURE
3268 * \retval #PSA_ERROR_TAMPERING_DETECTED
3269 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003270 * The library has not been previously initialized by psa_crypto_init().
3271 * It is implementation-dependent whether a failure to initialize
3272 * results in this error code.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003273 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003274psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskinee59236f2018-01-27 23:32:46 +01003275 psa_key_type_t type,
3276 size_t bits,
3277 const void *extra,
3278 size_t extra_size);
3279
3280/**@}*/
3281
3282#ifdef __cplusplus
3283}
3284#endif
3285
3286/* The file "crypto_sizes.h" contains definitions for size calculation
3287 * macros whose definitions are implementation-specific. */
3288#include "crypto_sizes.h"
3289
3290/* The file "crypto_struct.h" contains definitions for
3291 * implementation-specific structs that are declared above. */
3292#include "crypto_struct.h"
3293
3294/* The file "crypto_extra.h" contains vendor-specific definitions. This
3295 * can include vendor-defined algorithms, extra functions, etc. */
3296#include "crypto_extra.h"
3297
3298#endif /* PSA_CRYPTO_H */