blob: 06f9eb81f730870a49435a6fb7096450f9d85d1b [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
89 * \retval #PSA_ERROR_TAMPERING_DETECTED
90 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
96/** \defgroup key_management Key management
97 * @{
98 */
99
Gilles Peskineae32aac2018-11-30 14:39:32 +0100100/** \brief Retrieve the lifetime of an open key.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100101 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100102 * \param handle Handle to query.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100103 * \param[out] lifetime On success, the lifetime value.
104 *
105 * \retval #PSA_SUCCESS
106 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100107 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100108 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
109 * \retval #PSA_ERROR_HARDWARE_FAILURE
110 * \retval #PSA_ERROR_TAMPERING_DETECTED
111 * \retval #PSA_ERROR_BAD_STATE
112 * The library has not been previously initialized by psa_crypto_init().
113 * It is implementation-dependent whether a failure to initialize
114 * results in this error code.
115 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100116psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100117 psa_key_lifetime_t *lifetime);
118
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100119
Gilles Peskinef535eb22018-11-30 14:08:36 +0100120/** Allocate a key slot for a transient key, i.e. a key which is only stored
121 * in volatile memory.
122 *
123 * The allocated key slot and its handle remain valid until the
124 * application calls psa_close_key() or psa_destroy_key() or until the
125 * application terminates.
126 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100127 * \param[out] handle On success, a handle to a volatile key slot.
128 *
129 * \retval #PSA_SUCCESS
130 * Success. The application can now use the value of `*handle`
131 * to access the newly allocated key slot.
132 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
133 * There was not enough memory, or the maximum number of key slots
134 * has been reached.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100135 */
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100136psa_status_t psa_allocate_key(psa_key_handle_t *handle);
Gilles Peskinef535eb22018-11-30 14:08:36 +0100137
138/** Open a handle to an existing persistent key.
139 *
140 * Open a handle to a key which was previously created with psa_create_key().
141 *
142 * \param lifetime The lifetime of the key. This designates a storage
143 * area where the key material is stored. This must not
144 * be #PSA_KEY_LIFETIME_VOLATILE.
145 * \param id The persistent identifier of the key.
146 * \param[out] handle On success, a handle to a key slot which contains
147 * the data and metadata loaded from the specified
148 * persistent location.
149 *
150 * \retval #PSA_SUCCESS
151 * Success. The application can now use the value of `*handle`
152 * to access the newly allocated key slot.
153 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
154 * \retval #PSA_ERROR_EMPTY_SLOT
155 * \retval #PSA_ERROR_INVALID_ARGUMENT
156 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
157 * \retval #PSA_ERROR_INVALID_ARGUMENT
158 * \p id is invalid for the specified lifetime.
159 * \retval #PSA_ERROR_NOT_SUPPORTED
160 * \p lifetime is not supported.
161 * \retval #PSA_ERROR_NOT_PERMITTED
162 * The specified key exists, but the application does not have the
163 * permission to access it. Note that this specification does not
164 * define any way to create such a key, but it may be possible
165 * through implementation-specific means.
166 */
167psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
168 psa_key_id_t id,
169 psa_key_handle_t *handle);
170
171/** Create a new persistent key slot.
172 *
173 * Create a new persistent key slot and return a handle to it. The handle
174 * remains valid until the application calls psa_close_key() or terminates.
175 * The application can open the key again with psa_open_key() until it
176 * removes the key by calling psa_destroy_key().
177 *
178 * \param lifetime The lifetime of the key. This designates a storage
179 * area where the key material is stored. This must not
180 * be #PSA_KEY_LIFETIME_VOLATILE.
181 * \param id The persistent identifier of the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100182 * \param[out] handle On success, a handle to the newly created key slot.
183 * When key material is later created in this key slot,
184 * it will be saved to the specified persistent location.
185 *
186 * \retval #PSA_SUCCESS
187 * Success. The application can now use the value of `*handle`
188 * to access the newly allocated key slot.
189 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
190 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
191 * \retval #PSA_ERROR_OCCUPIED_SLOT
192 * There is already a key with the identifier \p id in the storage
193 * area designated by \p lifetime.
194 * \retval #PSA_ERROR_INVALID_ARGUMENT
195 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
196 * \retval #PSA_ERROR_INVALID_ARGUMENT
197 * \p id is invalid for the specified lifetime.
198 * \retval #PSA_ERROR_NOT_SUPPORTED
199 * \p lifetime is not supported.
200 * \retval #PSA_ERROR_NOT_PERMITTED
201 * \p lifetime is valid, but the application does not have the
202 * permission to create a key there.
203 */
204psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
205 psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100206 psa_key_handle_t *handle);
207
208/** Close a key handle.
209 *
210 * If the handle designates a volatile key, destroy the key material and
211 * free all associated resources, just like psa_destroy_key().
212 *
213 * If the handle designates a persistent key, free all resources associated
214 * with the key in volatile memory. The key slot in persistent storage is
215 * not affected and can be opened again later with psa_open_key().
216 *
217 * \param handle The key handle to close.
218 *
219 * \retval #PSA_SUCCESS
220 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100221 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100222 */
223psa_status_t psa_close_key(psa_key_handle_t handle);
224
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100225/**@}*/
226
227/** \defgroup import_export Key import and export
228 * @{
229 */
230
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100231/**
232 * \brief Import a key in binary format.
233 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100234 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100235 * documentation of psa_export_public_key() for the format of public keys
236 * and to the documentation of psa_export_key() for the format for
237 * other key types.
238 *
239 * This specification supports a single format for each key type.
240 * Implementations may support other formats as long as the standard
241 * format is supported. Implementations that support other formats
242 * should ensure that the formats are clearly unambiguous so as to
243 * minimize the risk that an invalid input is accidentally interpreted
244 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100245 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100246 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100247 * It must have been obtained by calling
248 * psa_allocate_key() or psa_create_key() and must
249 * not contain key material yet.
Gilles Peskinef7933932018-10-31 14:07:52 +0100250 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
251 * import, the key slot will contain a key of this type.
252 * \param[in] data Buffer containing the key data. The content of this
253 * buffer is interpreted according to \p type. It must
254 * contain the format described in the documentation
255 * of psa_export_key() or psa_export_public_key() for
256 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200257 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100258 *
Gilles Peskine28538492018-07-11 17:34:00 +0200259 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100260 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100261 * If the key is persistent, the key material and the key's metadata
262 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100263 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200264 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200265 * The key type or key size is not supported, either by the
266 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200267 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100268 * The key slot is invalid,
269 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200270 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200271 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200272 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
273 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
274 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100275 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200276 * \retval #PSA_ERROR_HARDWARE_FAILURE
277 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300278 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300279 * The library has not been previously initialized by psa_crypto_init().
280 * It is implementation-dependent whether a failure to initialize
281 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100283psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100284 psa_key_type_t type,
285 const uint8_t *data,
286 size_t data_length);
287
288/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100289 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200290 *
291 * This function destroys the content of the key slot from both volatile
292 * memory and, if applicable, non-volatile storage. Implementations shall
293 * make a best effort to ensure that any previous content of the slot is
294 * unrecoverable.
295 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100296 * This function also erases any metadata such as policies and frees all
297 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200298 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100299 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100300 *
Gilles Peskine28538492018-07-11 17:34:00 +0200301 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200302 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200303 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200304 * The slot holds content and cannot be erased because it is
305 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100306 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200307 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200308 * There was an failure in communication with the cryptoprocessor.
309 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200310 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200311 * The storage is corrupted. Implementations shall make a best effort
312 * to erase key material even in this stage, however applications
313 * should be aware that it may be impossible to guarantee that the
314 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200315 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200316 * An unexpected condition which is not a storage corruption or
317 * a communication failure occurred. The cryptoprocessor may have
318 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300319 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300320 * The library has not been previously initialized by psa_crypto_init().
321 * It is implementation-dependent whether a failure to initialize
322 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100323 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100324psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100325
326/**
327 * \brief Get basic metadata about a key.
328 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100329 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200330 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100331 * This may be a null pointer, in which case the key type
332 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200333 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100334 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100335 * is not written.
336 *
Gilles Peskine28538492018-07-11 17:34:00 +0200337 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100338 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200339 * \retval #PSA_ERROR_EMPTY_SLOT
Gilles Peskineae32aac2018-11-30 14:39:32 +0100340 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200341 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
342 * \retval #PSA_ERROR_HARDWARE_FAILURE
343 * \retval #PSA_ERROR_TAMPERING_DETECTED
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_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100350 psa_key_type_t *type,
351 size_t *bits);
352
353/**
354 * \brief Export a key in binary format.
355 *
356 * The output of this function can be passed to psa_import_key() to
357 * create an equivalent object.
358 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100359 * If the implementation of psa_import_key() supports other formats
360 * beyond the format specified here, the output from psa_export_key()
361 * must use the representation specified here, not the original
362 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100364 * For standard key types, the output format is as follows:
365 *
366 * - For symmetric keys (including MAC keys), the format is the
367 * raw bytes of the key.
368 * - For DES, the key data consists of 8 bytes. The parity bits must be
369 * correct.
370 * - For Triple-DES, the format is the concatenation of the
371 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100372 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200373 * is the non-encrypted DER encoding of the representation defined by
374 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
375 * ```
376 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200377 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200378 * modulus INTEGER, -- n
379 * publicExponent INTEGER, -- e
380 * privateExponent INTEGER, -- d
381 * prime1 INTEGER, -- p
382 * prime2 INTEGER, -- q
383 * exponent1 INTEGER, -- d mod (p-1)
384 * exponent2 INTEGER, -- d mod (q-1)
385 * coefficient INTEGER, -- (inverse of q) mod p
386 * }
387 * ```
388 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
389 * is the non-encrypted DER encoding of the representation used by
Gilles Peskinec6290c02018-08-13 17:24:59 +0200390 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200391 * ```
392 * DSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200393 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200394 * prime INTEGER, -- p
395 * subprime INTEGER, -- q
396 * generator INTEGER, -- g
397 * public INTEGER, -- y
398 * private INTEGER, -- x
399 * }
400 * ```
401 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100402 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100403 * a representation of the private value as a `ceiling(m/8)`-byte string
404 * where `m` is the bit size associated with the curve, i.e. the bit size
405 * of the order of the curve's coordinate field. This byte string is
406 * in little-endian order for Montgomery curves (curve types
407 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
408 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
409 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100410 * This is the content of the `privateKey` field of the `ECPrivateKey`
411 * format defined by RFC 5915.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200412 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
413 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100414 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100415 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200416 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200417 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200418 * \param[out] data_length On success, the number of bytes
419 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100420 *
Gilles Peskine28538492018-07-11 17:34:00 +0200421 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100422 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200423 * \retval #PSA_ERROR_EMPTY_SLOT
424 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100425 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200426 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
427 * The size of the \p data buffer is too small. You can determine a
428 * sufficient buffer size by calling
429 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
430 * where \c type is the key type
431 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200432 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
433 * \retval #PSA_ERROR_HARDWARE_FAILURE
434 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300435 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300436 * The library has not been previously initialized by psa_crypto_init().
437 * It is implementation-dependent whether a failure to initialize
438 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100439 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100440psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100441 uint8_t *data,
442 size_t data_size,
443 size_t *data_length);
444
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100445/**
446 * \brief Export a public key or the public part of a key pair in binary format.
447 *
448 * The output of this function can be passed to psa_import_key() to
449 * create an object that is equivalent to the public key.
450 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000451 * This specification supports a single format for each key type.
452 * Implementations may support other formats as long as the standard
453 * format is supported. Implementations that support other formats
454 * should ensure that the formats are clearly unambiguous so as to
455 * minimize the risk that an invalid input is accidentally interpreted
456 * according to a different format.
457 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000458 * For standard key types, the output format is as follows:
459 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
460 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
461 * ```
462 * RSAPublicKey ::= SEQUENCE {
463 * modulus INTEGER, -- n
464 * publicExponent INTEGER } -- e
465 * ```
Jaeden Ameroccdce902019-01-10 11:42:27 +0000466 * - For elliptic curve public keys (key types for which
467 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
468 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint:
469 * Let `m` be the bit size associated with the curve, i.e. the bit size of
470 * `q` for a curve over `F_q`. The representation consists of:
471 * - The byte 0x04;
472 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
473 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero25384a22019-01-10 10:23:21 +0000474 *
475 * For other public key types, the format is the DER representation defined by
476 * RFC 5280 as `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200477 * specified below.
478 * ```
479 * SubjectPublicKeyInfo ::= SEQUENCE {
480 * algorithm AlgorithmIdentifier,
481 * subjectPublicKey BIT STRING }
482 * AlgorithmIdentifier ::= SEQUENCE {
483 * algorithm OBJECT IDENTIFIER,
484 * parameters ANY DEFINED BY algorithm OPTIONAL }
485 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200486 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
487 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
488 * `DSAPublicKey`,
489 * with the OID `id-dsa`,
490 * and with the parameters `DSS-Parms`.
491 * ```
492 * id-dsa OBJECT IDENTIFIER ::= {
493 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
494 *
495 * Dss-Parms ::= SEQUENCE {
496 * p INTEGER,
497 * q INTEGER,
498 * g INTEGER }
499 * DSAPublicKey ::= INTEGER -- public key, Y
500 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100501 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100502 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200503 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200504 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200505 * \param[out] data_length On success, the number of bytes
506 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100507 *
Gilles Peskine28538492018-07-11 17:34:00 +0200508 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100509 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200510 * \retval #PSA_ERROR_EMPTY_SLOT
511 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200512 * The key is neither a public key nor a key pair.
513 * \retval #PSA_ERROR_NOT_SUPPORTED
514 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
515 * The size of the \p data buffer is too small. You can determine a
516 * sufficient buffer size by calling
517 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
518 * where \c type is the key type
519 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200520 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
521 * \retval #PSA_ERROR_HARDWARE_FAILURE
522 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300523 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300524 * The library has not been previously initialized by psa_crypto_init().
525 * It is implementation-dependent whether a failure to initialize
526 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100527 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100528psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100529 uint8_t *data,
530 size_t data_size,
531 size_t *data_length);
532
533/**@}*/
534
535/** \defgroup policy Key policies
536 * @{
537 */
538
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100539/** The type of the key policy data structure.
540 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000541 * Before calling any function on a key policy, the application must initialize
542 * it by any of the following means:
543 * - Set the structure to all-bits-zero, for example:
544 * \code
545 * psa_key_policy_t policy;
546 * memset(&policy, 0, sizeof(policy));
547 * \endcode
548 * - Initialize the structure to logical zero values, for example:
549 * \code
550 * psa_key_policy_t policy = {0};
551 * \endcode
552 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
553 * for example:
554 * \code
555 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
556 * \endcode
557 * - Assign the result of the function psa_key_policy_init()
558 * to the structure, for example:
559 * \code
560 * psa_key_policy_t policy;
561 * policy = psa_key_policy_init();
562 * \endcode
563 *
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100564 * This is an implementation-defined \c struct. Applications should not
565 * make any assumptions about the content of this structure except
566 * as directed by the documentation of a specific implementation. */
567typedef struct psa_key_policy_s psa_key_policy_t;
568
Jaeden Amero70261c52019-01-04 11:47:20 +0000569/** \def PSA_KEY_POLICY_INIT
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200570 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000571 * This macro returns a suitable initializer for a key policy object of type
572 * #psa_key_policy_t.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200573 */
Jaeden Amero70261c52019-01-04 11:47:20 +0000574#ifdef __DOXYGEN_ONLY__
575/* This is an example definition for documentation purposes.
576 * Implementations should define a suitable value in `crypto_struct.h`.
577 */
578#define PSA_KEY_POLICY_INIT {0}
579#endif
580
581/** Return an initial value for a key policy that forbids all usage of the key.
582 */
583static psa_key_policy_t psa_key_policy_init(void);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100584
Gilles Peskine7e198532018-03-08 07:50:30 +0100585/** \brief Set the standard fields of a policy structure.
586 *
587 * Note that this function does not make any consistency check of the
588 * parameters. The values are only checked when applying the policy to
589 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200590 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000591 * \param[in,out] policy The key policy to modify. It must have been
592 * initialized as per the documentation for
593 * #psa_key_policy_t.
594 * \param usage The permitted uses for the key.
595 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100596 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100597void psa_key_policy_set_usage(psa_key_policy_t *policy,
598 psa_key_usage_t usage,
599 psa_algorithm_t alg);
600
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200601/** \brief Retrieve the usage field of a policy structure.
602 *
603 * \param[in] policy The policy object to query.
604 *
605 * \return The permitted uses for a key with this policy.
606 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200607psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100608
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200609/** \brief Retrieve the algorithm field of a policy structure.
610 *
611 * \param[in] policy The policy object to query.
612 *
613 * \return The permitted algorithm for a key with this policy.
614 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200615psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100616
617/** \brief Set the usage policy on a key slot.
618 *
619 * This function must be called on an empty key slot, before importing,
620 * generating or creating a key in the slot. Changing the policy of an
621 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100622 *
623 * Implementations may set restrictions on supported key policies
624 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200625 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100626 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200627 * \param[in] policy The policy object to query.
628 *
629 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100630 * Success.
631 * If the key is persistent, it is implementation-defined whether
632 * the policy has been saved to persistent storage. Implementations
633 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100634 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200635 * \retval #PSA_ERROR_OCCUPIED_SLOT
636 * \retval #PSA_ERROR_NOT_SUPPORTED
637 * \retval #PSA_ERROR_INVALID_ARGUMENT
638 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
639 * \retval #PSA_ERROR_HARDWARE_FAILURE
640 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300641 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300642 * The library has not been previously initialized by psa_crypto_init().
643 * It is implementation-dependent whether a failure to initialize
644 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100645 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100646psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100647 const psa_key_policy_t *policy);
648
Gilles Peskine7e198532018-03-08 07:50:30 +0100649/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200650 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100651 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200652 * \param[out] policy On success, the key's policy.
653 *
654 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100655 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200656 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
657 * \retval #PSA_ERROR_HARDWARE_FAILURE
658 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300659 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300660 * The library has not been previously initialized by psa_crypto_init().
661 * It is implementation-dependent whether a failure to initialize
662 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100663 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100664psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100665 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100666
667/**@}*/
668
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100669/** \defgroup hash Message digests
670 * @{
671 */
672
Gilles Peskine308b91d2018-02-08 09:47:44 +0100673/** The type of the state data structure for multipart hash operations.
674 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000675 * Before calling any function on a hash operation object, the application must
676 * initialize it by any of the following means:
677 * - Set the structure to all-bits-zero, for example:
678 * \code
679 * psa_hash_operation_t operation;
680 * memset(&operation, 0, sizeof(operation));
681 * \endcode
682 * - Initialize the structure to logical zero values, for example:
683 * \code
684 * psa_hash_operation_t operation = {0};
685 * \endcode
686 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
687 * for example:
688 * \code
689 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
690 * \endcode
691 * - Assign the result of the function psa_hash_operation_init()
692 * to the structure, for example:
693 * \code
694 * psa_hash_operation_t operation;
695 * operation = psa_hash_operation_init();
696 * \endcode
697 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100698 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100699 * make any assumptions about the content of this structure except
700 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100701typedef struct psa_hash_operation_s psa_hash_operation_t;
702
Jaeden Amero6a25b412019-01-04 11:47:44 +0000703/** \def PSA_HASH_OPERATION_INIT
704 *
705 * This macro returns a suitable initializer for a hash operation object
706 * of type #psa_hash_operation_t.
707 */
708#ifdef __DOXYGEN_ONLY__
709/* This is an example definition for documentation purposes.
710 * Implementations should define a suitable value in `crypto_struct.h`.
711 */
712#define PSA_HASH_OPERATION_INIT {0}
713#endif
714
715/** Return an initial value for a hash operation object.
716 */
717static psa_hash_operation_t psa_hash_operation_init(void);
718
Gilles Peskine308b91d2018-02-08 09:47:44 +0100719/** Start a multipart hash operation.
720 *
721 * The sequence of operations to calculate a hash (message digest)
722 * is as follows:
723 * -# Allocate an operation object which will be passed to all the functions
724 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000725 * -# Initialize the operation object with one of the methods described in the
726 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200727 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100728 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100729 * of the message each time. The hash that is calculated is the hash
730 * of the concatenation of these messages in order.
731 * -# To calculate the hash, call psa_hash_finish().
732 * To compare the hash with an expected value, call psa_hash_verify().
733 *
734 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000735 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100736 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200737 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100738 * eventually terminate the operation. The following events terminate an
739 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100740 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100741 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100742 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000743 * \param[in,out] operation The operation object to set up. It must have
744 * been initialized as per the documentation for
745 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200746 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
747 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100748 *
Gilles Peskine28538492018-07-11 17:34:00 +0200749 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100750 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200751 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200752 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200753 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
754 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
755 * \retval #PSA_ERROR_HARDWARE_FAILURE
756 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100757 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200758psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100759 psa_algorithm_t alg);
760
Gilles Peskine308b91d2018-02-08 09:47:44 +0100761/** Add a message fragment to a multipart hash operation.
762 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200763 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100764 *
765 * If this function returns an error status, the operation becomes inactive.
766 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200767 * \param[in,out] operation Active hash operation.
768 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200769 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100770 *
Gilles Peskine28538492018-07-11 17:34:00 +0200771 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100772 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200773 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100774 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200775 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
776 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
777 * \retval #PSA_ERROR_HARDWARE_FAILURE
778 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100779 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100780psa_status_t psa_hash_update(psa_hash_operation_t *operation,
781 const uint8_t *input,
782 size_t input_length);
783
Gilles Peskine308b91d2018-02-08 09:47:44 +0100784/** Finish the calculation of the hash of a message.
785 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200786 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100787 * This function calculates the hash of the message formed by concatenating
788 * the inputs passed to preceding calls to psa_hash_update().
789 *
790 * When this function returns, the operation becomes inactive.
791 *
792 * \warning Applications should not call this function if they expect
793 * a specific value for the hash. Call psa_hash_verify() instead.
794 * Beware that comparing integrity or authenticity data such as
795 * hash values with a function such as \c memcmp is risky
796 * because the time taken by the comparison may leak information
797 * about the hashed data which could allow an attacker to guess
798 * a valid hash and thereby bypass security controls.
799 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200800 * \param[in,out] operation Active hash operation.
801 * \param[out] hash Buffer where the hash is to be written.
802 * \param hash_size Size of the \p hash buffer in bytes.
803 * \param[out] hash_length On success, the number of bytes
804 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200805 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200806 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100807 *
Gilles Peskine28538492018-07-11 17:34:00 +0200808 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100809 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200810 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100811 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200812 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200813 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200814 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100815 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200816 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
817 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
818 * \retval #PSA_ERROR_HARDWARE_FAILURE
819 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100820 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100821psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
822 uint8_t *hash,
823 size_t hash_size,
824 size_t *hash_length);
825
Gilles Peskine308b91d2018-02-08 09:47:44 +0100826/** Finish the calculation of the hash of a message and compare it with
827 * an expected value.
828 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200829 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100830 * This function calculates the hash of the message formed by concatenating
831 * the inputs passed to preceding calls to psa_hash_update(). It then
832 * compares the calculated hash with the expected hash passed as a
833 * parameter to this function.
834 *
835 * When this function returns, the operation becomes inactive.
836 *
Gilles Peskine19067982018-03-20 17:54:53 +0100837 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100838 * comparison between the actual hash and the expected hash is performed
839 * in constant time.
840 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200841 * \param[in,out] operation Active hash operation.
842 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200843 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100844 *
Gilles Peskine28538492018-07-11 17:34:00 +0200845 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100846 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200847 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100848 * The hash of the message was calculated successfully, but it
849 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200850 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100851 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200852 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
853 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
854 * \retval #PSA_ERROR_HARDWARE_FAILURE
855 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100856 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100857psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
858 const uint8_t *hash,
859 size_t hash_length);
860
Gilles Peskine308b91d2018-02-08 09:47:44 +0100861/** Abort a hash operation.
862 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100863 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200864 * \p operation structure itself. Once aborted, the operation object
865 * can be reused for another operation by calling
866 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100867 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200868 * You may call this function any time after the operation object has
869 * been initialized by any of the following methods:
870 * - A call to psa_hash_setup(), whether it succeeds or not.
871 * - Initializing the \c struct to all-bits-zero.
872 * - Initializing the \c struct to logical zeros, e.g.
873 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100874 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200875 * In particular, calling psa_hash_abort() after the operation has been
876 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
877 * psa_hash_verify() is safe and has no effect.
878 *
879 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100880 *
Gilles Peskine28538492018-07-11 17:34:00 +0200881 * \retval #PSA_SUCCESS
882 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200883 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +0200884 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
885 * \retval #PSA_ERROR_HARDWARE_FAILURE
886 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100887 */
888psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100889
Gilles Peskineebb2c3e2019-01-19 12:03:41 +0100890/** Clone a hash operation.
891 *
Gilles Peskinee43aa392019-01-21 14:50:37 +0100892 * This function copies the state of an ongoing hash operation to
893 * a new operation object. In other words, this function is equivalent
894 * to calling psa_hash_setup() on \p target_operation with the same
895 * algorithm that \p source_operation was set up for, then
896 * psa_hash_update() on \p target_operation with the same input that
897 * that was passed to \p source_operation. After this function returns, the
898 * two objects are independent, i.e. subsequent calls involving one of
899 * the objects do not affect the other object.
900 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +0100901 * \param[in] source_operation The active hash operation to clone.
902 * \param[in,out] target_operation The operation object to set up.
903 * It must be initialized but not active.
904 *
905 * \retval #PSA_SUCCESS
906 * \retval #PSA_ERROR_BAD_STATE
907 * \p source_operation is not an active hash operation.
908 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +0100909 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +0100910 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
911 * \retval #PSA_ERROR_HARDWARE_FAILURE
912 * \retval #PSA_ERROR_TAMPERING_DETECTED
913 */
914psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
915 psa_hash_operation_t *target_operation);
916
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100917/**@}*/
918
Gilles Peskine8c9def32018-02-08 10:02:12 +0100919/** \defgroup MAC Message authentication codes
920 * @{
921 */
922
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100923/** The type of the state data structure for multipart MAC operations.
924 *
Jaeden Amero769ce272019-01-04 11:48:03 +0000925 * Before calling any function on a MAC operation object, the application must
926 * initialize it by any of the following means:
927 * - Set the structure to all-bits-zero, for example:
928 * \code
929 * psa_mac_operation_t operation;
930 * memset(&operation, 0, sizeof(operation));
931 * \endcode
932 * - Initialize the structure to logical zero values, for example:
933 * \code
934 * psa_mac_operation_t operation = {0};
935 * \endcode
936 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
937 * for example:
938 * \code
939 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
940 * \endcode
941 * - Assign the result of the function psa_mac_operation_init()
942 * to the structure, for example:
943 * \code
944 * psa_mac_operation_t operation;
945 * operation = psa_mac_operation_init();
946 * \endcode
947 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100948 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100949 * make any assumptions about the content of this structure except
950 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100951typedef struct psa_mac_operation_s psa_mac_operation_t;
952
Jaeden Amero769ce272019-01-04 11:48:03 +0000953/** \def PSA_MAC_OPERATION_INIT
954 *
955 * This macro returns a suitable initializer for a MAC operation object of type
956 * #psa_mac_operation_t.
957 */
958#ifdef __DOXYGEN_ONLY__
959/* This is an example definition for documentation purposes.
960 * Implementations should define a suitable value in `crypto_struct.h`.
961 */
962#define PSA_MAC_OPERATION_INIT {0}
963#endif
964
965/** Return an initial value for a MAC operation object.
966 */
967static psa_mac_operation_t psa_mac_operation_init(void);
968
Gilles Peskine89167cb2018-07-08 20:12:23 +0200969/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100970 *
Gilles Peskine89167cb2018-07-08 20:12:23 +0200971 * This function sets up the calculation of the MAC
972 * (message authentication code) of a byte string.
973 * To verify the MAC of a message against an
974 * expected value, use psa_mac_verify_setup() instead.
975 *
976 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100977 * -# Allocate an operation object which will be passed to all the functions
978 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +0000979 * -# Initialize the operation object with one of the methods described in the
980 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200981 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100982 * The key remains associated with the operation even if the content
983 * of the key slot changes.
984 * -# Call psa_mac_update() zero, one or more times, passing a fragment
985 * of the message each time. The MAC that is calculated is the MAC
986 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200987 * -# At the end of the message, call psa_mac_sign_finish() to finish
988 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100989 *
990 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +0000991 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100992 *
Gilles Peskine89167cb2018-07-08 20:12:23 +0200993 * After a successful call to psa_mac_sign_setup(), the application must
994 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100995 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +0200996 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100997 *
Jaeden Amero769ce272019-01-04 11:48:03 +0000998 * \param[in,out] operation The operation object to set up. It must have
999 * been initialized as per the documentation for
1000 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001001 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001002 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1003 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001004 *
Gilles Peskine28538492018-07-11 17:34:00 +02001005 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001006 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001007 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001008 * \retval #PSA_ERROR_EMPTY_SLOT
1009 * \retval #PSA_ERROR_NOT_PERMITTED
1010 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001011 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001012 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001013 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001014 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1015 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1016 * \retval #PSA_ERROR_HARDWARE_FAILURE
1017 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001018 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001019 * The library has not been previously initialized by psa_crypto_init().
1020 * It is implementation-dependent whether a failure to initialize
1021 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001022 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001023psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001024 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001025 psa_algorithm_t alg);
1026
1027/** Start a multipart MAC verification operation.
1028 *
1029 * This function sets up the verification of the MAC
1030 * (message authentication code) of a byte string against an expected value.
1031 *
1032 * The sequence of operations to verify a MAC is as follows:
1033 * -# Allocate an operation object which will be passed to all the functions
1034 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001035 * -# Initialize the operation object with one of the methods described in the
1036 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001037 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1038 * The key remains associated with the operation even if the content
1039 * of the key slot changes.
1040 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1041 * of the message each time. The MAC that is calculated is the MAC
1042 * of the concatenation of these messages in order.
1043 * -# At the end of the message, call psa_mac_verify_finish() to finish
1044 * calculating the actual MAC of the message and verify it against
1045 * the expected value.
1046 *
1047 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001048 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001049 *
1050 * After a successful call to psa_mac_verify_setup(), the application must
1051 * eventually terminate the operation through one of the following methods:
1052 * - A failed call to psa_mac_update().
1053 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1054 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001055 * \param[in,out] operation The operation object to set up. It must have
1056 * been initialized as per the documentation for
1057 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001058 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001059 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1060 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001061 *
Gilles Peskine28538492018-07-11 17:34:00 +02001062 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001063 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001064 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001065 * \retval #PSA_ERROR_EMPTY_SLOT
1066 * \retval #PSA_ERROR_NOT_PERMITTED
1067 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001068 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001069 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001070 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001071 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1072 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1073 * \retval #PSA_ERROR_HARDWARE_FAILURE
1074 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001075 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001076 * The library has not been previously initialized by psa_crypto_init().
1077 * It is implementation-dependent whether a failure to initialize
1078 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001079 */
1080psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001081 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001082 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001083
Gilles Peskinedcd14942018-07-12 00:30:52 +02001084/** Add a message fragment to a multipart MAC operation.
1085 *
1086 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1087 * before calling this function.
1088 *
1089 * If this function returns an error status, the operation becomes inactive.
1090 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001091 * \param[in,out] operation Active MAC operation.
1092 * \param[in] input Buffer containing the message fragment to add to
1093 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001094 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001095 *
1096 * \retval #PSA_SUCCESS
1097 * Success.
1098 * \retval #PSA_ERROR_BAD_STATE
1099 * The operation state is not valid (not started, or already completed).
1100 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1101 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1102 * \retval #PSA_ERROR_HARDWARE_FAILURE
1103 * \retval #PSA_ERROR_TAMPERING_DETECTED
1104 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001105psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1106 const uint8_t *input,
1107 size_t input_length);
1108
Gilles Peskinedcd14942018-07-12 00:30:52 +02001109/** Finish the calculation of the MAC of a message.
1110 *
1111 * The application must call psa_mac_sign_setup() before calling this function.
1112 * This function calculates the MAC of the message formed by concatenating
1113 * the inputs passed to preceding calls to psa_mac_update().
1114 *
1115 * When this function returns, the operation becomes inactive.
1116 *
1117 * \warning Applications should not call this function if they expect
1118 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1119 * Beware that comparing integrity or authenticity data such as
1120 * MAC values with a function such as \c memcmp is risky
1121 * because the time taken by the comparison may leak information
1122 * about the MAC value which could allow an attacker to guess
1123 * a valid MAC and thereby bypass security controls.
1124 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001125 * \param[in,out] operation Active MAC operation.
1126 * \param[out] mac Buffer where the MAC value is to be written.
1127 * \param mac_size Size of the \p mac buffer in bytes.
1128 * \param[out] mac_length On success, the number of bytes
1129 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001130 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001131 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001132 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001133 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001134 *
1135 * \retval #PSA_SUCCESS
1136 * Success.
1137 * \retval #PSA_ERROR_BAD_STATE
1138 * The operation state is not valid (not started, or already completed).
1139 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001140 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001141 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1142 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1143 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1144 * \retval #PSA_ERROR_HARDWARE_FAILURE
1145 * \retval #PSA_ERROR_TAMPERING_DETECTED
1146 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001147psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1148 uint8_t *mac,
1149 size_t mac_size,
1150 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001151
Gilles Peskinedcd14942018-07-12 00:30:52 +02001152/** Finish the calculation of the MAC of a message and compare it with
1153 * an expected value.
1154 *
1155 * The application must call psa_mac_verify_setup() before calling this function.
1156 * This function calculates the MAC of the message formed by concatenating
1157 * the inputs passed to preceding calls to psa_mac_update(). It then
1158 * compares the calculated MAC with the expected MAC passed as a
1159 * parameter to this function.
1160 *
1161 * When this function returns, the operation becomes inactive.
1162 *
1163 * \note Implementations shall make the best effort to ensure that the
1164 * comparison between the actual MAC and the expected MAC is performed
1165 * in constant time.
1166 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001167 * \param[in,out] operation Active MAC operation.
1168 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001169 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001170 *
1171 * \retval #PSA_SUCCESS
1172 * The expected MAC is identical to the actual MAC of the message.
1173 * \retval #PSA_ERROR_INVALID_SIGNATURE
1174 * The MAC of the message was calculated successfully, but it
1175 * differs from the expected MAC.
1176 * \retval #PSA_ERROR_BAD_STATE
1177 * The operation state is not valid (not started, or already completed).
1178 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1179 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1180 * \retval #PSA_ERROR_HARDWARE_FAILURE
1181 * \retval #PSA_ERROR_TAMPERING_DETECTED
1182 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001183psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1184 const uint8_t *mac,
1185 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001186
Gilles Peskinedcd14942018-07-12 00:30:52 +02001187/** Abort a MAC operation.
1188 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001189 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001190 * \p operation structure itself. Once aborted, the operation object
1191 * can be reused for another operation by calling
1192 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001193 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001194 * You may call this function any time after the operation object has
1195 * been initialized by any of the following methods:
1196 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1197 * it succeeds or not.
1198 * - Initializing the \c struct to all-bits-zero.
1199 * - Initializing the \c struct to logical zeros, e.g.
1200 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001201 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001202 * In particular, calling psa_mac_abort() after the operation has been
1203 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1204 * psa_mac_verify_finish() is safe and has no effect.
1205 *
1206 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001207 *
1208 * \retval #PSA_SUCCESS
1209 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001210 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001211 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1212 * \retval #PSA_ERROR_HARDWARE_FAILURE
1213 * \retval #PSA_ERROR_TAMPERING_DETECTED
1214 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001215psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1216
1217/**@}*/
1218
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001219/** \defgroup cipher Symmetric ciphers
1220 * @{
1221 */
1222
1223/** The type of the state data structure for multipart cipher operations.
1224 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001225 * Before calling any function on a cipher operation object, the application
1226 * must initialize it by any of the following means:
1227 * - Set the structure to all-bits-zero, for example:
1228 * \code
1229 * psa_cipher_operation_t operation;
1230 * memset(&operation, 0, sizeof(operation));
1231 * \endcode
1232 * - Initialize the structure to logical zero values, for example:
1233 * \code
1234 * psa_cipher_operation_t operation = {0};
1235 * \endcode
1236 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1237 * for example:
1238 * \code
1239 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1240 * \endcode
1241 * - Assign the result of the function psa_cipher_operation_init()
1242 * to the structure, for example:
1243 * \code
1244 * psa_cipher_operation_t operation;
1245 * operation = psa_cipher_operation_init();
1246 * \endcode
1247 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001248 * This is an implementation-defined \c struct. Applications should not
1249 * make any assumptions about the content of this structure except
1250 * as directed by the documentation of a specific implementation. */
1251typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1252
Jaeden Amero5bae2272019-01-04 11:48:27 +00001253/** \def PSA_CIPHER_OPERATION_INIT
1254 *
1255 * This macro returns a suitable initializer for a cipher operation object of
1256 * type #psa_cipher_operation_t.
1257 */
1258#ifdef __DOXYGEN_ONLY__
1259/* This is an example definition for documentation purposes.
1260 * Implementations should define a suitable value in `crypto_struct.h`.
1261 */
1262#define PSA_CIPHER_OPERATION_INIT {0}
1263#endif
1264
1265/** Return an initial value for a cipher operation object.
1266 */
1267static psa_cipher_operation_t psa_cipher_operation_init(void);
1268
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001269/** Set the key for a multipart symmetric encryption operation.
1270 *
1271 * The sequence of operations to encrypt a message with a symmetric cipher
1272 * is as follows:
1273 * -# Allocate an operation object which will be passed to all the functions
1274 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001275 * -# Initialize the operation object with one of the methods described in the
1276 * documentation for #psa_cipher_operation_t, e.g.
1277 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001278 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001279 * The key remains associated with the operation even if the content
1280 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03001281 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001282 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001283 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001284 * requires a specific IV value.
1285 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1286 * of the message each time.
1287 * -# Call psa_cipher_finish().
1288 *
1289 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001290 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001291 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001292 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001293 * eventually terminate the operation. The following events terminate an
1294 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03001295 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001296 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001297 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001298 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001299 * \param[in,out] operation The operation object to set up. It must have
1300 * been initialized as per the documentation for
1301 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001302 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001303 * \param alg The cipher algorithm to compute
1304 * (\c PSA_ALG_XXX value such that
1305 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001306 *
Gilles Peskine28538492018-07-11 17:34:00 +02001307 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001308 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001309 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001310 * \retval #PSA_ERROR_EMPTY_SLOT
1311 * \retval #PSA_ERROR_NOT_PERMITTED
1312 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001313 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001314 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001315 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001316 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1317 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1318 * \retval #PSA_ERROR_HARDWARE_FAILURE
1319 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001320 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001321 * The library has not been previously initialized by psa_crypto_init().
1322 * It is implementation-dependent whether a failure to initialize
1323 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001324 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001325psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001326 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001327 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001328
1329/** Set the key for a multipart symmetric decryption operation.
1330 *
1331 * The sequence of operations to decrypt a message with a symmetric cipher
1332 * is as follows:
1333 * -# Allocate an operation object which will be passed to all the functions
1334 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001335 * -# Initialize the operation object with one of the methods described in the
1336 * documentation for #psa_cipher_operation_t, e.g.
1337 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001338 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001339 * The key remains associated with the operation even if the content
1340 * of the key slot changes.
1341 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1342 * decryption. If the IV is prepended to the ciphertext, you can call
1343 * psa_cipher_update() on a buffer containing the IV followed by the
1344 * beginning of the message.
1345 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1346 * of the message each time.
1347 * -# Call psa_cipher_finish().
1348 *
1349 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001350 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001351 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001352 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001353 * eventually terminate the operation. The following events terminate an
1354 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001355 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001356 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001357 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001358 * \param[in,out] operation The operation object to set up. It must have
1359 * been initialized as per the documentation for
1360 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001361 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001362 * \param alg The cipher algorithm to compute
1363 * (\c PSA_ALG_XXX value such that
1364 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001365 *
Gilles Peskine28538492018-07-11 17:34:00 +02001366 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001367 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001368 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001369 * \retval #PSA_ERROR_EMPTY_SLOT
1370 * \retval #PSA_ERROR_NOT_PERMITTED
1371 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001372 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001373 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001374 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001375 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1376 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1377 * \retval #PSA_ERROR_HARDWARE_FAILURE
1378 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001379 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001380 * The library has not been previously initialized by psa_crypto_init().
1381 * It is implementation-dependent whether a failure to initialize
1382 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001383 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001384psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001385 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001386 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001387
Gilles Peskinedcd14942018-07-12 00:30:52 +02001388/** Generate an IV for a symmetric encryption operation.
1389 *
1390 * This function generates a random IV (initialization vector), nonce
1391 * or initial counter value for the encryption operation as appropriate
1392 * for the chosen algorithm, key type and key size.
1393 *
1394 * The application must call psa_cipher_encrypt_setup() before
1395 * calling this function.
1396 *
1397 * If this function returns an error status, the operation becomes inactive.
1398 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001399 * \param[in,out] operation Active cipher operation.
1400 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001401 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001402 * \param[out] iv_length On success, the number of bytes of the
1403 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001404 *
1405 * \retval #PSA_SUCCESS
1406 * Success.
1407 * \retval #PSA_ERROR_BAD_STATE
1408 * The operation state is not valid (not started, or IV already set).
1409 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001410 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001411 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1412 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1413 * \retval #PSA_ERROR_HARDWARE_FAILURE
1414 * \retval #PSA_ERROR_TAMPERING_DETECTED
1415 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001416psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1417 unsigned char *iv,
1418 size_t iv_size,
1419 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001420
Gilles Peskinedcd14942018-07-12 00:30:52 +02001421/** Set the IV for a symmetric encryption or decryption operation.
1422 *
1423 * This function sets the random IV (initialization vector), nonce
1424 * or initial counter value for the encryption or decryption operation.
1425 *
1426 * The application must call psa_cipher_encrypt_setup() before
1427 * calling this function.
1428 *
1429 * If this function returns an error status, the operation becomes inactive.
1430 *
1431 * \note When encrypting, applications should use psa_cipher_generate_iv()
1432 * instead of this function, unless implementing a protocol that requires
1433 * a non-random IV.
1434 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001435 * \param[in,out] operation Active cipher operation.
1436 * \param[in] iv Buffer containing the IV to use.
1437 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001438 *
1439 * \retval #PSA_SUCCESS
1440 * Success.
1441 * \retval #PSA_ERROR_BAD_STATE
1442 * The operation state is not valid (not started, or IV already set).
1443 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001444 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001445 * or the chosen algorithm does not use an IV.
1446 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1447 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1448 * \retval #PSA_ERROR_HARDWARE_FAILURE
1449 * \retval #PSA_ERROR_TAMPERING_DETECTED
1450 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001451psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1452 const unsigned char *iv,
1453 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001454
Gilles Peskinedcd14942018-07-12 00:30:52 +02001455/** Encrypt or decrypt a message fragment in an active cipher operation.
1456 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001457 * Before calling this function, you must:
1458 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1459 * The choice of setup function determines whether this function
1460 * encrypts or decrypts its input.
1461 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1462 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001463 *
1464 * If this function returns an error status, the operation becomes inactive.
1465 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001466 * \param[in,out] operation Active cipher operation.
1467 * \param[in] input Buffer containing the message fragment to
1468 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001469 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001470 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001471 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001472 * \param[out] output_length On success, the number of bytes
1473 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001474 *
1475 * \retval #PSA_SUCCESS
1476 * Success.
1477 * \retval #PSA_ERROR_BAD_STATE
1478 * The operation state is not valid (not started, IV required but
1479 * not set, or already completed).
1480 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1481 * The size of the \p output buffer is too small.
1482 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1483 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1484 * \retval #PSA_ERROR_HARDWARE_FAILURE
1485 * \retval #PSA_ERROR_TAMPERING_DETECTED
1486 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001487psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1488 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001489 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001490 unsigned char *output,
1491 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001492 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001493
Gilles Peskinedcd14942018-07-12 00:30:52 +02001494/** Finish encrypting or decrypting a message in a cipher operation.
1495 *
1496 * The application must call psa_cipher_encrypt_setup() or
1497 * psa_cipher_decrypt_setup() before calling this function. The choice
1498 * of setup function determines whether this function encrypts or
1499 * decrypts its input.
1500 *
1501 * This function finishes the encryption or decryption of the message
1502 * formed by concatenating the inputs passed to preceding calls to
1503 * psa_cipher_update().
1504 *
1505 * When this function returns, the operation becomes inactive.
1506 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001507 * \param[in,out] operation Active cipher operation.
1508 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001509 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001510 * \param[out] output_length On success, the number of bytes
1511 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001512 *
1513 * \retval #PSA_SUCCESS
1514 * Success.
1515 * \retval #PSA_ERROR_BAD_STATE
1516 * The operation state is not valid (not started, IV required but
1517 * not set, or already completed).
1518 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1519 * The size of the \p output buffer is too small.
1520 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1521 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1522 * \retval #PSA_ERROR_HARDWARE_FAILURE
1523 * \retval #PSA_ERROR_TAMPERING_DETECTED
1524 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001525psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001526 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001527 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001528 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001529
Gilles Peskinedcd14942018-07-12 00:30:52 +02001530/** Abort a cipher operation.
1531 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001532 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001533 * \p operation structure itself. Once aborted, the operation object
1534 * can be reused for another operation by calling
1535 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001536 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001537 * You may call this function any time after the operation object has
1538 * been initialized by any of the following methods:
1539 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1540 * whether it succeeds or not.
1541 * - Initializing the \c struct to all-bits-zero.
1542 * - Initializing the \c struct to logical zeros, e.g.
1543 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001544 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001545 * In particular, calling psa_cipher_abort() after the operation has been
1546 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1547 * is safe and has no effect.
1548 *
1549 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001550 *
1551 * \retval #PSA_SUCCESS
1552 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001553 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001554 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1555 * \retval #PSA_ERROR_HARDWARE_FAILURE
1556 * \retval #PSA_ERROR_TAMPERING_DETECTED
1557 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001558psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1559
1560/**@}*/
1561
Gilles Peskine3b555712018-03-03 21:27:57 +01001562/** \defgroup aead Authenticated encryption with associated data (AEAD)
1563 * @{
1564 */
1565
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001566/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001567 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001568 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001569 * \param alg The AEAD algorithm to compute
1570 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001571 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001572 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001573 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001574 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001575 * but not encrypted.
1576 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001577 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001578 * encrypted.
1579 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001580 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001581 * encrypted data. The additional data is not
1582 * part of this output. For algorithms where the
1583 * encrypted data and the authentication tag
1584 * are defined as separate outputs, the
1585 * authentication tag is appended to the
1586 * encrypted data.
1587 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1588 * This must be at least
1589 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1590 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001591 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001592 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001593 *
Gilles Peskine28538492018-07-11 17:34:00 +02001594 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001595 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001596 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001597 * \retval #PSA_ERROR_EMPTY_SLOT
1598 * \retval #PSA_ERROR_NOT_PERMITTED
1599 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001600 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001601 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001602 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001603 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1604 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1605 * \retval #PSA_ERROR_HARDWARE_FAILURE
1606 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001607 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001608 * The library has not been previously initialized by psa_crypto_init().
1609 * It is implementation-dependent whether a failure to initialize
1610 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001611 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001612psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001613 psa_algorithm_t alg,
1614 const uint8_t *nonce,
1615 size_t nonce_length,
1616 const uint8_t *additional_data,
1617 size_t additional_data_length,
1618 const uint8_t *plaintext,
1619 size_t plaintext_length,
1620 uint8_t *ciphertext,
1621 size_t ciphertext_size,
1622 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001623
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001624/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001625 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001626 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001627 * \param alg The AEAD algorithm to compute
1628 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001629 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001630 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001631 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001632 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001633 * but not encrypted.
1634 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001635 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001636 * encrypted. For algorithms where the
1637 * encrypted data and the authentication tag
1638 * are defined as separate inputs, the buffer
1639 * must contain the encrypted data followed
1640 * by the authentication tag.
1641 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001642 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001643 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1644 * This must be at least
1645 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1646 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001647 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001648 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001649 *
Gilles Peskine28538492018-07-11 17:34:00 +02001650 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001651 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001652 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001653 * \retval #PSA_ERROR_EMPTY_SLOT
1654 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001655 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001656 * \retval #PSA_ERROR_NOT_PERMITTED
1657 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001658 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001659 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001660 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001661 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1662 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1663 * \retval #PSA_ERROR_HARDWARE_FAILURE
1664 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001665 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001666 * The library has not been previously initialized by psa_crypto_init().
1667 * It is implementation-dependent whether a failure to initialize
1668 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001669 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001670psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001671 psa_algorithm_t alg,
1672 const uint8_t *nonce,
1673 size_t nonce_length,
1674 const uint8_t *additional_data,
1675 size_t additional_data_length,
1676 const uint8_t *ciphertext,
1677 size_t ciphertext_length,
1678 uint8_t *plaintext,
1679 size_t plaintext_size,
1680 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001681
1682/**@}*/
1683
Gilles Peskine20035e32018-02-03 22:44:14 +01001684/** \defgroup asymmetric Asymmetric cryptography
1685 * @{
1686 */
1687
1688/**
1689 * \brief Sign a hash or short message with a private key.
1690 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001691 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001692 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001693 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1694 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1695 * to determine the hash algorithm to use.
1696 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001697 * \param handle Handle to the key to use for the operation.
1698 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001699 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001700 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001701 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001702 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001703 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001704 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001705 * \param[out] signature_length On success, the number of bytes
1706 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001707 *
Gilles Peskine28538492018-07-11 17:34:00 +02001708 * \retval #PSA_SUCCESS
1709 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001710 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01001711 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001712 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001713 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001714 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001715 * \retval #PSA_ERROR_NOT_SUPPORTED
1716 * \retval #PSA_ERROR_INVALID_ARGUMENT
1717 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1718 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1719 * \retval #PSA_ERROR_HARDWARE_FAILURE
1720 * \retval #PSA_ERROR_TAMPERING_DETECTED
1721 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001722 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001723 * The library has not been previously initialized by psa_crypto_init().
1724 * It is implementation-dependent whether a failure to initialize
1725 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001726 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001727psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001728 psa_algorithm_t alg,
1729 const uint8_t *hash,
1730 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01001731 uint8_t *signature,
1732 size_t signature_size,
1733 size_t *signature_length);
1734
1735/**
1736 * \brief Verify the signature a hash or short message using a public key.
1737 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001738 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001739 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001740 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1741 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1742 * to determine the hash algorithm to use.
1743 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001744 * \param handle Handle to the key to use for the operation.
1745 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001746 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001747 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001748 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02001749 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001750 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001751 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001752 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001753 *
Gilles Peskine28538492018-07-11 17:34:00 +02001754 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001755 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02001756 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001757 * The calculation was perfomed successfully, but the passed
1758 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02001759 * \retval #PSA_ERROR_NOT_SUPPORTED
1760 * \retval #PSA_ERROR_INVALID_ARGUMENT
1761 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1762 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1763 * \retval #PSA_ERROR_HARDWARE_FAILURE
1764 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001765 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001766 * The library has not been previously initialized by psa_crypto_init().
1767 * It is implementation-dependent whether a failure to initialize
1768 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001769 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001770psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001771 psa_algorithm_t alg,
1772 const uint8_t *hash,
1773 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001774 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001775 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01001776
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001777/**
1778 * \brief Encrypt a short message with a public key.
1779 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001780 * \param handle Handle to the key to use for the operation.
1781 * It must be a public key or an asymmetric
1782 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001783 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001784 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001785 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001786 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001787 * \param[in] salt A salt or label, if supported by the
1788 * encryption algorithm.
1789 * If the algorithm does not support a
1790 * salt, pass \c NULL.
1791 * If the algorithm supports an optional
1792 * salt and you do not want to pass a salt,
1793 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001794 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001795 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1796 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001797 * \param salt_length Size of the \p salt buffer in bytes.
1798 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001799 * \param[out] output Buffer where the encrypted message is to
1800 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001801 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001802 * \param[out] output_length On success, the number of bytes
1803 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001804 *
Gilles Peskine28538492018-07-11 17:34:00 +02001805 * \retval #PSA_SUCCESS
1806 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001807 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001808 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001809 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001810 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001811 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001812 * \retval #PSA_ERROR_NOT_SUPPORTED
1813 * \retval #PSA_ERROR_INVALID_ARGUMENT
1814 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1815 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1816 * \retval #PSA_ERROR_HARDWARE_FAILURE
1817 * \retval #PSA_ERROR_TAMPERING_DETECTED
1818 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001819 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001820 * The library has not been previously initialized by psa_crypto_init().
1821 * It is implementation-dependent whether a failure to initialize
1822 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001823 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001824psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001825 psa_algorithm_t alg,
1826 const uint8_t *input,
1827 size_t input_length,
1828 const uint8_t *salt,
1829 size_t salt_length,
1830 uint8_t *output,
1831 size_t output_size,
1832 size_t *output_length);
1833
1834/**
1835 * \brief Decrypt a short message with a private key.
1836 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001837 * \param handle Handle to the key to use for the operation.
1838 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001839 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001840 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001841 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001842 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001843 * \param[in] salt A salt or label, if supported by the
1844 * encryption algorithm.
1845 * If the algorithm does not support a
1846 * salt, pass \c NULL.
1847 * If the algorithm supports an optional
1848 * salt and you do not want to pass a salt,
1849 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001850 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001851 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1852 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001853 * \param salt_length Size of the \p salt buffer in bytes.
1854 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001855 * \param[out] output Buffer where the decrypted message is to
1856 * be written.
1857 * \param output_size Size of the \c output buffer in bytes.
1858 * \param[out] output_length On success, the number of bytes
1859 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001860 *
Gilles Peskine28538492018-07-11 17:34:00 +02001861 * \retval #PSA_SUCCESS
1862 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001863 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001864 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001865 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001866 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001867 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001868 * \retval #PSA_ERROR_NOT_SUPPORTED
1869 * \retval #PSA_ERROR_INVALID_ARGUMENT
1870 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1871 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1872 * \retval #PSA_ERROR_HARDWARE_FAILURE
1873 * \retval #PSA_ERROR_TAMPERING_DETECTED
1874 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1875 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03001876 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001877 * The library has not been previously initialized by psa_crypto_init().
1878 * It is implementation-dependent whether a failure to initialize
1879 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001880 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001881psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001882 psa_algorithm_t alg,
1883 const uint8_t *input,
1884 size_t input_length,
1885 const uint8_t *salt,
1886 size_t salt_length,
1887 uint8_t *output,
1888 size_t output_size,
1889 size_t *output_length);
1890
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001891/**@}*/
1892
Gilles Peskineedd76872018-07-20 17:42:05 +02001893/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02001894 * @{
1895 */
1896
1897/** The type of the state data structure for generators.
1898 *
1899 * Before calling any function on a generator, the application must
1900 * initialize it by any of the following means:
1901 * - Set the structure to all-bits-zero, for example:
1902 * \code
1903 * psa_crypto_generator_t generator;
1904 * memset(&generator, 0, sizeof(generator));
1905 * \endcode
1906 * - Initialize the structure to logical zero values, for example:
1907 * \code
1908 * psa_crypto_generator_t generator = {0};
1909 * \endcode
1910 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
1911 * for example:
1912 * \code
1913 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1914 * \endcode
1915 * - Assign the result of the function psa_crypto_generator_init()
1916 * to the structure, for example:
1917 * \code
1918 * psa_crypto_generator_t generator;
1919 * generator = psa_crypto_generator_init();
1920 * \endcode
1921 *
1922 * This is an implementation-defined \c struct. Applications should not
1923 * make any assumptions about the content of this structure except
1924 * as directed by the documentation of a specific implementation.
1925 */
1926typedef struct psa_crypto_generator_s psa_crypto_generator_t;
1927
1928/** \def PSA_CRYPTO_GENERATOR_INIT
1929 *
1930 * This macro returns a suitable initializer for a generator object
1931 * of type #psa_crypto_generator_t.
1932 */
1933#ifdef __DOXYGEN_ONLY__
1934/* This is an example definition for documentation purposes.
1935 * Implementations should define a suitable value in `crypto_struct.h`.
1936 */
1937#define PSA_CRYPTO_GENERATOR_INIT {0}
1938#endif
1939
1940/** Return an initial value for a generator object.
1941 */
1942static psa_crypto_generator_t psa_crypto_generator_init(void);
1943
1944/** Retrieve the current capacity of a generator.
1945 *
1946 * The capacity of a generator is the maximum number of bytes that it can
1947 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
1948 *
1949 * \param[in] generator The generator to query.
1950 * \param[out] capacity On success, the capacity of the generator.
1951 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001952 * \retval #PSA_SUCCESS
1953 * \retval #PSA_ERROR_BAD_STATE
1954 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02001955 */
1956psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
1957 size_t *capacity);
1958
1959/** Read some data from a generator.
1960 *
1961 * This function reads and returns a sequence of bytes from a generator.
1962 * The data that is read is discarded from the generator. The generator's
1963 * capacity is decreased by the number of bytes read.
1964 *
1965 * \param[in,out] generator The generator object to read from.
1966 * \param[out] output Buffer where the generator output will be
1967 * written.
1968 * \param output_length Number of bytes to output.
1969 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001970 * \retval #PSA_SUCCESS
1971 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02001972 * There were fewer than \p output_length bytes
1973 * in the generator. Note that in this case, no
1974 * output is written to the output buffer.
1975 * The generator's capacity is set to 0, thus
1976 * subsequent calls to this function will not
1977 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001978 * \retval #PSA_ERROR_BAD_STATE
1979 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1980 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1981 * \retval #PSA_ERROR_HARDWARE_FAILURE
1982 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02001983 */
1984psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
1985 uint8_t *output,
1986 size_t output_length);
1987
1988/** Create a symmetric key from data read from a generator.
1989 *
1990 * This function reads a sequence of bytes from a generator and imports
1991 * these bytes as a key.
1992 * The data that is read is discarded from the generator. The generator's
1993 * capacity is decreased by the number of bytes read.
1994 *
1995 * This function is equivalent to calling #psa_generator_read and
1996 * passing the resulting output to #psa_import_key, but
1997 * if the implementation provides an isolation boundary then
1998 * the key material is not exposed outside the isolation boundary.
1999 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002000 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002001 * It must have been obtained by calling
2002 * psa_allocate_key() or psa_create_key() and must
2003 * not contain key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002004 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2005 * This must be a symmetric key type.
2006 * \param bits Key size in bits.
2007 * \param[in,out] generator The generator object to read from.
2008 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002009 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002010 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002011 * If the key is persistent, the key material and the key's metadata
2012 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002013 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002014 * There were fewer than \p output_length bytes
2015 * in the generator. Note that in this case, no
2016 * output is written to the output buffer.
2017 * The generator's capacity is set to 0, thus
2018 * subsequent calls to this function will not
2019 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002020 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002021 * The key type or key size is not supported, either by the
2022 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002023 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002024 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002025 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002026 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002027 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2028 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2029 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2030 * \retval #PSA_ERROR_HARDWARE_FAILURE
2031 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002032 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002033 * The library has not been previously initialized by psa_crypto_init().
2034 * It is implementation-dependent whether a failure to initialize
2035 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002036 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002037psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002038 psa_key_type_t type,
2039 size_t bits,
2040 psa_crypto_generator_t *generator);
2041
2042/** Abort a generator.
2043 *
2044 * Once a generator has been aborted, its capacity is zero.
2045 * Aborting a generator frees all associated resources except for the
2046 * \c generator structure itself.
2047 *
2048 * This function may be called at any time as long as the generator
2049 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2050 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2051 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2052 * on a generator that has not been set up.
2053 *
2054 * Once aborted, the generator object may be called.
2055 *
2056 * \param[in,out] generator The generator to abort.
2057 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002058 * \retval #PSA_SUCCESS
2059 * \retval #PSA_ERROR_BAD_STATE
2060 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2061 * \retval #PSA_ERROR_HARDWARE_FAILURE
2062 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002063 */
2064psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2065
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002066/** Use the maximum possible capacity for a generator.
2067 *
2068 * Use this value as the capacity argument when setting up a generator
2069 * to indicate that the generator should have the maximum possible capacity.
2070 * The value of the maximum possible capacity depends on the generator
2071 * algorithm.
2072 */
2073#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2074
Gilles Peskineeab56e42018-07-12 17:12:33 +02002075/**@}*/
2076
Gilles Peskineea0fb492018-07-12 17:17:20 +02002077/** \defgroup derivation Key derivation
2078 * @{
2079 */
2080
2081/** Set up a key derivation operation.
2082 *
2083 * A key derivation algorithm takes three inputs: a secret input \p key and
2084 * two non-secret inputs \p label and p salt.
2085 * The result of this function is a byte generator which can
2086 * be used to produce keys and other cryptographic material.
2087 *
2088 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002089 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2090 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002091 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002092 * \param[in,out] generator The generator object to set up. It must have
2093 * been initialized as per the documentation for
2094 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002095 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002096 * \param alg The key derivation algorithm to compute
2097 * (\c PSA_ALG_XXX value such that
2098 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2099 * \param[in] salt Salt to use.
2100 * \param salt_length Size of the \p salt buffer in bytes.
2101 * \param[in] label Label to use.
2102 * \param label_length Size of the \p label buffer in bytes.
2103 * \param capacity The maximum number of bytes that the
2104 * generator will be able to provide.
2105 *
2106 * \retval #PSA_SUCCESS
2107 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002108 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02002109 * \retval #PSA_ERROR_EMPTY_SLOT
2110 * \retval #PSA_ERROR_NOT_PERMITTED
2111 * \retval #PSA_ERROR_INVALID_ARGUMENT
2112 * \c key is not compatible with \c alg,
2113 * or \p capacity is too large for the specified algorithm and key.
2114 * \retval #PSA_ERROR_NOT_SUPPORTED
2115 * \c alg is not supported or is not a key derivation algorithm.
2116 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2117 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2118 * \retval #PSA_ERROR_HARDWARE_FAILURE
2119 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002120 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002121 * The library has not been previously initialized by psa_crypto_init().
2122 * It is implementation-dependent whether a failure to initialize
2123 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002124 */
2125psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002126 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002127 psa_algorithm_t alg,
2128 const uint8_t *salt,
2129 size_t salt_length,
2130 const uint8_t *label,
2131 size_t label_length,
2132 size_t capacity);
2133
Gilles Peskine01d718c2018-09-18 12:01:02 +02002134/** Set up a key agreement operation.
2135 *
2136 * A key agreement algorithm takes two inputs: a private key \p private_key
2137 * a public key \p peer_key.
2138 * The result of this function is a byte generator which can
2139 * be used to produce keys and other cryptographic material.
2140 *
Gilles Peskine211a4362018-10-25 22:22:31 +02002141 * The resulting generator always has the maximum capacity permitted by
2142 * the algorithm.
2143 *
Jaeden Amero21fec0c2019-01-14 16:56:20 +00002144 * \param[in,out] generator The generator object to set up. It must have been
2145 * initialized as per the documentation for
2146 * #psa_crypto_generator_t and not yet in use.
2147 * \param private_key Handle to the private key to use.
2148 * \param[in] peer_key Public key of the peer. The peer key must be in the
2149 * same format that psa_import_key() accepts for the
2150 * public key type corresponding to the type of
2151 * \p private_key. That is, this function performs the
2152 * equivalent of
2153 * `psa_import_key(internal_public_key_handle,
2154 * PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(private_key_type),
2155 * peer_key, peer_key_length)` where
2156 * `private_key_type` is the type of \p private_key.
2157 * For example, for EC keys, this means that \p
2158 * peer_key is interpreted as a point on the curve
2159 * that the private key is associated with. The
2160 * standard formats for public keys are documented in
2161 * the documentation of psa_export_public_key().
2162 * \param peer_key_length Size of \p peer_key in bytes.
2163 * \param alg The key agreement algorithm to compute
2164 * (\c PSA_ALG_XXX value such that
2165 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
Gilles Peskine01d718c2018-09-18 12:01:02 +02002166 *
2167 * \retval #PSA_SUCCESS
2168 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002169 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02002170 * \retval #PSA_ERROR_EMPTY_SLOT
2171 * \retval #PSA_ERROR_NOT_PERMITTED
2172 * \retval #PSA_ERROR_INVALID_ARGUMENT
2173 * \c private_key is not compatible with \c alg,
2174 * or \p peer_key is not valid for \c alg or not compatible with
2175 * \c private_key.
2176 * \retval #PSA_ERROR_NOT_SUPPORTED
2177 * \c alg is not supported or is not a key derivation algorithm.
2178 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2179 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2180 * \retval #PSA_ERROR_HARDWARE_FAILURE
2181 * \retval #PSA_ERROR_TAMPERING_DETECTED
2182 */
2183psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002184 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02002185 const uint8_t *peer_key,
2186 size_t peer_key_length,
2187 psa_algorithm_t alg);
2188
Gilles Peskineea0fb492018-07-12 17:17:20 +02002189/**@}*/
2190
Gilles Peskineedd76872018-07-20 17:42:05 +02002191/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002192 * @{
2193 */
2194
2195/**
2196 * \brief Generate random bytes.
2197 *
2198 * \warning This function **can** fail! Callers MUST check the return status
2199 * and MUST NOT use the content of the output buffer if the return
2200 * status is not #PSA_SUCCESS.
2201 *
2202 * \note To generate a key, use psa_generate_key() instead.
2203 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002204 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002205 * \param output_size Number of bytes to generate and output.
2206 *
Gilles Peskine28538492018-07-11 17:34:00 +02002207 * \retval #PSA_SUCCESS
2208 * \retval #PSA_ERROR_NOT_SUPPORTED
2209 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2210 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2211 * \retval #PSA_ERROR_HARDWARE_FAILURE
2212 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002213 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002214 * The library has not been previously initialized by psa_crypto_init().
2215 * It is implementation-dependent whether a failure to initialize
2216 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002217 */
2218psa_status_t psa_generate_random(uint8_t *output,
2219 size_t output_size);
2220
Gilles Peskine4c317f42018-07-12 01:24:09 +02002221/** Extra parameters for RSA key generation.
2222 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002223 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002224 * parameter to psa_generate_key().
2225 */
2226typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002227 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002228} psa_generate_key_extra_rsa;
2229
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002230/**
2231 * \brief Generate a key or key pair.
2232 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002233 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002234 * It must have been obtained by calling
2235 * psa_allocate_key() or psa_create_key() and must
2236 * not contain key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002237 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2238 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002239 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002240 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002241 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002242 * default parameters. Implementation that support
2243 * the generation of vendor-specific key types
2244 * that allow extra parameters shall document
2245 * the format of these extra parameters and
2246 * the default values. For standard parameters,
2247 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002248 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002249 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2250 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002251 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002252 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2253 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002254 * - For an RSA key (\p type is
2255 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2256 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002257 * specifying the public exponent. The
2258 * default public exponent used when \p extra
2259 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002260 * \param extra_size Size of the buffer that \p extra
2261 * points to, in bytes. Note that if \p extra is
2262 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002263 *
Gilles Peskine28538492018-07-11 17:34:00 +02002264 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002265 * Success.
2266 * If the key is persistent, the key material and the key's metadata
2267 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002268 * \retval #PSA_ERROR_INVALID_HANDLE
2269 * \retval #PSA_ERROR_OCCUPIED_SLOT
2270 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02002271 * \retval #PSA_ERROR_NOT_SUPPORTED
2272 * \retval #PSA_ERROR_INVALID_ARGUMENT
2273 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2274 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2275 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2276 * \retval #PSA_ERROR_HARDWARE_FAILURE
2277 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002278 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002279 * The library has not been previously initialized by psa_crypto_init().
2280 * It is implementation-dependent whether a failure to initialize
2281 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002282 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002283psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002284 psa_key_type_t type,
2285 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002286 const void *extra,
2287 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002288
2289/**@}*/
2290
Gilles Peskinee59236f2018-01-27 23:32:46 +01002291#ifdef __cplusplus
2292}
2293#endif
2294
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002295/* The file "crypto_sizes.h" contains definitions for size calculation
2296 * macros whose definitions are implementation-specific. */
2297#include "crypto_sizes.h"
2298
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002299/* The file "crypto_struct.h" contains definitions for
2300 * implementation-specific structs that are declared above. */
2301#include "crypto_struct.h"
2302
2303/* The file "crypto_extra.h" contains vendor-specific definitions. This
2304 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002305#include "crypto_extra.h"
2306
2307#endif /* PSA_CRYPTO_H */