blob: 57edf7c8909fea807fab1401618dabf4aab0037e [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 *
234 * \param handle The key handle to close.
235 *
236 * \retval #PSA_SUCCESS
237 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100238 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100239 */
240psa_status_t psa_close_key(psa_key_handle_t handle);
241
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100242/**@}*/
243
244/** \defgroup import_export Key import and export
245 * @{
246 */
247
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100248/**
249 * \brief Import a key in binary format.
250 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100251 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100252 * documentation of psa_export_public_key() for the format of public keys
253 * and to the documentation of psa_export_key() for the format for
254 * other key types.
255 *
256 * This specification supports a single format for each key type.
257 * Implementations may support other formats as long as the standard
258 * format is supported. Implementations that support other formats
259 * should ensure that the formats are clearly unambiguous so as to
260 * minimize the risk that an invalid input is accidentally interpreted
261 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100262 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100263 * \param handle Handle to the slot where the key will be stored.
264 * This must be a valid slot for a key of the chosen
265 * type: it must have been obtained by calling
266 * psa_allocate_key() or psa_create_key() with the
267 * correct \p type and with a maximum size that is
268 * compatible with \p data.
Gilles Peskinef7933932018-10-31 14:07:52 +0100269 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
270 * import, the key slot will contain a key of this type.
271 * \param[in] data Buffer containing the key data. The content of this
272 * buffer is interpreted according to \p type. It must
273 * contain the format described in the documentation
274 * of psa_export_key() or psa_export_public_key() for
275 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200276 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100277 *
Gilles Peskine28538492018-07-11 17:34:00 +0200278 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100279 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100280 * If the key is persistent, the key material and the key's metadata
281 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100282 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200283 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200284 * The key type or key size is not supported, either by the
285 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200286 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100287 * The key slot is invalid,
288 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200289 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200290 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200291 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
292 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
293 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100294 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200295 * \retval #PSA_ERROR_HARDWARE_FAILURE
296 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300297 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300298 * The library has not been previously initialized by psa_crypto_init().
299 * It is implementation-dependent whether a failure to initialize
300 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100301 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100302psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100303 psa_key_type_t type,
304 const uint8_t *data,
305 size_t data_length);
306
307/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100308 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200309 *
310 * This function destroys the content of the key slot from both volatile
311 * memory and, if applicable, non-volatile storage. Implementations shall
312 * make a best effort to ensure that any previous content of the slot is
313 * unrecoverable.
314 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100315 * This function also erases any metadata such as policies and frees all
316 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200317 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100318 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100319 *
Gilles Peskine28538492018-07-11 17:34:00 +0200320 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200321 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200322 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200323 * The slot holds content and cannot be erased because it is
324 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100325 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200326 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200327 * There was an failure in communication with the cryptoprocessor.
328 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200329 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200330 * The storage is corrupted. Implementations shall make a best effort
331 * to erase key material even in this stage, however applications
332 * should be aware that it may be impossible to guarantee that the
333 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200334 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200335 * An unexpected condition which is not a storage corruption or
336 * a communication failure occurred. The cryptoprocessor may have
337 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300338 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300339 * The library has not been previously initialized by psa_crypto_init().
340 * It is implementation-dependent whether a failure to initialize
341 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100342 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100343psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100344
345/**
346 * \brief Get basic metadata about a key.
347 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100348 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200349 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100350 * This may be a null pointer, in which case the key type
351 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200352 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100353 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100354 * is not written.
355 *
Gilles Peskine28538492018-07-11 17:34:00 +0200356 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100357 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200358 * \retval #PSA_ERROR_EMPTY_SLOT
Gilles Peskineae32aac2018-11-30 14:39:32 +0100359 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200360 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
361 * \retval #PSA_ERROR_HARDWARE_FAILURE
362 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300363 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300364 * The library has not been previously initialized by psa_crypto_init().
365 * It is implementation-dependent whether a failure to initialize
366 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100367 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100368psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100369 psa_key_type_t *type,
370 size_t *bits);
371
372/**
Jaeden Amero283dfd12019-01-11 12:06:22 +0000373 * \brief Set domain parameters for a key.
374 *
375 * Some key types require additional domain parameters to be set before import
376 * or generation of the key. The domain parameters can be set with this
377 * function or, for key generation, through the \c extra parameter of
378 * psa_generate_key().
379 *
380 * The format for the required domain parameters varies by the key type.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000381 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
382 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
383 * ```
384 * Dss-Parms ::= SEQUENCE {
385 * p INTEGER,
386 * q INTEGER,
387 * g INTEGER
388 * }
389 * ```
Jaeden Amero283dfd12019-01-11 12:06:22 +0000390 *
391 * \param handle Handle to the key to set domain parameters for.
392 * \param[in] data Buffer containing the key domain parameters. The content
393 * of this buffer is interpreted according to \p type. of
394 * psa_export_key() or psa_export_public_key() for the
395 * chosen type.
396 * \param data_length Size of the \p data buffer in bytes.
397 *
398 * \retval #PSA_SUCCESS
399 * \retval #PSA_ERROR_INVALID_HANDLE
400 * \retval #PSA_ERROR_OCCUPIED_SLOT
401 * There is already a key in the specified slot.
402 * \retval #PSA_ERROR_INVALID_ARGUMENT
403 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
404 * \retval #PSA_ERROR_HARDWARE_FAILURE
405 * \retval #PSA_ERROR_TAMPERING_DETECTED
406 * \retval #PSA_ERROR_BAD_STATE
407 * The library has not been previously initialized by psa_crypto_init().
408 * It is implementation-dependent whether a failure to initialize
409 * results in this error code.
410 */
411psa_status_t psa_set_key_domain_parameters(psa_key_handle_t handle,
412 const uint8_t *data,
413 size_t data_length);
414
415/**
416 * \brief Get domain parameters for a key.
417 *
418 * Get the domain parameters for a key with this function, if any. The format
419 * of the domain parameters written to \p data is specified in the
420 * documentation for psa_set_key_domain_parameters().
421 *
422 * \param handle Handle to the key to get domain parameters from.
423 * \param[out] data On success, the key domain parameters.
424 * \param data_size Size of the \p data buffer in bytes.
425 * \param[out] data_length On success, the number of bytes
426 * that make up the key domain parameters data.
427 *
428 * \retval #PSA_SUCCESS
429 * \retval #PSA_ERROR_INVALID_HANDLE
430 * \retval #PSA_ERROR_EMPTY_SLOT
431 * There is no key in the specified slot.
432 * \retval #PSA_ERROR_INVALID_ARGUMENT
433 * \retval #PSA_ERROR_NOT_SUPPORTED
434 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
435 * \retval #PSA_ERROR_HARDWARE_FAILURE
436 * \retval #PSA_ERROR_TAMPERING_DETECTED
437 * \retval #PSA_ERROR_BAD_STATE
438 * The library has not been previously initialized by psa_crypto_init().
439 * It is implementation-dependent whether a failure to initialize
440 * results in this error code.
441 */
442psa_status_t psa_get_key_domain_parameters(psa_key_handle_t handle,
443 uint8_t *data,
444 size_t data_size,
445 size_t *data_length);
446
447/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100448 * \brief Export a key in binary format.
449 *
450 * The output of this function can be passed to psa_import_key() to
451 * create an equivalent object.
452 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100453 * If the implementation of psa_import_key() supports other formats
454 * beyond the format specified here, the output from psa_export_key()
455 * must use the representation specified here, not the original
456 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100457 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100458 * For standard key types, the output format is as follows:
459 *
460 * - For symmetric keys (including MAC keys), the format is the
461 * raw bytes of the key.
462 * - For DES, the key data consists of 8 bytes. The parity bits must be
463 * correct.
464 * - For Triple-DES, the format is the concatenation of the
465 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100466 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200467 * is the non-encrypted DER encoding of the representation defined by
468 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
469 * ```
470 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200471 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200472 * modulus INTEGER, -- n
473 * publicExponent INTEGER, -- e
474 * privateExponent INTEGER, -- d
475 * prime1 INTEGER, -- p
476 * prime2 INTEGER, -- q
477 * exponent1 INTEGER, -- d mod (p-1)
478 * exponent2 INTEGER, -- d mod (q-1)
479 * coefficient INTEGER, -- (inverse of q) mod p
480 * }
481 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000482 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
483 * representation of the private key `x` as a big-endian byte string. The
484 * length of the byte string is the private key size in bytes (leading zeroes
485 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200486 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100487 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100488 * a representation of the private value as a `ceiling(m/8)`-byte string
489 * where `m` is the bit size associated with the curve, i.e. the bit size
490 * of the order of the curve's coordinate field. This byte string is
491 * in little-endian order for Montgomery curves (curve types
492 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
493 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
494 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100495 * This is the content of the `privateKey` field of the `ECPrivateKey`
496 * format defined by RFC 5915.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200497 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
498 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100499 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100500 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200501 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200502 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200503 * \param[out] data_length On success, the number of bytes
504 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100505 *
Gilles Peskine28538492018-07-11 17:34:00 +0200506 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100507 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200508 * \retval #PSA_ERROR_EMPTY_SLOT
509 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100510 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200511 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
512 * The size of the \p data buffer is too small. You can determine a
513 * sufficient buffer size by calling
514 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
515 * where \c type is the key type
516 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200517 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
518 * \retval #PSA_ERROR_HARDWARE_FAILURE
519 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300520 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300521 * The library has not been previously initialized by psa_crypto_init().
522 * It is implementation-dependent whether a failure to initialize
523 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100524 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100525psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100526 uint8_t *data,
527 size_t data_size,
528 size_t *data_length);
529
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100530/**
531 * \brief Export a public key or the public part of a key pair in binary format.
532 *
533 * The output of this function can be passed to psa_import_key() to
534 * create an object that is equivalent to the public key.
535 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000536 * This specification supports a single format for each key type.
537 * Implementations may support other formats as long as the standard
538 * format is supported. Implementations that support other formats
539 * should ensure that the formats are clearly unambiguous so as to
540 * minimize the risk that an invalid input is accidentally interpreted
541 * according to a different format.
542 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000543 * For standard key types, the output format is as follows:
544 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
545 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
546 * ```
547 * RSAPublicKey ::= SEQUENCE {
548 * modulus INTEGER, -- n
549 * publicExponent INTEGER } -- e
550 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000551 * - For elliptic curve public keys (key types for which
552 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
553 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
554 * Let `m` be the bit size associated with the curve, i.e. the bit size of
555 * `q` for a curve over `F_q`. The representation consists of:
556 * - The byte 0x04;
557 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
558 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000559 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
560 * representation of the public key `y = g^x mod p` as a big-endian byte
561 * string. The length of the byte string is the length of the base prime `p`
562 * in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100563 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100564 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200565 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200566 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200567 * \param[out] data_length On success, the number of bytes
568 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100569 *
Gilles Peskine28538492018-07-11 17:34:00 +0200570 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100571 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200572 * \retval #PSA_ERROR_EMPTY_SLOT
573 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200574 * The key is neither a public key nor a key pair.
575 * \retval #PSA_ERROR_NOT_SUPPORTED
576 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
577 * The size of the \p data buffer is too small. You can determine a
578 * sufficient buffer size by calling
579 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
580 * where \c type is the key type
581 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200582 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
583 * \retval #PSA_ERROR_HARDWARE_FAILURE
584 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300585 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300586 * The library has not been previously initialized by psa_crypto_init().
587 * It is implementation-dependent whether a failure to initialize
588 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100589 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100590psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100591 uint8_t *data,
592 size_t data_size,
593 size_t *data_length);
594
595/**@}*/
596
597/** \defgroup policy Key policies
598 * @{
599 */
600
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100601/** The type of the key policy data structure.
602 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000603 * Before calling any function on a key policy, the application must initialize
604 * it by any of the following means:
605 * - Set the structure to all-bits-zero, for example:
606 * \code
607 * psa_key_policy_t policy;
608 * memset(&policy, 0, sizeof(policy));
609 * \endcode
610 * - Initialize the structure to logical zero values, for example:
611 * \code
612 * psa_key_policy_t policy = {0};
613 * \endcode
614 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
615 * for example:
616 * \code
617 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
618 * \endcode
619 * - Assign the result of the function psa_key_policy_init()
620 * to the structure, for example:
621 * \code
622 * psa_key_policy_t policy;
623 * policy = psa_key_policy_init();
624 * \endcode
625 *
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100626 * This is an implementation-defined \c struct. Applications should not
627 * make any assumptions about the content of this structure except
628 * as directed by the documentation of a specific implementation. */
629typedef struct psa_key_policy_s psa_key_policy_t;
630
Jaeden Amero70261c52019-01-04 11:47:20 +0000631/** \def PSA_KEY_POLICY_INIT
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200632 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000633 * This macro returns a suitable initializer for a key policy object of type
634 * #psa_key_policy_t.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200635 */
Jaeden Amero70261c52019-01-04 11:47:20 +0000636#ifdef __DOXYGEN_ONLY__
637/* This is an example definition for documentation purposes.
638 * Implementations should define a suitable value in `crypto_struct.h`.
639 */
640#define PSA_KEY_POLICY_INIT {0}
641#endif
642
643/** Return an initial value for a key policy that forbids all usage of the key.
644 */
645static psa_key_policy_t psa_key_policy_init(void);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100646
Gilles Peskine7e198532018-03-08 07:50:30 +0100647/** \brief Set the standard fields of a policy structure.
648 *
649 * Note that this function does not make any consistency check of the
650 * parameters. The values are only checked when applying the policy to
651 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200652 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000653 * \param[in,out] policy The key policy to modify. It must have been
654 * initialized as per the documentation for
655 * #psa_key_policy_t.
656 * \param usage The permitted uses for the key.
657 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100658 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100659void psa_key_policy_set_usage(psa_key_policy_t *policy,
660 psa_key_usage_t usage,
661 psa_algorithm_t alg);
662
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200663/** \brief Retrieve the usage field of a policy structure.
664 *
665 * \param[in] policy The policy object to query.
666 *
667 * \return The permitted uses for a key with this policy.
668 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200669psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100670
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200671/** \brief Retrieve the algorithm field of a policy structure.
672 *
673 * \param[in] policy The policy object to query.
674 *
675 * \return The permitted algorithm for a key with this policy.
676 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200677psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100678
679/** \brief Set the usage policy on a key slot.
680 *
681 * This function must be called on an empty key slot, before importing,
682 * generating or creating a key in the slot. Changing the policy of an
683 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100684 *
685 * Implementations may set restrictions on supported key policies
686 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200687 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100688 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200689 * \param[in] policy The policy object to query.
690 *
691 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100692 * Success.
693 * If the key is persistent, it is implementation-defined whether
694 * the policy has been saved to persistent storage. Implementations
695 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100696 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200697 * \retval #PSA_ERROR_OCCUPIED_SLOT
698 * \retval #PSA_ERROR_NOT_SUPPORTED
699 * \retval #PSA_ERROR_INVALID_ARGUMENT
700 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
701 * \retval #PSA_ERROR_HARDWARE_FAILURE
702 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300703 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300704 * The library has not been previously initialized by psa_crypto_init().
705 * It is implementation-dependent whether a failure to initialize
706 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100707 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100708psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100709 const psa_key_policy_t *policy);
710
Gilles Peskine7e198532018-03-08 07:50:30 +0100711/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200712 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100713 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200714 * \param[out] policy On success, the key's policy.
715 *
716 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100717 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200718 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
719 * \retval #PSA_ERROR_HARDWARE_FAILURE
720 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300721 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300722 * The library has not been previously initialized by psa_crypto_init().
723 * It is implementation-dependent whether a failure to initialize
724 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100725 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100726psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100727 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100728
729/**@}*/
730
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100731/** \defgroup hash Message digests
732 * @{
733 */
734
Gilles Peskine308b91d2018-02-08 09:47:44 +0100735/** The type of the state data structure for multipart hash operations.
736 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000737 * Before calling any function on a hash operation object, the application must
738 * initialize it by any of the following means:
739 * - Set the structure to all-bits-zero, for example:
740 * \code
741 * psa_hash_operation_t operation;
742 * memset(&operation, 0, sizeof(operation));
743 * \endcode
744 * - Initialize the structure to logical zero values, for example:
745 * \code
746 * psa_hash_operation_t operation = {0};
747 * \endcode
748 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
749 * for example:
750 * \code
751 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
752 * \endcode
753 * - Assign the result of the function psa_hash_operation_init()
754 * to the structure, for example:
755 * \code
756 * psa_hash_operation_t operation;
757 * operation = psa_hash_operation_init();
758 * \endcode
759 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100760 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100761 * make any assumptions about the content of this structure except
762 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100763typedef struct psa_hash_operation_s psa_hash_operation_t;
764
Jaeden Amero6a25b412019-01-04 11:47:44 +0000765/** \def PSA_HASH_OPERATION_INIT
766 *
767 * This macro returns a suitable initializer for a hash operation object
768 * of type #psa_hash_operation_t.
769 */
770#ifdef __DOXYGEN_ONLY__
771/* This is an example definition for documentation purposes.
772 * Implementations should define a suitable value in `crypto_struct.h`.
773 */
774#define PSA_HASH_OPERATION_INIT {0}
775#endif
776
777/** Return an initial value for a hash operation object.
778 */
779static psa_hash_operation_t psa_hash_operation_init(void);
780
Gilles Peskine308b91d2018-02-08 09:47:44 +0100781/** Start a multipart hash operation.
782 *
783 * The sequence of operations to calculate a hash (message digest)
784 * is as follows:
785 * -# Allocate an operation object which will be passed to all the functions
786 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000787 * -# Initialize the operation object with one of the methods described in the
788 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200789 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100790 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100791 * of the message each time. The hash that is calculated is the hash
792 * of the concatenation of these messages in order.
793 * -# To calculate the hash, call psa_hash_finish().
794 * To compare the hash with an expected value, call psa_hash_verify().
795 *
796 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000797 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100798 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200799 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100800 * eventually terminate the operation. The following events terminate an
801 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100802 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100803 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100804 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000805 * \param[in,out] operation The operation object to set up. It must have
806 * been initialized as per the documentation for
807 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200808 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
809 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100810 *
Gilles Peskine28538492018-07-11 17:34:00 +0200811 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100812 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200813 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200814 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200815 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
816 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
817 * \retval #PSA_ERROR_HARDWARE_FAILURE
818 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100819 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200820psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100821 psa_algorithm_t alg);
822
Gilles Peskine308b91d2018-02-08 09:47:44 +0100823/** Add a message fragment to a multipart hash operation.
824 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200825 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100826 *
827 * If this function returns an error status, the operation becomes inactive.
828 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200829 * \param[in,out] operation Active hash operation.
830 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200831 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100832 *
Gilles Peskine28538492018-07-11 17:34:00 +0200833 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100834 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200835 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100836 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200837 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
838 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
839 * \retval #PSA_ERROR_HARDWARE_FAILURE
840 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100841 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100842psa_status_t psa_hash_update(psa_hash_operation_t *operation,
843 const uint8_t *input,
844 size_t input_length);
845
Gilles Peskine308b91d2018-02-08 09:47:44 +0100846/** Finish the calculation of the hash of a message.
847 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200848 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100849 * This function calculates the hash of the message formed by concatenating
850 * the inputs passed to preceding calls to psa_hash_update().
851 *
852 * When this function returns, the operation becomes inactive.
853 *
854 * \warning Applications should not call this function if they expect
855 * a specific value for the hash. Call psa_hash_verify() instead.
856 * Beware that comparing integrity or authenticity data such as
857 * hash values with a function such as \c memcmp is risky
858 * because the time taken by the comparison may leak information
859 * about the hashed data which could allow an attacker to guess
860 * a valid hash and thereby bypass security controls.
861 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200862 * \param[in,out] operation Active hash operation.
863 * \param[out] hash Buffer where the hash is to be written.
864 * \param hash_size Size of the \p hash buffer in bytes.
865 * \param[out] hash_length On success, the number of bytes
866 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200867 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200868 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100869 *
Gilles Peskine28538492018-07-11 17:34:00 +0200870 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100871 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200872 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100873 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200874 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200875 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200876 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100877 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200878 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
879 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
880 * \retval #PSA_ERROR_HARDWARE_FAILURE
881 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100882 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100883psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
884 uint8_t *hash,
885 size_t hash_size,
886 size_t *hash_length);
887
Gilles Peskine308b91d2018-02-08 09:47:44 +0100888/** Finish the calculation of the hash of a message and compare it with
889 * an expected value.
890 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200891 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100892 * This function calculates the hash of the message formed by concatenating
893 * the inputs passed to preceding calls to psa_hash_update(). It then
894 * compares the calculated hash with the expected hash passed as a
895 * parameter to this function.
896 *
897 * When this function returns, the operation becomes inactive.
898 *
Gilles Peskine19067982018-03-20 17:54:53 +0100899 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100900 * comparison between the actual hash and the expected hash is performed
901 * in constant time.
902 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200903 * \param[in,out] operation Active hash operation.
904 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200905 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100906 *
Gilles Peskine28538492018-07-11 17:34:00 +0200907 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100908 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200909 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100910 * The hash of the message was calculated successfully, but it
911 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200912 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100913 * The operation state is not valid (not started, or already completed).
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 Peskine9ef733f2018-02-07 21:05:37 +0100919psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
920 const uint8_t *hash,
921 size_t hash_length);
922
Gilles Peskine308b91d2018-02-08 09:47:44 +0100923/** Abort a hash operation.
924 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100925 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200926 * \p operation structure itself. Once aborted, the operation object
927 * can be reused for another operation by calling
928 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100929 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200930 * You may call this function any time after the operation object has
931 * been initialized by any of the following methods:
932 * - A call to psa_hash_setup(), whether it succeeds or not.
933 * - Initializing the \c struct to all-bits-zero.
934 * - Initializing the \c struct to logical zeros, e.g.
935 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100936 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200937 * In particular, calling psa_hash_abort() after the operation has been
938 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
939 * psa_hash_verify() is safe and has no effect.
940 *
941 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100942 *
Gilles Peskine28538492018-07-11 17:34:00 +0200943 * \retval #PSA_SUCCESS
944 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200945 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +0200946 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
947 * \retval #PSA_ERROR_HARDWARE_FAILURE
948 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100949 */
950psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100951
952/**@}*/
953
Gilles Peskine8c9def32018-02-08 10:02:12 +0100954/** \defgroup MAC Message authentication codes
955 * @{
956 */
957
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100958/** The type of the state data structure for multipart MAC operations.
959 *
Jaeden Amero769ce272019-01-04 11:48:03 +0000960 * Before calling any function on a MAC operation object, the application must
961 * initialize it by any of the following means:
962 * - Set the structure to all-bits-zero, for example:
963 * \code
964 * psa_mac_operation_t operation;
965 * memset(&operation, 0, sizeof(operation));
966 * \endcode
967 * - Initialize the structure to logical zero values, for example:
968 * \code
969 * psa_mac_operation_t operation = {0};
970 * \endcode
971 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
972 * for example:
973 * \code
974 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
975 * \endcode
976 * - Assign the result of the function psa_mac_operation_init()
977 * to the structure, for example:
978 * \code
979 * psa_mac_operation_t operation;
980 * operation = psa_mac_operation_init();
981 * \endcode
982 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100983 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100984 * make any assumptions about the content of this structure except
985 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100986typedef struct psa_mac_operation_s psa_mac_operation_t;
987
Jaeden Amero769ce272019-01-04 11:48:03 +0000988/** \def PSA_MAC_OPERATION_INIT
989 *
990 * This macro returns a suitable initializer for a MAC operation object of type
991 * #psa_mac_operation_t.
992 */
993#ifdef __DOXYGEN_ONLY__
994/* This is an example definition for documentation purposes.
995 * Implementations should define a suitable value in `crypto_struct.h`.
996 */
997#define PSA_MAC_OPERATION_INIT {0}
998#endif
999
1000/** Return an initial value for a MAC operation object.
1001 */
1002static psa_mac_operation_t psa_mac_operation_init(void);
1003
Gilles Peskine89167cb2018-07-08 20:12:23 +02001004/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001005 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001006 * This function sets up the calculation of the MAC
1007 * (message authentication code) of a byte string.
1008 * To verify the MAC of a message against an
1009 * expected value, use psa_mac_verify_setup() instead.
1010 *
1011 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001012 * -# Allocate an operation object which will be passed to all the functions
1013 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001014 * -# Initialize the operation object with one of the methods described in the
1015 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001016 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001017 * The key remains associated with the operation even if the content
1018 * of the key slot changes.
1019 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1020 * of the message each time. The MAC that is calculated is the MAC
1021 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001022 * -# At the end of the message, call psa_mac_sign_finish() to finish
1023 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001024 *
1025 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001026 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001027 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001028 * After a successful call to psa_mac_sign_setup(), the application must
1029 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001030 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001031 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001032 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001033 * \param[in,out] operation The operation object to set up. It must have
1034 * been initialized as per the documentation for
1035 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001036 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001037 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1038 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001039 *
Gilles Peskine28538492018-07-11 17:34:00 +02001040 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001041 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001042 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001043 * \retval #PSA_ERROR_EMPTY_SLOT
1044 * \retval #PSA_ERROR_NOT_PERMITTED
1045 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001046 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001047 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001048 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001049 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1050 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1051 * \retval #PSA_ERROR_HARDWARE_FAILURE
1052 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001053 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001054 * The library has not been previously initialized by psa_crypto_init().
1055 * It is implementation-dependent whether a failure to initialize
1056 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001057 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001058psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001059 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001060 psa_algorithm_t alg);
1061
1062/** Start a multipart MAC verification operation.
1063 *
1064 * This function sets up the verification of the MAC
1065 * (message authentication code) of a byte string against an expected value.
1066 *
1067 * The sequence of operations to verify a MAC is as follows:
1068 * -# Allocate an operation object which will be passed to all the functions
1069 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001070 * -# Initialize the operation object with one of the methods described in the
1071 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001072 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1073 * The key remains associated with the operation even if the content
1074 * of the key slot changes.
1075 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1076 * of the message each time. The MAC that is calculated is the MAC
1077 * of the concatenation of these messages in order.
1078 * -# At the end of the message, call psa_mac_verify_finish() to finish
1079 * calculating the actual MAC of the message and verify it against
1080 * the expected value.
1081 *
1082 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001083 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001084 *
1085 * After a successful call to psa_mac_verify_setup(), the application must
1086 * eventually terminate the operation through one of the following methods:
1087 * - A failed call to psa_mac_update().
1088 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1089 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001090 * \param[in,out] operation The operation object to set up. It must have
1091 * been initialized as per the documentation for
1092 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001093 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001094 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1095 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001096 *
Gilles Peskine28538492018-07-11 17:34:00 +02001097 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001098 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001099 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001100 * \retval #PSA_ERROR_EMPTY_SLOT
1101 * \retval #PSA_ERROR_NOT_PERMITTED
1102 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001103 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001104 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001105 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001106 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1107 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1108 * \retval #PSA_ERROR_HARDWARE_FAILURE
1109 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001110 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001111 * 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.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001114 */
1115psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001116 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001117 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001118
Gilles Peskinedcd14942018-07-12 00:30:52 +02001119/** Add a message fragment to a multipart MAC operation.
1120 *
1121 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1122 * before calling this function.
1123 *
1124 * If this function returns an error status, the operation becomes inactive.
1125 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001126 * \param[in,out] operation Active MAC operation.
1127 * \param[in] input Buffer containing the message fragment to add to
1128 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001129 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001130 *
1131 * \retval #PSA_SUCCESS
1132 * Success.
1133 * \retval #PSA_ERROR_BAD_STATE
1134 * The operation state is not valid (not started, or already completed).
1135 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1136 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1137 * \retval #PSA_ERROR_HARDWARE_FAILURE
1138 * \retval #PSA_ERROR_TAMPERING_DETECTED
1139 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001140psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1141 const uint8_t *input,
1142 size_t input_length);
1143
Gilles Peskinedcd14942018-07-12 00:30:52 +02001144/** Finish the calculation of the MAC of a message.
1145 *
1146 * The application must call psa_mac_sign_setup() before calling this function.
1147 * This function calculates the MAC of the message formed by concatenating
1148 * the inputs passed to preceding calls to psa_mac_update().
1149 *
1150 * When this function returns, the operation becomes inactive.
1151 *
1152 * \warning Applications should not call this function if they expect
1153 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1154 * Beware that comparing integrity or authenticity data such as
1155 * MAC values with a function such as \c memcmp is risky
1156 * because the time taken by the comparison may leak information
1157 * about the MAC value which could allow an attacker to guess
1158 * a valid MAC and thereby bypass security controls.
1159 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001160 * \param[in,out] operation Active MAC operation.
1161 * \param[out] mac Buffer where the MAC value is to be written.
1162 * \param mac_size Size of the \p mac buffer in bytes.
1163 * \param[out] mac_length On success, the number of bytes
1164 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001165 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001166 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001167 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001168 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001169 *
1170 * \retval #PSA_SUCCESS
1171 * Success.
1172 * \retval #PSA_ERROR_BAD_STATE
1173 * The operation state is not valid (not started, or already completed).
1174 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001175 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001176 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1177 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1178 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1179 * \retval #PSA_ERROR_HARDWARE_FAILURE
1180 * \retval #PSA_ERROR_TAMPERING_DETECTED
1181 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001182psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1183 uint8_t *mac,
1184 size_t mac_size,
1185 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001186
Gilles Peskinedcd14942018-07-12 00:30:52 +02001187/** Finish the calculation of the MAC of a message and compare it with
1188 * an expected value.
1189 *
1190 * The application must call psa_mac_verify_setup() before calling this function.
1191 * This function calculates the MAC of the message formed by concatenating
1192 * the inputs passed to preceding calls to psa_mac_update(). It then
1193 * compares the calculated MAC with the expected MAC passed as a
1194 * parameter to this function.
1195 *
1196 * When this function returns, the operation becomes inactive.
1197 *
1198 * \note Implementations shall make the best effort to ensure that the
1199 * comparison between the actual MAC and the expected MAC is performed
1200 * in constant time.
1201 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001202 * \param[in,out] operation Active MAC operation.
1203 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001204 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001205 *
1206 * \retval #PSA_SUCCESS
1207 * The expected MAC is identical to the actual MAC of the message.
1208 * \retval #PSA_ERROR_INVALID_SIGNATURE
1209 * The MAC of the message was calculated successfully, but it
1210 * differs from the expected MAC.
1211 * \retval #PSA_ERROR_BAD_STATE
1212 * The operation state is not valid (not started, or already completed).
1213 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1214 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1215 * \retval #PSA_ERROR_HARDWARE_FAILURE
1216 * \retval #PSA_ERROR_TAMPERING_DETECTED
1217 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001218psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1219 const uint8_t *mac,
1220 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001221
Gilles Peskinedcd14942018-07-12 00:30:52 +02001222/** Abort a MAC operation.
1223 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001224 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001225 * \p operation structure itself. Once aborted, the operation object
1226 * can be reused for another operation by calling
1227 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001228 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001229 * You may call this function any time after the operation object has
1230 * been initialized by any of the following methods:
1231 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1232 * it succeeds or not.
1233 * - Initializing the \c struct to all-bits-zero.
1234 * - Initializing the \c struct to logical zeros, e.g.
1235 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001236 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001237 * In particular, calling psa_mac_abort() after the operation has been
1238 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1239 * psa_mac_verify_finish() is safe and has no effect.
1240 *
1241 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001242 *
1243 * \retval #PSA_SUCCESS
1244 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001245 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001246 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1247 * \retval #PSA_ERROR_HARDWARE_FAILURE
1248 * \retval #PSA_ERROR_TAMPERING_DETECTED
1249 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001250psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1251
1252/**@}*/
1253
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001254/** \defgroup cipher Symmetric ciphers
1255 * @{
1256 */
1257
1258/** The type of the state data structure for multipart cipher operations.
1259 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001260 * Before calling any function on a cipher operation object, the application
1261 * must initialize it by any of the following means:
1262 * - Set the structure to all-bits-zero, for example:
1263 * \code
1264 * psa_cipher_operation_t operation;
1265 * memset(&operation, 0, sizeof(operation));
1266 * \endcode
1267 * - Initialize the structure to logical zero values, for example:
1268 * \code
1269 * psa_cipher_operation_t operation = {0};
1270 * \endcode
1271 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1272 * for example:
1273 * \code
1274 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1275 * \endcode
1276 * - Assign the result of the function psa_cipher_operation_init()
1277 * to the structure, for example:
1278 * \code
1279 * psa_cipher_operation_t operation;
1280 * operation = psa_cipher_operation_init();
1281 * \endcode
1282 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001283 * This is an implementation-defined \c struct. Applications should not
1284 * make any assumptions about the content of this structure except
1285 * as directed by the documentation of a specific implementation. */
1286typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1287
Jaeden Amero5bae2272019-01-04 11:48:27 +00001288/** \def PSA_CIPHER_OPERATION_INIT
1289 *
1290 * This macro returns a suitable initializer for a cipher operation object of
1291 * type #psa_cipher_operation_t.
1292 */
1293#ifdef __DOXYGEN_ONLY__
1294/* This is an example definition for documentation purposes.
1295 * Implementations should define a suitable value in `crypto_struct.h`.
1296 */
1297#define PSA_CIPHER_OPERATION_INIT {0}
1298#endif
1299
1300/** Return an initial value for a cipher operation object.
1301 */
1302static psa_cipher_operation_t psa_cipher_operation_init(void);
1303
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001304/** Set the key for a multipart symmetric encryption operation.
1305 *
1306 * The sequence of operations to encrypt a message with a symmetric cipher
1307 * is as follows:
1308 * -# Allocate an operation object which will be passed to all the functions
1309 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001310 * -# Initialize the operation object with one of the methods described in the
1311 * documentation for #psa_cipher_operation_t, e.g.
1312 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001313 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001314 * The key remains associated with the operation even if the content
1315 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03001316 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001317 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001318 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001319 * requires a specific IV value.
1320 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1321 * of the message each time.
1322 * -# Call psa_cipher_finish().
1323 *
1324 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001325 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001326 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001327 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001328 * eventually terminate the operation. The following events terminate an
1329 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03001330 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001331 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001332 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001333 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001334 * \param[in,out] operation The operation object to set up. It must have
1335 * been initialized as per the documentation for
1336 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001337 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001338 * \param alg The cipher algorithm to compute
1339 * (\c PSA_ALG_XXX value such that
1340 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001341 *
Gilles Peskine28538492018-07-11 17:34:00 +02001342 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001343 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001344 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001345 * \retval #PSA_ERROR_EMPTY_SLOT
1346 * \retval #PSA_ERROR_NOT_PERMITTED
1347 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001348 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001349 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001350 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001351 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1352 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1353 * \retval #PSA_ERROR_HARDWARE_FAILURE
1354 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001355 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001356 * The library has not been previously initialized by psa_crypto_init().
1357 * It is implementation-dependent whether a failure to initialize
1358 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001359 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001360psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001361 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001362 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001363
1364/** Set the key for a multipart symmetric decryption operation.
1365 *
1366 * The sequence of operations to decrypt a message with a symmetric cipher
1367 * is as follows:
1368 * -# Allocate an operation object which will be passed to all the functions
1369 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001370 * -# Initialize the operation object with one of the methods described in the
1371 * documentation for #psa_cipher_operation_t, e.g.
1372 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001373 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001374 * The key remains associated with the operation even if the content
1375 * of the key slot changes.
1376 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1377 * decryption. If the IV is prepended to the ciphertext, you can call
1378 * psa_cipher_update() on a buffer containing the IV followed by the
1379 * beginning of the message.
1380 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1381 * of the message each time.
1382 * -# Call psa_cipher_finish().
1383 *
1384 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001385 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001386 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001387 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001388 * eventually terminate the operation. The following events terminate an
1389 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001390 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001391 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001392 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001393 * \param[in,out] operation The operation object to set up. It must have
1394 * been initialized as per the documentation for
1395 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001396 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001397 * \param alg The cipher algorithm to compute
1398 * (\c PSA_ALG_XXX value such that
1399 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001400 *
Gilles Peskine28538492018-07-11 17:34:00 +02001401 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001402 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001403 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001404 * \retval #PSA_ERROR_EMPTY_SLOT
1405 * \retval #PSA_ERROR_NOT_PERMITTED
1406 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001407 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001408 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001409 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001410 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1411 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1412 * \retval #PSA_ERROR_HARDWARE_FAILURE
1413 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001414 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001415 * The library has not been previously initialized by psa_crypto_init().
1416 * It is implementation-dependent whether a failure to initialize
1417 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001418 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001419psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001420 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001421 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001422
Gilles Peskinedcd14942018-07-12 00:30:52 +02001423/** Generate an IV for a symmetric encryption operation.
1424 *
1425 * This function generates a random IV (initialization vector), nonce
1426 * or initial counter value for the encryption operation as appropriate
1427 * for the chosen algorithm, key type and key size.
1428 *
1429 * The application must call psa_cipher_encrypt_setup() before
1430 * calling this function.
1431 *
1432 * If this function returns an error status, the operation becomes inactive.
1433 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001434 * \param[in,out] operation Active cipher operation.
1435 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001436 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001437 * \param[out] iv_length On success, the number of bytes of the
1438 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001439 *
1440 * \retval #PSA_SUCCESS
1441 * Success.
1442 * \retval #PSA_ERROR_BAD_STATE
1443 * The operation state is not valid (not started, or IV already set).
1444 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001445 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001446 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1447 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1448 * \retval #PSA_ERROR_HARDWARE_FAILURE
1449 * \retval #PSA_ERROR_TAMPERING_DETECTED
1450 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001451psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1452 unsigned char *iv,
1453 size_t iv_size,
1454 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001455
Gilles Peskinedcd14942018-07-12 00:30:52 +02001456/** Set the IV for a symmetric encryption or decryption operation.
1457 *
1458 * This function sets the random IV (initialization vector), nonce
1459 * or initial counter value for the encryption or decryption operation.
1460 *
1461 * The application must call psa_cipher_encrypt_setup() before
1462 * calling this function.
1463 *
1464 * If this function returns an error status, the operation becomes inactive.
1465 *
1466 * \note When encrypting, applications should use psa_cipher_generate_iv()
1467 * instead of this function, unless implementing a protocol that requires
1468 * a non-random IV.
1469 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001470 * \param[in,out] operation Active cipher operation.
1471 * \param[in] iv Buffer containing the IV to use.
1472 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001473 *
1474 * \retval #PSA_SUCCESS
1475 * Success.
1476 * \retval #PSA_ERROR_BAD_STATE
1477 * The operation state is not valid (not started, or IV already set).
1478 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001479 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001480 * or the chosen algorithm does not use an IV.
1481 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1482 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1483 * \retval #PSA_ERROR_HARDWARE_FAILURE
1484 * \retval #PSA_ERROR_TAMPERING_DETECTED
1485 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001486psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1487 const unsigned char *iv,
1488 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001489
Gilles Peskinedcd14942018-07-12 00:30:52 +02001490/** Encrypt or decrypt a message fragment in an active cipher operation.
1491 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001492 * Before calling this function, you must:
1493 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1494 * The choice of setup function determines whether this function
1495 * encrypts or decrypts its input.
1496 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1497 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001498 *
1499 * If this function returns an error status, the operation becomes inactive.
1500 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001501 * \param[in,out] operation Active cipher operation.
1502 * \param[in] input Buffer containing the message fragment to
1503 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001504 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001505 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001506 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001507 * \param[out] output_length On success, the number of bytes
1508 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001509 *
1510 * \retval #PSA_SUCCESS
1511 * Success.
1512 * \retval #PSA_ERROR_BAD_STATE
1513 * The operation state is not valid (not started, IV required but
1514 * not set, or already completed).
1515 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1516 * The size of the \p output buffer is too small.
1517 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1518 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1519 * \retval #PSA_ERROR_HARDWARE_FAILURE
1520 * \retval #PSA_ERROR_TAMPERING_DETECTED
1521 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001522psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1523 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001524 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001525 unsigned char *output,
1526 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001527 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001528
Gilles Peskinedcd14942018-07-12 00:30:52 +02001529/** Finish encrypting or decrypting a message in a cipher operation.
1530 *
1531 * The application must call psa_cipher_encrypt_setup() or
1532 * psa_cipher_decrypt_setup() before calling this function. The choice
1533 * of setup function determines whether this function encrypts or
1534 * decrypts its input.
1535 *
1536 * This function finishes the encryption or decryption of the message
1537 * formed by concatenating the inputs passed to preceding calls to
1538 * psa_cipher_update().
1539 *
1540 * When this function returns, the operation becomes inactive.
1541 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001542 * \param[in,out] operation Active cipher operation.
1543 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001544 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001545 * \param[out] output_length On success, the number of bytes
1546 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001547 *
1548 * \retval #PSA_SUCCESS
1549 * Success.
1550 * \retval #PSA_ERROR_BAD_STATE
1551 * The operation state is not valid (not started, IV required but
1552 * not set, or already completed).
1553 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1554 * The size of the \p output buffer is too small.
1555 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1556 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1557 * \retval #PSA_ERROR_HARDWARE_FAILURE
1558 * \retval #PSA_ERROR_TAMPERING_DETECTED
1559 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001560psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001561 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001562 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001563 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001564
Gilles Peskinedcd14942018-07-12 00:30:52 +02001565/** Abort a cipher operation.
1566 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001567 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001568 * \p operation structure itself. Once aborted, the operation object
1569 * can be reused for another operation by calling
1570 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001571 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001572 * You may call this function any time after the operation object has
1573 * been initialized by any of the following methods:
1574 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1575 * whether it succeeds or not.
1576 * - Initializing the \c struct to all-bits-zero.
1577 * - Initializing the \c struct to logical zeros, e.g.
1578 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001579 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001580 * In particular, calling psa_cipher_abort() after the operation has been
1581 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1582 * is safe and has no effect.
1583 *
1584 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001585 *
1586 * \retval #PSA_SUCCESS
1587 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001588 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001589 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1590 * \retval #PSA_ERROR_HARDWARE_FAILURE
1591 * \retval #PSA_ERROR_TAMPERING_DETECTED
1592 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001593psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1594
1595/**@}*/
1596
Gilles Peskine3b555712018-03-03 21:27:57 +01001597/** \defgroup aead Authenticated encryption with associated data (AEAD)
1598 * @{
1599 */
1600
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001601/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001602 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001603 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001604 * \param alg The AEAD algorithm to compute
1605 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001606 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001607 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001608 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001609 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001610 * but not encrypted.
1611 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001612 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001613 * encrypted.
1614 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001615 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001616 * encrypted data. The additional data is not
1617 * part of this output. For algorithms where the
1618 * encrypted data and the authentication tag
1619 * are defined as separate outputs, the
1620 * authentication tag is appended to the
1621 * encrypted data.
1622 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1623 * This must be at least
1624 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1625 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001626 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001627 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001628 *
Gilles Peskine28538492018-07-11 17:34:00 +02001629 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001630 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001631 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001632 * \retval #PSA_ERROR_EMPTY_SLOT
1633 * \retval #PSA_ERROR_NOT_PERMITTED
1634 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001635 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001636 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001637 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001638 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1639 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1640 * \retval #PSA_ERROR_HARDWARE_FAILURE
1641 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001642 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001643 * The library has not been previously initialized by psa_crypto_init().
1644 * It is implementation-dependent whether a failure to initialize
1645 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001646 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001647psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001648 psa_algorithm_t alg,
1649 const uint8_t *nonce,
1650 size_t nonce_length,
1651 const uint8_t *additional_data,
1652 size_t additional_data_length,
1653 const uint8_t *plaintext,
1654 size_t plaintext_length,
1655 uint8_t *ciphertext,
1656 size_t ciphertext_size,
1657 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001658
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001659/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001660 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001661 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001662 * \param alg The AEAD algorithm to compute
1663 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001664 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001665 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001666 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001667 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001668 * but not encrypted.
1669 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001670 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001671 * encrypted. For algorithms where the
1672 * encrypted data and the authentication tag
1673 * are defined as separate inputs, the buffer
1674 * must contain the encrypted data followed
1675 * by the authentication tag.
1676 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001677 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001678 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1679 * This must be at least
1680 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1681 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001682 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001683 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001684 *
Gilles Peskine28538492018-07-11 17:34:00 +02001685 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001686 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001687 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001688 * \retval #PSA_ERROR_EMPTY_SLOT
1689 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001690 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001691 * \retval #PSA_ERROR_NOT_PERMITTED
1692 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001693 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001694 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001695 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001696 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1697 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1698 * \retval #PSA_ERROR_HARDWARE_FAILURE
1699 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001700 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001701 * The library has not been previously initialized by psa_crypto_init().
1702 * It is implementation-dependent whether a failure to initialize
1703 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001704 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001705psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001706 psa_algorithm_t alg,
1707 const uint8_t *nonce,
1708 size_t nonce_length,
1709 const uint8_t *additional_data,
1710 size_t additional_data_length,
1711 const uint8_t *ciphertext,
1712 size_t ciphertext_length,
1713 uint8_t *plaintext,
1714 size_t plaintext_size,
1715 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001716
1717/**@}*/
1718
Gilles Peskine20035e32018-02-03 22:44:14 +01001719/** \defgroup asymmetric Asymmetric cryptography
1720 * @{
1721 */
1722
1723/**
1724 * \brief Sign a hash or short message with a private key.
1725 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001726 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001727 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001728 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1729 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1730 * to determine the hash algorithm to use.
1731 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001732 * \param handle Handle to the key to use for the operation.
1733 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001734 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001735 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001736 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001737 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001738 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001739 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001740 * \param[out] signature_length On success, the number of bytes
1741 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001742 *
Gilles Peskine28538492018-07-11 17:34:00 +02001743 * \retval #PSA_SUCCESS
1744 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001745 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01001746 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001747 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001748 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001749 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001750 * \retval #PSA_ERROR_NOT_SUPPORTED
1751 * \retval #PSA_ERROR_INVALID_ARGUMENT
1752 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1753 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1754 * \retval #PSA_ERROR_HARDWARE_FAILURE
1755 * \retval #PSA_ERROR_TAMPERING_DETECTED
1756 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001757 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001758 * The library has not been previously initialized by psa_crypto_init().
1759 * It is implementation-dependent whether a failure to initialize
1760 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001761 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001762psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001763 psa_algorithm_t alg,
1764 const uint8_t *hash,
1765 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01001766 uint8_t *signature,
1767 size_t signature_size,
1768 size_t *signature_length);
1769
1770/**
1771 * \brief Verify the signature a hash or short message using a public key.
1772 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001773 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001774 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001775 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1776 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1777 * to determine the hash algorithm to use.
1778 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001779 * \param handle Handle to the key to use for the operation.
1780 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001781 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001782 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001783 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02001784 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001785 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001786 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001787 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001788 *
Gilles Peskine28538492018-07-11 17:34:00 +02001789 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001790 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02001791 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001792 * The calculation was perfomed successfully, but the passed
1793 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02001794 * \retval #PSA_ERROR_NOT_SUPPORTED
1795 * \retval #PSA_ERROR_INVALID_ARGUMENT
1796 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1797 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1798 * \retval #PSA_ERROR_HARDWARE_FAILURE
1799 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001800 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001801 * The library has not been previously initialized by psa_crypto_init().
1802 * It is implementation-dependent whether a failure to initialize
1803 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001804 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001805psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001806 psa_algorithm_t alg,
1807 const uint8_t *hash,
1808 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001809 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001810 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01001811
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001812/**
1813 * \brief Encrypt a short message with a public key.
1814 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001815 * \param handle Handle to the key to use for the operation.
1816 * It must be a public key or an asymmetric
1817 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001818 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001819 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001820 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001821 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001822 * \param[in] salt A salt or label, if supported by the
1823 * encryption algorithm.
1824 * If the algorithm does not support a
1825 * salt, pass \c NULL.
1826 * If the algorithm supports an optional
1827 * salt and you do not want to pass a salt,
1828 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001829 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001830 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1831 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001832 * \param salt_length Size of the \p salt buffer in bytes.
1833 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001834 * \param[out] output Buffer where the encrypted message is to
1835 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001836 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001837 * \param[out] output_length On success, the number of bytes
1838 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001839 *
Gilles Peskine28538492018-07-11 17:34:00 +02001840 * \retval #PSA_SUCCESS
1841 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001842 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001843 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001844 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001845 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001846 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001847 * \retval #PSA_ERROR_NOT_SUPPORTED
1848 * \retval #PSA_ERROR_INVALID_ARGUMENT
1849 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1850 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1851 * \retval #PSA_ERROR_HARDWARE_FAILURE
1852 * \retval #PSA_ERROR_TAMPERING_DETECTED
1853 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001854 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001855 * The library has not been previously initialized by psa_crypto_init().
1856 * It is implementation-dependent whether a failure to initialize
1857 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001858 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001859psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001860 psa_algorithm_t alg,
1861 const uint8_t *input,
1862 size_t input_length,
1863 const uint8_t *salt,
1864 size_t salt_length,
1865 uint8_t *output,
1866 size_t output_size,
1867 size_t *output_length);
1868
1869/**
1870 * \brief Decrypt a short message with a private key.
1871 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001872 * \param handle Handle to the key to use for the operation.
1873 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001874 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001875 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001876 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001877 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001878 * \param[in] salt A salt or label, if supported by the
1879 * encryption algorithm.
1880 * If the algorithm does not support a
1881 * salt, pass \c NULL.
1882 * If the algorithm supports an optional
1883 * salt and you do not want to pass a salt,
1884 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001885 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001886 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1887 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001888 * \param salt_length Size of the \p salt buffer in bytes.
1889 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001890 * \param[out] output Buffer where the decrypted message is to
1891 * be written.
1892 * \param output_size Size of the \c output buffer in bytes.
1893 * \param[out] output_length On success, the number of bytes
1894 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001895 *
Gilles Peskine28538492018-07-11 17:34:00 +02001896 * \retval #PSA_SUCCESS
1897 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001898 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001899 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001900 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001901 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001902 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001903 * \retval #PSA_ERROR_NOT_SUPPORTED
1904 * \retval #PSA_ERROR_INVALID_ARGUMENT
1905 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1906 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1907 * \retval #PSA_ERROR_HARDWARE_FAILURE
1908 * \retval #PSA_ERROR_TAMPERING_DETECTED
1909 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1910 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03001911 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001912 * The library has not been previously initialized by psa_crypto_init().
1913 * It is implementation-dependent whether a failure to initialize
1914 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001915 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001916psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001917 psa_algorithm_t alg,
1918 const uint8_t *input,
1919 size_t input_length,
1920 const uint8_t *salt,
1921 size_t salt_length,
1922 uint8_t *output,
1923 size_t output_size,
1924 size_t *output_length);
1925
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001926/**@}*/
1927
Gilles Peskineedd76872018-07-20 17:42:05 +02001928/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02001929 * @{
1930 */
1931
1932/** The type of the state data structure for generators.
1933 *
1934 * Before calling any function on a generator, the application must
1935 * initialize it by any of the following means:
1936 * - Set the structure to all-bits-zero, for example:
1937 * \code
1938 * psa_crypto_generator_t generator;
1939 * memset(&generator, 0, sizeof(generator));
1940 * \endcode
1941 * - Initialize the structure to logical zero values, for example:
1942 * \code
1943 * psa_crypto_generator_t generator = {0};
1944 * \endcode
1945 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
1946 * for example:
1947 * \code
1948 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1949 * \endcode
1950 * - Assign the result of the function psa_crypto_generator_init()
1951 * to the structure, for example:
1952 * \code
1953 * psa_crypto_generator_t generator;
1954 * generator = psa_crypto_generator_init();
1955 * \endcode
1956 *
1957 * This is an implementation-defined \c struct. Applications should not
1958 * make any assumptions about the content of this structure except
1959 * as directed by the documentation of a specific implementation.
1960 */
1961typedef struct psa_crypto_generator_s psa_crypto_generator_t;
1962
1963/** \def PSA_CRYPTO_GENERATOR_INIT
1964 *
1965 * This macro returns a suitable initializer for a generator object
1966 * of type #psa_crypto_generator_t.
1967 */
1968#ifdef __DOXYGEN_ONLY__
1969/* This is an example definition for documentation purposes.
1970 * Implementations should define a suitable value in `crypto_struct.h`.
1971 */
1972#define PSA_CRYPTO_GENERATOR_INIT {0}
1973#endif
1974
1975/** Return an initial value for a generator object.
1976 */
1977static psa_crypto_generator_t psa_crypto_generator_init(void);
1978
1979/** Retrieve the current capacity of a generator.
1980 *
1981 * The capacity of a generator is the maximum number of bytes that it can
1982 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
1983 *
1984 * \param[in] generator The generator to query.
1985 * \param[out] capacity On success, the capacity of the generator.
1986 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001987 * \retval #PSA_SUCCESS
1988 * \retval #PSA_ERROR_BAD_STATE
1989 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02001990 */
1991psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
1992 size_t *capacity);
1993
1994/** Read some data from a generator.
1995 *
1996 * This function reads and returns a sequence of bytes from a generator.
1997 * The data that is read is discarded from the generator. The generator's
1998 * capacity is decreased by the number of bytes read.
1999 *
2000 * \param[in,out] generator The generator object to read from.
2001 * \param[out] output Buffer where the generator output will be
2002 * written.
2003 * \param output_length Number of bytes to output.
2004 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002005 * \retval #PSA_SUCCESS
2006 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002007 * There were fewer than \p output_length bytes
2008 * in the generator. Note that in this case, no
2009 * output is written to the output buffer.
2010 * The generator's capacity is set to 0, thus
2011 * subsequent calls to this function will not
2012 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002013 * \retval #PSA_ERROR_BAD_STATE
2014 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2015 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2016 * \retval #PSA_ERROR_HARDWARE_FAILURE
2017 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002018 */
2019psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2020 uint8_t *output,
2021 size_t output_length);
2022
2023/** Create a symmetric key from data read from a generator.
2024 *
2025 * This function reads a sequence of bytes from a generator and imports
2026 * these bytes as a key.
2027 * The data that is read is discarded from the generator. The generator's
2028 * capacity is decreased by the number of bytes read.
2029 *
2030 * This function is equivalent to calling #psa_generator_read and
2031 * passing the resulting output to #psa_import_key, but
2032 * if the implementation provides an isolation boundary then
2033 * the key material is not exposed outside the isolation boundary.
2034 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002035 * \param handle Handle to the slot where the key will be stored.
2036 * This must be a valid slot for a key of the chosen
2037 * type: it must have been obtained by calling
2038 * psa_allocate_key() or psa_create_key() with the
2039 * correct \p type and with a maximum size that is
2040 * compatible with \p bits.
2041 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002042 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2043 * This must be a symmetric key type.
2044 * \param bits Key size in bits.
2045 * \param[in,out] generator The generator object to read from.
2046 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002047 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002048 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002049 * If the key is persistent, the key material and the key's metadata
2050 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002051 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002052 * There were fewer than \p output_length bytes
2053 * in the generator. Note that in this case, no
2054 * output is written to the output buffer.
2055 * The generator's capacity is set to 0, thus
2056 * subsequent calls to this function will not
2057 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002058 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002059 * The key type or key size is not supported, either by the
2060 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002061 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002062 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002063 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002064 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002065 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2066 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2067 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2068 * \retval #PSA_ERROR_HARDWARE_FAILURE
2069 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002070 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002071 * The library has not been previously initialized by psa_crypto_init().
2072 * It is implementation-dependent whether a failure to initialize
2073 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002074 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002075psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002076 psa_key_type_t type,
2077 size_t bits,
2078 psa_crypto_generator_t *generator);
2079
2080/** Abort a generator.
2081 *
2082 * Once a generator has been aborted, its capacity is zero.
2083 * Aborting a generator frees all associated resources except for the
2084 * \c generator structure itself.
2085 *
2086 * This function may be called at any time as long as the generator
2087 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2088 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2089 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2090 * on a generator that has not been set up.
2091 *
2092 * Once aborted, the generator object may be called.
2093 *
2094 * \param[in,out] generator The generator to abort.
2095 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002096 * \retval #PSA_SUCCESS
2097 * \retval #PSA_ERROR_BAD_STATE
2098 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2099 * \retval #PSA_ERROR_HARDWARE_FAILURE
2100 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002101 */
2102psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2103
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002104/** Use the maximum possible capacity for a generator.
2105 *
2106 * Use this value as the capacity argument when setting up a generator
2107 * to indicate that the generator should have the maximum possible capacity.
2108 * The value of the maximum possible capacity depends on the generator
2109 * algorithm.
2110 */
2111#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2112
Gilles Peskineeab56e42018-07-12 17:12:33 +02002113/**@}*/
2114
Gilles Peskineea0fb492018-07-12 17:17:20 +02002115/** \defgroup derivation Key derivation
2116 * @{
2117 */
2118
2119/** Set up a key derivation operation.
2120 *
2121 * A key derivation algorithm takes three inputs: a secret input \p key and
2122 * two non-secret inputs \p label and p salt.
2123 * The result of this function is a byte generator which can
2124 * be used to produce keys and other cryptographic material.
2125 *
2126 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002127 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2128 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002129 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002130 * \param[in,out] generator The generator object to set up. It must have
2131 * been initialized as per the documentation for
2132 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002133 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002134 * \param alg The key derivation algorithm to compute
2135 * (\c PSA_ALG_XXX value such that
2136 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2137 * \param[in] salt Salt to use.
2138 * \param salt_length Size of the \p salt buffer in bytes.
2139 * \param[in] label Label to use.
2140 * \param label_length Size of the \p label buffer in bytes.
2141 * \param capacity The maximum number of bytes that the
2142 * generator will be able to provide.
2143 *
2144 * \retval #PSA_SUCCESS
2145 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002146 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02002147 * \retval #PSA_ERROR_EMPTY_SLOT
2148 * \retval #PSA_ERROR_NOT_PERMITTED
2149 * \retval #PSA_ERROR_INVALID_ARGUMENT
2150 * \c key is not compatible with \c alg,
2151 * or \p capacity is too large for the specified algorithm and key.
2152 * \retval #PSA_ERROR_NOT_SUPPORTED
2153 * \c alg is not supported or is not a key derivation algorithm.
2154 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2155 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2156 * \retval #PSA_ERROR_HARDWARE_FAILURE
2157 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002158 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002159 * The library has not been previously initialized by psa_crypto_init().
2160 * It is implementation-dependent whether a failure to initialize
2161 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002162 */
2163psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002164 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002165 psa_algorithm_t alg,
2166 const uint8_t *salt,
2167 size_t salt_length,
2168 const uint8_t *label,
2169 size_t label_length,
2170 size_t capacity);
2171
Gilles Peskine01d718c2018-09-18 12:01:02 +02002172/** Set up a key agreement operation.
2173 *
2174 * A key agreement algorithm takes two inputs: a private key \p private_key
2175 * a public key \p peer_key.
2176 * The result of this function is a byte generator which can
2177 * be used to produce keys and other cryptographic material.
2178 *
Gilles Peskine211a4362018-10-25 22:22:31 +02002179 * The resulting generator always has the maximum capacity permitted by
2180 * the algorithm.
2181 *
Jaeden Amero8afbff82019-01-14 16:56:20 +00002182 * \param[in,out] generator The generator object to set up. It must have been
2183 * initialized as per the documentation for
2184 * #psa_crypto_generator_t and not yet in use.
2185 * \param private_key Handle to the private key to use.
2186 * \param[in] peer_key Public key of the peer. The peer key must be in the
2187 * same format that psa_import_key() accepts for the
2188 * public key type corresponding to the type of
2189 * private_key. That is, this function performs the
2190 * equivalent of
2191 * `psa_import_key(internal_public_key_handle,
2192 * PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(private_key_type),
2193 * peer_key, peer_key_length)` where
2194 * `private_key_type` is the type of `private_key`.
2195 * For example, for EC keys, this means that peer_key
2196 * is interpreted as a point on the curve that the
2197 * private key is on. The standard formats for public
2198 * keys are documented in the documentation of
2199 * psa_export_public_key().
2200 * \param peer_key_length Size of \p peer_key in bytes.
2201 * \param alg The key agreement algorithm to compute
2202 * (\c PSA_ALG_XXX value such that
2203 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
Gilles Peskine01d718c2018-09-18 12:01:02 +02002204 *
2205 * \retval #PSA_SUCCESS
2206 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002207 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02002208 * \retval #PSA_ERROR_EMPTY_SLOT
2209 * \retval #PSA_ERROR_NOT_PERMITTED
2210 * \retval #PSA_ERROR_INVALID_ARGUMENT
2211 * \c private_key is not compatible with \c alg,
2212 * or \p peer_key is not valid for \c alg or not compatible with
2213 * \c private_key.
2214 * \retval #PSA_ERROR_NOT_SUPPORTED
2215 * \c alg is not supported or is not a key derivation algorithm.
2216 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2217 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2218 * \retval #PSA_ERROR_HARDWARE_FAILURE
2219 * \retval #PSA_ERROR_TAMPERING_DETECTED
2220 */
2221psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002222 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02002223 const uint8_t *peer_key,
2224 size_t peer_key_length,
2225 psa_algorithm_t alg);
2226
Gilles Peskineea0fb492018-07-12 17:17:20 +02002227/**@}*/
2228
Gilles Peskineedd76872018-07-20 17:42:05 +02002229/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002230 * @{
2231 */
2232
2233/**
2234 * \brief Generate random bytes.
2235 *
2236 * \warning This function **can** fail! Callers MUST check the return status
2237 * and MUST NOT use the content of the output buffer if the return
2238 * status is not #PSA_SUCCESS.
2239 *
2240 * \note To generate a key, use psa_generate_key() instead.
2241 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002242 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002243 * \param output_size Number of bytes to generate and output.
2244 *
Gilles Peskine28538492018-07-11 17:34:00 +02002245 * \retval #PSA_SUCCESS
2246 * \retval #PSA_ERROR_NOT_SUPPORTED
2247 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2248 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2249 * \retval #PSA_ERROR_HARDWARE_FAILURE
2250 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002251 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002252 * The library has not been previously initialized by psa_crypto_init().
2253 * It is implementation-dependent whether a failure to initialize
2254 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002255 */
2256psa_status_t psa_generate_random(uint8_t *output,
2257 size_t output_size);
2258
Gilles Peskine4c317f42018-07-12 01:24:09 +02002259/** Extra parameters for RSA key generation.
2260 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002261 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002262 * parameter to psa_generate_key().
2263 */
2264typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002265 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002266} psa_generate_key_extra_rsa;
2267
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002268/**
2269 * \brief Generate a key or key pair.
2270 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002271 * \param handle Handle to the slot where the key will be stored.
2272 * This must be a valid slot for a key of the chosen
2273 * type: it must have been obtained by calling
2274 * psa_allocate_key() or psa_create_key() with the
2275 * correct \p type and with a maximum size that is
2276 * compatible with \p bits.
2277 * It must not contain any key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002278 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2279 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002280 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002281 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002282 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002283 * default parameters. Implementation that support
2284 * the generation of vendor-specific key types
2285 * that allow extra parameters shall document
2286 * the format of these extra parameters and
2287 * the default values. For standard parameters,
2288 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002289 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002290 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2291 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002292 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002293 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2294 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002295 * - For an RSA key (\p type is
2296 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2297 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002298 * specifying the public exponent. The
2299 * default public exponent used when \p extra
2300 * is \c NULL is 65537.
Jaeden Amero1308fb52019-01-11 13:50:43 +00002301 * - For an DSA key (\p type is
2302 * #PSA_KEY_TYPE_DSA_KEYPAIR), \p extra is an
2303 * optional structure specifying the key domain
2304 * parameters. The key domain parameters can also be
2305 * provided by psa_set_key_domain_parameters(),
2306 * which documents the format of the structure.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002307 * \param extra_size Size of the buffer that \p extra
2308 * points to, in bytes. Note that if \p extra is
2309 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002310 *
Gilles Peskine28538492018-07-11 17:34:00 +02002311 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002312 * Success.
2313 * If the key is persistent, the key material and the key's metadata
2314 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002315 * \retval #PSA_ERROR_INVALID_HANDLE
2316 * \retval #PSA_ERROR_OCCUPIED_SLOT
2317 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02002318 * \retval #PSA_ERROR_NOT_SUPPORTED
2319 * \retval #PSA_ERROR_INVALID_ARGUMENT
2320 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2321 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2322 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2323 * \retval #PSA_ERROR_HARDWARE_FAILURE
2324 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002325 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002326 * The library has not been previously initialized by psa_crypto_init().
2327 * It is implementation-dependent whether a failure to initialize
2328 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002329 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002330psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002331 psa_key_type_t type,
2332 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002333 const void *extra,
2334 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002335
2336/**@}*/
2337
Gilles Peskinee59236f2018-01-27 23:32:46 +01002338#ifdef __cplusplus
2339}
2340#endif
2341
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002342/* The file "crypto_sizes.h" contains definitions for size calculation
2343 * macros whose definitions are implementation-specific. */
2344#include "crypto_sizes.h"
2345
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346/* The file "crypto_struct.h" contains definitions for
2347 * implementation-specific structs that are declared above. */
2348#include "crypto_struct.h"
2349
2350/* The file "crypto_extra.h" contains vendor-specific definitions. This
2351 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002352#include "crypto_extra.h"
2353
2354#endif /* PSA_CRYPTO_H */