blob: 705462edaf83bf7f047f884dd55c7c9e350dba50 [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 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200451 * The format is the DER representation defined by RFC 5280 as
452 * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
453 * specified below.
454 * ```
455 * SubjectPublicKeyInfo ::= SEQUENCE {
456 * algorithm AlgorithmIdentifier,
457 * subjectPublicKey BIT STRING }
458 * AlgorithmIdentifier ::= SEQUENCE {
459 * algorithm OBJECT IDENTIFIER,
460 * parameters ANY DEFINED BY algorithm OPTIONAL }
461 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100462 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200463 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY),
464 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.1 as
465 * `RSAPublicKey`,
466 * with the OID `rsaEncryption`,
467 * and with the parameters `NULL`.
468 * ```
469 * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
470 * rsadsi(113549) pkcs(1) 1 }
471 * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 }
472 *
473 * RSAPublicKey ::= SEQUENCE {
474 * modulus INTEGER, -- n
475 * publicExponent INTEGER } -- e
476 * ```
477 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
478 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
479 * `DSAPublicKey`,
480 * with the OID `id-dsa`,
481 * and with the parameters `DSS-Parms`.
482 * ```
483 * id-dsa OBJECT IDENTIFIER ::= {
484 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
485 *
486 * Dss-Parms ::= SEQUENCE {
487 * p INTEGER,
488 * q INTEGER,
489 * g INTEGER }
490 * DSAPublicKey ::= INTEGER -- public key, Y
491 * ```
492 * - For elliptic curve public keys (key types for which
493 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true),
494 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.5 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200495 * `ECPoint`, which contains the uncompressed
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200496 * representation defined by SEC1 &sect;2.3.3.
497 * The OID is `id-ecPublicKey`,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200498 * and the parameters must be given as a `namedCurve` OID as specified in
Gilles Peskinec6290c02018-08-13 17:24:59 +0200499 * RFC 5480 &sect;2.1.1.1 or other applicable standards.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200500 * ```
501 * ansi-X9-62 OBJECT IDENTIFIER ::=
502 * { iso(1) member-body(2) us(840) 10045 }
503 * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 }
504 * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 }
505 *
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200506 * ECPoint ::= ...
507 * -- first 8 bits: 0x04;
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100508 * -- then x_P as a `ceiling(m/8)`-byte string, big endian;
509 * -- then y_P as a `ceiling(m/8)`-byte string, big endian;
510 * -- where `m` is the bit size associated with the curve,
Gilles Peskine7b5b4a02018-11-14 21:05:10 +0100511 * -- i.e. the bit size of `q` for a curve over `F_q`.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200512 *
513 * EcpkParameters ::= CHOICE { -- other choices are not allowed
514 * namedCurve OBJECT IDENTIFIER }
515 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100516 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100517 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200518 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200519 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200520 * \param[out] data_length On success, the number of bytes
521 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100522 *
Gilles Peskine28538492018-07-11 17:34:00 +0200523 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100524 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200525 * \retval #PSA_ERROR_EMPTY_SLOT
526 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200527 * The key is neither a public key nor a key pair.
528 * \retval #PSA_ERROR_NOT_SUPPORTED
529 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
530 * The size of the \p data buffer is too small. You can determine a
531 * sufficient buffer size by calling
532 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
533 * where \c type is the key type
534 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200535 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
536 * \retval #PSA_ERROR_HARDWARE_FAILURE
537 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300538 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300539 * The library has not been previously initialized by psa_crypto_init().
540 * It is implementation-dependent whether a failure to initialize
541 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100542 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100543psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100544 uint8_t *data,
545 size_t data_size,
546 size_t *data_length);
547
548/**@}*/
549
550/** \defgroup policy Key policies
551 * @{
552 */
553
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100554/** The type of the key policy data structure.
555 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000556 * Before calling any function on a key policy, the application must initialize
557 * it by any of the following means:
558 * - Set the structure to all-bits-zero, for example:
559 * \code
560 * psa_key_policy_t policy;
561 * memset(&policy, 0, sizeof(policy));
562 * \endcode
563 * - Initialize the structure to logical zero values, for example:
564 * \code
565 * psa_key_policy_t policy = {0};
566 * \endcode
567 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
568 * for example:
569 * \code
570 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
571 * \endcode
572 * - Assign the result of the function psa_key_policy_init()
573 * to the structure, for example:
574 * \code
575 * psa_key_policy_t policy;
576 * policy = psa_key_policy_init();
577 * \endcode
578 *
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100579 * This is an implementation-defined \c struct. Applications should not
580 * make any assumptions about the content of this structure except
581 * as directed by the documentation of a specific implementation. */
582typedef struct psa_key_policy_s psa_key_policy_t;
583
Jaeden Amero70261c52019-01-04 11:47:20 +0000584/** \def PSA_KEY_POLICY_INIT
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200585 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000586 * This macro returns a suitable initializer for a key policy object of type
587 * #psa_key_policy_t.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200588 */
Jaeden Amero70261c52019-01-04 11:47:20 +0000589#ifdef __DOXYGEN_ONLY__
590/* This is an example definition for documentation purposes.
591 * Implementations should define a suitable value in `crypto_struct.h`.
592 */
593#define PSA_KEY_POLICY_INIT {0}
594#endif
595
596/** Return an initial value for a key policy that forbids all usage of the key.
597 */
598static psa_key_policy_t psa_key_policy_init(void);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100599
Gilles Peskine7e198532018-03-08 07:50:30 +0100600/** \brief Set the standard fields of a policy structure.
601 *
602 * Note that this function does not make any consistency check of the
603 * parameters. The values are only checked when applying the policy to
604 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200605 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000606 * \param[in,out] policy The key policy to modify. It must have been
607 * initialized as per the documentation for
608 * #psa_key_policy_t.
609 * \param usage The permitted uses for the key.
610 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100611 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100612void psa_key_policy_set_usage(psa_key_policy_t *policy,
613 psa_key_usage_t usage,
614 psa_algorithm_t alg);
615
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200616/** \brief Retrieve the usage field of a policy structure.
617 *
618 * \param[in] policy The policy object to query.
619 *
620 * \return The permitted uses for a key with this policy.
621 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200622psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100623
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200624/** \brief Retrieve the algorithm field of a policy structure.
625 *
626 * \param[in] policy The policy object to query.
627 *
628 * \return The permitted algorithm for a key with this policy.
629 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200630psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100631
632/** \brief Set the usage policy on a key slot.
633 *
634 * This function must be called on an empty key slot, before importing,
635 * generating or creating a key in the slot. Changing the policy of an
636 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100637 *
638 * Implementations may set restrictions on supported key policies
639 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200640 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100641 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200642 * \param[in] policy The policy object to query.
643 *
644 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100645 * Success.
646 * If the key is persistent, it is implementation-defined whether
647 * the policy has been saved to persistent storage. Implementations
648 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100649 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200650 * \retval #PSA_ERROR_OCCUPIED_SLOT
651 * \retval #PSA_ERROR_NOT_SUPPORTED
652 * \retval #PSA_ERROR_INVALID_ARGUMENT
653 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
654 * \retval #PSA_ERROR_HARDWARE_FAILURE
655 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300656 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300657 * The library has not been previously initialized by psa_crypto_init().
658 * It is implementation-dependent whether a failure to initialize
659 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100660 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100661psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100662 const psa_key_policy_t *policy);
663
Gilles Peskine7e198532018-03-08 07:50:30 +0100664/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200665 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100666 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200667 * \param[out] policy On success, the key's policy.
668 *
669 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100670 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200671 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
672 * \retval #PSA_ERROR_HARDWARE_FAILURE
673 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300674 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300675 * The library has not been previously initialized by psa_crypto_init().
676 * It is implementation-dependent whether a failure to initialize
677 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100678 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100679psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100680 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100681
682/**@}*/
683
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100684/** \defgroup hash Message digests
685 * @{
686 */
687
Gilles Peskine308b91d2018-02-08 09:47:44 +0100688/** The type of the state data structure for multipart hash operations.
689 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000690 * Before calling any function on a hash operation object, the application must
691 * initialize it by any of the following means:
692 * - Set the structure to all-bits-zero, for example:
693 * \code
694 * psa_hash_operation_t operation;
695 * memset(&operation, 0, sizeof(operation));
696 * \endcode
697 * - Initialize the structure to logical zero values, for example:
698 * \code
699 * psa_hash_operation_t operation = {0};
700 * \endcode
701 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
702 * for example:
703 * \code
704 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
705 * \endcode
706 * - Assign the result of the function psa_hash_operation_init()
707 * to the structure, for example:
708 * \code
709 * psa_hash_operation_t operation;
710 * operation = psa_hash_operation_init();
711 * \endcode
712 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100713 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100714 * make any assumptions about the content of this structure except
715 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100716typedef struct psa_hash_operation_s psa_hash_operation_t;
717
Jaeden Amero6a25b412019-01-04 11:47:44 +0000718/** \def PSA_HASH_OPERATION_INIT
719 *
720 * This macro returns a suitable initializer for a hash operation object
721 * of type #psa_hash_operation_t.
722 */
723#ifdef __DOXYGEN_ONLY__
724/* This is an example definition for documentation purposes.
725 * Implementations should define a suitable value in `crypto_struct.h`.
726 */
727#define PSA_HASH_OPERATION_INIT {0}
728#endif
729
730/** Return an initial value for a hash operation object.
731 */
732static psa_hash_operation_t psa_hash_operation_init(void);
733
Gilles Peskine308b91d2018-02-08 09:47:44 +0100734/** Start a multipart hash operation.
735 *
736 * The sequence of operations to calculate a hash (message digest)
737 * is as follows:
738 * -# Allocate an operation object which will be passed to all the functions
739 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000740 * -# Initialize the operation object with one of the methods described in the
741 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200742 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100743 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100744 * of the message each time. The hash that is calculated is the hash
745 * of the concatenation of these messages in order.
746 * -# To calculate the hash, call psa_hash_finish().
747 * To compare the hash with an expected value, call psa_hash_verify().
748 *
749 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000750 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100751 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200752 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100753 * eventually terminate the operation. The following events terminate an
754 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100755 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100756 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100757 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000758 * \param[in,out] operation The operation object to set up. It must have
759 * been initialized as per the documentation for
760 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200761 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
762 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100763 *
Gilles Peskine28538492018-07-11 17:34:00 +0200764 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100765 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200766 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200767 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200768 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
769 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
770 * \retval #PSA_ERROR_HARDWARE_FAILURE
771 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100772 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200773psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100774 psa_algorithm_t alg);
775
Gilles Peskine308b91d2018-02-08 09:47:44 +0100776/** Add a message fragment to a multipart hash operation.
777 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200778 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100779 *
780 * If this function returns an error status, the operation becomes inactive.
781 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200782 * \param[in,out] operation Active hash operation.
783 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200784 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100785 *
Gilles Peskine28538492018-07-11 17:34:00 +0200786 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100787 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200788 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100789 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200790 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
791 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
792 * \retval #PSA_ERROR_HARDWARE_FAILURE
793 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100794 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100795psa_status_t psa_hash_update(psa_hash_operation_t *operation,
796 const uint8_t *input,
797 size_t input_length);
798
Gilles Peskine308b91d2018-02-08 09:47:44 +0100799/** Finish the calculation of the hash of a message.
800 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200801 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100802 * This function calculates the hash of the message formed by concatenating
803 * the inputs passed to preceding calls to psa_hash_update().
804 *
805 * When this function returns, the operation becomes inactive.
806 *
807 * \warning Applications should not call this function if they expect
808 * a specific value for the hash. Call psa_hash_verify() instead.
809 * Beware that comparing integrity or authenticity data such as
810 * hash values with a function such as \c memcmp is risky
811 * because the time taken by the comparison may leak information
812 * about the hashed data which could allow an attacker to guess
813 * a valid hash and thereby bypass security controls.
814 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200815 * \param[in,out] operation Active hash operation.
816 * \param[out] hash Buffer where the hash is to be written.
817 * \param hash_size Size of the \p hash buffer in bytes.
818 * \param[out] hash_length On success, the number of bytes
819 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200820 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200821 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100822 *
Gilles Peskine28538492018-07-11 17:34:00 +0200823 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100824 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200825 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100826 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200827 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200828 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200829 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100830 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200831 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
832 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
833 * \retval #PSA_ERROR_HARDWARE_FAILURE
834 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100835 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100836psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
837 uint8_t *hash,
838 size_t hash_size,
839 size_t *hash_length);
840
Gilles Peskine308b91d2018-02-08 09:47:44 +0100841/** Finish the calculation of the hash of a message and compare it with
842 * an expected value.
843 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200844 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100845 * This function calculates the hash of the message formed by concatenating
846 * the inputs passed to preceding calls to psa_hash_update(). It then
847 * compares the calculated hash with the expected hash passed as a
848 * parameter to this function.
849 *
850 * When this function returns, the operation becomes inactive.
851 *
Gilles Peskine19067982018-03-20 17:54:53 +0100852 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100853 * comparison between the actual hash and the expected hash is performed
854 * in constant time.
855 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200856 * \param[in,out] operation Active hash operation.
857 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200858 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100859 *
Gilles Peskine28538492018-07-11 17:34:00 +0200860 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100861 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200862 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100863 * The hash of the message was calculated successfully, but it
864 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200865 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100866 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200867 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
868 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
869 * \retval #PSA_ERROR_HARDWARE_FAILURE
870 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100871 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100872psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
873 const uint8_t *hash,
874 size_t hash_length);
875
Gilles Peskine308b91d2018-02-08 09:47:44 +0100876/** Abort a hash operation.
877 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100878 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200879 * \p operation structure itself. Once aborted, the operation object
880 * can be reused for another operation by calling
881 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100882 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200883 * You may call this function any time after the operation object has
884 * been initialized by any of the following methods:
885 * - A call to psa_hash_setup(), whether it succeeds or not.
886 * - Initializing the \c struct to all-bits-zero.
887 * - Initializing the \c struct to logical zeros, e.g.
888 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100889 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200890 * In particular, calling psa_hash_abort() after the operation has been
891 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
892 * psa_hash_verify() is safe and has no effect.
893 *
894 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100895 *
Gilles Peskine28538492018-07-11 17:34:00 +0200896 * \retval #PSA_SUCCESS
897 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200898 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +0200899 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
900 * \retval #PSA_ERROR_HARDWARE_FAILURE
901 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100902 */
903psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100904
905/**@}*/
906
Gilles Peskine8c9def32018-02-08 10:02:12 +0100907/** \defgroup MAC Message authentication codes
908 * @{
909 */
910
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100911/** The type of the state data structure for multipart MAC operations.
912 *
Jaeden Amero769ce272019-01-04 11:48:03 +0000913 * Before calling any function on a MAC operation object, the application must
914 * initialize it by any of the following means:
915 * - Set the structure to all-bits-zero, for example:
916 * \code
917 * psa_mac_operation_t operation;
918 * memset(&operation, 0, sizeof(operation));
919 * \endcode
920 * - Initialize the structure to logical zero values, for example:
921 * \code
922 * psa_mac_operation_t operation = {0};
923 * \endcode
924 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
925 * for example:
926 * \code
927 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
928 * \endcode
929 * - Assign the result of the function psa_mac_operation_init()
930 * to the structure, for example:
931 * \code
932 * psa_mac_operation_t operation;
933 * operation = psa_mac_operation_init();
934 * \endcode
935 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100936 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100937 * make any assumptions about the content of this structure except
938 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100939typedef struct psa_mac_operation_s psa_mac_operation_t;
940
Jaeden Amero769ce272019-01-04 11:48:03 +0000941/** \def PSA_MAC_OPERATION_INIT
942 *
943 * This macro returns a suitable initializer for a MAC operation object of type
944 * #psa_mac_operation_t.
945 */
946#ifdef __DOXYGEN_ONLY__
947/* This is an example definition for documentation purposes.
948 * Implementations should define a suitable value in `crypto_struct.h`.
949 */
950#define PSA_MAC_OPERATION_INIT {0}
951#endif
952
953/** Return an initial value for a MAC operation object.
954 */
955static psa_mac_operation_t psa_mac_operation_init(void);
956
Gilles Peskine89167cb2018-07-08 20:12:23 +0200957/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100958 *
Gilles Peskine89167cb2018-07-08 20:12:23 +0200959 * This function sets up the calculation of the MAC
960 * (message authentication code) of a byte string.
961 * To verify the MAC of a message against an
962 * expected value, use psa_mac_verify_setup() instead.
963 *
964 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100965 * -# Allocate an operation object which will be passed to all the functions
966 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +0000967 * -# Initialize the operation object with one of the methods described in the
968 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200969 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100970 * The key remains associated with the operation even if the content
971 * of the key slot changes.
972 * -# Call psa_mac_update() zero, one or more times, passing a fragment
973 * of the message each time. The MAC that is calculated is the MAC
974 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200975 * -# At the end of the message, call psa_mac_sign_finish() to finish
976 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100977 *
978 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +0000979 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100980 *
Gilles Peskine89167cb2018-07-08 20:12:23 +0200981 * After a successful call to psa_mac_sign_setup(), the application must
982 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100983 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +0200984 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100985 *
Jaeden Amero769ce272019-01-04 11:48:03 +0000986 * \param[in,out] operation The operation object to set up. It must have
987 * been initialized as per the documentation for
988 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100989 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200990 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
991 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100992 *
Gilles Peskine28538492018-07-11 17:34:00 +0200993 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100994 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100995 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200996 * \retval #PSA_ERROR_EMPTY_SLOT
997 * \retval #PSA_ERROR_NOT_PERMITTED
998 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200999 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001000 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001001 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001002 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1003 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1004 * \retval #PSA_ERROR_HARDWARE_FAILURE
1005 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001006 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001007 * The library has not been previously initialized by psa_crypto_init().
1008 * It is implementation-dependent whether a failure to initialize
1009 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001010 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001011psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001012 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001013 psa_algorithm_t alg);
1014
1015/** Start a multipart MAC verification operation.
1016 *
1017 * This function sets up the verification of the MAC
1018 * (message authentication code) of a byte string against an expected value.
1019 *
1020 * The sequence of operations to verify a MAC is as follows:
1021 * -# Allocate an operation object which will be passed to all the functions
1022 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001023 * -# Initialize the operation object with one of the methods described in the
1024 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001025 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1026 * The key remains associated with the operation even if the content
1027 * of the key slot changes.
1028 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1029 * of the message each time. The MAC that is calculated is the MAC
1030 * of the concatenation of these messages in order.
1031 * -# At the end of the message, call psa_mac_verify_finish() to finish
1032 * calculating the actual MAC of the message and verify it against
1033 * the expected value.
1034 *
1035 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001036 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001037 *
1038 * After a successful call to psa_mac_verify_setup(), the application must
1039 * eventually terminate the operation through one of the following methods:
1040 * - A failed call to psa_mac_update().
1041 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1042 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001043 * \param[in,out] operation The operation object to set up. It must have
1044 * been initialized as per the documentation for
1045 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001046 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001047 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1048 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001049 *
Gilles Peskine28538492018-07-11 17:34:00 +02001050 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001051 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001052 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001053 * \retval #PSA_ERROR_EMPTY_SLOT
1054 * \retval #PSA_ERROR_NOT_PERMITTED
1055 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001056 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001057 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001058 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001059 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1060 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1061 * \retval #PSA_ERROR_HARDWARE_FAILURE
1062 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001063 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001064 * The library has not been previously initialized by psa_crypto_init().
1065 * It is implementation-dependent whether a failure to initialize
1066 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001067 */
1068psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001069 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001070 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001071
Gilles Peskinedcd14942018-07-12 00:30:52 +02001072/** Add a message fragment to a multipart MAC operation.
1073 *
1074 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1075 * before calling this function.
1076 *
1077 * If this function returns an error status, the operation becomes inactive.
1078 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001079 * \param[in,out] operation Active MAC operation.
1080 * \param[in] input Buffer containing the message fragment to add to
1081 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001082 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001083 *
1084 * \retval #PSA_SUCCESS
1085 * Success.
1086 * \retval #PSA_ERROR_BAD_STATE
1087 * The operation state is not valid (not started, or already completed).
1088 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1089 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1090 * \retval #PSA_ERROR_HARDWARE_FAILURE
1091 * \retval #PSA_ERROR_TAMPERING_DETECTED
1092 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001093psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1094 const uint8_t *input,
1095 size_t input_length);
1096
Gilles Peskinedcd14942018-07-12 00:30:52 +02001097/** Finish the calculation of the MAC of a message.
1098 *
1099 * The application must call psa_mac_sign_setup() before calling this function.
1100 * This function calculates the MAC of the message formed by concatenating
1101 * the inputs passed to preceding calls to psa_mac_update().
1102 *
1103 * When this function returns, the operation becomes inactive.
1104 *
1105 * \warning Applications should not call this function if they expect
1106 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1107 * Beware that comparing integrity or authenticity data such as
1108 * MAC values with a function such as \c memcmp is risky
1109 * because the time taken by the comparison may leak information
1110 * about the MAC value which could allow an attacker to guess
1111 * a valid MAC and thereby bypass security controls.
1112 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001113 * \param[in,out] operation Active MAC operation.
1114 * \param[out] mac Buffer where the MAC value is to be written.
1115 * \param mac_size Size of the \p mac buffer in bytes.
1116 * \param[out] mac_length On success, the number of bytes
1117 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001118 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001119 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001120 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001121 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001122 *
1123 * \retval #PSA_SUCCESS
1124 * Success.
1125 * \retval #PSA_ERROR_BAD_STATE
1126 * The operation state is not valid (not started, or already completed).
1127 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001128 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001129 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1130 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1131 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1132 * \retval #PSA_ERROR_HARDWARE_FAILURE
1133 * \retval #PSA_ERROR_TAMPERING_DETECTED
1134 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001135psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1136 uint8_t *mac,
1137 size_t mac_size,
1138 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001139
Gilles Peskinedcd14942018-07-12 00:30:52 +02001140/** Finish the calculation of the MAC of a message and compare it with
1141 * an expected value.
1142 *
1143 * The application must call psa_mac_verify_setup() before calling this function.
1144 * This function calculates the MAC of the message formed by concatenating
1145 * the inputs passed to preceding calls to psa_mac_update(). It then
1146 * compares the calculated MAC with the expected MAC passed as a
1147 * parameter to this function.
1148 *
1149 * When this function returns, the operation becomes inactive.
1150 *
1151 * \note Implementations shall make the best effort to ensure that the
1152 * comparison between the actual MAC and the expected MAC is performed
1153 * in constant time.
1154 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001155 * \param[in,out] operation Active MAC operation.
1156 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001157 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001158 *
1159 * \retval #PSA_SUCCESS
1160 * The expected MAC is identical to the actual MAC of the message.
1161 * \retval #PSA_ERROR_INVALID_SIGNATURE
1162 * The MAC of the message was calculated successfully, but it
1163 * differs from the expected MAC.
1164 * \retval #PSA_ERROR_BAD_STATE
1165 * The operation state is not valid (not started, or already completed).
1166 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1167 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1168 * \retval #PSA_ERROR_HARDWARE_FAILURE
1169 * \retval #PSA_ERROR_TAMPERING_DETECTED
1170 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001171psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1172 const uint8_t *mac,
1173 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001174
Gilles Peskinedcd14942018-07-12 00:30:52 +02001175/** Abort a MAC operation.
1176 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001177 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001178 * \p operation structure itself. Once aborted, the operation object
1179 * can be reused for another operation by calling
1180 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001181 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001182 * You may call this function any time after the operation object has
1183 * been initialized by any of the following methods:
1184 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1185 * it succeeds or not.
1186 * - Initializing the \c struct to all-bits-zero.
1187 * - Initializing the \c struct to logical zeros, e.g.
1188 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001189 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001190 * In particular, calling psa_mac_abort() after the operation has been
1191 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1192 * psa_mac_verify_finish() is safe and has no effect.
1193 *
1194 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001195 *
1196 * \retval #PSA_SUCCESS
1197 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001198 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001199 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1200 * \retval #PSA_ERROR_HARDWARE_FAILURE
1201 * \retval #PSA_ERROR_TAMPERING_DETECTED
1202 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001203psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1204
1205/**@}*/
1206
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001207/** \defgroup cipher Symmetric ciphers
1208 * @{
1209 */
1210
1211/** The type of the state data structure for multipart cipher operations.
1212 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001213 * Before calling any function on a cipher operation object, the application
1214 * must initialize it by any of the following means:
1215 * - Set the structure to all-bits-zero, for example:
1216 * \code
1217 * psa_cipher_operation_t operation;
1218 * memset(&operation, 0, sizeof(operation));
1219 * \endcode
1220 * - Initialize the structure to logical zero values, for example:
1221 * \code
1222 * psa_cipher_operation_t operation = {0};
1223 * \endcode
1224 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1225 * for example:
1226 * \code
1227 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1228 * \endcode
1229 * - Assign the result of the function psa_cipher_operation_init()
1230 * to the structure, for example:
1231 * \code
1232 * psa_cipher_operation_t operation;
1233 * operation = psa_cipher_operation_init();
1234 * \endcode
1235 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001236 * This is an implementation-defined \c struct. Applications should not
1237 * make any assumptions about the content of this structure except
1238 * as directed by the documentation of a specific implementation. */
1239typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1240
Jaeden Amero5bae2272019-01-04 11:48:27 +00001241/** \def PSA_CIPHER_OPERATION_INIT
1242 *
1243 * This macro returns a suitable initializer for a cipher operation object of
1244 * type #psa_cipher_operation_t.
1245 */
1246#ifdef __DOXYGEN_ONLY__
1247/* This is an example definition for documentation purposes.
1248 * Implementations should define a suitable value in `crypto_struct.h`.
1249 */
1250#define PSA_CIPHER_OPERATION_INIT {0}
1251#endif
1252
1253/** Return an initial value for a cipher operation object.
1254 */
1255static psa_cipher_operation_t psa_cipher_operation_init(void);
1256
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001257/** Set the key for a multipart symmetric encryption operation.
1258 *
1259 * The sequence of operations to encrypt a message with a symmetric cipher
1260 * is as follows:
1261 * -# Allocate an operation object which will be passed to all the functions
1262 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001263 * -# Initialize the operation object with one of the methods described in the
1264 * documentation for #psa_cipher_operation_t, e.g.
1265 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001266 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001267 * The key remains associated with the operation even if the content
1268 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03001269 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001270 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001271 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001272 * requires a specific IV value.
1273 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1274 * of the message each time.
1275 * -# Call psa_cipher_finish().
1276 *
1277 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001278 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001279 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001280 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001281 * eventually terminate the operation. The following events terminate an
1282 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03001283 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001284 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001285 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001286 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001287 * \param[in,out] operation The operation object to set up. It must have
1288 * been initialized as per the documentation for
1289 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001290 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001291 * \param alg The cipher algorithm to compute
1292 * (\c PSA_ALG_XXX value such that
1293 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001294 *
Gilles Peskine28538492018-07-11 17:34:00 +02001295 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001296 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001297 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001298 * \retval #PSA_ERROR_EMPTY_SLOT
1299 * \retval #PSA_ERROR_NOT_PERMITTED
1300 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001301 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001302 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001303 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001304 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1305 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1306 * \retval #PSA_ERROR_HARDWARE_FAILURE
1307 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001308 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001309 * The library has not been previously initialized by psa_crypto_init().
1310 * It is implementation-dependent whether a failure to initialize
1311 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001312 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001313psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001314 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001315 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001316
1317/** Set the key for a multipart symmetric decryption operation.
1318 *
1319 * The sequence of operations to decrypt a message with a symmetric cipher
1320 * is as follows:
1321 * -# Allocate an operation object which will be passed to all the functions
1322 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001323 * -# Initialize the operation object with one of the methods described in the
1324 * documentation for #psa_cipher_operation_t, e.g.
1325 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001326 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001327 * The key remains associated with the operation even if the content
1328 * of the key slot changes.
1329 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1330 * decryption. If the IV is prepended to the ciphertext, you can call
1331 * psa_cipher_update() on a buffer containing the IV followed by the
1332 * beginning of the message.
1333 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1334 * of the message each time.
1335 * -# Call psa_cipher_finish().
1336 *
1337 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001338 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001339 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001340 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001341 * eventually terminate the operation. The following events terminate an
1342 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001343 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001344 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001345 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001346 * \param[in,out] operation The operation object to set up. It must have
1347 * been initialized as per the documentation for
1348 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001349 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001350 * \param alg The cipher algorithm to compute
1351 * (\c PSA_ALG_XXX value such that
1352 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001353 *
Gilles Peskine28538492018-07-11 17:34:00 +02001354 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001355 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001356 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001357 * \retval #PSA_ERROR_EMPTY_SLOT
1358 * \retval #PSA_ERROR_NOT_PERMITTED
1359 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001360 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001361 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001362 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001363 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1364 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1365 * \retval #PSA_ERROR_HARDWARE_FAILURE
1366 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001367 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001368 * The library has not been previously initialized by psa_crypto_init().
1369 * It is implementation-dependent whether a failure to initialize
1370 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001371 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001372psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001373 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001374 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001375
Gilles Peskinedcd14942018-07-12 00:30:52 +02001376/** Generate an IV for a symmetric encryption operation.
1377 *
1378 * This function generates a random IV (initialization vector), nonce
1379 * or initial counter value for the encryption operation as appropriate
1380 * for the chosen algorithm, key type and key size.
1381 *
1382 * The application must call psa_cipher_encrypt_setup() before
1383 * calling this function.
1384 *
1385 * If this function returns an error status, the operation becomes inactive.
1386 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001387 * \param[in,out] operation Active cipher operation.
1388 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001389 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001390 * \param[out] iv_length On success, the number of bytes of the
1391 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001392 *
1393 * \retval #PSA_SUCCESS
1394 * Success.
1395 * \retval #PSA_ERROR_BAD_STATE
1396 * The operation state is not valid (not started, or IV already set).
1397 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001398 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001399 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1400 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1401 * \retval #PSA_ERROR_HARDWARE_FAILURE
1402 * \retval #PSA_ERROR_TAMPERING_DETECTED
1403 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001404psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1405 unsigned char *iv,
1406 size_t iv_size,
1407 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001408
Gilles Peskinedcd14942018-07-12 00:30:52 +02001409/** Set the IV for a symmetric encryption or decryption operation.
1410 *
1411 * This function sets the random IV (initialization vector), nonce
1412 * or initial counter value for the encryption or decryption operation.
1413 *
1414 * The application must call psa_cipher_encrypt_setup() before
1415 * calling this function.
1416 *
1417 * If this function returns an error status, the operation becomes inactive.
1418 *
1419 * \note When encrypting, applications should use psa_cipher_generate_iv()
1420 * instead of this function, unless implementing a protocol that requires
1421 * a non-random IV.
1422 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001423 * \param[in,out] operation Active cipher operation.
1424 * \param[in] iv Buffer containing the IV to use.
1425 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001426 *
1427 * \retval #PSA_SUCCESS
1428 * Success.
1429 * \retval #PSA_ERROR_BAD_STATE
1430 * The operation state is not valid (not started, or IV already set).
1431 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001432 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001433 * or the chosen algorithm does not use an IV.
1434 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1435 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1436 * \retval #PSA_ERROR_HARDWARE_FAILURE
1437 * \retval #PSA_ERROR_TAMPERING_DETECTED
1438 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001439psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1440 const unsigned char *iv,
1441 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001442
Gilles Peskinedcd14942018-07-12 00:30:52 +02001443/** Encrypt or decrypt a message fragment in an active cipher operation.
1444 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001445 * Before calling this function, you must:
1446 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1447 * The choice of setup function determines whether this function
1448 * encrypts or decrypts its input.
1449 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1450 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001451 *
1452 * If this function returns an error status, the operation becomes inactive.
1453 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001454 * \param[in,out] operation Active cipher operation.
1455 * \param[in] input Buffer containing the message fragment to
1456 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001457 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001458 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001459 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001460 * \param[out] output_length On success, the number of bytes
1461 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001462 *
1463 * \retval #PSA_SUCCESS
1464 * Success.
1465 * \retval #PSA_ERROR_BAD_STATE
1466 * The operation state is not valid (not started, IV required but
1467 * not set, or already completed).
1468 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1469 * The size of the \p output buffer is too small.
1470 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1471 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1472 * \retval #PSA_ERROR_HARDWARE_FAILURE
1473 * \retval #PSA_ERROR_TAMPERING_DETECTED
1474 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001475psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1476 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001477 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001478 unsigned char *output,
1479 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001480 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001481
Gilles Peskinedcd14942018-07-12 00:30:52 +02001482/** Finish encrypting or decrypting a message in a cipher operation.
1483 *
1484 * The application must call psa_cipher_encrypt_setup() or
1485 * psa_cipher_decrypt_setup() before calling this function. The choice
1486 * of setup function determines whether this function encrypts or
1487 * decrypts its input.
1488 *
1489 * This function finishes the encryption or decryption of the message
1490 * formed by concatenating the inputs passed to preceding calls to
1491 * psa_cipher_update().
1492 *
1493 * When this function returns, the operation becomes inactive.
1494 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001495 * \param[in,out] operation Active cipher operation.
1496 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001497 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001498 * \param[out] output_length On success, the number of bytes
1499 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001500 *
1501 * \retval #PSA_SUCCESS
1502 * Success.
1503 * \retval #PSA_ERROR_BAD_STATE
1504 * The operation state is not valid (not started, IV required but
1505 * not set, or already completed).
1506 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1507 * The size of the \p output buffer is too small.
1508 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1509 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1510 * \retval #PSA_ERROR_HARDWARE_FAILURE
1511 * \retval #PSA_ERROR_TAMPERING_DETECTED
1512 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001513psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001514 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001515 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001516 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001517
Gilles Peskinedcd14942018-07-12 00:30:52 +02001518/** Abort a cipher operation.
1519 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001520 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001521 * \p operation structure itself. Once aborted, the operation object
1522 * can be reused for another operation by calling
1523 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001524 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001525 * You may call this function any time after the operation object has
1526 * been initialized by any of the following methods:
1527 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1528 * whether it succeeds or not.
1529 * - Initializing the \c struct to all-bits-zero.
1530 * - Initializing the \c struct to logical zeros, e.g.
1531 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001532 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001533 * In particular, calling psa_cipher_abort() after the operation has been
1534 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1535 * is safe and has no effect.
1536 *
1537 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001538 *
1539 * \retval #PSA_SUCCESS
1540 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001541 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001542 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1543 * \retval #PSA_ERROR_HARDWARE_FAILURE
1544 * \retval #PSA_ERROR_TAMPERING_DETECTED
1545 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001546psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1547
1548/**@}*/
1549
Gilles Peskine3b555712018-03-03 21:27:57 +01001550/** \defgroup aead Authenticated encryption with associated data (AEAD)
1551 * @{
1552 */
1553
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001554/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001555 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001556 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001557 * \param alg The AEAD algorithm to compute
1558 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001559 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001560 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001561 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001562 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001563 * but not encrypted.
1564 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001565 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001566 * encrypted.
1567 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001568 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001569 * encrypted data. The additional data is not
1570 * part of this output. For algorithms where the
1571 * encrypted data and the authentication tag
1572 * are defined as separate outputs, the
1573 * authentication tag is appended to the
1574 * encrypted data.
1575 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1576 * This must be at least
1577 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1578 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001579 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001580 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001581 *
Gilles Peskine28538492018-07-11 17:34:00 +02001582 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001583 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001584 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001585 * \retval #PSA_ERROR_EMPTY_SLOT
1586 * \retval #PSA_ERROR_NOT_PERMITTED
1587 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001588 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001589 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001590 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001591 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1592 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1593 * \retval #PSA_ERROR_HARDWARE_FAILURE
1594 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001595 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001596 * The library has not been previously initialized by psa_crypto_init().
1597 * It is implementation-dependent whether a failure to initialize
1598 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001599 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001600psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001601 psa_algorithm_t alg,
1602 const uint8_t *nonce,
1603 size_t nonce_length,
1604 const uint8_t *additional_data,
1605 size_t additional_data_length,
1606 const uint8_t *plaintext,
1607 size_t plaintext_length,
1608 uint8_t *ciphertext,
1609 size_t ciphertext_size,
1610 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001611
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001612/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001613 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001614 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001615 * \param alg The AEAD algorithm to compute
1616 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001617 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001618 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001619 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001620 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001621 * but not encrypted.
1622 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001623 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001624 * encrypted. For algorithms where the
1625 * encrypted data and the authentication tag
1626 * are defined as separate inputs, the buffer
1627 * must contain the encrypted data followed
1628 * by the authentication tag.
1629 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001630 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001631 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1632 * This must be at least
1633 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1634 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001635 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001636 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001637 *
Gilles Peskine28538492018-07-11 17:34:00 +02001638 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001639 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001640 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001641 * \retval #PSA_ERROR_EMPTY_SLOT
1642 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001643 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001644 * \retval #PSA_ERROR_NOT_PERMITTED
1645 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001646 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001647 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001648 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001649 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1650 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1651 * \retval #PSA_ERROR_HARDWARE_FAILURE
1652 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001653 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001654 * The library has not been previously initialized by psa_crypto_init().
1655 * It is implementation-dependent whether a failure to initialize
1656 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001657 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001658psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001659 psa_algorithm_t alg,
1660 const uint8_t *nonce,
1661 size_t nonce_length,
1662 const uint8_t *additional_data,
1663 size_t additional_data_length,
1664 const uint8_t *ciphertext,
1665 size_t ciphertext_length,
1666 uint8_t *plaintext,
1667 size_t plaintext_size,
1668 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001669
1670/**@}*/
1671
Gilles Peskine20035e32018-02-03 22:44:14 +01001672/** \defgroup asymmetric Asymmetric cryptography
1673 * @{
1674 */
1675
1676/**
1677 * \brief Sign a hash or short message with a private key.
1678 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001679 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001680 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001681 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1682 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1683 * to determine the hash algorithm to use.
1684 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001685 * \param handle Handle to the key to use for the operation.
1686 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001687 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001688 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001689 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001690 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001691 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001692 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001693 * \param[out] signature_length On success, the number of bytes
1694 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001695 *
Gilles Peskine28538492018-07-11 17:34:00 +02001696 * \retval #PSA_SUCCESS
1697 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001698 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01001699 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001700 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001701 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001702 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001703 * \retval #PSA_ERROR_NOT_SUPPORTED
1704 * \retval #PSA_ERROR_INVALID_ARGUMENT
1705 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1706 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1707 * \retval #PSA_ERROR_HARDWARE_FAILURE
1708 * \retval #PSA_ERROR_TAMPERING_DETECTED
1709 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001710 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001711 * The library has not been previously initialized by psa_crypto_init().
1712 * It is implementation-dependent whether a failure to initialize
1713 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001714 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001715psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001716 psa_algorithm_t alg,
1717 const uint8_t *hash,
1718 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01001719 uint8_t *signature,
1720 size_t signature_size,
1721 size_t *signature_length);
1722
1723/**
1724 * \brief Verify the signature a hash or short message using a public key.
1725 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001726 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001727 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001728 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1729 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1730 * to determine the hash algorithm to use.
1731 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001732 * \param handle Handle to the key to use for the operation.
1733 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001734 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001735 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001736 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02001737 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001738 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001739 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001740 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001741 *
Gilles Peskine28538492018-07-11 17:34:00 +02001742 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001743 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02001744 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001745 * The calculation was perfomed successfully, but the passed
1746 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02001747 * \retval #PSA_ERROR_NOT_SUPPORTED
1748 * \retval #PSA_ERROR_INVALID_ARGUMENT
1749 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1750 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1751 * \retval #PSA_ERROR_HARDWARE_FAILURE
1752 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001753 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001754 * The library has not been previously initialized by psa_crypto_init().
1755 * It is implementation-dependent whether a failure to initialize
1756 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001757 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001758psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001759 psa_algorithm_t alg,
1760 const uint8_t *hash,
1761 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001762 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001763 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01001764
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001765/**
1766 * \brief Encrypt a short message with a public key.
1767 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001768 * \param handle Handle to the key to use for the operation.
1769 * It must be a public key or an asymmetric
1770 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001771 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001772 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001773 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001774 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001775 * \param[in] salt A salt or label, if supported by the
1776 * encryption algorithm.
1777 * If the algorithm does not support a
1778 * salt, pass \c NULL.
1779 * If the algorithm supports an optional
1780 * salt and you do not want to pass a salt,
1781 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001782 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001783 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1784 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001785 * \param salt_length Size of the \p salt buffer in bytes.
1786 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001787 * \param[out] output Buffer where the encrypted message is to
1788 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001789 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001790 * \param[out] output_length On success, the number of bytes
1791 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001792 *
Gilles Peskine28538492018-07-11 17:34:00 +02001793 * \retval #PSA_SUCCESS
1794 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001795 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001796 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001797 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001798 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001799 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001800 * \retval #PSA_ERROR_NOT_SUPPORTED
1801 * \retval #PSA_ERROR_INVALID_ARGUMENT
1802 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1803 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1804 * \retval #PSA_ERROR_HARDWARE_FAILURE
1805 * \retval #PSA_ERROR_TAMPERING_DETECTED
1806 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001807 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001808 * The library has not been previously initialized by psa_crypto_init().
1809 * It is implementation-dependent whether a failure to initialize
1810 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001811 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001812psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001813 psa_algorithm_t alg,
1814 const uint8_t *input,
1815 size_t input_length,
1816 const uint8_t *salt,
1817 size_t salt_length,
1818 uint8_t *output,
1819 size_t output_size,
1820 size_t *output_length);
1821
1822/**
1823 * \brief Decrypt a short message with a private key.
1824 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001825 * \param handle Handle to the key to use for the operation.
1826 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001827 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001828 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001829 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001830 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001831 * \param[in] salt A salt or label, if supported by the
1832 * encryption algorithm.
1833 * If the algorithm does not support a
1834 * salt, pass \c NULL.
1835 * If the algorithm supports an optional
1836 * salt and you do not want to pass a salt,
1837 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001838 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001839 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1840 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001841 * \param salt_length Size of the \p salt buffer in bytes.
1842 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001843 * \param[out] output Buffer where the decrypted message is to
1844 * be written.
1845 * \param output_size Size of the \c output buffer in bytes.
1846 * \param[out] output_length On success, the number of bytes
1847 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001848 *
Gilles Peskine28538492018-07-11 17:34:00 +02001849 * \retval #PSA_SUCCESS
1850 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001851 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001852 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001853 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001854 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001855 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001856 * \retval #PSA_ERROR_NOT_SUPPORTED
1857 * \retval #PSA_ERROR_INVALID_ARGUMENT
1858 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1859 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1860 * \retval #PSA_ERROR_HARDWARE_FAILURE
1861 * \retval #PSA_ERROR_TAMPERING_DETECTED
1862 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1863 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03001864 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001865 * The library has not been previously initialized by psa_crypto_init().
1866 * It is implementation-dependent whether a failure to initialize
1867 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001868 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001869psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001870 psa_algorithm_t alg,
1871 const uint8_t *input,
1872 size_t input_length,
1873 const uint8_t *salt,
1874 size_t salt_length,
1875 uint8_t *output,
1876 size_t output_size,
1877 size_t *output_length);
1878
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001879/**@}*/
1880
Gilles Peskineedd76872018-07-20 17:42:05 +02001881/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02001882 * @{
1883 */
1884
1885/** The type of the state data structure for generators.
1886 *
1887 * Before calling any function on a generator, the application must
1888 * initialize it by any of the following means:
1889 * - Set the structure to all-bits-zero, for example:
1890 * \code
1891 * psa_crypto_generator_t generator;
1892 * memset(&generator, 0, sizeof(generator));
1893 * \endcode
1894 * - Initialize the structure to logical zero values, for example:
1895 * \code
1896 * psa_crypto_generator_t generator = {0};
1897 * \endcode
1898 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
1899 * for example:
1900 * \code
1901 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1902 * \endcode
1903 * - Assign the result of the function psa_crypto_generator_init()
1904 * to the structure, for example:
1905 * \code
1906 * psa_crypto_generator_t generator;
1907 * generator = psa_crypto_generator_init();
1908 * \endcode
1909 *
1910 * This is an implementation-defined \c struct. Applications should not
1911 * make any assumptions about the content of this structure except
1912 * as directed by the documentation of a specific implementation.
1913 */
1914typedef struct psa_crypto_generator_s psa_crypto_generator_t;
1915
1916/** \def PSA_CRYPTO_GENERATOR_INIT
1917 *
1918 * This macro returns a suitable initializer for a generator object
1919 * of type #psa_crypto_generator_t.
1920 */
1921#ifdef __DOXYGEN_ONLY__
1922/* This is an example definition for documentation purposes.
1923 * Implementations should define a suitable value in `crypto_struct.h`.
1924 */
1925#define PSA_CRYPTO_GENERATOR_INIT {0}
1926#endif
1927
1928/** Return an initial value for a generator object.
1929 */
1930static psa_crypto_generator_t psa_crypto_generator_init(void);
1931
1932/** Retrieve the current capacity of a generator.
1933 *
1934 * The capacity of a generator is the maximum number of bytes that it can
1935 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
1936 *
1937 * \param[in] generator The generator to query.
1938 * \param[out] capacity On success, the capacity of the generator.
1939 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001940 * \retval #PSA_SUCCESS
1941 * \retval #PSA_ERROR_BAD_STATE
1942 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02001943 */
1944psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
1945 size_t *capacity);
1946
1947/** Read some data from a generator.
1948 *
1949 * This function reads and returns a sequence of bytes from a generator.
1950 * The data that is read is discarded from the generator. The generator's
1951 * capacity is decreased by the number of bytes read.
1952 *
1953 * \param[in,out] generator The generator object to read from.
1954 * \param[out] output Buffer where the generator output will be
1955 * written.
1956 * \param output_length Number of bytes to output.
1957 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001958 * \retval #PSA_SUCCESS
1959 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02001960 * There were fewer than \p output_length bytes
1961 * in the generator. Note that in this case, no
1962 * output is written to the output buffer.
1963 * The generator's capacity is set to 0, thus
1964 * subsequent calls to this function will not
1965 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001966 * \retval #PSA_ERROR_BAD_STATE
1967 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1968 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1969 * \retval #PSA_ERROR_HARDWARE_FAILURE
1970 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02001971 */
1972psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
1973 uint8_t *output,
1974 size_t output_length);
1975
1976/** Create a symmetric key from data read from a generator.
1977 *
1978 * This function reads a sequence of bytes from a generator and imports
1979 * these bytes as a key.
1980 * The data that is read is discarded from the generator. The generator's
1981 * capacity is decreased by the number of bytes read.
1982 *
1983 * This function is equivalent to calling #psa_generator_read and
1984 * passing the resulting output to #psa_import_key, but
1985 * if the implementation provides an isolation boundary then
1986 * the key material is not exposed outside the isolation boundary.
1987 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001988 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001989 * It must have been obtained by calling
1990 * psa_allocate_key() or psa_create_key() and must
1991 * not contain key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02001992 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
1993 * This must be a symmetric key type.
1994 * \param bits Key size in bits.
1995 * \param[in,out] generator The generator object to read from.
1996 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001997 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02001998 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01001999 * If the key is persistent, the key material and the key's metadata
2000 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002001 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002002 * There were fewer than \p output_length bytes
2003 * in the generator. Note that in this case, no
2004 * output is written to the output buffer.
2005 * The generator's capacity is set to 0, thus
2006 * subsequent calls to this function will not
2007 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002008 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002009 * The key type or key size is not supported, either by the
2010 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002011 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002012 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002013 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002014 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002015 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2016 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2017 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2018 * \retval #PSA_ERROR_HARDWARE_FAILURE
2019 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002020 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002021 * The library has not been previously initialized by psa_crypto_init().
2022 * It is implementation-dependent whether a failure to initialize
2023 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002024 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002025psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002026 psa_key_type_t type,
2027 size_t bits,
2028 psa_crypto_generator_t *generator);
2029
2030/** Abort a generator.
2031 *
2032 * Once a generator has been aborted, its capacity is zero.
2033 * Aborting a generator frees all associated resources except for the
2034 * \c generator structure itself.
2035 *
2036 * This function may be called at any time as long as the generator
2037 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2038 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2039 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2040 * on a generator that has not been set up.
2041 *
2042 * Once aborted, the generator object may be called.
2043 *
2044 * \param[in,out] generator The generator to abort.
2045 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002046 * \retval #PSA_SUCCESS
2047 * \retval #PSA_ERROR_BAD_STATE
2048 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2049 * \retval #PSA_ERROR_HARDWARE_FAILURE
2050 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002051 */
2052psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2053
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002054/** Use the maximum possible capacity for a generator.
2055 *
2056 * Use this value as the capacity argument when setting up a generator
2057 * to indicate that the generator should have the maximum possible capacity.
2058 * The value of the maximum possible capacity depends on the generator
2059 * algorithm.
2060 */
2061#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2062
Gilles Peskineeab56e42018-07-12 17:12:33 +02002063/**@}*/
2064
Gilles Peskineea0fb492018-07-12 17:17:20 +02002065/** \defgroup derivation Key derivation
2066 * @{
2067 */
2068
2069/** Set up a key derivation operation.
2070 *
2071 * A key derivation algorithm takes three inputs: a secret input \p key and
2072 * two non-secret inputs \p label and p salt.
2073 * The result of this function is a byte generator which can
2074 * be used to produce keys and other cryptographic material.
2075 *
2076 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002077 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2078 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002079 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002080 * \param[in,out] generator The generator object to set up. It must have
2081 * been initialized as per the documentation for
2082 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002083 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002084 * \param alg The key derivation algorithm to compute
2085 * (\c PSA_ALG_XXX value such that
2086 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2087 * \param[in] salt Salt to use.
2088 * \param salt_length Size of the \p salt buffer in bytes.
2089 * \param[in] label Label to use.
2090 * \param label_length Size of the \p label buffer in bytes.
2091 * \param capacity The maximum number of bytes that the
2092 * generator will be able to provide.
2093 *
2094 * \retval #PSA_SUCCESS
2095 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002096 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02002097 * \retval #PSA_ERROR_EMPTY_SLOT
2098 * \retval #PSA_ERROR_NOT_PERMITTED
2099 * \retval #PSA_ERROR_INVALID_ARGUMENT
2100 * \c key is not compatible with \c alg,
2101 * or \p capacity is too large for the specified algorithm and key.
2102 * \retval #PSA_ERROR_NOT_SUPPORTED
2103 * \c alg is not supported or is not a key derivation algorithm.
2104 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2105 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2106 * \retval #PSA_ERROR_HARDWARE_FAILURE
2107 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002108 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002109 * The library has not been previously initialized by psa_crypto_init().
2110 * It is implementation-dependent whether a failure to initialize
2111 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002112 */
2113psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002114 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002115 psa_algorithm_t alg,
2116 const uint8_t *salt,
2117 size_t salt_length,
2118 const uint8_t *label,
2119 size_t label_length,
2120 size_t capacity);
2121
Gilles Peskine01d718c2018-09-18 12:01:02 +02002122/** Set up a key agreement operation.
2123 *
2124 * A key agreement algorithm takes two inputs: a private key \p private_key
2125 * a public key \p peer_key.
2126 * The result of this function is a byte generator which can
2127 * be used to produce keys and other cryptographic material.
2128 *
Gilles Peskine211a4362018-10-25 22:22:31 +02002129 * The resulting generator always has the maximum capacity permitted by
2130 * the algorithm.
2131 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002132 * \param[in,out] generator The generator object to set up. It must have
2133 * been initialized as per the documentation for
2134 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002135 * \param private_key Handle to the private key to use.
Gilles Peskined171e782018-11-15 17:46:21 +01002136 * \param[in] peer_key Public key of the peer. It must be
2137 * in the same format that psa_import_key()
2138 * accepts. The standard formats for public
2139 * keys are documented in the documentation
2140 * of psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02002141 * \param peer_key_length Size of \p peer_key in bytes.
2142 * \param alg The key agreement algorithm to compute
2143 * (\c PSA_ALG_XXX value such that
2144 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
2145 *
2146 * \retval #PSA_SUCCESS
2147 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002148 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02002149 * \retval #PSA_ERROR_EMPTY_SLOT
2150 * \retval #PSA_ERROR_NOT_PERMITTED
2151 * \retval #PSA_ERROR_INVALID_ARGUMENT
2152 * \c private_key is not compatible with \c alg,
2153 * or \p peer_key is not valid for \c alg or not compatible with
2154 * \c private_key.
2155 * \retval #PSA_ERROR_NOT_SUPPORTED
2156 * \c alg is not supported or is not a key derivation algorithm.
2157 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2158 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2159 * \retval #PSA_ERROR_HARDWARE_FAILURE
2160 * \retval #PSA_ERROR_TAMPERING_DETECTED
2161 */
2162psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002163 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02002164 const uint8_t *peer_key,
2165 size_t peer_key_length,
2166 psa_algorithm_t alg);
2167
Gilles Peskineea0fb492018-07-12 17:17:20 +02002168/**@}*/
2169
Gilles Peskineedd76872018-07-20 17:42:05 +02002170/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002171 * @{
2172 */
2173
2174/**
2175 * \brief Generate random bytes.
2176 *
2177 * \warning This function **can** fail! Callers MUST check the return status
2178 * and MUST NOT use the content of the output buffer if the return
2179 * status is not #PSA_SUCCESS.
2180 *
2181 * \note To generate a key, use psa_generate_key() instead.
2182 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002183 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002184 * \param output_size Number of bytes to generate and output.
2185 *
Gilles Peskine28538492018-07-11 17:34:00 +02002186 * \retval #PSA_SUCCESS
2187 * \retval #PSA_ERROR_NOT_SUPPORTED
2188 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2189 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2190 * \retval #PSA_ERROR_HARDWARE_FAILURE
2191 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002192 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002193 * The library has not been previously initialized by psa_crypto_init().
2194 * It is implementation-dependent whether a failure to initialize
2195 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002196 */
2197psa_status_t psa_generate_random(uint8_t *output,
2198 size_t output_size);
2199
Gilles Peskine4c317f42018-07-12 01:24:09 +02002200/** Extra parameters for RSA key generation.
2201 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002202 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002203 * parameter to psa_generate_key().
2204 */
2205typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002206 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002207} psa_generate_key_extra_rsa;
2208
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002209/**
2210 * \brief Generate a key or key pair.
2211 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002212 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002213 * It must have been obtained by calling
2214 * psa_allocate_key() or psa_create_key() and must
2215 * not contain key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002216 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2217 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002218 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002219 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002220 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002221 * default parameters. Implementation that support
2222 * the generation of vendor-specific key types
2223 * that allow extra parameters shall document
2224 * the format of these extra parameters and
2225 * the default values. For standard parameters,
2226 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002227 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002228 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2229 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002230 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002231 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2232 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002233 * - For an RSA key (\p type is
2234 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2235 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002236 * specifying the public exponent. The
2237 * default public exponent used when \p extra
2238 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002239 * \param extra_size Size of the buffer that \p extra
2240 * points to, in bytes. Note that if \p extra is
2241 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002242 *
Gilles Peskine28538492018-07-11 17:34:00 +02002243 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002244 * Success.
2245 * If the key is persistent, the key material and the key's metadata
2246 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002247 * \retval #PSA_ERROR_INVALID_HANDLE
2248 * \retval #PSA_ERROR_OCCUPIED_SLOT
2249 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02002250 * \retval #PSA_ERROR_NOT_SUPPORTED
2251 * \retval #PSA_ERROR_INVALID_ARGUMENT
2252 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2253 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2254 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2255 * \retval #PSA_ERROR_HARDWARE_FAILURE
2256 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002257 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002258 * The library has not been previously initialized by psa_crypto_init().
2259 * It is implementation-dependent whether a failure to initialize
2260 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002261 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002262psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002263 psa_key_type_t type,
2264 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002265 const void *extra,
2266 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002267
2268/**@}*/
2269
Gilles Peskinee59236f2018-01-27 23:32:46 +01002270#ifdef __cplusplus
2271}
2272#endif
2273
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002274/* The file "crypto_sizes.h" contains definitions for size calculation
2275 * macros whose definitions are implementation-specific. */
2276#include "crypto_sizes.h"
2277
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002278/* The file "crypto_struct.h" contains definitions for
2279 * implementation-specific structs that are declared above. */
2280#include "crypto_struct.h"
2281
2282/* The file "crypto_extra.h" contains vendor-specific definitions. This
2283 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002284#include "crypto_extra.h"
2285
2286#endif /* PSA_CRYPTO_H */