blob: 55c0c041329507cb19b2845c44aaaefe03bb855d [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 Peskine2f9c4dc2018-01-28 13:16:24 +010039/** \brief Key slot number.
40 *
41 * This type represents key slots. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043 * 0 is not a valid key slot number. The meaning of other values is
44 * implementation dependent.
45 *
46 * At any given point in time, each key slot either contains a
47 * cryptographic object, or is empty. Key slots are persistent:
48 * once set, the cryptographic object remains in the key slot until
49 * explicitly destroyed.
50 */
51typedef _unsigned_integral_type_ psa_key_slot_t;
52
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010053/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010054#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010055
Gilles Peskinee59236f2018-01-27 23:32:46 +010056#ifdef __cplusplus
57extern "C" {
58#endif
59
60/** \defgroup basic Basic definitions
61 * @{
62 */
63
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020064#if defined(PSA_SUCCESS)
65/* If PSA_SUCCESS is defined, assume that PSA crypto is being used
66 * together with PSA IPC, which also defines the identifier
67 * PSA_SUCCESS. We must not define PSA_SUCCESS ourselves in that case;
68 * the other error code names don't clash. Also define psa_status_t as
69 * an alias for the type used by PSA IPC. This is a temporary hack
mohammad160313f43942018-08-05 12:09:44 +030070 * until we unify error reporting in PSA IPC and PSA crypto.
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020071 *
72 * Note that psa_defs.h must be included before this header!
73 */
74typedef psa_error_t psa_status_t;
75
76#else /* defined(PSA_SUCCESS) */
77
Gilles Peskinee59236f2018-01-27 23:32:46 +010078/**
79 * \brief Function return status.
80 *
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020081 * This is either #PSA_SUCCESS (which is zero), indicating success,
82 * or a nonzero value indicating that an error occurred. Errors are
83 * encoded as one of the \c PSA_ERROR_xxx values defined here.
Gilles Peskinee59236f2018-01-27 23:32:46 +010084 */
itayzafrirc2a79762018-06-18 16:20:16 +030085typedef int32_t psa_status_t;
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020086
itayzafrirc2a79762018-06-18 16:20:16 +030087/** The action was completed successfully. */
88#define PSA_SUCCESS ((psa_status_t)0)
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020089
90#endif /* !defined(PSA_SUCCESS) */
itayzafrirc2a79762018-06-18 16:20:16 +030091
itayzafrirf26dbfc2018-08-01 16:09:08 +030092/** An error occurred that does not correspond to any defined
93 * failure cause.
94 *
95 * Implementations may use this error code if none of the other standard
96 * error codes are applicable. */
97#define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)1)
98
itayzafrirc2a79762018-06-18 16:20:16 +030099/** The requested operation or a parameter is not supported
100 * by this implementation.
101 *
102 * Implementations should return this error code when an enumeration
103 * parameter such as a key type, algorithm, etc. is not recognized.
104 * If a combination of parameters is recognized and identified as
105 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300106#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)2)
itayzafrirc2a79762018-06-18 16:20:16 +0300107
108/** The requested action is denied by a policy.
109 *
110 * Implementations should return this error code when the parameters
111 * are recognized as valid and supported, and a policy explicitly
112 * denies the requested operation.
113 *
114 * If a subset of the parameters of a function call identify a
115 * forbidden operation, and another subset of the parameters are
116 * not valid or not supported, it is unspecified whether the function
117 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
118 * #PSA_ERROR_INVALID_ARGUMENT. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300119#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)3)
itayzafrirc2a79762018-06-18 16:20:16 +0300120
121/** An output buffer is too small.
122 *
Gilles Peskinebe42f312018-07-13 14:38:15 +0200123 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
itayzafrirc2a79762018-06-18 16:20:16 +0300124 * description to determine a sufficient buffer size.
125 *
126 * Implementations should preferably return this error code only
127 * in cases when performing the operation with a larger output
128 * buffer would succeed. However implementations may return this
129 * error if a function has invalid or unsupported parameters in addition
130 * to the parameters that determine the necessary output buffer size. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300131#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)4)
itayzafrirc2a79762018-06-18 16:20:16 +0300132
133/** A slot is occupied, but must be empty to carry out the
134 * requested action.
135 *
136 * If the slot number is invalid (i.e. the requested action could
137 * not be performed even after erasing the slot's content),
138 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300139#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)5)
itayzafrirc2a79762018-06-18 16:20:16 +0300140
141/** A slot is empty, but must be occupied to carry out the
142 * requested action.
143 *
144 * If the slot number is invalid (i.e. the requested action could
145 * not be performed even after creating appropriate content in the slot),
146 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300147#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)6)
itayzafrirc2a79762018-06-18 16:20:16 +0300148
149/** The requested action cannot be performed in the current state.
150 *
151 * Multipart operations return this error when one of the
152 * functions is called out of sequence. Refer to the function
153 * descriptions for permitted sequencing of functions.
154 *
155 * Implementations shall not return this error code to indicate
156 * that a key slot is occupied when it needs to be free or vice versa,
157 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
158 * as applicable. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300159#define PSA_ERROR_BAD_STATE ((psa_status_t)7)
itayzafrirc2a79762018-06-18 16:20:16 +0300160
161/** The parameters passed to the function are invalid.
162 *
163 * Implementations may return this error any time a parameter or
164 * combination of parameters are recognized as invalid.
165 *
166 * Implementations shall not return this error code to indicate
167 * that a key slot is occupied when it needs to be free or vice versa,
168 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
169 * as applicable. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300170#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)8)
itayzafrirc2a79762018-06-18 16:20:16 +0300171
172/** There is not enough runtime memory.
173 *
174 * If the action is carried out across multiple security realms, this
175 * error can refer to available memory in any of the security realms. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300176#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)9)
itayzafrirc2a79762018-06-18 16:20:16 +0300177
178/** There is not enough persistent storage.
179 *
180 * Functions that modify the key storage return this error code if
181 * there is insufficient storage space on the host media. In addition,
182 * many functions that do not otherwise access storage may return this
183 * error code if the implementation requires a mandatory log entry for
184 * the requested action and the log storage space is full. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300185#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)10)
itayzafrirc2a79762018-06-18 16:20:16 +0300186
187/** There was a communication failure inside the implementation.
188 *
189 * This can indicate a communication failure between the application
190 * and an external cryptoprocessor or between the cryptoprocessor and
191 * an external volatile or persistent memory. A communication failure
192 * may be transient or permanent depending on the cause.
193 *
194 * \warning If a function returns this error, it is undetermined
195 * whether the requested action has completed or not. Implementations
196 * should return #PSA_SUCCESS on successful completion whenver
197 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
198 * if the requested action was completed successfully in an external
199 * cryptoprocessor but there was a breakdown of communication before
200 * the cryptoprocessor could report the status to the application.
201 */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300202#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)11)
itayzafrirc2a79762018-06-18 16:20:16 +0300203
204/** There was a storage failure that may have led to data loss.
205 *
206 * This error indicates that some persistent storage is corrupted.
207 * It should not be used for a corruption of volatile memory
208 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
209 * between the cryptoprocessor and its external storage (use
210 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
211 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
212 *
213 * Note that a storage failure does not indicate that any data that was
214 * previously read is invalid. However this previously read data may no
215 * longer be readable from storage.
216 *
217 * When a storage failure occurs, it is no longer possible to ensure
218 * the global integrity of the keystore. Depending on the global
219 * integrity guarantees offered by the implementation, access to other
220 * data may or may not fail even if the data is still readable but
221 * its integrity canont be guaranteed.
222 *
223 * Implementations should only use this error code to report a
224 * permanent storage corruption. However application writers should
225 * keep in mind that transient errors while reading the storage may be
226 * reported using this error code. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300227#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)12)
itayzafrirc2a79762018-06-18 16:20:16 +0300228
229/** A hardware failure was detected.
230 *
231 * A hardware failure may be transient or permanent depending on the
232 * cause. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300233#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)13)
itayzafrirc2a79762018-06-18 16:20:16 +0300234
235/** A tampering attempt was detected.
236 *
237 * If an application receives this error code, there is no guarantee
238 * that previously accessed or computed data was correct and remains
239 * confidential. Applications should not perform any security function
240 * and should enter a safe failure state.
241 *
242 * Implementations may return this error code if they detect an invalid
243 * state that cannot happen during normal operation and that indicates
244 * that the implementation's security guarantees no longer hold. Depending
245 * on the implementation architecture and on its security and safety goals,
246 * the implementation may forcibly terminate the application.
247 *
248 * This error code is intended as a last resort when a security breach
249 * is detected and it is unsure whether the keystore data is still
250 * protected. Implementations shall only return this error code
251 * to report an alarm from a tampering detector, to indicate that
252 * the confidentiality of stored data can no longer be guaranteed,
253 * or to indicate that the integrity of previously returned data is now
254 * considered compromised. Implementations shall not use this error code
255 * to indicate a hardware failure that merely makes it impossible to
256 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
257 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
258 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
259 * instead).
260 *
261 * This error indicates an attack against the application. Implementations
262 * shall not return this error code as a consequence of the behavior of
263 * the application itself. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300264#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)14)
itayzafrirc2a79762018-06-18 16:20:16 +0300265
266/** There is not enough entropy to generate random data needed
267 * for the requested action.
268 *
269 * This error indicates a failure of a hardware random generator.
270 * Application writers should note that this error can be returned not
271 * only by functions whose purpose is to generate random data, such
272 * as key, IV or nonce generation, but also by functions that execute
273 * an algorithm with a randomized result, as well as functions that
274 * use randomization of intermediate computations as a countermeasure
275 * to certain attacks.
276 *
277 * Implementations should avoid returning this error after psa_crypto_init()
278 * has succeeded. Implementations should generate sufficient
279 * entropy during initialization and subsequently use a cryptographically
280 * secure pseudorandom generator (PRNG). However implementations may return
281 * this error at any time if a policy requires the PRNG to be reseeded
282 * during normal operation. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300283#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)15)
itayzafrirc2a79762018-06-18 16:20:16 +0300284
285/** The signature, MAC or hash is incorrect.
286 *
287 * Verification functions return this error if the verification
288 * calculations completed successfully, and the value to be verified
289 * was determined to be incorrect.
290 *
291 * If the value to verify has an invalid size, implementations may return
292 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300293#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)16)
itayzafrirc2a79762018-06-18 16:20:16 +0300294
295/** The decrypted padding is incorrect.
296 *
297 * \warning In some protocols, when decrypting data, it is essential that
298 * the behavior of the application does not depend on whether the padding
299 * is correct, down to precise timing. Applications should prefer
300 * protocols that use authenticated encryption rather than plain
301 * encryption. If the application must perform a decryption of
302 * unauthenticated data, the application writer should take care not
303 * to reveal whether the padding is invalid.
304 *
305 * Implementations should strive to make valid and invalid padding
306 * as close as possible to indistinguishable to an external observer.
307 * In particular, the timing of a decryption operation should not
308 * depend on the validity of the padding. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300309#define PSA_ERROR_INVALID_PADDING ((psa_status_t)17)
itayzafrirc2a79762018-06-18 16:20:16 +0300310
Gilles Peskineeab56e42018-07-12 17:12:33 +0200311/** The generator has insufficient capacity left.
312 *
313 * Once a function returns this error, attempts to read from the
314 * generator will always return this error. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300315#define PSA_ERROR_INSUFFICIENT_CAPACITY ((psa_status_t)18)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100316
317/**
318 * \brief Library initialization.
319 *
320 * Applications must call this function before calling any other
321 * function in this module.
322 *
323 * Applications may call this function more than once. Once a call
324 * succeeds, subsequent calls are guaranteed to succeed.
325 *
Gilles Peskine28538492018-07-11 17:34:00 +0200326 * \retval #PSA_SUCCESS
327 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
328 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
329 * \retval #PSA_ERROR_HARDWARE_FAILURE
330 * \retval #PSA_ERROR_TAMPERING_DETECTED
331 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100332 */
333psa_status_t psa_crypto_init(void);
334
Gilles Peskine2905a7a2018-03-07 16:39:31 +0100335#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
336#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Gilles Peskine0189e752018-02-03 23:57:22 +0100337
Gilles Peskinee59236f2018-01-27 23:32:46 +0100338/**@}*/
339
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100340/** \defgroup crypto_types Key and algorithm types
341 * @{
342 */
343
Gilles Peskine308b91d2018-02-08 09:47:44 +0100344/** \brief Encoding of a key type.
345 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100346typedef uint32_t psa_key_type_t;
347
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100348/** An invalid key type value.
349 *
350 * Zero is not the encoding of any key type.
351 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100352#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100353
354/** Vendor-defined flag
355 *
356 * Key types defined by this standard will never have the
357 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
358 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
359 * respect the bitwise structure used by standard encodings whenever practical.
360 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100361#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200363#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x70000000)
364#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x40000000)
365#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x50000000)
366#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x60000000)
367#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x70000000)
368
369#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x10000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200370
Gilles Peskine35855962018-04-19 08:39:16 +0200371/** Raw data.
372 *
373 * A "key" of this type cannot be used for any cryptographic operation.
374 * Applications may use this type to store arbitrary data in the keystore. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200375#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x50000001)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100376
Gilles Peskine35855962018-04-19 08:39:16 +0200377/** HMAC key.
378 *
379 * The key policy determines which underlying hash algorithm the key can be
380 * used for.
381 *
382 * HMAC keys should generally have the same size as the underlying hash.
Gilles Peskinebe42f312018-07-13 14:38:15 +0200383 * This size can be calculated with #PSA_HASH_SIZE(\c alg) where
384 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200385#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x51000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200386
Gilles Peskineea0fb492018-07-12 17:17:20 +0200387/** A secret for key derivation.
388 *
389 * The key policy determines which key derivation algorithm the key
390 * can be used for.
391 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200392#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x52000000)
Gilles Peskineea0fb492018-07-12 17:17:20 +0200393
Gilles Peskine35855962018-04-19 08:39:16 +0200394/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
395 *
396 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
397 * 32 bytes (AES-256).
398 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200399#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x40000001)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200400
Gilles Peskine35855962018-04-19 08:39:16 +0200401/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
402 *
403 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
404 * 24 bytes (3-key 3DES).
405 *
406 * Note that single DES and 2-key 3DES are weak and strongly
407 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
408 * is weak and deprecated and should only be used in legacy protocols.
409 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200410#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x40000002)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200411
Gilles Peskine35855962018-04-19 08:39:16 +0200412/** Key for an cipher, AEAD or MAC algorithm based on the
413 * Camellia block cipher. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200414#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x40000003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200415
Gilles Peskine35855962018-04-19 08:39:16 +0200416/** Key for the RC4 stream cipher.
417 *
418 * Note that RC4 is weak and deprecated and should only be used in
419 * legacy protocols. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200420#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x40000004)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100421
Gilles Peskine308b91d2018-02-08 09:47:44 +0100422/** RSA public key. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200423#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x60010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100424/** RSA key pair (private and public key). */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200425#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x70010000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200426
Gilles Peskine06dc2632018-03-08 07:47:25 +0100427/** DSA public key. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200428#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x60020000)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100429/** DSA key pair (private and public key). */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200430#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x70020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200431
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200432#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x60030000)
433#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x70030000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100434#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200435/** Elliptic curve key pair. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100436#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
437 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200438/** Elliptic curve public key. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100439#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
440 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100441
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100442/** Whether a key type is vendor-defined. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100443#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100444 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100445
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200446/** Whether a key type is an unstructured array of bytes.
447 *
448 * This encompasses both symmetric keys and non-key data.
449 */
450#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
451 (((type) & PSA_KEY_TYPE_CATEGORY_MASK & ~(psa_key_type_t)0x10000000) == \
452 PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
453
Gilles Peskine06dc2632018-03-08 07:47:25 +0100454/** Whether a key type is asymmetric: either a key pair or a public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100455#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200456 (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
457 & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
458 PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100459/** Whether a key type is the public part of a key pair. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100460#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200461 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100462/** Whether a key type is a key pair containing a private part and a public
463 * part. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100464#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200465 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
466/** The key pair type corresponding to a public key type.
467 *
468 * You may also pass a key pair type as \p type, it will be left unchanged.
469 *
470 * \param type A public key type or key pair type.
471 *
472 * \return The corresponding key pair type.
473 * If \p type is not a public key or a key pair,
474 * the return value is undefined.
475 */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100476#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200477 ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
478/** The public key type corresponding to a key pair type.
479 *
480 * You may also pass a key pair type as \p type, it will be left unchanged.
481 *
482 * \param type A public key type or key pair type.
483 *
484 * \return The corresponding public key type.
485 * If \p type is not a public key or a key pair,
486 * the return value is undefined.
487 */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100488#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200489 ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
Gilles Peskined8008d62018-06-29 19:51:51 +0200490/** Whether a key type is an RSA key (pair or public-only). */
491#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskine3bd1a422018-07-19 11:55:51 +0200492 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
493
Gilles Peskined8008d62018-06-29 19:51:51 +0200494/** Whether a key type is an elliptic curve key (pair or public-only). */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100495#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100496 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
497 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine55728b02018-07-16 23:08:16 +0200498#define PSA_KEY_TYPE_IS_ECC_KEYPAIR(type) \
499 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
500 PSA_KEY_TYPE_ECC_KEYPAIR_BASE)
501#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
502 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
503 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100504
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200505/** The type of PSA elliptic curve identifiers. */
506typedef uint16_t psa_ecc_curve_t;
507/** Extract the curve from an elliptic curve key type. */
508#define PSA_KEY_TYPE_GET_CURVE(type) \
509 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
510 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
511 0))
512
513/* The encoding of curve identifiers is currently aligned with the
514 * TLS Supported Groups Registry (formerly known as the
515 * TLS EC Named Curve Registry)
516 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
517 * The values are defined by RFC 4492, RFC 7027 and RFC 7919. */
518#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
519#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
520#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
521#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
522#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
523#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
524#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
525#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
526#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
527#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
528#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
529#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
530#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
531#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
532#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
533#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
534#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
535#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
536#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
537#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
538#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
539#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
540#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
541#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
542#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
543#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
544#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
545#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
546#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
547#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
548#define PSA_ECC_CURVE_FFDHE_2048 ((psa_ecc_curve_t) 0x0100)
549#define PSA_ECC_CURVE_FFDHE_3072 ((psa_ecc_curve_t) 0x0101)
550#define PSA_ECC_CURVE_FFDHE_4096 ((psa_ecc_curve_t) 0x0102)
551#define PSA_ECC_CURVE_FFDHE_6144 ((psa_ecc_curve_t) 0x0103)
552#define PSA_ECC_CURVE_FFDHE_8192 ((psa_ecc_curve_t) 0x0104)
553
Gilles Peskine7e198532018-03-08 07:50:30 +0100554/** The block size of a block cipher.
555 *
556 * \param type A cipher key type (value of type #psa_key_type_t).
557 *
558 * \return The block size for a block cipher, or 1 for a stream cipher.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200559 * The return value is undefined if \p type is not a supported
Gilles Peskine35855962018-04-19 08:39:16 +0200560 * cipher key type.
561 *
562 * \note It is possible to build stream cipher algorithms on top of a block
563 * cipher, for example CTR mode (#PSA_ALG_CTR).
564 * This macro only takes the key type into account, so it cannot be
565 * used to determine the size of the data that #psa_cipher_update()
566 * might buffer for future processing in general.
Gilles Peskine7e198532018-03-08 07:50:30 +0100567 *
568 * \note This macro returns a compile-time constant if its argument is one.
569 *
570 * \warning This macro may evaluate its argument multiple times.
571 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100572#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100573 ( \
574 (type) == PSA_KEY_TYPE_AES ? 16 : \
575 (type) == PSA_KEY_TYPE_DES ? 8 : \
576 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100577 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100578 0)
579
Gilles Peskine308b91d2018-02-08 09:47:44 +0100580/** \brief Encoding of a cryptographic algorithm.
581 *
582 * For algorithms that can be applied to multiple key types, this type
583 * does not encode the key type. For example, for symmetric ciphers
584 * based on a block cipher, #psa_algorithm_t encodes the block cipher
585 * mode and the padding mode while the block cipher itself is encoded
586 * via #psa_key_type_t.
587 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100588typedef uint32_t psa_algorithm_t;
589
Gilles Peskine98f0a242018-02-06 18:57:29 +0100590#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
591#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
592#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
593#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
594#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
595#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
596#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
597#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
598#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
599#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100600
Gilles Peskine98f0a242018-02-06 18:57:29 +0100601#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
602 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200603
Gilles Peskine308b91d2018-02-08 09:47:44 +0100604/** Whether the specified algorithm is a hash algorithm.
605 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100606 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100607 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200608 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
609 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskine7e198532018-03-08 07:50:30 +0100610 * algorithm identifier.
611 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100612#define PSA_ALG_IS_HASH(alg) \
613 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200614
615/** Whether the specified algorithm is a MAC algorithm.
616 *
617 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
618 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200619 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
620 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200621 * algorithm identifier.
622 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100623#define PSA_ALG_IS_MAC(alg) \
624 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200625
626/** Whether the specified algorithm is a symmetric cipher algorithm.
627 *
628 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
629 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200630 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
631 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200632 * algorithm identifier.
633 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100634#define PSA_ALG_IS_CIPHER(alg) \
635 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200636
637/** Whether the specified algorithm is an authenticated encryption
638 * with associated data (AEAD) algorithm.
639 *
640 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
641 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200642 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
643 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200644 * algorithm identifier.
645 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100646#define PSA_ALG_IS_AEAD(alg) \
647 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200648
649/** Whether the specified algorithm is a public-key signature algorithm.
650 *
651 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
652 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200653 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
654 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200655 * algorithm identifier.
656 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100657#define PSA_ALG_IS_SIGN(alg) \
658 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200659
660/** Whether the specified algorithm is a public-key encryption algorithm.
661 *
662 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
663 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200664 * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
665 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200666 * algorithm identifier.
667 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100668#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
669 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200670
671/** Whether the specified algorithm is a key agreement algorithm.
672 *
673 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
674 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200675 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
676 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200677 * algorithm identifier.
678 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100679#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
680 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200681
682/** Whether the specified algorithm is a key derivation algorithm.
683 *
684 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
685 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200686 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
687 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200688 * algorithm identifier.
689 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100690#define PSA_ALG_IS_KEY_DERIVATION(alg) \
691 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
692
693#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
694#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
695#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
696#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100697#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
698#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskineedd76872018-07-20 17:42:05 +0200699/** SHA2-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100700#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
Gilles Peskineedd76872018-07-20 17:42:05 +0200701/** SHA2-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100702#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
Gilles Peskineedd76872018-07-20 17:42:05 +0200703/** SHA2-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100704#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
Gilles Peskineedd76872018-07-20 17:42:05 +0200705/** SHA2-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100706#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
Gilles Peskineedd76872018-07-20 17:42:05 +0200707/** SHA2-512/224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100708#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
Gilles Peskineedd76872018-07-20 17:42:05 +0200709/** SHA2-512/256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100710#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
Gilles Peskineedd76872018-07-20 17:42:05 +0200711/** SHA3-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100712#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
Gilles Peskineedd76872018-07-20 17:42:05 +0200713/** SHA3-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100714#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
Gilles Peskineedd76872018-07-20 17:42:05 +0200715/** SHA3-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100716#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
Gilles Peskineedd76872018-07-20 17:42:05 +0200717/** SHA3-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100718#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
719
Gilles Peskine8c9def32018-02-08 10:02:12 +0100720#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100721#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200722/** Macro to build an HMAC algorithm.
723 *
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200724 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
Gilles Peskine35855962018-04-19 08:39:16 +0200725 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200726 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200727 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine35855962018-04-19 08:39:16 +0200728 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200729 * \return The corresponding HMAC algorithm.
730 * \return Unspecified if \p alg is not a supported
731 * hash algorithm.
Gilles Peskine35855962018-04-19 08:39:16 +0200732 */
733#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100734 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200735
Gilles Peskine8c9def32018-02-08 10:02:12 +0100736#define PSA_ALG_HMAC_HASH(hmac_alg) \
737 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200738
739/** Whether the specified algorithm is an HMAC algorithm.
740 *
741 * HMAC is a family of MAC algorithms that are based on a hash function.
742 *
743 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
744 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200745 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
746 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200747 * algorithm identifier.
748 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100749#define PSA_ALG_IS_HMAC(alg) \
750 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
751 PSA_ALG_HMAC_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200752
Gilles Peskine8c9def32018-02-08 10:02:12 +0100753#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
754#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
755#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
756#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200757
758/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
759 *
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200760 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
761 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200762 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
763 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200764 * algorithm identifier.
765 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100766#define PSA_ALG_IS_CIPHER_MAC(alg) \
767 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
768 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100769
Gilles Peskine8c9def32018-02-08 10:02:12 +0100770#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100771#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100772#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100773#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200774
775/** Use a block cipher mode without padding.
776 *
777 * This padding mode may only be used with messages whose lengths are a
778 * whole number of blocks for the chosen block cipher.
779 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100780#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200781
Gilles Peskine98f0a242018-02-06 18:57:29 +0100782#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200783
784/** Whether the specified algorithm is a block cipher.
785 *
786 * A block cipher is a symmetric cipher that encrypts or decrypts messages
787 * by chopping them into fixed-size blocks. Processing a message requires
788 * applying a _padding mode_ to transform the message into one whose
789 * length is a whole number of blocks. To construct an algorithm
790 * identifier for a block cipher, apply a bitwise-or between the block
791 * cipher mode and the padding mode. For example, CBC with PKCS#7 padding
792 * is `PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7`.
793 *
794 * The transformation applied to each block is determined by the key type.
795 * For example, to use AES-128-CBC-PKCS7, use the algorithm above with
796 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
797 *
798 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
799 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200800 * \return 1 if \p alg is a block cipher algorithm, 0 otherwise.
801 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200802 * algorithm identifier or if it is not a symmetric cipher algorithm.
803 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100804#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
805 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
806 PSA_ALG_BLOCK_CIPHER_BASE)
807
Gilles Peskinedcd14942018-07-12 00:30:52 +0200808/** The CBC block cipher mode.
809 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100810#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100811#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
812#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
813#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200814
815#define PSA_ALG_STREAM_CIPHER_BASE ((psa_algorithm_t)0x04800000)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200816
Gilles Peskinedcd14942018-07-12 00:30:52 +0200817/** The CTR stream cipher mode.
818 *
819 * CTR is a stream cipher which is built from a block cipher. The
820 * underlying block cipher is determined by the key type. For example,
821 * to use AES-128-CTR, use this algorithm with
822 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
823 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100824#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200825
Gilles Peskinedcd14942018-07-12 00:30:52 +0200826/** The ARC4 stream cipher algorithm.
827 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100828#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100829
Gilles Peskinedcd14942018-07-12 00:30:52 +0200830/** Whether the specified algorithm is a stream cipher.
831 *
832 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
833 * by applying a bitwise-xor with a stream of bytes that is generated
834 * from a key.
835 *
836 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
837 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200838 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
839 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200840 * algorithm identifier or if it is not a symmetric cipher algorithm.
841 */
Moran Pekerbed71a22018-04-22 20:19:20 +0300842#define PSA_ALG_IS_STREAM_CIPHER(alg) \
843 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200844 PSA_ALG_STREAM_CIPHER_BASE)
Moran Pekerbed71a22018-04-22 20:19:20 +0300845
Gilles Peskine8c9def32018-02-08 10:02:12 +0100846#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
847#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100848
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200849#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
850/** RSA PKCS#1 v1.5 signature with hashing.
851 *
852 * This is the signature scheme defined by RFC 8017
853 * (PKCS#1: RSA Cryptography Specifications) under the name
854 * RSASSA-PKCS1-v1_5.
855 *
856 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200857 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200858 *
859 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
860 * \return Unspecified if \p alg is not a supported
861 * hash algorithm.
862 */
Gilles Peskinea5926232018-03-28 14:16:50 +0200863#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200864 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
865/** Raw PKCS#1 v1.5 signature.
866 *
867 * The input to this algorithm is the DigestInfo structure used by
868 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
869 * steps 3&ndash;6.
870 */
871#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
Gilles Peskinea5926232018-03-28 14:16:50 +0200872#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200873 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200874
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200875#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
876/** RSA PSS signature with hashing.
877 *
878 * This is the signature scheme defined by RFC 8017
879 * (PKCS#1: RSA Cryptography Specifications) under the name
Gilles Peskinea4d20bd2018-06-29 23:35:02 +0200880 * RSASSA-PSS, with the message generation function MGF1, and with
881 * a salt length equal to the length of the hash. The specified
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200882 * hash algorithm is used to hash the input message, to create the
883 * salted hash, and for the mask generation.
884 *
885 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200886 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200887 *
888 * \return The corresponding RSA PSS signature algorithm.
889 * \return Unspecified if \p alg is not a supported
890 * hash algorithm.
891 */
892#define PSA_ALG_RSA_PSS(hash_alg) \
893 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
894#define PSA_ALG_IS_RSA_PSS(alg) \
895 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
896
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200897#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
898/** DSA signature with hashing.
899 *
900 * This is the signature scheme defined by FIPS 186-4,
901 * with a random per-message secret number (*k*).
902 *
903 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200904 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200905 *
906 * \return The corresponding DSA signature algorithm.
907 * \return Unspecified if \p alg is not a supported
908 * hash algorithm.
909 */
910#define PSA_ALG_DSA(hash_alg) \
911 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
912#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
913#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
914#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
915 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
916#define PSA_ALG_IS_DSA(alg) \
917 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
918 PSA_ALG_DSA_BASE)
919#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
920 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +0200921#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
922 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
923#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
924 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200925
926#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
927/** ECDSA signature with hashing.
928 *
929 * This is the ECDSA signature scheme defined by ANSI X9.62,
930 * with a random per-message secret number (*k*).
931 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200932 * The representation of the signature as a byte string consists of
933 * the concatentation of the signature values *r* and *s*. Each of
934 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
935 * of the base point of the curve in octets. Each value is represented
936 * in big-endian order (most significant octet first).
937 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200938 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200939 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200940 *
941 * \return The corresponding ECDSA signature algorithm.
942 * \return Unspecified if \p alg is not a supported
943 * hash algorithm.
944 */
945#define PSA_ALG_ECDSA(hash_alg) \
946 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
947/** ECDSA signature without hashing.
948 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200949 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200950 * without specifying a hash algorithm. This algorithm may only be
951 * used to sign or verify a sequence of bytes that should be an
952 * already-calculated hash. Note that the input is padded with
953 * zeros on the left or truncated on the left as required to fit
954 * the curve size.
955 */
956#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
957#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
958/** Deterministic ECDSA signature with hashing.
959 *
960 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
961 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200962 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
963 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200964 * Note that when this algorithm is used for verification, signatures
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200965 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200966 * same private key are accepted. In other words,
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200967 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
968 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200969 *
970 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200971 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200972 *
973 * \return The corresponding deterministic ECDSA signature
974 * algorithm.
975 * \return Unspecified if \p alg is not a supported
976 * hash algorithm.
977 */
978#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
979 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
980#define PSA_ALG_IS_ECDSA(alg) \
981 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
982 PSA_ALG_ECDSA_BASE)
983#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
984 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +0200985#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
986 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
987#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
988 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200989
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200990/** Get the hash used by a hash-and-sign signature algorithm.
991 *
992 * A hash-and-sign algorithm is a signature algorithm which is
993 * composed of two phases: first a hashing phase which does not use
994 * the key and produces a hash of the input message, then a signing
995 * phase which only uses the hash and the key and not the message
996 * itself.
997 *
998 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200999 * #PSA_ALG_IS_SIGN(\p alg) is true).
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001000 *
1001 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1002 * algorithm.
1003 * \return 0 if \p alg is a signature algorithm that does not
1004 * follow the hash-and-sign structure.
1005 * \return Unspecified if \p alg is not a signature algorithm or
1006 * if it is not supported by the implementation.
1007 */
1008#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001009 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
1010 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
Gilles Peskine54622ae2018-06-29 22:24:24 +02001011 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001012 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1013 0)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014
Gilles Peskinedcd14942018-07-12 00:30:52 +02001015/** RSA PKCS#1 v1.5 encryption.
1016 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001017#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001018
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001019#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001020/** RSA OAEP encryption.
1021 *
1022 * This is the encryption scheme defined by RFC 8017
1023 * (PKCS#1: RSA Cryptography Specifications) under the name
1024 * RSAES-OAEP, with the message generation function MGF1.
1025 *
1026 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1027 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1028 * for MGF1.
1029 *
1030 * \return The corresponding RSA OAEP signature algorithm.
1031 * \return Unspecified if \p alg is not a supported
1032 * hash algorithm.
1033 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001034#define PSA_ALG_RSA_OAEP(hash_alg) \
1035 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1036#define PSA_ALG_IS_RSA_OAEP(alg) \
1037 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
Gilles Peskine072ac562018-06-30 00:21:29 +02001038#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1039 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1040 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1041 0)
Gilles Peskined1e8e412018-06-07 09:49:39 +02001042
Gilles Peskinebef7f142018-07-12 17:22:21 +02001043#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100)
1044/** Macro to build an HKDF algorithm.
1045 *
1046 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1047 *
1048 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1049 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1050 *
1051 * \return The corresponding HKDF algorithm.
1052 * \return Unspecified if \p alg is not a supported
1053 * hash algorithm.
1054 */
1055#define PSA_ALG_HKDF(hash_alg) \
1056 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1057/** Whether the specified algorithm is an HKDF algorithm.
1058 *
1059 * HKDF is a family of key derivation algorithms that are based on a hash
1060 * function and the HMAC construction.
1061 *
1062 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1063 *
1064 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1065 * This macro may return either 0 or 1 if \c alg is not a supported
1066 * key derivation algorithm identifier.
1067 */
1068#define PSA_ALG_IS_HKDF(alg) \
1069 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1070#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1071 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1072
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001073/**@}*/
1074
1075/** \defgroup key_management Key management
1076 * @{
1077 */
1078
1079/**
1080 * \brief Import a key in binary format.
1081 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +01001082 * This function supports any output from psa_export_key(). Refer to the
1083 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001084 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001085 * \param key Slot where the key will be stored. This must be a
1086 * valid slot for a key of the chosen type. It must
1087 * be unoccupied.
1088 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001089 * \param[in] data Buffer containing the key data.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001090 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001091 *
Gilles Peskine28538492018-07-11 17:34:00 +02001092 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001093 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001094 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001095 * The key type or key size is not supported, either by the
1096 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001097 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +01001098 * The key slot is invalid,
1099 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +02001100 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001101 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001102 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1103 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1104 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1105 * \retval #PSA_ERROR_HARDWARE_FAILURE
1106 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001107 */
1108psa_status_t psa_import_key(psa_key_slot_t key,
1109 psa_key_type_t type,
1110 const uint8_t *data,
1111 size_t data_length);
1112
1113/**
Gilles Peskine154bd952018-04-19 08:38:16 +02001114 * \brief Destroy a key and restore the slot to its default state.
1115 *
1116 * This function destroys the content of the key slot from both volatile
1117 * memory and, if applicable, non-volatile storage. Implementations shall
1118 * make a best effort to ensure that any previous content of the slot is
1119 * unrecoverable.
1120 *
1121 * This function also erases any metadata such as policies. It returns the
1122 * specified slot to its default state.
1123 *
1124 * \param key The key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001125 *
Gilles Peskine28538492018-07-11 17:34:00 +02001126 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +02001127 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +02001128 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001129 * The slot holds content and cannot be erased because it is
1130 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskine28538492018-07-11 17:34:00 +02001131 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001132 * The specified slot number does not designate a valid slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001133 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001134 * There was an failure in communication with the cryptoprocessor.
1135 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +02001136 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001137 * The storage is corrupted. Implementations shall make a best effort
1138 * to erase key material even in this stage, however applications
1139 * should be aware that it may be impossible to guarantee that the
1140 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +02001141 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001142 * An unexpected condition which is not a storage corruption or
1143 * a communication failure occurred. The cryptoprocessor may have
1144 * been compromised.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001145 */
1146psa_status_t psa_destroy_key(psa_key_slot_t key);
1147
1148/**
1149 * \brief Get basic metadata about a key.
1150 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001151 * \param key Slot whose content is queried. This must
1152 * be an occupied key slot.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001153 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001154 * This may be a null pointer, in which case the key type
1155 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001156 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +01001157 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +01001158 * is not written.
1159 *
Gilles Peskine28538492018-07-11 17:34:00 +02001160 * \retval #PSA_SUCCESS
1161 * \retval #PSA_ERROR_EMPTY_SLOT
1162 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1163 * \retval #PSA_ERROR_HARDWARE_FAILURE
1164 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001165 */
1166psa_status_t psa_get_key_information(psa_key_slot_t key,
1167 psa_key_type_t *type,
1168 size_t *bits);
1169
1170/**
1171 * \brief Export a key in binary format.
1172 *
1173 * The output of this function can be passed to psa_import_key() to
1174 * create an equivalent object.
1175 *
1176 * If a key is created with psa_import_key() and then exported with
1177 * this function, it is not guaranteed that the resulting data is
1178 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +01001179 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001180 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001181 * For standard key types, the output format is as follows:
1182 *
1183 * - For symmetric keys (including MAC keys), the format is the
1184 * raw bytes of the key.
1185 * - For DES, the key data consists of 8 bytes. The parity bits must be
1186 * correct.
1187 * - For Triple-DES, the format is the concatenation of the
1188 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +01001189 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine2743e422018-06-27 22:57:11 +02001190 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
1191 * as RSAPrivateKey.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001192 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
Gilles Peskine971f7062018-03-20 17:52:58 +01001193 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001194 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001195 * \param key Slot whose content is to be exported. This must
1196 * be an occupied key slot.
1197 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001198 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001199 * \param[out] data_length On success, the number of bytes
1200 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001201 *
Gilles Peskine28538492018-07-11 17:34:00 +02001202 * \retval #PSA_SUCCESS
1203 * \retval #PSA_ERROR_EMPTY_SLOT
1204 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +01001205 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine28538492018-07-11 17:34:00 +02001206 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1207 * \retval #PSA_ERROR_HARDWARE_FAILURE
1208 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001209 */
1210psa_status_t psa_export_key(psa_key_slot_t key,
1211 uint8_t *data,
1212 size_t data_size,
1213 size_t *data_length);
1214
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001215/**
1216 * \brief Export a public key or the public part of a key pair in binary format.
1217 *
1218 * The output of this function can be passed to psa_import_key() to
1219 * create an object that is equivalent to the public key.
1220 *
1221 * For standard key types, the output format is as follows:
1222 *
1223 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
Moran Pekerdd4ea382018-04-03 15:30:03 +03001224 * the format is the DER representation of the public key defined by RFC 5280
Gilles Peskine971f7062018-03-20 17:52:58 +01001225 * as SubjectPublicKeyInfo.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001226 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001227 * \param key Slot whose content is to be exported. This must
1228 * be an occupied key slot.
1229 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001230 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001231 * \param[out] data_length On success, the number of bytes
1232 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001233 *
Gilles Peskine28538492018-07-11 17:34:00 +02001234 * \retval #PSA_SUCCESS
1235 * \retval #PSA_ERROR_EMPTY_SLOT
1236 * \retval #PSA_ERROR_INVALID_ARGUMENT
1237 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1238 * \retval #PSA_ERROR_HARDWARE_FAILURE
1239 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001240 */
1241psa_status_t psa_export_public_key(psa_key_slot_t key,
1242 uint8_t *data,
1243 size_t data_size,
1244 size_t *data_length);
1245
1246/**@}*/
1247
1248/** \defgroup policy Key policies
1249 * @{
1250 */
1251
1252/** \brief Encoding of permitted usage on a key. */
1253typedef uint32_t psa_key_usage_t;
1254
Gilles Peskine7e198532018-03-08 07:50:30 +01001255/** Whether the key may be exported.
1256 *
1257 * A public key or the public part of a key pair may always be exported
1258 * regardless of the value of this permission flag.
1259 *
1260 * If a key does not have export permission, implementations shall not
1261 * allow the key to be exported in plain form from the cryptoprocessor,
1262 * whether through psa_export_key() or through a proprietary interface.
1263 * The key may however be exportable in a wrapped form, i.e. in a form
1264 * where it is encrypted by another key.
1265 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001266#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1267
Gilles Peskine7e198532018-03-08 07:50:30 +01001268/** Whether the key may be used to encrypt a message.
1269 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001270 * This flag allows the key to be used for a symmetric encryption operation,
1271 * for an AEAD encryption-and-authentication operation,
1272 * or for an asymmetric encryption operation,
1273 * if otherwise permitted by the key's type and policy.
1274 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001275 * For a key pair, this concerns the public key.
1276 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001277#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +01001278
1279/** Whether the key may be used to decrypt a message.
1280 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001281 * This flag allows the key to be used for a symmetric decryption operation,
1282 * for an AEAD decryption-and-verification operation,
1283 * or for an asymmetric decryption operation,
1284 * if otherwise permitted by the key's type and policy.
1285 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001286 * For a key pair, this concerns the private key.
1287 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001288#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +01001289
1290/** Whether the key may be used to sign a message.
1291 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001292 * This flag allows the key to be used for a MAC calculation operation
1293 * or for an asymmetric signature operation,
1294 * if otherwise permitted by the key's type and policy.
1295 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001296 * For a key pair, this concerns the private key.
1297 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001298#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +01001299
1300/** Whether the key may be used to verify a message signature.
1301 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001302 * This flag allows the key to be used for a MAC verification operation
1303 * or for an asymmetric signature verification operation,
1304 * if otherwise permitted by by the key's type and policy.
1305 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001306 * For a key pair, this concerns the public key.
1307 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001308#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1309
Gilles Peskineea0fb492018-07-12 17:17:20 +02001310/** Whether the key may be used to derive other keys.
1311 */
1312#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1313
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001314/** The type of the key policy data structure.
1315 *
1316 * This is an implementation-defined \c struct. Applications should not
1317 * make any assumptions about the content of this structure except
1318 * as directed by the documentation of a specific implementation. */
1319typedef struct psa_key_policy_s psa_key_policy_t;
1320
1321/** \brief Initialize a key policy structure to a default that forbids all
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001322 * usage of the key.
1323 *
1324 * \param[out] policy The policy object to initialize.
1325 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001326void psa_key_policy_init(psa_key_policy_t *policy);
1327
Gilles Peskine7e198532018-03-08 07:50:30 +01001328/** \brief Set the standard fields of a policy structure.
1329 *
1330 * Note that this function does not make any consistency check of the
1331 * parameters. The values are only checked when applying the policy to
1332 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001333 *
1334 * \param[out] policy The policy object to modify.
1335 * \param usage The permitted uses for the key.
1336 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +01001337 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001338void psa_key_policy_set_usage(psa_key_policy_t *policy,
1339 psa_key_usage_t usage,
1340 psa_algorithm_t alg);
1341
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001342/** \brief Retrieve the usage field of a policy structure.
1343 *
1344 * \param[in] policy The policy object to query.
1345 *
1346 * \return The permitted uses for a key with this policy.
1347 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001348psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001349
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001350/** \brief Retrieve the algorithm field of a policy structure.
1351 *
1352 * \param[in] policy The policy object to query.
1353 *
1354 * \return The permitted algorithm for a key with this policy.
1355 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001356psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001357
1358/** \brief Set the usage policy on a key slot.
1359 *
1360 * This function must be called on an empty key slot, before importing,
1361 * generating or creating a key in the slot. Changing the policy of an
1362 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +01001363 *
1364 * Implementations may set restrictions on supported key policies
1365 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001366 *
1367 * \param key The key slot whose policy is to be changed.
1368 * \param[in] policy The policy object to query.
1369 *
1370 * \retval #PSA_SUCCESS
1371 * \retval #PSA_ERROR_OCCUPIED_SLOT
1372 * \retval #PSA_ERROR_NOT_SUPPORTED
1373 * \retval #PSA_ERROR_INVALID_ARGUMENT
1374 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1375 * \retval #PSA_ERROR_HARDWARE_FAILURE
1376 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001377 */
1378psa_status_t psa_set_key_policy(psa_key_slot_t key,
1379 const psa_key_policy_t *policy);
1380
Gilles Peskine7e198532018-03-08 07:50:30 +01001381/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001382 *
1383 * \param key The key slot whose policy is being queried.
1384 * \param[out] policy On success, the key's policy.
1385 *
1386 * \retval #PSA_SUCCESS
1387 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1388 * \retval #PSA_ERROR_HARDWARE_FAILURE
1389 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7e198532018-03-08 07:50:30 +01001390 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001391psa_status_t psa_get_key_policy(psa_key_slot_t key,
1392 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +01001393
1394/**@}*/
1395
Gilles Peskine609b6a52018-03-03 21:31:50 +01001396/** \defgroup persistence Key lifetime
1397 * @{
1398 */
1399
1400/** Encoding of key lifetimes.
1401 */
1402typedef uint32_t psa_key_lifetime_t;
1403
1404/** A volatile key slot retains its content as long as the application is
1405 * running. It is guaranteed to be erased on a power reset.
1406 */
1407#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1408
1409/** A persistent key slot retains its content as long as it is not explicitly
1410 * destroyed.
1411 */
1412#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1413
1414/** A write-once key slot may not be modified once a key has been set.
1415 * It will retain its content as long as the device remains operational.
1416 */
1417#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
1418
Gilles Peskined393e182018-03-08 07:49:16 +01001419/** \brief Retrieve the lifetime of a key slot.
1420 *
1421 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001422 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001423 * \param key Slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001424 * \param[out] lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001425 *
Gilles Peskine28538492018-07-11 17:34:00 +02001426 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001427 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001428 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -07001429 * The key slot is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001430 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1431 * \retval #PSA_ERROR_HARDWARE_FAILURE
1432 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001433 */
Gilles Peskine609b6a52018-03-03 21:31:50 +01001434psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1435 psa_key_lifetime_t *lifetime);
1436
Gilles Peskined393e182018-03-08 07:49:16 +01001437/** \brief Change the lifetime of a key slot.
1438 *
1439 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +01001440 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +01001441 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001442 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001443 * \param key Slot whose lifetime is to be changed.
1444 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001445 *
Gilles Peskine28538492018-07-11 17:34:00 +02001446 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001447 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001448 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603804cd712018-03-20 22:44:08 +02001449 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -07001450 * or the lifetime value is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001451 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001452 * The implementation does not support the specified lifetime value,
1453 * at least for the specified key slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001454 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001455 * The slot contains a key, and the implementation does not support
1456 * changing the lifetime of an occupied slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001457 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1458 * \retval #PSA_ERROR_HARDWARE_FAILURE
1459 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001460 */
1461psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -07001462 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +01001463
Gilles Peskine609b6a52018-03-03 21:31:50 +01001464/**@}*/
1465
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001466/** \defgroup hash Message digests
1467 * @{
1468 */
1469
Gilles Peskine308b91d2018-02-08 09:47:44 +01001470/** The type of the state data structure for multipart hash operations.
1471 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001472 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001473 * make any assumptions about the content of this structure except
1474 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001475typedef struct psa_hash_operation_s psa_hash_operation_t;
1476
Gilles Peskine308b91d2018-02-08 09:47:44 +01001477/** The size of the output of psa_hash_finish(), in bytes.
1478 *
1479 * This is also the hash size that psa_hash_verify() expects.
1480 *
1481 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001482 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
Gilles Peskinebe42f312018-07-13 14:38:15 +02001483 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
Gilles Peskine35855962018-04-19 08:39:16 +02001484 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001485 *
1486 * \return The hash size for the specified hash algorithm.
1487 * If the hash algorithm is not recognized, return 0.
1488 * An implementation may return either 0 or the correct size
1489 * for a hash algorithm that it recognizes, but does not support.
1490 */
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001491#define PSA_HASH_SIZE(alg) \
1492 ( \
1493 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD2 ? 16 : \
1494 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD4 ? 16 : \
1495 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD5 ? 16 : \
1496 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
1497 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
1498 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
1499 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
1500 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
1501 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
1502 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
1503 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
1504 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
1505 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
1506 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
1507 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001508 0)
1509
Gilles Peskine308b91d2018-02-08 09:47:44 +01001510/** Start a multipart hash operation.
1511 *
1512 * The sequence of operations to calculate a hash (message digest)
1513 * is as follows:
1514 * -# Allocate an operation object which will be passed to all the functions
1515 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001516 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001517 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001518 * of the message each time. The hash that is calculated is the hash
1519 * of the concatenation of these messages in order.
1520 * -# To calculate the hash, call psa_hash_finish().
1521 * To compare the hash with an expected value, call psa_hash_verify().
1522 *
1523 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001524 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001525 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001526 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001527 * eventually terminate the operation. The following events terminate an
1528 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001529 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001530 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001531 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001532 * \param[out] operation The operation object to use.
1533 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1534 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001535 *
Gilles Peskine28538492018-07-11 17:34:00 +02001536 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001537 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001538 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001539 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001540 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1541 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1542 * \retval #PSA_ERROR_HARDWARE_FAILURE
1543 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001544 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001545psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001546 psa_algorithm_t alg);
1547
Gilles Peskine308b91d2018-02-08 09:47:44 +01001548/** Add a message fragment to a multipart hash operation.
1549 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001550 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001551 *
1552 * If this function returns an error status, the operation becomes inactive.
1553 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001554 * \param[in,out] operation Active hash operation.
1555 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001556 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001557 *
Gilles Peskine28538492018-07-11 17:34:00 +02001558 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001559 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001560 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001561 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001562 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1563 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1564 * \retval #PSA_ERROR_HARDWARE_FAILURE
1565 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001566 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001567psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1568 const uint8_t *input,
1569 size_t input_length);
1570
Gilles Peskine308b91d2018-02-08 09:47:44 +01001571/** Finish the calculation of the hash of a message.
1572 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001573 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001574 * This function calculates the hash of the message formed by concatenating
1575 * the inputs passed to preceding calls to psa_hash_update().
1576 *
1577 * When this function returns, the operation becomes inactive.
1578 *
1579 * \warning Applications should not call this function if they expect
1580 * a specific value for the hash. Call psa_hash_verify() instead.
1581 * Beware that comparing integrity or authenticity data such as
1582 * hash values with a function such as \c memcmp is risky
1583 * because the time taken by the comparison may leak information
1584 * about the hashed data which could allow an attacker to guess
1585 * a valid hash and thereby bypass security controls.
1586 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001587 * \param[in,out] operation Active hash operation.
1588 * \param[out] hash Buffer where the hash is to be written.
1589 * \param hash_size Size of the \p hash buffer in bytes.
1590 * \param[out] hash_length On success, the number of bytes
1591 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001592 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001593 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001594 *
Gilles Peskine28538492018-07-11 17:34:00 +02001595 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001596 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001597 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001598 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001599 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001600 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001601 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001602 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001603 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1604 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1605 * \retval #PSA_ERROR_HARDWARE_FAILURE
1606 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001607 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001608psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1609 uint8_t *hash,
1610 size_t hash_size,
1611 size_t *hash_length);
1612
Gilles Peskine308b91d2018-02-08 09:47:44 +01001613/** Finish the calculation of the hash of a message and compare it with
1614 * an expected value.
1615 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001616 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001617 * This function calculates the hash of the message formed by concatenating
1618 * the inputs passed to preceding calls to psa_hash_update(). It then
1619 * compares the calculated hash with the expected hash passed as a
1620 * parameter to this function.
1621 *
1622 * When this function returns, the operation becomes inactive.
1623 *
Gilles Peskine19067982018-03-20 17:54:53 +01001624 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001625 * comparison between the actual hash and the expected hash is performed
1626 * in constant time.
1627 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001628 * \param[in,out] operation Active hash operation.
1629 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001630 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001631 *
Gilles Peskine28538492018-07-11 17:34:00 +02001632 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001633 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001634 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001635 * The hash of the message was calculated successfully, but it
1636 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001637 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001638 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001639 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1640 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1641 * \retval #PSA_ERROR_HARDWARE_FAILURE
1642 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001643 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001644psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1645 const uint8_t *hash,
1646 size_t hash_length);
1647
Gilles Peskine308b91d2018-02-08 09:47:44 +01001648/** Abort a hash operation.
1649 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001650 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001651 * \p operation structure itself. Once aborted, the operation object
1652 * can be reused for another operation by calling
1653 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001654 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001655 * You may call this function any time after the operation object has
1656 * been initialized by any of the following methods:
1657 * - A call to psa_hash_setup(), whether it succeeds or not.
1658 * - Initializing the \c struct to all-bits-zero.
1659 * - Initializing the \c struct to logical zeros, e.g.
1660 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001661 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001662 * In particular, calling psa_hash_abort() after the operation has been
1663 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1664 * psa_hash_verify() is safe and has no effect.
1665 *
1666 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001667 *
Gilles Peskine28538492018-07-11 17:34:00 +02001668 * \retval #PSA_SUCCESS
1669 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001670 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001671 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1672 * \retval #PSA_ERROR_HARDWARE_FAILURE
1673 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001674 */
1675psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001676
1677/**@}*/
1678
Gilles Peskine8c9def32018-02-08 10:02:12 +01001679/** \defgroup MAC Message authentication codes
1680 * @{
1681 */
1682
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001683/** The type of the state data structure for multipart MAC operations.
1684 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001685 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001686 * make any assumptions about the content of this structure except
1687 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001688typedef struct psa_mac_operation_s psa_mac_operation_t;
1689
Gilles Peskine89167cb2018-07-08 20:12:23 +02001690/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001691 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001692 * This function sets up the calculation of the MAC
1693 * (message authentication code) of a byte string.
1694 * To verify the MAC of a message against an
1695 * expected value, use psa_mac_verify_setup() instead.
1696 *
1697 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001698 * -# Allocate an operation object which will be passed to all the functions
1699 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001700 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001701 * The key remains associated with the operation even if the content
1702 * of the key slot changes.
1703 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1704 * of the message each time. The MAC that is calculated is the MAC
1705 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001706 * -# At the end of the message, call psa_mac_sign_finish() to finish
1707 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001708 *
1709 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +02001710 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001711 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001712 * After a successful call to psa_mac_sign_setup(), the application must
1713 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001714 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001715 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001716 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001717 * \param[out] operation The operation object to use.
1718 * \param key Slot containing the key to use for the operation.
1719 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1720 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001721 *
Gilles Peskine28538492018-07-11 17:34:00 +02001722 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001723 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001724 * \retval #PSA_ERROR_EMPTY_SLOT
1725 * \retval #PSA_ERROR_NOT_PERMITTED
1726 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001727 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001728 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001729 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001730 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1731 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1732 * \retval #PSA_ERROR_HARDWARE_FAILURE
1733 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001734 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001735psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1736 psa_key_slot_t key,
1737 psa_algorithm_t alg);
1738
1739/** Start a multipart MAC verification operation.
1740 *
1741 * This function sets up the verification of the MAC
1742 * (message authentication code) of a byte string against an expected value.
1743 *
1744 * The sequence of operations to verify a MAC is as follows:
1745 * -# Allocate an operation object which will be passed to all the functions
1746 * listed here.
1747 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1748 * The key remains associated with the operation even if the content
1749 * of the key slot changes.
1750 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1751 * of the message each time. The MAC that is calculated is the MAC
1752 * of the concatenation of these messages in order.
1753 * -# At the end of the message, call psa_mac_verify_finish() to finish
1754 * calculating the actual MAC of the message and verify it against
1755 * the expected value.
1756 *
1757 * The application may call psa_mac_abort() at any time after the operation
1758 * has been initialized with psa_mac_verify_setup().
1759 *
1760 * After a successful call to psa_mac_verify_setup(), the application must
1761 * eventually terminate the operation through one of the following methods:
1762 * - A failed call to psa_mac_update().
1763 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1764 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001765 * \param[out] operation The operation object to use.
1766 * \param key Slot containing the key to use for the operation.
1767 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1768 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001769 *
Gilles Peskine28538492018-07-11 17:34:00 +02001770 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001771 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001772 * \retval #PSA_ERROR_EMPTY_SLOT
1773 * \retval #PSA_ERROR_NOT_PERMITTED
1774 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001775 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001776 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001777 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001778 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1779 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1780 * \retval #PSA_ERROR_HARDWARE_FAILURE
1781 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001782 */
1783psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
1784 psa_key_slot_t key,
1785 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001786
Gilles Peskinedcd14942018-07-12 00:30:52 +02001787/** Add a message fragment to a multipart MAC operation.
1788 *
1789 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1790 * before calling this function.
1791 *
1792 * If this function returns an error status, the operation becomes inactive.
1793 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001794 * \param[in,out] operation Active MAC operation.
1795 * \param[in] input Buffer containing the message fragment to add to
1796 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001797 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001798 *
1799 * \retval #PSA_SUCCESS
1800 * Success.
1801 * \retval #PSA_ERROR_BAD_STATE
1802 * The operation state is not valid (not started, or already completed).
1803 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1804 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1805 * \retval #PSA_ERROR_HARDWARE_FAILURE
1806 * \retval #PSA_ERROR_TAMPERING_DETECTED
1807 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001808psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1809 const uint8_t *input,
1810 size_t input_length);
1811
Gilles Peskinedcd14942018-07-12 00:30:52 +02001812/** Finish the calculation of the MAC of a message.
1813 *
1814 * The application must call psa_mac_sign_setup() before calling this function.
1815 * This function calculates the MAC of the message formed by concatenating
1816 * the inputs passed to preceding calls to psa_mac_update().
1817 *
1818 * When this function returns, the operation becomes inactive.
1819 *
1820 * \warning Applications should not call this function if they expect
1821 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1822 * Beware that comparing integrity or authenticity data such as
1823 * MAC values with a function such as \c memcmp is risky
1824 * because the time taken by the comparison may leak information
1825 * about the MAC value which could allow an attacker to guess
1826 * a valid MAC and thereby bypass security controls.
1827 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001828 * \param[in,out] operation Active MAC operation.
1829 * \param[out] mac Buffer where the MAC value is to be written.
1830 * \param mac_size Size of the \p mac buffer in bytes.
1831 * \param[out] mac_length On success, the number of bytes
1832 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001833 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001834 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001835 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001836 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001837 *
1838 * \retval #PSA_SUCCESS
1839 * Success.
1840 * \retval #PSA_ERROR_BAD_STATE
1841 * The operation state is not valid (not started, or already completed).
1842 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001843 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001844 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1845 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1846 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1847 * \retval #PSA_ERROR_HARDWARE_FAILURE
1848 * \retval #PSA_ERROR_TAMPERING_DETECTED
1849 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001850psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1851 uint8_t *mac,
1852 size_t mac_size,
1853 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001854
Gilles Peskinedcd14942018-07-12 00:30:52 +02001855/** Finish the calculation of the MAC of a message and compare it with
1856 * an expected value.
1857 *
1858 * The application must call psa_mac_verify_setup() before calling this function.
1859 * This function calculates the MAC of the message formed by concatenating
1860 * the inputs passed to preceding calls to psa_mac_update(). It then
1861 * compares the calculated MAC with the expected MAC passed as a
1862 * parameter to this function.
1863 *
1864 * When this function returns, the operation becomes inactive.
1865 *
1866 * \note Implementations shall make the best effort to ensure that the
1867 * comparison between the actual MAC and the expected MAC is performed
1868 * in constant time.
1869 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001870 * \param[in,out] operation Active MAC operation.
1871 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001872 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001873 *
1874 * \retval #PSA_SUCCESS
1875 * The expected MAC is identical to the actual MAC of the message.
1876 * \retval #PSA_ERROR_INVALID_SIGNATURE
1877 * The MAC of the message was calculated successfully, but it
1878 * differs from the expected MAC.
1879 * \retval #PSA_ERROR_BAD_STATE
1880 * The operation state is not valid (not started, or already completed).
1881 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1882 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1883 * \retval #PSA_ERROR_HARDWARE_FAILURE
1884 * \retval #PSA_ERROR_TAMPERING_DETECTED
1885 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001886psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1887 const uint8_t *mac,
1888 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001889
Gilles Peskinedcd14942018-07-12 00:30:52 +02001890/** Abort a MAC operation.
1891 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001892 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001893 * \p operation structure itself. Once aborted, the operation object
1894 * can be reused for another operation by calling
1895 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001896 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001897 * You may call this function any time after the operation object has
1898 * been initialized by any of the following methods:
1899 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1900 * it succeeds or not.
1901 * - Initializing the \c struct to all-bits-zero.
1902 * - Initializing the \c struct to logical zeros, e.g.
1903 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001904 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001905 * In particular, calling psa_mac_abort() after the operation has been
1906 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1907 * psa_mac_verify_finish() is safe and has no effect.
1908 *
1909 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001910 *
1911 * \retval #PSA_SUCCESS
1912 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001913 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001914 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1915 * \retval #PSA_ERROR_HARDWARE_FAILURE
1916 * \retval #PSA_ERROR_TAMPERING_DETECTED
1917 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001918psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1919
1920/**@}*/
1921
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001922/** \defgroup cipher Symmetric ciphers
1923 * @{
1924 */
1925
1926/** The type of the state data structure for multipart cipher operations.
1927 *
1928 * This is an implementation-defined \c struct. Applications should not
1929 * make any assumptions about the content of this structure except
1930 * as directed by the documentation of a specific implementation. */
1931typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1932
1933/** Set the key for a multipart symmetric encryption operation.
1934 *
1935 * The sequence of operations to encrypt a message with a symmetric cipher
1936 * is as follows:
1937 * -# Allocate an operation object which will be passed to all the functions
1938 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001939 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001940 * The key remains associated with the operation even if the content
1941 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03001942 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001943 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001944 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001945 * requires a specific IV value.
1946 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1947 * of the message each time.
1948 * -# Call psa_cipher_finish().
1949 *
1950 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001951 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001952 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001953 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001954 * eventually terminate the operation. The following events terminate an
1955 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03001956 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001957 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001958 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001959 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001960 * \param[out] operation The operation object to use.
1961 * \param key Slot containing the key to use for the operation.
1962 * \param alg The cipher algorithm to compute
1963 * (\c PSA_ALG_XXX value such that
1964 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001965 *
Gilles Peskine28538492018-07-11 17:34:00 +02001966 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001967 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001968 * \retval #PSA_ERROR_EMPTY_SLOT
1969 * \retval #PSA_ERROR_NOT_PERMITTED
1970 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001971 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001972 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001973 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001974 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1975 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1976 * \retval #PSA_ERROR_HARDWARE_FAILURE
1977 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001978 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001979psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
1980 psa_key_slot_t key,
1981 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001982
1983/** Set the key for a multipart symmetric decryption operation.
1984 *
1985 * The sequence of operations to decrypt a message with a symmetric cipher
1986 * is as follows:
1987 * -# Allocate an operation object which will be passed to all the functions
1988 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001989 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001990 * The key remains associated with the operation even if the content
1991 * of the key slot changes.
1992 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1993 * decryption. If the IV is prepended to the ciphertext, you can call
1994 * psa_cipher_update() on a buffer containing the IV followed by the
1995 * beginning of the message.
1996 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1997 * of the message each time.
1998 * -# Call psa_cipher_finish().
1999 *
2000 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02002001 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002002 *
Gilles Peskinefe119512018-07-08 21:39:34 +02002003 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002004 * eventually terminate the operation. The following events terminate an
2005 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002006 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002007 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002008 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002009 * \param[out] operation The operation object to use.
2010 * \param key Slot containing the key to use for the operation.
2011 * \param alg The cipher algorithm to compute
2012 * (\c PSA_ALG_XXX value such that
2013 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002014 *
Gilles Peskine28538492018-07-11 17:34:00 +02002015 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002016 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002017 * \retval #PSA_ERROR_EMPTY_SLOT
2018 * \retval #PSA_ERROR_NOT_PERMITTED
2019 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002020 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002021 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002022 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002023 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2024 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2025 * \retval #PSA_ERROR_HARDWARE_FAILURE
2026 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002027 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002028psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
2029 psa_key_slot_t key,
2030 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002031
Gilles Peskinedcd14942018-07-12 00:30:52 +02002032/** Generate an IV for a symmetric encryption operation.
2033 *
2034 * This function generates a random IV (initialization vector), nonce
2035 * or initial counter value for the encryption operation as appropriate
2036 * for the chosen algorithm, key type and key size.
2037 *
2038 * The application must call psa_cipher_encrypt_setup() before
2039 * calling this function.
2040 *
2041 * If this function returns an error status, the operation becomes inactive.
2042 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002043 * \param[in,out] operation Active cipher operation.
2044 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002045 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002046 * \param[out] iv_length On success, the number of bytes of the
2047 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002048 *
2049 * \retval #PSA_SUCCESS
2050 * Success.
2051 * \retval #PSA_ERROR_BAD_STATE
2052 * The operation state is not valid (not started, or IV already set).
2053 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002054 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002055 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2056 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2057 * \retval #PSA_ERROR_HARDWARE_FAILURE
2058 * \retval #PSA_ERROR_TAMPERING_DETECTED
2059 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002060psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
2061 unsigned char *iv,
2062 size_t iv_size,
2063 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002064
Gilles Peskinedcd14942018-07-12 00:30:52 +02002065/** Set the IV for a symmetric encryption or decryption operation.
2066 *
2067 * This function sets the random IV (initialization vector), nonce
2068 * or initial counter value for the encryption or decryption operation.
2069 *
2070 * The application must call psa_cipher_encrypt_setup() before
2071 * calling this function.
2072 *
2073 * If this function returns an error status, the operation becomes inactive.
2074 *
2075 * \note When encrypting, applications should use psa_cipher_generate_iv()
2076 * instead of this function, unless implementing a protocol that requires
2077 * a non-random IV.
2078 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002079 * \param[in,out] operation Active cipher operation.
2080 * \param[in] iv Buffer containing the IV to use.
2081 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002082 *
2083 * \retval #PSA_SUCCESS
2084 * Success.
2085 * \retval #PSA_ERROR_BAD_STATE
2086 * The operation state is not valid (not started, or IV already set).
2087 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002088 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02002089 * or the chosen algorithm does not use an IV.
2090 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2091 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2092 * \retval #PSA_ERROR_HARDWARE_FAILURE
2093 * \retval #PSA_ERROR_TAMPERING_DETECTED
2094 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002095psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
2096 const unsigned char *iv,
2097 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002098
Gilles Peskinedcd14942018-07-12 00:30:52 +02002099/** Encrypt or decrypt a message fragment in an active cipher operation.
2100 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02002101 * Before calling this function, you must:
2102 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
2103 * The choice of setup function determines whether this function
2104 * encrypts or decrypts its input.
2105 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
2106 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002107 *
2108 * If this function returns an error status, the operation becomes inactive.
2109 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002110 * \param[in,out] operation Active cipher operation.
2111 * \param[in] input Buffer containing the message fragment to
2112 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002113 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002114 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002115 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002116 * \param[out] output_length On success, the number of bytes
2117 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002118 *
2119 * \retval #PSA_SUCCESS
2120 * Success.
2121 * \retval #PSA_ERROR_BAD_STATE
2122 * The operation state is not valid (not started, IV required but
2123 * not set, or already completed).
2124 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2125 * The size of the \p output buffer is too small.
2126 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2127 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2128 * \retval #PSA_ERROR_HARDWARE_FAILURE
2129 * \retval #PSA_ERROR_TAMPERING_DETECTED
2130 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002131psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2132 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002133 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002134 unsigned char *output,
2135 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002136 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002137
Gilles Peskinedcd14942018-07-12 00:30:52 +02002138/** Finish encrypting or decrypting a message in a cipher operation.
2139 *
2140 * The application must call psa_cipher_encrypt_setup() or
2141 * psa_cipher_decrypt_setup() before calling this function. The choice
2142 * of setup function determines whether this function encrypts or
2143 * decrypts its input.
2144 *
2145 * This function finishes the encryption or decryption of the message
2146 * formed by concatenating the inputs passed to preceding calls to
2147 * psa_cipher_update().
2148 *
2149 * When this function returns, the operation becomes inactive.
2150 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002151 * \param[in,out] operation Active cipher operation.
2152 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002153 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002154 * \param[out] output_length On success, the number of bytes
2155 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002156 *
2157 * \retval #PSA_SUCCESS
2158 * Success.
2159 * \retval #PSA_ERROR_BAD_STATE
2160 * The operation state is not valid (not started, IV required but
2161 * not set, or already completed).
2162 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2163 * The size of the \p output buffer is too small.
2164 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2165 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2166 * \retval #PSA_ERROR_HARDWARE_FAILURE
2167 * \retval #PSA_ERROR_TAMPERING_DETECTED
2168 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002169psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002170 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002171 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002172 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002173
Gilles Peskinedcd14942018-07-12 00:30:52 +02002174/** Abort a cipher operation.
2175 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002176 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002177 * \p operation structure itself. Once aborted, the operation object
2178 * can be reused for another operation by calling
2179 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002180 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002181 * You may call this function any time after the operation object has
2182 * been initialized by any of the following methods:
2183 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2184 * whether it succeeds or not.
2185 * - Initializing the \c struct to all-bits-zero.
2186 * - Initializing the \c struct to logical zeros, e.g.
2187 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002188 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002189 * In particular, calling psa_cipher_abort() after the operation has been
2190 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2191 * is safe and has no effect.
2192 *
2193 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002194 *
2195 * \retval #PSA_SUCCESS
2196 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002197 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002198 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2199 * \retval #PSA_ERROR_HARDWARE_FAILURE
2200 * \retval #PSA_ERROR_TAMPERING_DETECTED
2201 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002202psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2203
2204/**@}*/
2205
Gilles Peskine3b555712018-03-03 21:27:57 +01002206/** \defgroup aead Authenticated encryption with associated data (AEAD)
2207 * @{
2208 */
2209
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002210/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01002211 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002212 * \param alg An AEAD algorithm
2213 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002214 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002215 *
2216 * \return The tag size for the specified algorithm.
2217 * If the AEAD algorithm does not have an identified
2218 * tag that can be distinguished from the rest of
2219 * the ciphertext, return 0.
2220 * If the AEAD algorithm is not recognized, return 0.
2221 * An implementation may return either 0 or a
2222 * correct size for an AEAD algorithm that it
2223 * recognizes, but does not support.
2224 */
2225#define PSA_AEAD_TAG_SIZE(alg) \
2226 ((alg) == PSA_ALG_GCM ? 16 : \
2227 (alg) == PSA_ALG_CCM ? 16 : \
2228 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01002229
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002230/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002231 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002232 * \param key Slot containing the key to use.
2233 * \param alg The AEAD algorithm to compute
2234 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002235 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002236 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002237 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002238 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002239 * but not encrypted.
2240 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002241 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002242 * encrypted.
2243 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002244 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002245 * encrypted data. The additional data is not
2246 * part of this output. For algorithms where the
2247 * encrypted data and the authentication tag
2248 * are defined as separate outputs, the
2249 * authentication tag is appended to the
2250 * encrypted data.
2251 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2252 * This must be at least
2253 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2254 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002255 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002256 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002257 *
Gilles Peskine28538492018-07-11 17:34:00 +02002258 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002259 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002260 * \retval #PSA_ERROR_EMPTY_SLOT
2261 * \retval #PSA_ERROR_NOT_PERMITTED
2262 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002263 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002264 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002265 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002266 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2267 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2268 * \retval #PSA_ERROR_HARDWARE_FAILURE
2269 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01002270 */
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002271psa_status_t psa_aead_encrypt(psa_key_slot_t key,
2272 psa_algorithm_t alg,
2273 const uint8_t *nonce,
2274 size_t nonce_length,
2275 const uint8_t *additional_data,
2276 size_t additional_data_length,
2277 const uint8_t *plaintext,
2278 size_t plaintext_length,
2279 uint8_t *ciphertext,
2280 size_t ciphertext_size,
2281 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002282
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002283/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002284 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002285 * \param key Slot containing the key to use.
2286 * \param alg The AEAD algorithm to compute
2287 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002288 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002289 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002290 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002291 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002292 * but not encrypted.
2293 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002294 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002295 * encrypted. For algorithms where the
2296 * encrypted data and the authentication tag
2297 * are defined as separate inputs, the buffer
2298 * must contain the encrypted data followed
2299 * by the authentication tag.
2300 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002301 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002302 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2303 * This must be at least
2304 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2305 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002306 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03002307 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002308 *
Gilles Peskine28538492018-07-11 17:34:00 +02002309 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002310 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002311 * \retval #PSA_ERROR_EMPTY_SLOT
2312 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002313 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002314 * \retval #PSA_ERROR_NOT_PERMITTED
2315 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002316 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002317 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002318 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002319 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2320 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2321 * \retval #PSA_ERROR_HARDWARE_FAILURE
2322 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01002323 */
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002324psa_status_t psa_aead_decrypt(psa_key_slot_t key,
2325 psa_algorithm_t alg,
2326 const uint8_t *nonce,
2327 size_t nonce_length,
2328 const uint8_t *additional_data,
2329 size_t additional_data_length,
2330 const uint8_t *ciphertext,
2331 size_t ciphertext_length,
2332 uint8_t *plaintext,
2333 size_t plaintext_size,
2334 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002335
2336/**@}*/
2337
Gilles Peskine20035e32018-02-03 22:44:14 +01002338/** \defgroup asymmetric Asymmetric cryptography
2339 * @{
2340 */
2341
2342/**
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002343 * \brief ECDSA signature size for a given curve bit size
Gilles Peskine0189e752018-02-03 23:57:22 +01002344 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002345 * \param curve_bits Curve size in bits.
2346 * \return Signature size in bytes.
Gilles Peskine0189e752018-02-03 23:57:22 +01002347 *
2348 * \note This macro returns a compile-time constant if its argument is one.
Gilles Peskine0189e752018-02-03 23:57:22 +01002349 */
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002350#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
2351 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskine0189e752018-02-03 23:57:22 +01002352
Gilles Peskine0189e752018-02-03 23:57:22 +01002353/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002354 * \brief Sign a hash or short message with a private key.
2355 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002356 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002357 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002358 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2359 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2360 * to determine the hash algorithm to use.
2361 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002362 * \param key Key slot containing an asymmetric key pair.
2363 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002364 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002365 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002366 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002367 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002368 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002369 * \param[out] signature_length On success, the number of bytes
2370 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002371 *
Gilles Peskine28538492018-07-11 17:34:00 +02002372 * \retval #PSA_SUCCESS
2373 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002374 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002375 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002376 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002377 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002378 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002379 * \retval #PSA_ERROR_NOT_SUPPORTED
2380 * \retval #PSA_ERROR_INVALID_ARGUMENT
2381 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2382 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2383 * \retval #PSA_ERROR_HARDWARE_FAILURE
2384 * \retval #PSA_ERROR_TAMPERING_DETECTED
2385 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01002386 */
2387psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
2388 psa_algorithm_t alg,
2389 const uint8_t *hash,
2390 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002391 uint8_t *signature,
2392 size_t signature_size,
2393 size_t *signature_length);
2394
2395/**
2396 * \brief Verify the signature a hash or short message using a public key.
2397 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002398 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002399 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002400 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2401 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2402 * to determine the hash algorithm to use.
2403 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01002404 * \param key Key slot containing a public key or an
2405 * asymmetric key pair.
2406 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002407 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002408 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002409 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002410 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002411 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002412 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002413 *
Gilles Peskine28538492018-07-11 17:34:00 +02002414 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002415 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002416 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002417 * The calculation was perfomed successfully, but the passed
2418 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002419 * \retval #PSA_ERROR_NOT_SUPPORTED
2420 * \retval #PSA_ERROR_INVALID_ARGUMENT
2421 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2422 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2423 * \retval #PSA_ERROR_HARDWARE_FAILURE
2424 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01002425 */
2426psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
2427 psa_algorithm_t alg,
2428 const uint8_t *hash,
2429 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002430 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002431 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002432
Gilles Peskine723feff2018-05-31 20:08:13 +02002433#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
Gilles Peskine072ac562018-06-30 00:21:29 +02002434 (PSA_ALG_IS_RSA_OAEP(alg) ? \
2435 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskine723feff2018-05-31 20:08:13 +02002436 11 /*PKCS#1v1.5*/)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002437
2438/**
2439 * \brief Encrypt a short message with a public key.
2440 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002441 * \param key Key slot containing a public key or an
2442 * asymmetric key pair.
2443 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002444 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002445 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002446 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002447 * \param[in] salt A salt or label, if supported by the
2448 * encryption algorithm.
2449 * If the algorithm does not support a
2450 * salt, pass \c NULL.
2451 * If the algorithm supports an optional
2452 * salt and you do not want to pass a salt,
2453 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002454 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002455 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2456 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002457 * \param salt_length Size of the \p salt buffer in bytes.
2458 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002459 * \param[out] output Buffer where the encrypted message is to
2460 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002461 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002462 * \param[out] output_length On success, the number of bytes
2463 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002464 *
Gilles Peskine28538492018-07-11 17:34:00 +02002465 * \retval #PSA_SUCCESS
2466 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002467 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002468 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002469 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002470 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002471 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002472 * \retval #PSA_ERROR_NOT_SUPPORTED
2473 * \retval #PSA_ERROR_INVALID_ARGUMENT
2474 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2475 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2476 * \retval #PSA_ERROR_HARDWARE_FAILURE
2477 * \retval #PSA_ERROR_TAMPERING_DETECTED
2478 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002479 */
2480psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
2481 psa_algorithm_t alg,
2482 const uint8_t *input,
2483 size_t input_length,
2484 const uint8_t *salt,
2485 size_t salt_length,
2486 uint8_t *output,
2487 size_t output_size,
2488 size_t *output_length);
2489
2490/**
2491 * \brief Decrypt a short message with a private key.
2492 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002493 * \param key Key slot containing an asymmetric key pair.
2494 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002495 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002496 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002497 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002498 * \param[in] salt A salt or label, if supported by the
2499 * encryption algorithm.
2500 * If the algorithm does not support a
2501 * salt, pass \c NULL.
2502 * If the algorithm supports an optional
2503 * salt and you do not want to pass a salt,
2504 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002505 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002506 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2507 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002508 * \param salt_length Size of the \p salt buffer in bytes.
2509 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002510 * \param[out] output Buffer where the decrypted message is to
2511 * be written.
2512 * \param output_size Size of the \c output buffer in bytes.
2513 * \param[out] output_length On success, the number of bytes
2514 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002515 *
Gilles Peskine28538492018-07-11 17:34:00 +02002516 * \retval #PSA_SUCCESS
2517 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002518 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002519 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002520 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002521 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002522 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002523 * \retval #PSA_ERROR_NOT_SUPPORTED
2524 * \retval #PSA_ERROR_INVALID_ARGUMENT
2525 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2526 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2527 * \retval #PSA_ERROR_HARDWARE_FAILURE
2528 * \retval #PSA_ERROR_TAMPERING_DETECTED
2529 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2530 * \retval #PSA_ERROR_INVALID_PADDING
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002531 */
2532psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
2533 psa_algorithm_t alg,
2534 const uint8_t *input,
2535 size_t input_length,
2536 const uint8_t *salt,
2537 size_t salt_length,
2538 uint8_t *output,
2539 size_t output_size,
2540 size_t *output_length);
2541
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002542/**@}*/
2543
Gilles Peskineedd76872018-07-20 17:42:05 +02002544/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002545 * @{
2546 */
2547
2548/** The type of the state data structure for generators.
2549 *
2550 * Before calling any function on a generator, the application must
2551 * initialize it by any of the following means:
2552 * - Set the structure to all-bits-zero, for example:
2553 * \code
2554 * psa_crypto_generator_t generator;
2555 * memset(&generator, 0, sizeof(generator));
2556 * \endcode
2557 * - Initialize the structure to logical zero values, for example:
2558 * \code
2559 * psa_crypto_generator_t generator = {0};
2560 * \endcode
2561 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2562 * for example:
2563 * \code
2564 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2565 * \endcode
2566 * - Assign the result of the function psa_crypto_generator_init()
2567 * to the structure, for example:
2568 * \code
2569 * psa_crypto_generator_t generator;
2570 * generator = psa_crypto_generator_init();
2571 * \endcode
2572 *
2573 * This is an implementation-defined \c struct. Applications should not
2574 * make any assumptions about the content of this structure except
2575 * as directed by the documentation of a specific implementation.
2576 */
2577typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2578
2579/** \def PSA_CRYPTO_GENERATOR_INIT
2580 *
2581 * This macro returns a suitable initializer for a generator object
2582 * of type #psa_crypto_generator_t.
2583 */
2584#ifdef __DOXYGEN_ONLY__
2585/* This is an example definition for documentation purposes.
2586 * Implementations should define a suitable value in `crypto_struct.h`.
2587 */
2588#define PSA_CRYPTO_GENERATOR_INIT {0}
2589#endif
2590
2591/** Return an initial value for a generator object.
2592 */
2593static psa_crypto_generator_t psa_crypto_generator_init(void);
2594
2595/** Retrieve the current capacity of a generator.
2596 *
2597 * The capacity of a generator is the maximum number of bytes that it can
2598 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2599 *
2600 * \param[in] generator The generator to query.
2601 * \param[out] capacity On success, the capacity of the generator.
2602 *
2603 * \retval PSA_SUCCESS
2604 * \retval PSA_ERROR_BAD_STATE
2605 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2606 */
2607psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2608 size_t *capacity);
2609
2610/** Read some data from a generator.
2611 *
2612 * This function reads and returns a sequence of bytes from a generator.
2613 * The data that is read is discarded from the generator. The generator's
2614 * capacity is decreased by the number of bytes read.
2615 *
2616 * \param[in,out] generator The generator object to read from.
2617 * \param[out] output Buffer where the generator output will be
2618 * written.
2619 * \param output_length Number of bytes to output.
2620 *
2621 * \retval PSA_SUCCESS
2622 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2623 * There were fewer than \p output_length bytes
2624 * in the generator. Note that in this case, no
2625 * output is written to the output buffer.
2626 * The generator's capacity is set to 0, thus
2627 * subsequent calls to this function will not
2628 * succeed, even with a smaller output buffer.
2629 * \retval PSA_ERROR_BAD_STATE
2630 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2631 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2632 * \retval PSA_ERROR_HARDWARE_FAILURE
2633 * \retval PSA_ERROR_TAMPERING_DETECTED
2634 */
2635psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2636 uint8_t *output,
2637 size_t output_length);
2638
2639/** Create a symmetric key from data read from a generator.
2640 *
2641 * This function reads a sequence of bytes from a generator and imports
2642 * these bytes as a key.
2643 * The data that is read is discarded from the generator. The generator's
2644 * capacity is decreased by the number of bytes read.
2645 *
2646 * This function is equivalent to calling #psa_generator_read and
2647 * passing the resulting output to #psa_import_key, but
2648 * if the implementation provides an isolation boundary then
2649 * the key material is not exposed outside the isolation boundary.
2650 *
2651 * \param key Slot where the key will be stored. This must be a
2652 * valid slot for a key of the chosen type. It must
2653 * be unoccupied.
2654 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2655 * This must be a symmetric key type.
2656 * \param bits Key size in bits.
2657 * \param[in,out] generator The generator object to read from.
2658 *
2659 * \retval PSA_SUCCESS
2660 * Success.
2661 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2662 * There were fewer than \p output_length bytes
2663 * in the generator. Note that in this case, no
2664 * output is written to the output buffer.
2665 * The generator's capacity is set to 0, thus
2666 * subsequent calls to this function will not
2667 * succeed, even with a smaller output buffer.
2668 * \retval PSA_ERROR_NOT_SUPPORTED
2669 * The key type or key size is not supported, either by the
2670 * implementation in general or in this particular slot.
2671 * \retval PSA_ERROR_BAD_STATE
2672 * \retval PSA_ERROR_INVALID_ARGUMENT
2673 * The key slot is invalid.
2674 * \retval PSA_ERROR_OCCUPIED_SLOT
2675 * There is already a key in the specified slot.
2676 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2677 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
2678 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2679 * \retval PSA_ERROR_HARDWARE_FAILURE
2680 * \retval PSA_ERROR_TAMPERING_DETECTED
2681 */
2682psa_status_t psa_generator_import_key(psa_key_slot_t key,
2683 psa_key_type_t type,
2684 size_t bits,
2685 psa_crypto_generator_t *generator);
2686
2687/** Abort a generator.
2688 *
2689 * Once a generator has been aborted, its capacity is zero.
2690 * Aborting a generator frees all associated resources except for the
2691 * \c generator structure itself.
2692 *
2693 * This function may be called at any time as long as the generator
2694 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2695 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2696 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2697 * on a generator that has not been set up.
2698 *
2699 * Once aborted, the generator object may be called.
2700 *
2701 * \param[in,out] generator The generator to abort.
2702 *
2703 * \retval PSA_SUCCESS
2704 * \retval PSA_ERROR_BAD_STATE
2705 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2706 * \retval PSA_ERROR_HARDWARE_FAILURE
2707 * \retval PSA_ERROR_TAMPERING_DETECTED
2708 */
2709psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2710
2711/**@}*/
2712
Gilles Peskineea0fb492018-07-12 17:17:20 +02002713/** \defgroup derivation Key derivation
2714 * @{
2715 */
2716
2717/** Set up a key derivation operation.
2718 *
2719 * A key derivation algorithm takes three inputs: a secret input \p key and
2720 * two non-secret inputs \p label and p salt.
2721 * The result of this function is a byte generator which can
2722 * be used to produce keys and other cryptographic material.
2723 *
2724 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002725 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2726 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002727 *
2728 * \param[in,out] generator The generator object to set up. It must
2729 * have been initialized to .
2730 * \param key Slot containing the secret key to use.
2731 * \param alg The key derivation algorithm to compute
2732 * (\c PSA_ALG_XXX value such that
2733 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2734 * \param[in] salt Salt to use.
2735 * \param salt_length Size of the \p salt buffer in bytes.
2736 * \param[in] label Label to use.
2737 * \param label_length Size of the \p label buffer in bytes.
2738 * \param capacity The maximum number of bytes that the
2739 * generator will be able to provide.
2740 *
2741 * \retval #PSA_SUCCESS
2742 * Success.
2743 * \retval #PSA_ERROR_EMPTY_SLOT
2744 * \retval #PSA_ERROR_NOT_PERMITTED
2745 * \retval #PSA_ERROR_INVALID_ARGUMENT
2746 * \c key is not compatible with \c alg,
2747 * or \p capacity is too large for the specified algorithm and key.
2748 * \retval #PSA_ERROR_NOT_SUPPORTED
2749 * \c alg is not supported or is not a key derivation algorithm.
2750 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2751 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2752 * \retval #PSA_ERROR_HARDWARE_FAILURE
2753 * \retval #PSA_ERROR_TAMPERING_DETECTED
2754 */
2755psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01002756 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002757 psa_algorithm_t alg,
2758 const uint8_t *salt,
2759 size_t salt_length,
2760 const uint8_t *label,
2761 size_t label_length,
2762 size_t capacity);
2763
2764/**@}*/
2765
Gilles Peskineedd76872018-07-20 17:42:05 +02002766/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002767 * @{
2768 */
2769
2770/**
2771 * \brief Generate random bytes.
2772 *
2773 * \warning This function **can** fail! Callers MUST check the return status
2774 * and MUST NOT use the content of the output buffer if the return
2775 * status is not #PSA_SUCCESS.
2776 *
2777 * \note To generate a key, use psa_generate_key() instead.
2778 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002779 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002780 * \param output_size Number of bytes to generate and output.
2781 *
Gilles Peskine28538492018-07-11 17:34:00 +02002782 * \retval #PSA_SUCCESS
2783 * \retval #PSA_ERROR_NOT_SUPPORTED
2784 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2785 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2786 * \retval #PSA_ERROR_HARDWARE_FAILURE
2787 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002788 */
2789psa_status_t psa_generate_random(uint8_t *output,
2790 size_t output_size);
2791
Gilles Peskine4c317f42018-07-12 01:24:09 +02002792/** Extra parameters for RSA key generation.
2793 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002794 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002795 * parameter to psa_generate_key().
2796 */
2797typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002798 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002799} psa_generate_key_extra_rsa;
2800
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002801/**
2802 * \brief Generate a key or key pair.
2803 *
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002804 * \param key Slot where the key will be stored. This must be a
2805 * valid slot for a key of the chosen type. It must
2806 * be unoccupied.
2807 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2808 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002809 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002810 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002811 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002812 * default parameters. Implementation that support
2813 * the generation of vendor-specific key types
2814 * that allow extra parameters shall document
2815 * the format of these extra parameters and
2816 * the default values. For standard parameters,
2817 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002818 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002819 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2820 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002821 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002822 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2823 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002824 * - For an RSA key (\p type is
2825 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2826 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002827 * specifying the public exponent. The
2828 * default public exponent used when \p extra
2829 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002830 * \param extra_size Size of the buffer that \p extra
2831 * points to, in bytes. Note that if \p extra is
2832 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002833 *
Gilles Peskine28538492018-07-11 17:34:00 +02002834 * \retval #PSA_SUCCESS
2835 * \retval #PSA_ERROR_NOT_SUPPORTED
2836 * \retval #PSA_ERROR_INVALID_ARGUMENT
2837 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2838 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2839 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2840 * \retval #PSA_ERROR_HARDWARE_FAILURE
2841 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002842 */
2843psa_status_t psa_generate_key(psa_key_slot_t key,
2844 psa_key_type_t type,
2845 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002846 const void *extra,
2847 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002848
2849/**@}*/
2850
Gilles Peskinee59236f2018-01-27 23:32:46 +01002851#ifdef __cplusplus
2852}
2853#endif
2854
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002855/* The file "crypto_sizes.h" contains definitions for size calculation
2856 * macros whose definitions are implementation-specific. */
2857#include "crypto_sizes.h"
2858
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002859/* The file "crypto_struct.h" contains definitions for
2860 * implementation-specific structs that are declared above. */
2861#include "crypto_struct.h"
2862
2863/* The file "crypto_extra.h" contains vendor-specific definitions. This
2864 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002865#include "crypto_extra.h"
2866
2867#endif /* PSA_CRYPTO_H */