blob: cbb6cfb0cf8e51bd91e909f820de459cf25e21d7 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
89 * \retval #PSA_ERROR_TAMPERING_DETECTED
90 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
96/** \defgroup key_management Key management
97 * @{
98 */
99
Gilles Peskineae32aac2018-11-30 14:39:32 +0100100/** \brief Retrieve the lifetime of an open key.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100101 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100102 * \param handle Handle to query.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100103 * \param[out] lifetime On success, the lifetime value.
104 *
105 * \retval #PSA_SUCCESS
106 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100107 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100108 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
109 * \retval #PSA_ERROR_HARDWARE_FAILURE
110 * \retval #PSA_ERROR_TAMPERING_DETECTED
111 * \retval #PSA_ERROR_BAD_STATE
112 * The library has not been previously initialized by psa_crypto_init().
113 * It is implementation-dependent whether a failure to initialize
114 * results in this error code.
115 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100116psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100117 psa_key_lifetime_t *lifetime);
118
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100119
Gilles Peskinef535eb22018-11-30 14:08:36 +0100120/** Allocate a key slot for a transient key, i.e. a key which is only stored
121 * in volatile memory.
122 *
123 * The allocated key slot and its handle remain valid until the
124 * application calls psa_close_key() or psa_destroy_key() or until the
125 * application terminates.
126 *
127 * This function takes a key type and maximum size as arguments so that
128 * the implementation can reserve a corresponding amount of memory.
129 * Implementations are not required to enforce this limit: if the application
130 * later tries to create a larger key or a key of a different type, it
131 * is implementation-defined whether this may succeed.
132 *
133 * \param type The type of key that the slot will contain.
134 * \param max_bits The maximum key size that the slot will contain.
135 * \param[out] handle On success, a handle to a volatile key slot.
136 *
137 * \retval #PSA_SUCCESS
138 * Success. The application can now use the value of `*handle`
139 * to access the newly allocated key slot.
140 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
141 * There was not enough memory, or the maximum number of key slots
142 * has been reached.
143 * \retval #PSA_ERROR_INVALID_ARGUMENT
144 * This implementation does not support this key type.
145 */
146
147psa_status_t psa_allocate_key(psa_key_type_t type,
148 size_t max_bits,
149 psa_key_handle_t *handle);
150
151/** Open a handle to an existing persistent key.
152 *
153 * Open a handle to a key which was previously created with psa_create_key().
154 *
155 * \param lifetime The lifetime of the key. This designates a storage
156 * area where the key material is stored. This must not
157 * be #PSA_KEY_LIFETIME_VOLATILE.
158 * \param id The persistent identifier of the key.
159 * \param[out] handle On success, a handle to a key slot which contains
160 * the data and metadata loaded from the specified
161 * persistent location.
162 *
163 * \retval #PSA_SUCCESS
164 * Success. The application can now use the value of `*handle`
165 * to access the newly allocated key slot.
166 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
167 * \retval #PSA_ERROR_EMPTY_SLOT
168 * \retval #PSA_ERROR_INVALID_ARGUMENT
169 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
170 * \retval #PSA_ERROR_INVALID_ARGUMENT
171 * \p id is invalid for the specified lifetime.
172 * \retval #PSA_ERROR_NOT_SUPPORTED
173 * \p lifetime is not supported.
174 * \retval #PSA_ERROR_NOT_PERMITTED
175 * The specified key exists, but the application does not have the
176 * permission to access it. Note that this specification does not
177 * define any way to create such a key, but it may be possible
178 * through implementation-specific means.
179 */
180psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
181 psa_key_id_t id,
182 psa_key_handle_t *handle);
183
184/** Create a new persistent key slot.
185 *
186 * Create a new persistent key slot and return a handle to it. The handle
187 * remains valid until the application calls psa_close_key() or terminates.
188 * The application can open the key again with psa_open_key() until it
189 * removes the key by calling psa_destroy_key().
190 *
191 * \param lifetime The lifetime of the key. This designates a storage
192 * area where the key material is stored. This must not
193 * be #PSA_KEY_LIFETIME_VOLATILE.
194 * \param id The persistent identifier of the key.
195 * \param type The type of key that the slot will contain.
196 * \param max_bits The maximum key size that the slot will contain.
197 * \param[out] handle On success, a handle to the newly created key slot.
198 * When key material is later created in this key slot,
199 * it will be saved to the specified persistent location.
200 *
201 * \retval #PSA_SUCCESS
202 * Success. The application can now use the value of `*handle`
203 * to access the newly allocated key slot.
204 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
205 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
206 * \retval #PSA_ERROR_OCCUPIED_SLOT
207 * There is already a key with the identifier \p id in the storage
208 * area designated by \p lifetime.
209 * \retval #PSA_ERROR_INVALID_ARGUMENT
210 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
211 * \retval #PSA_ERROR_INVALID_ARGUMENT
212 * \p id is invalid for the specified lifetime.
213 * \retval #PSA_ERROR_NOT_SUPPORTED
214 * \p lifetime is not supported.
215 * \retval #PSA_ERROR_NOT_PERMITTED
216 * \p lifetime is valid, but the application does not have the
217 * permission to create a key there.
218 */
219psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
220 psa_key_id_t id,
221 psa_key_type_t type,
222 size_t max_bits,
223 psa_key_handle_t *handle);
224
225/** Close a key handle.
226 *
227 * If the handle designates a volatile key, destroy the key material and
228 * free all associated resources, just like psa_destroy_key().
229 *
230 * If the handle designates a persistent key, free all resources associated
231 * with the key in volatile memory. The key slot in persistent storage is
232 * not affected and can be opened again later with psa_open_key().
233 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100234 * If the key is currently in use in a multipart operation,
235 * the multipart operation is aborted.
236 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100237 * \param handle The key handle to close.
238 *
239 * \retval #PSA_SUCCESS
240 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100241 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100242 */
243psa_status_t psa_close_key(psa_key_handle_t handle);
244
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100245/**@}*/
246
247/** \defgroup import_export Key import and export
248 * @{
249 */
250
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100251/**
252 * \brief Import a key in binary format.
253 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100254 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100255 * documentation of psa_export_public_key() for the format of public keys
256 * and to the documentation of psa_export_key() for the format for
257 * other key types.
258 *
259 * This specification supports a single format for each key type.
260 * Implementations may support other formats as long as the standard
261 * format is supported. Implementations that support other formats
262 * should ensure that the formats are clearly unambiguous so as to
263 * minimize the risk that an invalid input is accidentally interpreted
264 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100265 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100266 * \param handle Handle to the slot where the key will be stored.
267 * This must be a valid slot for a key of the chosen
268 * type: it must have been obtained by calling
269 * psa_allocate_key() or psa_create_key() with the
270 * correct \p type and with a maximum size that is
271 * compatible with \p data.
Gilles Peskinef7933932018-10-31 14:07:52 +0100272 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
273 * import, the key slot will contain a key of this type.
274 * \param[in] data Buffer containing the key data. The content of this
275 * buffer is interpreted according to \p type. It must
276 * contain the format described in the documentation
277 * of psa_export_key() or psa_export_public_key() for
278 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200279 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100280 *
Gilles Peskine28538492018-07-11 17:34:00 +0200281 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100282 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100283 * If the key is persistent, the key material and the key's metadata
284 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100285 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200286 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200287 * The key type or key size is not supported, either by the
288 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200289 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100290 * The key slot is invalid,
291 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200292 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200293 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200294 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
295 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
296 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100297 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200298 * \retval #PSA_ERROR_HARDWARE_FAILURE
299 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300300 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300301 * The library has not been previously initialized by psa_crypto_init().
302 * It is implementation-dependent whether a failure to initialize
303 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100304 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100305psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 psa_key_type_t type,
307 const uint8_t *data,
308 size_t data_length);
309
310/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100311 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200312 *
313 * This function destroys the content of the key slot from both volatile
314 * memory and, if applicable, non-volatile storage. Implementations shall
315 * make a best effort to ensure that any previous content of the slot is
316 * unrecoverable.
317 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100318 * This function also erases any metadata such as policies and frees all
319 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200320 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100321 * If the key is currently in use in a multipart operation,
322 * the multipart operation is aborted.
323 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100324 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100325 *
Gilles Peskine28538492018-07-11 17:34:00 +0200326 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200327 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200328 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200329 * The slot holds content and cannot be erased because it is
330 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100331 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200332 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200333 * There was an failure in communication with the cryptoprocessor.
334 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200335 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200336 * The storage is corrupted. Implementations shall make a best effort
337 * to erase key material even in this stage, however applications
338 * should be aware that it may be impossible to guarantee that the
339 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200340 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200341 * An unexpected condition which is not a storage corruption or
342 * a communication failure occurred. The cryptoprocessor may have
343 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300344 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300345 * The library has not been previously initialized by psa_crypto_init().
346 * It is implementation-dependent whether a failure to initialize
347 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100348 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100349psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100350
351/**
352 * \brief Get basic metadata about a key.
353 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100354 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200355 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100356 * This may be a null pointer, in which case the key type
357 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200358 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100359 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100360 * is not written.
361 *
Gilles Peskine28538492018-07-11 17:34:00 +0200362 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100363 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200364 * \retval #PSA_ERROR_EMPTY_SLOT
Gilles Peskineae32aac2018-11-30 14:39:32 +0100365 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200366 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
367 * \retval #PSA_ERROR_HARDWARE_FAILURE
368 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300369 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300370 * The library has not been previously initialized by psa_crypto_init().
371 * It is implementation-dependent whether a failure to initialize
372 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100373 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100374psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100375 psa_key_type_t *type,
376 size_t *bits);
377
378/**
379 * \brief Export a key in binary format.
380 *
381 * The output of this function can be passed to psa_import_key() to
382 * create an equivalent object.
383 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100384 * If the implementation of psa_import_key() supports other formats
385 * beyond the format specified here, the output from psa_export_key()
386 * must use the representation specified here, not the original
387 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100388 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100389 * For standard key types, the output format is as follows:
390 *
391 * - For symmetric keys (including MAC keys), the format is the
392 * raw bytes of the key.
393 * - For DES, the key data consists of 8 bytes. The parity bits must be
394 * correct.
395 * - For Triple-DES, the format is the concatenation of the
396 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100397 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200398 * is the non-encrypted DER encoding of the representation defined by
399 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
400 * ```
401 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200402 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200403 * modulus INTEGER, -- n
404 * publicExponent INTEGER, -- e
405 * privateExponent INTEGER, -- d
406 * prime1 INTEGER, -- p
407 * prime2 INTEGER, -- q
408 * exponent1 INTEGER, -- d mod (p-1)
409 * exponent2 INTEGER, -- d mod (q-1)
410 * coefficient INTEGER, -- (inverse of q) mod p
411 * }
412 * ```
413 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
414 * is the non-encrypted DER encoding of the representation used by
Gilles Peskinec6290c02018-08-13 17:24:59 +0200415 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200416 * ```
417 * DSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200418 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200419 * prime INTEGER, -- p
420 * subprime INTEGER, -- q
421 * generator INTEGER, -- g
422 * public INTEGER, -- y
423 * private INTEGER, -- x
424 * }
425 * ```
426 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100427 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100428 * a representation of the private value as a `ceiling(m/8)`-byte string
429 * where `m` is the bit size associated with the curve, i.e. the bit size
430 * of the order of the curve's coordinate field. This byte string is
431 * in little-endian order for Montgomery curves (curve types
432 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
433 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
434 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100435 * This is the content of the `privateKey` field of the `ECPrivateKey`
436 * format defined by RFC 5915.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200437 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
438 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100439 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100440 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200441 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200442 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200443 * \param[out] data_length On success, the number of bytes
444 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100445 *
Gilles Peskine28538492018-07-11 17:34:00 +0200446 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100447 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200448 * \retval #PSA_ERROR_EMPTY_SLOT
449 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100450 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200451 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
452 * The size of the \p data buffer is too small. You can determine a
453 * sufficient buffer size by calling
454 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
455 * where \c type is the key type
456 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200457 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
458 * \retval #PSA_ERROR_HARDWARE_FAILURE
459 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300460 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300461 * The library has not been previously initialized by psa_crypto_init().
462 * It is implementation-dependent whether a failure to initialize
463 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100464 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100465psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100466 uint8_t *data,
467 size_t data_size,
468 size_t *data_length);
469
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100470/**
471 * \brief Export a public key or the public part of a key pair in binary format.
472 *
473 * The output of this function can be passed to psa_import_key() to
474 * create an object that is equivalent to the public key.
475 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200476 * The format is the DER representation defined by RFC 5280 as
477 * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
478 * specified below.
479 * ```
480 * SubjectPublicKeyInfo ::= SEQUENCE {
481 * algorithm AlgorithmIdentifier,
482 * subjectPublicKey BIT STRING }
483 * AlgorithmIdentifier ::= SEQUENCE {
484 * algorithm OBJECT IDENTIFIER,
485 * parameters ANY DEFINED BY algorithm OPTIONAL }
486 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100487 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200488 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY),
489 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.1 as
490 * `RSAPublicKey`,
491 * with the OID `rsaEncryption`,
492 * and with the parameters `NULL`.
493 * ```
494 * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
495 * rsadsi(113549) pkcs(1) 1 }
496 * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 }
497 *
498 * RSAPublicKey ::= SEQUENCE {
499 * modulus INTEGER, -- n
500 * publicExponent INTEGER } -- e
501 * ```
502 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
503 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
504 * `DSAPublicKey`,
505 * with the OID `id-dsa`,
506 * and with the parameters `DSS-Parms`.
507 * ```
508 * id-dsa OBJECT IDENTIFIER ::= {
509 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
510 *
511 * Dss-Parms ::= SEQUENCE {
512 * p INTEGER,
513 * q INTEGER,
514 * g INTEGER }
515 * DSAPublicKey ::= INTEGER -- public key, Y
516 * ```
517 * - For elliptic curve public keys (key types for which
518 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true),
519 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.5 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200520 * `ECPoint`, which contains the uncompressed
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200521 * representation defined by SEC1 &sect;2.3.3.
522 * The OID is `id-ecPublicKey`,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200523 * and the parameters must be given as a `namedCurve` OID as specified in
Gilles Peskinec6290c02018-08-13 17:24:59 +0200524 * RFC 5480 &sect;2.1.1.1 or other applicable standards.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200525 * ```
526 * ansi-X9-62 OBJECT IDENTIFIER ::=
527 * { iso(1) member-body(2) us(840) 10045 }
528 * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 }
529 * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 }
530 *
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200531 * ECPoint ::= ...
532 * -- first 8 bits: 0x04;
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100533 * -- then x_P as a `ceiling(m/8)`-byte string, big endian;
534 * -- then y_P as a `ceiling(m/8)`-byte string, big endian;
535 * -- where `m` is the bit size associated with the curve,
Gilles Peskine7b5b4a02018-11-14 21:05:10 +0100536 * -- i.e. the bit size of `q` for a curve over `F_q`.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200537 *
538 * EcpkParameters ::= CHOICE { -- other choices are not allowed
539 * namedCurve OBJECT IDENTIFIER }
540 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100541 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100542 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200543 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200544 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200545 * \param[out] data_length On success, the number of bytes
546 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100547 *
Gilles Peskine28538492018-07-11 17:34:00 +0200548 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100549 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200550 * \retval #PSA_ERROR_EMPTY_SLOT
551 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200552 * The key is neither a public key nor a key pair.
553 * \retval #PSA_ERROR_NOT_SUPPORTED
554 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
555 * The size of the \p data buffer is too small. You can determine a
556 * sufficient buffer size by calling
557 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
558 * where \c type is the key type
559 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200560 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
561 * \retval #PSA_ERROR_HARDWARE_FAILURE
562 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300563 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300564 * The library has not been previously initialized by psa_crypto_init().
565 * It is implementation-dependent whether a failure to initialize
566 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100567 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100568psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100569 uint8_t *data,
570 size_t data_size,
571 size_t *data_length);
572
573/**@}*/
574
575/** \defgroup policy Key policies
576 * @{
577 */
578
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100579/** The type of the key policy data structure.
580 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000581 * Before calling any function on a key policy, the application must initialize
582 * it by any of the following means:
583 * - Set the structure to all-bits-zero, for example:
584 * \code
585 * psa_key_policy_t policy;
586 * memset(&policy, 0, sizeof(policy));
587 * \endcode
588 * - Initialize the structure to logical zero values, for example:
589 * \code
590 * psa_key_policy_t policy = {0};
591 * \endcode
592 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
593 * for example:
594 * \code
595 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
596 * \endcode
597 * - Assign the result of the function psa_key_policy_init()
598 * to the structure, for example:
599 * \code
600 * psa_key_policy_t policy;
601 * policy = psa_key_policy_init();
602 * \endcode
603 *
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100604 * This is an implementation-defined \c struct. Applications should not
605 * make any assumptions about the content of this structure except
606 * as directed by the documentation of a specific implementation. */
607typedef struct psa_key_policy_s psa_key_policy_t;
608
Jaeden Amero70261c52019-01-04 11:47:20 +0000609/** \def PSA_KEY_POLICY_INIT
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200610 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000611 * This macro returns a suitable initializer for a key policy object of type
612 * #psa_key_policy_t.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200613 */
Jaeden Amero70261c52019-01-04 11:47:20 +0000614#ifdef __DOXYGEN_ONLY__
615/* This is an example definition for documentation purposes.
616 * Implementations should define a suitable value in `crypto_struct.h`.
617 */
618#define PSA_KEY_POLICY_INIT {0}
619#endif
620
621/** Return an initial value for a key policy that forbids all usage of the key.
622 */
623static psa_key_policy_t psa_key_policy_init(void);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100624
Gilles Peskine7e198532018-03-08 07:50:30 +0100625/** \brief Set the standard fields of a policy structure.
626 *
627 * Note that this function does not make any consistency check of the
628 * parameters. The values are only checked when applying the policy to
629 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200630 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000631 * \param[in,out] policy The key policy to modify. It must have been
632 * initialized as per the documentation for
633 * #psa_key_policy_t.
634 * \param usage The permitted uses for the key.
635 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100636 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100637void psa_key_policy_set_usage(psa_key_policy_t *policy,
638 psa_key_usage_t usage,
639 psa_algorithm_t alg);
640
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200641/** \brief Retrieve the usage field of a policy structure.
642 *
643 * \param[in] policy The policy object to query.
644 *
645 * \return The permitted uses for a key with this policy.
646 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200647psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100648
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200649/** \brief Retrieve the algorithm field of a policy structure.
650 *
651 * \param[in] policy The policy object to query.
652 *
653 * \return The permitted algorithm for a key with this policy.
654 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200655psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100656
657/** \brief Set the usage policy on a key slot.
658 *
659 * This function must be called on an empty key slot, before importing,
660 * generating or creating a key in the slot. Changing the policy of an
661 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100662 *
663 * Implementations may set restrictions on supported key policies
664 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200665 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100666 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200667 * \param[in] policy The policy object to query.
668 *
669 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100670 * Success.
671 * If the key is persistent, it is implementation-defined whether
672 * the policy has been saved to persistent storage. Implementations
673 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100674 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200675 * \retval #PSA_ERROR_OCCUPIED_SLOT
676 * \retval #PSA_ERROR_NOT_SUPPORTED
677 * \retval #PSA_ERROR_INVALID_ARGUMENT
678 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
679 * \retval #PSA_ERROR_HARDWARE_FAILURE
680 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300681 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300682 * The library has not been previously initialized by psa_crypto_init().
683 * It is implementation-dependent whether a failure to initialize
684 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100685 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100686psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100687 const psa_key_policy_t *policy);
688
Gilles Peskine7e198532018-03-08 07:50:30 +0100689/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200690 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100691 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200692 * \param[out] policy On success, the key's policy.
693 *
694 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100695 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200696 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
697 * \retval #PSA_ERROR_HARDWARE_FAILURE
698 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300699 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300700 * The library has not been previously initialized by psa_crypto_init().
701 * It is implementation-dependent whether a failure to initialize
702 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100703 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100704psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100705 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100706
707/**@}*/
708
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100709/** \defgroup hash Message digests
710 * @{
711 */
712
Gilles Peskine308b91d2018-02-08 09:47:44 +0100713/** The type of the state data structure for multipart hash operations.
714 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000715 * Before calling any function on a hash operation object, the application must
716 * initialize it by any of the following means:
717 * - Set the structure to all-bits-zero, for example:
718 * \code
719 * psa_hash_operation_t operation;
720 * memset(&operation, 0, sizeof(operation));
721 * \endcode
722 * - Initialize the structure to logical zero values, for example:
723 * \code
724 * psa_hash_operation_t operation = {0};
725 * \endcode
726 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
727 * for example:
728 * \code
729 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
730 * \endcode
731 * - Assign the result of the function psa_hash_operation_init()
732 * to the structure, for example:
733 * \code
734 * psa_hash_operation_t operation;
735 * operation = psa_hash_operation_init();
736 * \endcode
737 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100738 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100739 * make any assumptions about the content of this structure except
740 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100741typedef struct psa_hash_operation_s psa_hash_operation_t;
742
Jaeden Amero6a25b412019-01-04 11:47:44 +0000743/** \def PSA_HASH_OPERATION_INIT
744 *
745 * This macro returns a suitable initializer for a hash operation object
746 * of type #psa_hash_operation_t.
747 */
748#ifdef __DOXYGEN_ONLY__
749/* This is an example definition for documentation purposes.
750 * Implementations should define a suitable value in `crypto_struct.h`.
751 */
752#define PSA_HASH_OPERATION_INIT {0}
753#endif
754
755/** Return an initial value for a hash operation object.
756 */
757static psa_hash_operation_t psa_hash_operation_init(void);
758
Gilles Peskinef45adda2019-01-14 18:29:18 +0100759/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100760 *
761 * The sequence of operations to calculate a hash (message digest)
762 * is as follows:
763 * -# Allocate an operation object which will be passed to all the functions
764 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000765 * -# Initialize the operation object with one of the methods described in the
766 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200767 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100768 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100769 * of the message each time. The hash that is calculated is the hash
770 * of the concatenation of these messages in order.
771 * -# To calculate the hash, call psa_hash_finish().
772 * To compare the hash with an expected value, call psa_hash_verify().
773 *
774 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000775 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100776 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200777 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100778 * eventually terminate the operation. The following events terminate an
779 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100780 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100781 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100782 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000783 * \param[in,out] operation The operation object to set up. It must have
784 * been initialized as per the documentation for
785 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200786 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
787 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100788 *
Gilles Peskine28538492018-07-11 17:34:00 +0200789 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100790 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200791 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200792 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200793 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
794 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
795 * \retval #PSA_ERROR_HARDWARE_FAILURE
796 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100797 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200798psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100799 psa_algorithm_t alg);
800
Gilles Peskine308b91d2018-02-08 09:47:44 +0100801/** Add a message fragment to a multipart hash operation.
802 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200803 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100804 *
805 * If this function returns an error status, the operation becomes inactive.
806 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200807 * \param[in,out] operation Active hash operation.
808 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200809 * \param input_length Size of the \p input buffer in bytes.
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_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100814 * The operation state is not valid (not set up, or already completed).
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 Peskine9ef733f2018-02-07 21:05:37 +0100820psa_status_t psa_hash_update(psa_hash_operation_t *operation,
821 const uint8_t *input,
822 size_t input_length);
823
Gilles Peskine308b91d2018-02-08 09:47:44 +0100824/** Finish the calculation of the hash of a message.
825 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200826 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100827 * This function calculates the hash of the message formed by concatenating
828 * the inputs passed to preceding calls to psa_hash_update().
829 *
830 * When this function returns, the operation becomes inactive.
831 *
832 * \warning Applications should not call this function if they expect
833 * a specific value for the hash. Call psa_hash_verify() instead.
834 * Beware that comparing integrity or authenticity data such as
835 * hash values with a function such as \c memcmp is risky
836 * because the time taken by the comparison may leak information
837 * about the hashed data which could allow an attacker to guess
838 * a valid hash and thereby bypass security controls.
839 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200840 * \param[in,out] operation Active hash operation.
841 * \param[out] hash Buffer where the hash is to be written.
842 * \param hash_size Size of the \p hash buffer in bytes.
843 * \param[out] hash_length On success, the number of bytes
844 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200845 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200846 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100847 *
Gilles Peskine28538492018-07-11 17:34:00 +0200848 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100849 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200850 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100851 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200852 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200853 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200854 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100855 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200856 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
857 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
858 * \retval #PSA_ERROR_HARDWARE_FAILURE
859 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100860 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100861psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
862 uint8_t *hash,
863 size_t hash_size,
864 size_t *hash_length);
865
Gilles Peskine308b91d2018-02-08 09:47:44 +0100866/** Finish the calculation of the hash of a message and compare it with
867 * an expected value.
868 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200869 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100870 * This function calculates the hash of the message formed by concatenating
871 * the inputs passed to preceding calls to psa_hash_update(). It then
872 * compares the calculated hash with the expected hash passed as a
873 * parameter to this function.
874 *
875 * When this function returns, the operation becomes inactive.
876 *
Gilles Peskine19067982018-03-20 17:54:53 +0100877 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100878 * comparison between the actual hash and the expected hash is performed
879 * in constant time.
880 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200881 * \param[in,out] operation Active hash operation.
882 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200883 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100884 *
Gilles Peskine28538492018-07-11 17:34:00 +0200885 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100886 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200887 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100888 * The hash of the message was calculated successfully, but it
889 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200890 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100891 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200892 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
893 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
894 * \retval #PSA_ERROR_HARDWARE_FAILURE
895 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100896 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100897psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
898 const uint8_t *hash,
899 size_t hash_length);
900
Gilles Peskine308b91d2018-02-08 09:47:44 +0100901/** Abort a hash operation.
902 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100903 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200904 * \p operation structure itself. Once aborted, the operation object
905 * can be reused for another operation by calling
906 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100907 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200908 * You may call this function any time after the operation object has
909 * been initialized by any of the following methods:
910 * - A call to psa_hash_setup(), whether it succeeds or not.
911 * - Initializing the \c struct to all-bits-zero.
912 * - Initializing the \c struct to logical zeros, e.g.
913 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100914 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200915 * In particular, calling psa_hash_abort() after the operation has been
916 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
917 * psa_hash_verify() is safe and has no effect.
918 *
919 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100920 *
Gilles Peskine28538492018-07-11 17:34:00 +0200921 * \retval #PSA_SUCCESS
922 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200923 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +0200924 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
925 * \retval #PSA_ERROR_HARDWARE_FAILURE
926 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100927 */
928psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100929
930/**@}*/
931
Gilles Peskine8c9def32018-02-08 10:02:12 +0100932/** \defgroup MAC Message authentication codes
933 * @{
934 */
935
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100936/** The type of the state data structure for multipart MAC operations.
937 *
Jaeden Amero769ce272019-01-04 11:48:03 +0000938 * Before calling any function on a MAC operation object, the application must
939 * initialize it by any of the following means:
940 * - Set the structure to all-bits-zero, for example:
941 * \code
942 * psa_mac_operation_t operation;
943 * memset(&operation, 0, sizeof(operation));
944 * \endcode
945 * - Initialize the structure to logical zero values, for example:
946 * \code
947 * psa_mac_operation_t operation = {0};
948 * \endcode
949 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
950 * for example:
951 * \code
952 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
953 * \endcode
954 * - Assign the result of the function psa_mac_operation_init()
955 * to the structure, for example:
956 * \code
957 * psa_mac_operation_t operation;
958 * operation = psa_mac_operation_init();
959 * \endcode
960 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100961 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100962 * make any assumptions about the content of this structure except
963 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100964typedef struct psa_mac_operation_s psa_mac_operation_t;
965
Jaeden Amero769ce272019-01-04 11:48:03 +0000966/** \def PSA_MAC_OPERATION_INIT
967 *
968 * This macro returns a suitable initializer for a MAC operation object of type
969 * #psa_mac_operation_t.
970 */
971#ifdef __DOXYGEN_ONLY__
972/* This is an example definition for documentation purposes.
973 * Implementations should define a suitable value in `crypto_struct.h`.
974 */
975#define PSA_MAC_OPERATION_INIT {0}
976#endif
977
978/** Return an initial value for a MAC operation object.
979 */
980static psa_mac_operation_t psa_mac_operation_init(void);
981
Gilles Peskinef45adda2019-01-14 18:29:18 +0100982/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100983 *
Gilles Peskine89167cb2018-07-08 20:12:23 +0200984 * This function sets up the calculation of the MAC
985 * (message authentication code) of a byte string.
986 * To verify the MAC of a message against an
987 * expected value, use psa_mac_verify_setup() instead.
988 *
989 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100990 * -# Allocate an operation object which will be passed to all the functions
991 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +0000992 * -# Initialize the operation object with one of the methods described in the
993 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200994 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100995 * -# Call psa_mac_update() zero, one or more times, passing a fragment
996 * of the message each time. The MAC that is calculated is the MAC
997 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200998 * -# At the end of the message, call psa_mac_sign_finish() to finish
999 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001000 *
1001 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001002 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001003 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001004 * After a successful call to psa_mac_sign_setup(), the application must
1005 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001006 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001007 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001008 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001009 * \param[in,out] operation The operation object to set up. It must have
1010 * been initialized as per the documentation for
1011 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001012 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001013 * It must remain valid until the operation
1014 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001015 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1016 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001017 *
Gilles Peskine28538492018-07-11 17:34:00 +02001018 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001019 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001020 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001021 * \retval #PSA_ERROR_EMPTY_SLOT
1022 * \retval #PSA_ERROR_NOT_PERMITTED
1023 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001024 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001025 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001026 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001027 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1028 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1029 * \retval #PSA_ERROR_HARDWARE_FAILURE
1030 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001031 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001032 * The library has not been previously initialized by psa_crypto_init().
1033 * It is implementation-dependent whether a failure to initialize
1034 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001035 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001036psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001037 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001038 psa_algorithm_t alg);
1039
Gilles Peskinef45adda2019-01-14 18:29:18 +01001040/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001041 *
1042 * This function sets up the verification of the MAC
1043 * (message authentication code) of a byte string against an expected value.
1044 *
1045 * The sequence of operations to verify a MAC is as follows:
1046 * -# Allocate an operation object which will be passed to all the functions
1047 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001048 * -# Initialize the operation object with one of the methods described in the
1049 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001050 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001051 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1052 * of the message each time. The MAC that is calculated is the MAC
1053 * of the concatenation of these messages in order.
1054 * -# At the end of the message, call psa_mac_verify_finish() to finish
1055 * calculating the actual MAC of the message and verify it against
1056 * the expected value.
1057 *
1058 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001059 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001060 *
1061 * After a successful call to psa_mac_verify_setup(), the application must
1062 * eventually terminate the operation through one of the following methods:
1063 * - A failed call to psa_mac_update().
1064 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1065 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001066 * \param[in,out] operation The operation object to set up. It must have
1067 * been initialized as per the documentation for
1068 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001069 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001070 * It must remain valid until the operation
1071 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001072 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1073 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001074 *
Gilles Peskine28538492018-07-11 17:34:00 +02001075 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001076 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001077 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001078 * \retval #PSA_ERROR_EMPTY_SLOT
1079 * \retval #PSA_ERROR_NOT_PERMITTED
1080 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001081 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001082 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001083 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001084 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1085 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1086 * \retval #PSA_ERROR_HARDWARE_FAILURE
1087 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001088 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001089 * The library has not been previously initialized by psa_crypto_init().
1090 * It is implementation-dependent whether a failure to initialize
1091 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001092 */
1093psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001094 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001095 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001096
Gilles Peskinedcd14942018-07-12 00:30:52 +02001097/** Add a message fragment to a multipart MAC operation.
1098 *
1099 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1100 * before calling this function.
1101 *
1102 * If this function returns an error status, the operation becomes inactive.
1103 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001104 * \param[in,out] operation Active MAC operation.
1105 * \param[in] input Buffer containing the message fragment to add to
1106 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001107 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001108 *
1109 * \retval #PSA_SUCCESS
1110 * Success.
1111 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001112 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001113 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1114 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1115 * \retval #PSA_ERROR_HARDWARE_FAILURE
1116 * \retval #PSA_ERROR_TAMPERING_DETECTED
1117 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001118psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1119 const uint8_t *input,
1120 size_t input_length);
1121
Gilles Peskinedcd14942018-07-12 00:30:52 +02001122/** Finish the calculation of the MAC of a message.
1123 *
1124 * The application must call psa_mac_sign_setup() before calling this function.
1125 * This function calculates the MAC of the message formed by concatenating
1126 * the inputs passed to preceding calls to psa_mac_update().
1127 *
1128 * When this function returns, the operation becomes inactive.
1129 *
1130 * \warning Applications should not call this function if they expect
1131 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1132 * Beware that comparing integrity or authenticity data such as
1133 * MAC values with a function such as \c memcmp is risky
1134 * because the time taken by the comparison may leak information
1135 * about the MAC value which could allow an attacker to guess
1136 * a valid MAC and thereby bypass security controls.
1137 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001138 * \param[in,out] operation Active MAC operation.
1139 * \param[out] mac Buffer where the MAC value is to be written.
1140 * \param mac_size Size of the \p mac buffer in bytes.
1141 * \param[out] mac_length On success, the number of bytes
1142 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001143 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001144 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001145 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001146 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001147 *
1148 * \retval #PSA_SUCCESS
1149 * Success.
1150 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001151 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001152 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001153 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001154 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1155 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1156 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1157 * \retval #PSA_ERROR_HARDWARE_FAILURE
1158 * \retval #PSA_ERROR_TAMPERING_DETECTED
1159 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001160psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1161 uint8_t *mac,
1162 size_t mac_size,
1163 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001164
Gilles Peskinedcd14942018-07-12 00:30:52 +02001165/** Finish the calculation of the MAC of a message and compare it with
1166 * an expected value.
1167 *
1168 * The application must call psa_mac_verify_setup() before calling this function.
1169 * This function calculates the MAC of the message formed by concatenating
1170 * the inputs passed to preceding calls to psa_mac_update(). It then
1171 * compares the calculated MAC with the expected MAC passed as a
1172 * parameter to this function.
1173 *
1174 * When this function returns, the operation becomes inactive.
1175 *
1176 * \note Implementations shall make the best effort to ensure that the
1177 * comparison between the actual MAC and the expected MAC is performed
1178 * in constant time.
1179 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001180 * \param[in,out] operation Active MAC operation.
1181 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001182 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001183 *
1184 * \retval #PSA_SUCCESS
1185 * The expected MAC is identical to the actual MAC of the message.
1186 * \retval #PSA_ERROR_INVALID_SIGNATURE
1187 * The MAC of the message was calculated successfully, but it
1188 * differs from the expected MAC.
1189 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001190 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001191 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1192 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1193 * \retval #PSA_ERROR_HARDWARE_FAILURE
1194 * \retval #PSA_ERROR_TAMPERING_DETECTED
1195 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001196psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1197 const uint8_t *mac,
1198 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001199
Gilles Peskinedcd14942018-07-12 00:30:52 +02001200/** Abort a MAC operation.
1201 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001202 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001203 * \p operation structure itself. Once aborted, the operation object
1204 * can be reused for another operation by calling
1205 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001206 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001207 * You may call this function any time after the operation object has
1208 * been initialized by any of the following methods:
1209 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1210 * it succeeds or not.
1211 * - Initializing the \c struct to all-bits-zero.
1212 * - Initializing the \c struct to logical zeros, e.g.
1213 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001214 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001215 * In particular, calling psa_mac_abort() after the operation has been
1216 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1217 * psa_mac_verify_finish() is safe and has no effect.
1218 *
1219 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001220 *
1221 * \retval #PSA_SUCCESS
1222 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001223 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001224 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1225 * \retval #PSA_ERROR_HARDWARE_FAILURE
1226 * \retval #PSA_ERROR_TAMPERING_DETECTED
1227 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001228psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1229
1230/**@}*/
1231
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001232/** \defgroup cipher Symmetric ciphers
1233 * @{
1234 */
1235
1236/** The type of the state data structure for multipart cipher operations.
1237 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001238 * Before calling any function on a cipher operation object, the application
1239 * must initialize it by any of the following means:
1240 * - Set the structure to all-bits-zero, for example:
1241 * \code
1242 * psa_cipher_operation_t operation;
1243 * memset(&operation, 0, sizeof(operation));
1244 * \endcode
1245 * - Initialize the structure to logical zero values, for example:
1246 * \code
1247 * psa_cipher_operation_t operation = {0};
1248 * \endcode
1249 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1250 * for example:
1251 * \code
1252 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1253 * \endcode
1254 * - Assign the result of the function psa_cipher_operation_init()
1255 * to the structure, for example:
1256 * \code
1257 * psa_cipher_operation_t operation;
1258 * operation = psa_cipher_operation_init();
1259 * \endcode
1260 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001261 * This is an implementation-defined \c struct. Applications should not
1262 * make any assumptions about the content of this structure except
1263 * as directed by the documentation of a specific implementation. */
1264typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1265
Jaeden Amero5bae2272019-01-04 11:48:27 +00001266/** \def PSA_CIPHER_OPERATION_INIT
1267 *
1268 * This macro returns a suitable initializer for a cipher operation object of
1269 * type #psa_cipher_operation_t.
1270 */
1271#ifdef __DOXYGEN_ONLY__
1272/* This is an example definition for documentation purposes.
1273 * Implementations should define a suitable value in `crypto_struct.h`.
1274 */
1275#define PSA_CIPHER_OPERATION_INIT {0}
1276#endif
1277
1278/** Return an initial value for a cipher operation object.
1279 */
1280static psa_cipher_operation_t psa_cipher_operation_init(void);
1281
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001282/** Set the key for a multipart symmetric encryption operation.
1283 *
1284 * The sequence of operations to encrypt a message with a symmetric cipher
1285 * is as follows:
1286 * -# Allocate an operation object which will be passed to all the functions
1287 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001288 * -# Initialize the operation object with one of the methods described in the
1289 * documentation for #psa_cipher_operation_t, e.g.
1290 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001291 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001292 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001293 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001294 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001295 * requires a specific IV value.
1296 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1297 * of the message each time.
1298 * -# Call psa_cipher_finish().
1299 *
1300 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001301 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001302 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001303 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001304 * eventually terminate the operation. The following events terminate an
1305 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001306 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001307 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001308 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001309 * \param[in,out] operation The operation object to set up. It must have
1310 * been initialized as per the documentation for
1311 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001312 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001313 * It must remain valid until the operation
1314 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001315 * \param alg The cipher algorithm to compute
1316 * (\c PSA_ALG_XXX value such that
1317 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001318 *
Gilles Peskine28538492018-07-11 17:34:00 +02001319 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001320 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001321 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001322 * \retval #PSA_ERROR_EMPTY_SLOT
1323 * \retval #PSA_ERROR_NOT_PERMITTED
1324 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001325 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001326 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001327 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001328 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1329 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1330 * \retval #PSA_ERROR_HARDWARE_FAILURE
1331 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001332 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001333 * The library has not been previously initialized by psa_crypto_init().
1334 * It is implementation-dependent whether a failure to initialize
1335 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001336 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001337psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001338 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001339 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001340
1341/** Set the key for a multipart symmetric decryption operation.
1342 *
1343 * The sequence of operations to decrypt a message with a symmetric cipher
1344 * is as follows:
1345 * -# Allocate an operation object which will be passed to all the functions
1346 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001347 * -# Initialize the operation object with one of the methods described in the
1348 * documentation for #psa_cipher_operation_t, e.g.
1349 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001350 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001351 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001352 * decryption. If the IV is prepended to the ciphertext, you can call
1353 * psa_cipher_update() on a buffer containing the IV followed by the
1354 * beginning of the message.
1355 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1356 * of the message each time.
1357 * -# Call psa_cipher_finish().
1358 *
1359 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001360 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001361 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001362 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001363 * eventually terminate the operation. The following events terminate an
1364 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001365 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001366 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001367 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001368 * \param[in,out] operation The operation object to set up. It must have
1369 * been initialized as per the documentation for
1370 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001371 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001372 * It must remain valid until the operation
1373 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001374 * \param alg The cipher algorithm to compute
1375 * (\c PSA_ALG_XXX value such that
1376 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001377 *
Gilles Peskine28538492018-07-11 17:34:00 +02001378 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001379 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001380 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001381 * \retval #PSA_ERROR_EMPTY_SLOT
1382 * \retval #PSA_ERROR_NOT_PERMITTED
1383 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001384 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001385 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001386 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001387 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1388 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1389 * \retval #PSA_ERROR_HARDWARE_FAILURE
1390 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001391 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001392 * The library has not been previously initialized by psa_crypto_init().
1393 * It is implementation-dependent whether a failure to initialize
1394 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001395 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001396psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001397 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001398 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001399
Gilles Peskinedcd14942018-07-12 00:30:52 +02001400/** Generate an IV for a symmetric encryption operation.
1401 *
1402 * This function generates a random IV (initialization vector), nonce
1403 * or initial counter value for the encryption operation as appropriate
1404 * for the chosen algorithm, key type and key size.
1405 *
1406 * The application must call psa_cipher_encrypt_setup() before
1407 * calling this function.
1408 *
1409 * If this function returns an error status, the operation becomes inactive.
1410 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001411 * \param[in,out] operation Active cipher operation.
1412 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001413 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001414 * \param[out] iv_length On success, the number of bytes of the
1415 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001416 *
1417 * \retval #PSA_SUCCESS
1418 * Success.
1419 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001420 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001421 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001422 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001423 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1424 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1425 * \retval #PSA_ERROR_HARDWARE_FAILURE
1426 * \retval #PSA_ERROR_TAMPERING_DETECTED
1427 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001428psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1429 unsigned char *iv,
1430 size_t iv_size,
1431 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001432
Gilles Peskinedcd14942018-07-12 00:30:52 +02001433/** Set the IV for a symmetric encryption or decryption operation.
1434 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001435 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001436 * or initial counter value for the encryption or decryption operation.
1437 *
1438 * The application must call psa_cipher_encrypt_setup() before
1439 * calling this function.
1440 *
1441 * If this function returns an error status, the operation becomes inactive.
1442 *
1443 * \note When encrypting, applications should use psa_cipher_generate_iv()
1444 * instead of this function, unless implementing a protocol that requires
1445 * a non-random IV.
1446 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001447 * \param[in,out] operation Active cipher operation.
1448 * \param[in] iv Buffer containing the IV to use.
1449 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001450 *
1451 * \retval #PSA_SUCCESS
1452 * Success.
1453 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001454 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001455 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001456 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001457 * or the chosen algorithm does not use an IV.
1458 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1459 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1460 * \retval #PSA_ERROR_HARDWARE_FAILURE
1461 * \retval #PSA_ERROR_TAMPERING_DETECTED
1462 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001463psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1464 const unsigned char *iv,
1465 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001466
Gilles Peskinedcd14942018-07-12 00:30:52 +02001467/** Encrypt or decrypt a message fragment in an active cipher operation.
1468 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001469 * Before calling this function, you must:
1470 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1471 * The choice of setup function determines whether this function
1472 * encrypts or decrypts its input.
1473 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1474 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001475 *
1476 * If this function returns an error status, the operation becomes inactive.
1477 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001478 * \param[in,out] operation Active cipher operation.
1479 * \param[in] input Buffer containing the message fragment to
1480 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001481 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001482 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001483 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001484 * \param[out] output_length On success, the number of bytes
1485 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001486 *
1487 * \retval #PSA_SUCCESS
1488 * Success.
1489 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001490 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001491 * not set, or already completed).
1492 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1493 * The size of the \p output buffer is too small.
1494 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1495 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1496 * \retval #PSA_ERROR_HARDWARE_FAILURE
1497 * \retval #PSA_ERROR_TAMPERING_DETECTED
1498 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001499psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1500 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001501 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001502 unsigned char *output,
1503 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001504 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001505
Gilles Peskinedcd14942018-07-12 00:30:52 +02001506/** Finish encrypting or decrypting a message in a cipher operation.
1507 *
1508 * The application must call psa_cipher_encrypt_setup() or
1509 * psa_cipher_decrypt_setup() before calling this function. The choice
1510 * of setup function determines whether this function encrypts or
1511 * decrypts its input.
1512 *
1513 * This function finishes the encryption or decryption of the message
1514 * formed by concatenating the inputs passed to preceding calls to
1515 * psa_cipher_update().
1516 *
1517 * When this function returns, the operation becomes inactive.
1518 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001519 * \param[in,out] operation Active cipher operation.
1520 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001521 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001522 * \param[out] output_length On success, the number of bytes
1523 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001524 *
1525 * \retval #PSA_SUCCESS
1526 * Success.
1527 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001528 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001529 * not set, or already completed).
1530 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1531 * The size of the \p output buffer is too small.
1532 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1533 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1534 * \retval #PSA_ERROR_HARDWARE_FAILURE
1535 * \retval #PSA_ERROR_TAMPERING_DETECTED
1536 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001537psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001538 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001539 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001540 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001541
Gilles Peskinedcd14942018-07-12 00:30:52 +02001542/** Abort a cipher operation.
1543 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001544 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001545 * \p operation structure itself. Once aborted, the operation object
1546 * can be reused for another operation by calling
1547 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001548 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001549 * You may call this function any time after the operation object has
1550 * been initialized by any of the following methods:
1551 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1552 * whether it succeeds or not.
1553 * - Initializing the \c struct to all-bits-zero.
1554 * - Initializing the \c struct to logical zeros, e.g.
1555 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001556 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001557 * In particular, calling psa_cipher_abort() after the operation has been
1558 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1559 * is safe and has no effect.
1560 *
1561 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562 *
1563 * \retval #PSA_SUCCESS
1564 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001565 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001566 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1567 * \retval #PSA_ERROR_HARDWARE_FAILURE
1568 * \retval #PSA_ERROR_TAMPERING_DETECTED
1569 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001570psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1571
1572/**@}*/
1573
Gilles Peskine3b555712018-03-03 21:27:57 +01001574/** \defgroup aead Authenticated encryption with associated data (AEAD)
1575 * @{
1576 */
1577
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001578/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001579 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001580 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001581 * \param alg The AEAD algorithm to compute
1582 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001583 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001584 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001585 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001586 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001587 * but not encrypted.
1588 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001589 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001590 * encrypted.
1591 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001592 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001593 * encrypted data. The additional data is not
1594 * part of this output. For algorithms where the
1595 * encrypted data and the authentication tag
1596 * are defined as separate outputs, the
1597 * authentication tag is appended to the
1598 * encrypted data.
1599 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1600 * This must be at least
1601 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1602 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001603 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001604 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001605 *
Gilles Peskine28538492018-07-11 17:34:00 +02001606 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001607 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001608 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001609 * \retval #PSA_ERROR_EMPTY_SLOT
1610 * \retval #PSA_ERROR_NOT_PERMITTED
1611 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001612 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001613 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001614 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001615 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1616 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1617 * \retval #PSA_ERROR_HARDWARE_FAILURE
1618 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001619 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001620 * The library has not been previously initialized by psa_crypto_init().
1621 * It is implementation-dependent whether a failure to initialize
1622 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001623 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001624psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001625 psa_algorithm_t alg,
1626 const uint8_t *nonce,
1627 size_t nonce_length,
1628 const uint8_t *additional_data,
1629 size_t additional_data_length,
1630 const uint8_t *plaintext,
1631 size_t plaintext_length,
1632 uint8_t *ciphertext,
1633 size_t ciphertext_size,
1634 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001635
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001636/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001637 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001638 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001639 * \param alg The AEAD algorithm to compute
1640 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001641 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001642 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001643 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001644 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001645 * but not encrypted.
1646 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001647 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001648 * encrypted. For algorithms where the
1649 * encrypted data and the authentication tag
1650 * are defined as separate inputs, the buffer
1651 * must contain the encrypted data followed
1652 * by the authentication tag.
1653 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001654 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001655 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1656 * This must be at least
1657 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1658 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001659 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001660 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001661 *
Gilles Peskine28538492018-07-11 17:34:00 +02001662 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001663 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001664 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001665 * \retval #PSA_ERROR_EMPTY_SLOT
1666 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001667 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001668 * \retval #PSA_ERROR_NOT_PERMITTED
1669 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001670 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001671 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001672 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001673 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1674 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1675 * \retval #PSA_ERROR_HARDWARE_FAILURE
1676 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001677 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001678 * The library has not been previously initialized by psa_crypto_init().
1679 * It is implementation-dependent whether a failure to initialize
1680 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001681 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001682psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001683 psa_algorithm_t alg,
1684 const uint8_t *nonce,
1685 size_t nonce_length,
1686 const uint8_t *additional_data,
1687 size_t additional_data_length,
1688 const uint8_t *ciphertext,
1689 size_t ciphertext_length,
1690 uint8_t *plaintext,
1691 size_t plaintext_size,
1692 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001693
1694/**@}*/
1695
Gilles Peskine20035e32018-02-03 22:44:14 +01001696/** \defgroup asymmetric Asymmetric cryptography
1697 * @{
1698 */
1699
1700/**
1701 * \brief Sign a hash or short message with a private key.
1702 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001703 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001704 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001705 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1706 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1707 * to determine the hash algorithm to use.
1708 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001709 * \param handle Handle to the key to use for the operation.
1710 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001711 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001712 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001713 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001714 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001715 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001716 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001717 * \param[out] signature_length On success, the number of bytes
1718 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001719 *
Gilles Peskine28538492018-07-11 17:34:00 +02001720 * \retval #PSA_SUCCESS
1721 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001722 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01001723 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001724 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001725 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001726 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001727 * \retval #PSA_ERROR_NOT_SUPPORTED
1728 * \retval #PSA_ERROR_INVALID_ARGUMENT
1729 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1730 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1731 * \retval #PSA_ERROR_HARDWARE_FAILURE
1732 * \retval #PSA_ERROR_TAMPERING_DETECTED
1733 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001734 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001735 * The library has not been previously initialized by psa_crypto_init().
1736 * It is implementation-dependent whether a failure to initialize
1737 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001738 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001739psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001740 psa_algorithm_t alg,
1741 const uint8_t *hash,
1742 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01001743 uint8_t *signature,
1744 size_t signature_size,
1745 size_t *signature_length);
1746
1747/**
1748 * \brief Verify the signature a hash or short message using a public key.
1749 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001750 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001751 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001752 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1753 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1754 * to determine the hash algorithm to use.
1755 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001756 * \param handle Handle to the key to use for the operation.
1757 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001758 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001759 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001760 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02001761 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001762 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001763 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001764 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001765 *
Gilles Peskine28538492018-07-11 17:34:00 +02001766 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001767 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02001768 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001769 * The calculation was perfomed successfully, but the passed
1770 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02001771 * \retval #PSA_ERROR_NOT_SUPPORTED
1772 * \retval #PSA_ERROR_INVALID_ARGUMENT
1773 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1774 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1775 * \retval #PSA_ERROR_HARDWARE_FAILURE
1776 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001777 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001778 * The library has not been previously initialized by psa_crypto_init().
1779 * It is implementation-dependent whether a failure to initialize
1780 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001781 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001782psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001783 psa_algorithm_t alg,
1784 const uint8_t *hash,
1785 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001786 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001787 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01001788
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001789/**
1790 * \brief Encrypt a short message with a public key.
1791 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001792 * \param handle Handle to the key to use for the operation.
1793 * It must be a public key or an asymmetric
1794 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001795 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001796 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001797 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001798 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001799 * \param[in] salt A salt or label, if supported by the
1800 * encryption algorithm.
1801 * If the algorithm does not support a
1802 * salt, pass \c NULL.
1803 * If the algorithm supports an optional
1804 * salt and you do not want to pass a salt,
1805 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001806 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001807 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1808 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001809 * \param salt_length Size of the \p salt buffer in bytes.
1810 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001811 * \param[out] output Buffer where the encrypted message is to
1812 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001813 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001814 * \param[out] output_length On success, the number of bytes
1815 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001816 *
Gilles Peskine28538492018-07-11 17:34:00 +02001817 * \retval #PSA_SUCCESS
1818 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001819 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001820 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001821 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001822 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001823 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001824 * \retval #PSA_ERROR_NOT_SUPPORTED
1825 * \retval #PSA_ERROR_INVALID_ARGUMENT
1826 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1827 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1828 * \retval #PSA_ERROR_HARDWARE_FAILURE
1829 * \retval #PSA_ERROR_TAMPERING_DETECTED
1830 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001831 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001832 * The library has not been previously initialized by psa_crypto_init().
1833 * It is implementation-dependent whether a failure to initialize
1834 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001835 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001836psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001837 psa_algorithm_t alg,
1838 const uint8_t *input,
1839 size_t input_length,
1840 const uint8_t *salt,
1841 size_t salt_length,
1842 uint8_t *output,
1843 size_t output_size,
1844 size_t *output_length);
1845
1846/**
1847 * \brief Decrypt a short message with a private key.
1848 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001849 * \param handle Handle to the key to use for the operation.
1850 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001851 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001852 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001853 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001854 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001855 * \param[in] salt A salt or label, if supported by the
1856 * encryption algorithm.
1857 * If the algorithm does not support a
1858 * salt, pass \c NULL.
1859 * If the algorithm supports an optional
1860 * salt and you do not want to pass a salt,
1861 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001862 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001863 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1864 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001865 * \param salt_length Size of the \p salt buffer in bytes.
1866 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001867 * \param[out] output Buffer where the decrypted message is to
1868 * be written.
1869 * \param output_size Size of the \c output buffer in bytes.
1870 * \param[out] output_length On success, the number of bytes
1871 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001872 *
Gilles Peskine28538492018-07-11 17:34:00 +02001873 * \retval #PSA_SUCCESS
1874 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001875 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001876 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001877 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001878 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001879 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001880 * \retval #PSA_ERROR_NOT_SUPPORTED
1881 * \retval #PSA_ERROR_INVALID_ARGUMENT
1882 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1883 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1884 * \retval #PSA_ERROR_HARDWARE_FAILURE
1885 * \retval #PSA_ERROR_TAMPERING_DETECTED
1886 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1887 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03001888 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001889 * The library has not been previously initialized by psa_crypto_init().
1890 * It is implementation-dependent whether a failure to initialize
1891 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001892 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001893psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001894 psa_algorithm_t alg,
1895 const uint8_t *input,
1896 size_t input_length,
1897 const uint8_t *salt,
1898 size_t salt_length,
1899 uint8_t *output,
1900 size_t output_size,
1901 size_t *output_length);
1902
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001903/**@}*/
1904
Gilles Peskineedd76872018-07-20 17:42:05 +02001905/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02001906 * @{
1907 */
1908
1909/** The type of the state data structure for generators.
1910 *
1911 * Before calling any function on a generator, the application must
1912 * initialize it by any of the following means:
1913 * - Set the structure to all-bits-zero, for example:
1914 * \code
1915 * psa_crypto_generator_t generator;
1916 * memset(&generator, 0, sizeof(generator));
1917 * \endcode
1918 * - Initialize the structure to logical zero values, for example:
1919 * \code
1920 * psa_crypto_generator_t generator = {0};
1921 * \endcode
1922 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
1923 * for example:
1924 * \code
1925 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1926 * \endcode
1927 * - Assign the result of the function psa_crypto_generator_init()
1928 * to the structure, for example:
1929 * \code
1930 * psa_crypto_generator_t generator;
1931 * generator = psa_crypto_generator_init();
1932 * \endcode
1933 *
1934 * This is an implementation-defined \c struct. Applications should not
1935 * make any assumptions about the content of this structure except
1936 * as directed by the documentation of a specific implementation.
1937 */
1938typedef struct psa_crypto_generator_s psa_crypto_generator_t;
1939
1940/** \def PSA_CRYPTO_GENERATOR_INIT
1941 *
1942 * This macro returns a suitable initializer for a generator object
1943 * of type #psa_crypto_generator_t.
1944 */
1945#ifdef __DOXYGEN_ONLY__
1946/* This is an example definition for documentation purposes.
1947 * Implementations should define a suitable value in `crypto_struct.h`.
1948 */
1949#define PSA_CRYPTO_GENERATOR_INIT {0}
1950#endif
1951
1952/** Return an initial value for a generator object.
1953 */
1954static psa_crypto_generator_t psa_crypto_generator_init(void);
1955
1956/** Retrieve the current capacity of a generator.
1957 *
1958 * The capacity of a generator is the maximum number of bytes that it can
1959 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
1960 *
1961 * \param[in] generator The generator to query.
1962 * \param[out] capacity On success, the capacity of the generator.
1963 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001964 * \retval #PSA_SUCCESS
1965 * \retval #PSA_ERROR_BAD_STATE
1966 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02001967 */
1968psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
1969 size_t *capacity);
1970
1971/** Read some data from a generator.
1972 *
1973 * This function reads and returns a sequence of bytes from a generator.
1974 * The data that is read is discarded from the generator. The generator's
1975 * capacity is decreased by the number of bytes read.
1976 *
1977 * \param[in,out] generator The generator object to read from.
1978 * \param[out] output Buffer where the generator output will be
1979 * written.
1980 * \param output_length Number of bytes to output.
1981 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001982 * \retval #PSA_SUCCESS
1983 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02001984 * There were fewer than \p output_length bytes
1985 * in the generator. Note that in this case, no
1986 * output is written to the output buffer.
1987 * The generator's capacity is set to 0, thus
1988 * subsequent calls to this function will not
1989 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001990 * \retval #PSA_ERROR_BAD_STATE
1991 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1992 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1993 * \retval #PSA_ERROR_HARDWARE_FAILURE
1994 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02001995 */
1996psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
1997 uint8_t *output,
1998 size_t output_length);
1999
2000/** Create a symmetric key from data read from a generator.
2001 *
2002 * This function reads a sequence of bytes from a generator and imports
2003 * these bytes as a key.
2004 * The data that is read is discarded from the generator. The generator's
2005 * capacity is decreased by the number of bytes read.
2006 *
2007 * This function is equivalent to calling #psa_generator_read and
2008 * passing the resulting output to #psa_import_key, but
2009 * if the implementation provides an isolation boundary then
2010 * the key material is not exposed outside the isolation boundary.
2011 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002012 * \param handle Handle to the slot where the key will be stored.
2013 * This must be a valid slot for a key of the chosen
2014 * type: it must have been obtained by calling
2015 * psa_allocate_key() or psa_create_key() with the
2016 * correct \p type and with a maximum size that is
2017 * compatible with \p bits.
2018 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002019 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2020 * This must be a symmetric key type.
2021 * \param bits Key size in bits.
2022 * \param[in,out] generator The generator object to read from.
2023 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002024 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002025 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002026 * If the key is persistent, the key material and the key's metadata
2027 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002028 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002029 * There were fewer than \p output_length bytes
2030 * in the generator. Note that in this case, no
2031 * output is written to the output buffer.
2032 * The generator's capacity is set to 0, thus
2033 * subsequent calls to this function will not
2034 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002035 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002036 * The key type or key size is not supported, either by the
2037 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002038 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002039 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002040 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002041 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002042 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2043 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2044 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2045 * \retval #PSA_ERROR_HARDWARE_FAILURE
2046 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002047 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002048 * The library has not been previously initialized by psa_crypto_init().
2049 * It is implementation-dependent whether a failure to initialize
2050 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002051 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002052psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002053 psa_key_type_t type,
2054 size_t bits,
2055 psa_crypto_generator_t *generator);
2056
2057/** Abort a generator.
2058 *
2059 * Once a generator has been aborted, its capacity is zero.
2060 * Aborting a generator frees all associated resources except for the
2061 * \c generator structure itself.
2062 *
2063 * This function may be called at any time as long as the generator
2064 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2065 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2066 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2067 * on a generator that has not been set up.
2068 *
2069 * Once aborted, the generator object may be called.
2070 *
2071 * \param[in,out] generator The generator to abort.
2072 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002073 * \retval #PSA_SUCCESS
2074 * \retval #PSA_ERROR_BAD_STATE
2075 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2076 * \retval #PSA_ERROR_HARDWARE_FAILURE
2077 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002078 */
2079psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2080
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002081/** Use the maximum possible capacity for a generator.
2082 *
2083 * Use this value as the capacity argument when setting up a generator
2084 * to indicate that the generator should have the maximum possible capacity.
2085 * The value of the maximum possible capacity depends on the generator
2086 * algorithm.
2087 */
2088#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2089
Gilles Peskineeab56e42018-07-12 17:12:33 +02002090/**@}*/
2091
Gilles Peskineea0fb492018-07-12 17:17:20 +02002092/** \defgroup derivation Key derivation
2093 * @{
2094 */
2095
2096/** Set up a key derivation operation.
2097 *
2098 * A key derivation algorithm takes three inputs: a secret input \p key and
2099 * two non-secret inputs \p label and p salt.
2100 * The result of this function is a byte generator which can
2101 * be used to produce keys and other cryptographic material.
2102 *
2103 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002104 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2105 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002106 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002107 * \param[in,out] generator The generator object to set up. It must have
2108 * been initialized as per the documentation for
2109 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002110 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002111 * \param alg The key derivation algorithm to compute
2112 * (\c PSA_ALG_XXX value such that
2113 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2114 * \param[in] salt Salt to use.
2115 * \param salt_length Size of the \p salt buffer in bytes.
2116 * \param[in] label Label to use.
2117 * \param label_length Size of the \p label buffer in bytes.
2118 * \param capacity The maximum number of bytes that the
2119 * generator will be able to provide.
2120 *
2121 * \retval #PSA_SUCCESS
2122 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002123 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02002124 * \retval #PSA_ERROR_EMPTY_SLOT
2125 * \retval #PSA_ERROR_NOT_PERMITTED
2126 * \retval #PSA_ERROR_INVALID_ARGUMENT
2127 * \c key is not compatible with \c alg,
2128 * or \p capacity is too large for the specified algorithm and key.
2129 * \retval #PSA_ERROR_NOT_SUPPORTED
2130 * \c alg is not supported or is not a key derivation algorithm.
2131 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2132 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2133 * \retval #PSA_ERROR_HARDWARE_FAILURE
2134 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002135 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002136 * The library has not been previously initialized by psa_crypto_init().
2137 * It is implementation-dependent whether a failure to initialize
2138 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002139 */
2140psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002141 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002142 psa_algorithm_t alg,
2143 const uint8_t *salt,
2144 size_t salt_length,
2145 const uint8_t *label,
2146 size_t label_length,
2147 size_t capacity);
2148
Gilles Peskine01d718c2018-09-18 12:01:02 +02002149/** Set up a key agreement operation.
2150 *
2151 * A key agreement algorithm takes two inputs: a private key \p private_key
2152 * a public key \p peer_key.
2153 * The result of this function is a byte generator which can
2154 * be used to produce keys and other cryptographic material.
2155 *
Gilles Peskine211a4362018-10-25 22:22:31 +02002156 * The resulting generator always has the maximum capacity permitted by
2157 * the algorithm.
2158 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002159 * \param[in,out] generator The generator object to set up. It must have
2160 * been initialized as per the documentation for
2161 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002162 * \param private_key Handle to the private key to use.
Gilles Peskined171e782018-11-15 17:46:21 +01002163 * \param[in] peer_key Public key of the peer. It must be
2164 * in the same format that psa_import_key()
2165 * accepts. The standard formats for public
2166 * keys are documented in the documentation
2167 * of psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02002168 * \param peer_key_length Size of \p peer_key in bytes.
2169 * \param alg The key agreement algorithm to compute
2170 * (\c PSA_ALG_XXX value such that
2171 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
2172 *
2173 * \retval #PSA_SUCCESS
2174 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002175 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02002176 * \retval #PSA_ERROR_EMPTY_SLOT
2177 * \retval #PSA_ERROR_NOT_PERMITTED
2178 * \retval #PSA_ERROR_INVALID_ARGUMENT
2179 * \c private_key is not compatible with \c alg,
2180 * or \p peer_key is not valid for \c alg or not compatible with
2181 * \c private_key.
2182 * \retval #PSA_ERROR_NOT_SUPPORTED
2183 * \c alg is not supported or is not a key derivation algorithm.
2184 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2185 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2186 * \retval #PSA_ERROR_HARDWARE_FAILURE
2187 * \retval #PSA_ERROR_TAMPERING_DETECTED
2188 */
2189psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002190 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02002191 const uint8_t *peer_key,
2192 size_t peer_key_length,
2193 psa_algorithm_t alg);
2194
Gilles Peskineea0fb492018-07-12 17:17:20 +02002195/**@}*/
2196
Gilles Peskineedd76872018-07-20 17:42:05 +02002197/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002198 * @{
2199 */
2200
2201/**
2202 * \brief Generate random bytes.
2203 *
2204 * \warning This function **can** fail! Callers MUST check the return status
2205 * and MUST NOT use the content of the output buffer if the return
2206 * status is not #PSA_SUCCESS.
2207 *
2208 * \note To generate a key, use psa_generate_key() instead.
2209 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002210 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002211 * \param output_size Number of bytes to generate and output.
2212 *
Gilles Peskine28538492018-07-11 17:34:00 +02002213 * \retval #PSA_SUCCESS
2214 * \retval #PSA_ERROR_NOT_SUPPORTED
2215 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2216 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2217 * \retval #PSA_ERROR_HARDWARE_FAILURE
2218 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002219 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002220 * The library has not been previously initialized by psa_crypto_init().
2221 * It is implementation-dependent whether a failure to initialize
2222 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002223 */
2224psa_status_t psa_generate_random(uint8_t *output,
2225 size_t output_size);
2226
Gilles Peskine4c317f42018-07-12 01:24:09 +02002227/** Extra parameters for RSA key generation.
2228 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002229 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002230 * parameter to psa_generate_key().
2231 */
2232typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002233 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002234} psa_generate_key_extra_rsa;
2235
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002236/**
2237 * \brief Generate a key or key pair.
2238 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002239 * \param handle Handle to the slot where the key will be stored.
2240 * This must be a valid slot for a key of the chosen
2241 * type: it must have been obtained by calling
2242 * psa_allocate_key() or psa_create_key() with the
2243 * correct \p type and with a maximum size that is
2244 * compatible with \p bits.
2245 * It must not contain any key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002246 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2247 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002248 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002249 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002250 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002251 * default parameters. Implementation that support
2252 * the generation of vendor-specific key types
2253 * that allow extra parameters shall document
2254 * the format of these extra parameters and
2255 * the default values. For standard parameters,
2256 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002257 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002258 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2259 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002260 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002261 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2262 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002263 * - For an RSA key (\p type is
2264 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2265 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002266 * specifying the public exponent. The
2267 * default public exponent used when \p extra
2268 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002269 * \param extra_size Size of the buffer that \p extra
2270 * points to, in bytes. Note that if \p extra is
2271 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002272 *
Gilles Peskine28538492018-07-11 17:34:00 +02002273 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002274 * Success.
2275 * If the key is persistent, the key material and the key's metadata
2276 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002277 * \retval #PSA_ERROR_INVALID_HANDLE
2278 * \retval #PSA_ERROR_OCCUPIED_SLOT
2279 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02002280 * \retval #PSA_ERROR_NOT_SUPPORTED
2281 * \retval #PSA_ERROR_INVALID_ARGUMENT
2282 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2283 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2284 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2285 * \retval #PSA_ERROR_HARDWARE_FAILURE
2286 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002287 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002288 * The library has not been previously initialized by psa_crypto_init().
2289 * It is implementation-dependent whether a failure to initialize
2290 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002291 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002292psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002293 psa_key_type_t type,
2294 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002295 const void *extra,
2296 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002297
2298/**@}*/
2299
Gilles Peskinee59236f2018-01-27 23:32:46 +01002300#ifdef __cplusplus
2301}
2302#endif
2303
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002304/* The file "crypto_sizes.h" contains definitions for size calculation
2305 * macros whose definitions are implementation-specific. */
2306#include "crypto_sizes.h"
2307
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002308/* The file "crypto_struct.h" contains definitions for
2309 * implementation-specific structs that are declared above. */
2310#include "crypto_struct.h"
2311
2312/* The file "crypto_extra.h" contains vendor-specific definitions. This
2313 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002314#include "crypto_extra.h"
2315
2316#endif /* PSA_CRYPTO_H */