blob: 0ba34929be9e2e2c6e742b32805caf0495056414 [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
70 * until we unify error reporting in PSA IPC and PSA crypo.
71 *
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
92/** The requested operation or a parameter is not supported
93 * by this implementation.
94 *
95 * Implementations should return this error code when an enumeration
96 * parameter such as a key type, algorithm, etc. is not recognized.
97 * If a combination of parameters is recognized and identified as
98 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
99#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)1)
100
101/** The requested action is denied by a policy.
102 *
103 * Implementations should return this error code when the parameters
104 * are recognized as valid and supported, and a policy explicitly
105 * denies the requested operation.
106 *
107 * If a subset of the parameters of a function call identify a
108 * forbidden operation, and another subset of the parameters are
109 * not valid or not supported, it is unspecified whether the function
110 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
111 * #PSA_ERROR_INVALID_ARGUMENT. */
112#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)2)
113
114/** An output buffer is too small.
115 *
Gilles Peskinebe42f312018-07-13 14:38:15 +0200116 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
itayzafrirc2a79762018-06-18 16:20:16 +0300117 * description to determine a sufficient buffer size.
118 *
119 * Implementations should preferably return this error code only
120 * in cases when performing the operation with a larger output
121 * buffer would succeed. However implementations may return this
122 * error if a function has invalid or unsupported parameters in addition
123 * to the parameters that determine the necessary output buffer size. */
124#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)3)
125
126/** A slot is occupied, but must be empty to carry out the
127 * requested action.
128 *
129 * If the slot number is invalid (i.e. the requested action could
130 * not be performed even after erasing the slot's content),
131 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
132#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)4)
133
134/** A slot is empty, but must be occupied to carry out the
135 * requested action.
136 *
137 * If the slot number is invalid (i.e. the requested action could
138 * not be performed even after creating appropriate content in the slot),
139 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
140#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)5)
141
142/** The requested action cannot be performed in the current state.
143 *
144 * Multipart operations return this error when one of the
145 * functions is called out of sequence. Refer to the function
146 * descriptions for permitted sequencing of functions.
147 *
148 * Implementations shall not return this error code to indicate
149 * that a key slot is occupied when it needs to be free or vice versa,
150 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
151 * as applicable. */
152#define PSA_ERROR_BAD_STATE ((psa_status_t)6)
153
154/** The parameters passed to the function are invalid.
155 *
156 * Implementations may return this error any time a parameter or
157 * combination of parameters are recognized as invalid.
158 *
159 * Implementations shall not return this error code to indicate
160 * that a key slot is occupied when it needs to be free or vice versa,
161 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
162 * as applicable. */
163#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)7)
164
165/** There is not enough runtime memory.
166 *
167 * If the action is carried out across multiple security realms, this
168 * error can refer to available memory in any of the security realms. */
169#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)8)
170
171/** There is not enough persistent storage.
172 *
173 * Functions that modify the key storage return this error code if
174 * there is insufficient storage space on the host media. In addition,
175 * many functions that do not otherwise access storage may return this
176 * error code if the implementation requires a mandatory log entry for
177 * the requested action and the log storage space is full. */
178#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)9)
179
180/** There was a communication failure inside the implementation.
181 *
182 * This can indicate a communication failure between the application
183 * and an external cryptoprocessor or between the cryptoprocessor and
184 * an external volatile or persistent memory. A communication failure
185 * may be transient or permanent depending on the cause.
186 *
187 * \warning If a function returns this error, it is undetermined
188 * whether the requested action has completed or not. Implementations
189 * should return #PSA_SUCCESS on successful completion whenver
190 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
191 * if the requested action was completed successfully in an external
192 * cryptoprocessor but there was a breakdown of communication before
193 * the cryptoprocessor could report the status to the application.
194 */
195#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)10)
196
197/** There was a storage failure that may have led to data loss.
198 *
199 * This error indicates that some persistent storage is corrupted.
200 * It should not be used for a corruption of volatile memory
201 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
202 * between the cryptoprocessor and its external storage (use
203 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
204 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
205 *
206 * Note that a storage failure does not indicate that any data that was
207 * previously read is invalid. However this previously read data may no
208 * longer be readable from storage.
209 *
210 * When a storage failure occurs, it is no longer possible to ensure
211 * the global integrity of the keystore. Depending on the global
212 * integrity guarantees offered by the implementation, access to other
213 * data may or may not fail even if the data is still readable but
214 * its integrity canont be guaranteed.
215 *
216 * Implementations should only use this error code to report a
217 * permanent storage corruption. However application writers should
218 * keep in mind that transient errors while reading the storage may be
219 * reported using this error code. */
220#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)11)
221
222/** A hardware failure was detected.
223 *
224 * A hardware failure may be transient or permanent depending on the
225 * cause. */
226#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)12)
227
228/** A tampering attempt was detected.
229 *
230 * If an application receives this error code, there is no guarantee
231 * that previously accessed or computed data was correct and remains
232 * confidential. Applications should not perform any security function
233 * and should enter a safe failure state.
234 *
235 * Implementations may return this error code if they detect an invalid
236 * state that cannot happen during normal operation and that indicates
237 * that the implementation's security guarantees no longer hold. Depending
238 * on the implementation architecture and on its security and safety goals,
239 * the implementation may forcibly terminate the application.
240 *
241 * This error code is intended as a last resort when a security breach
242 * is detected and it is unsure whether the keystore data is still
243 * protected. Implementations shall only return this error code
244 * to report an alarm from a tampering detector, to indicate that
245 * the confidentiality of stored data can no longer be guaranteed,
246 * or to indicate that the integrity of previously returned data is now
247 * considered compromised. Implementations shall not use this error code
248 * to indicate a hardware failure that merely makes it impossible to
249 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
250 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
251 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
252 * instead).
253 *
254 * This error indicates an attack against the application. Implementations
255 * shall not return this error code as a consequence of the behavior of
256 * the application itself. */
257#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)13)
258
259/** There is not enough entropy to generate random data needed
260 * for the requested action.
261 *
262 * This error indicates a failure of a hardware random generator.
263 * Application writers should note that this error can be returned not
264 * only by functions whose purpose is to generate random data, such
265 * as key, IV or nonce generation, but also by functions that execute
266 * an algorithm with a randomized result, as well as functions that
267 * use randomization of intermediate computations as a countermeasure
268 * to certain attacks.
269 *
270 * Implementations should avoid returning this error after psa_crypto_init()
271 * has succeeded. Implementations should generate sufficient
272 * entropy during initialization and subsequently use a cryptographically
273 * secure pseudorandom generator (PRNG). However implementations may return
274 * this error at any time if a policy requires the PRNG to be reseeded
275 * during normal operation. */
276#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)14)
277
278/** The signature, MAC or hash is incorrect.
279 *
280 * Verification functions return this error if the verification
281 * calculations completed successfully, and the value to be verified
282 * was determined to be incorrect.
283 *
284 * If the value to verify has an invalid size, implementations may return
285 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
286#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)15)
287
288/** The decrypted padding is incorrect.
289 *
290 * \warning In some protocols, when decrypting data, it is essential that
291 * the behavior of the application does not depend on whether the padding
292 * is correct, down to precise timing. Applications should prefer
293 * protocols that use authenticated encryption rather than plain
294 * encryption. If the application must perform a decryption of
295 * unauthenticated data, the application writer should take care not
296 * to reveal whether the padding is invalid.
297 *
298 * Implementations should strive to make valid and invalid padding
299 * as close as possible to indistinguishable to an external observer.
300 * In particular, the timing of a decryption operation should not
301 * depend on the validity of the padding. */
302#define PSA_ERROR_INVALID_PADDING ((psa_status_t)16)
303
Gilles Peskineeab56e42018-07-12 17:12:33 +0200304/** The generator has insufficient capacity left.
305 *
306 * Once a function returns this error, attempts to read from the
307 * generator will always return this error. */
308#define PSA_ERROR_INSUFFICIENT_CAPACITY ((psa_status_t)17)
309
itayzafrirc2a79762018-06-18 16:20:16 +0300310/** An error occurred that does not correspond to any defined
311 * failure cause.
312 *
313 * Implementations may use this error code if none of the other standard
314 * error codes are applicable. */
Gilles Peskineeab56e42018-07-12 17:12:33 +0200315#define PSA_ERROR_UNKNOWN_ERROR ((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 Peskine98f0a242018-02-06 18:57:29 +0100363#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x7e000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200364
Gilles Peskine35855962018-04-19 08:39:16 +0200365/** Raw data.
366 *
367 * A "key" of this type cannot be used for any cryptographic operation.
368 * Applications may use this type to store arbitrary data in the keystore. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100369#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x02000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200370
Gilles Peskine98f0a242018-02-06 18:57:29 +0100371#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x04000000)
372#define PSA_KEY_TYPE_CATEGORY_ASYMMETRIC ((psa_key_type_t)0x06000000)
373#define PSA_KEY_TYPE_PAIR_FLAG ((psa_key_type_t)0x01000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100374
Gilles Peskine35855962018-04-19 08:39:16 +0200375/** HMAC key.
376 *
377 * The key policy determines which underlying hash algorithm the key can be
378 * used for.
379 *
380 * HMAC keys should generally have the same size as the underlying hash.
Gilles Peskinebe42f312018-07-13 14:38:15 +0200381 * This size can be calculated with #PSA_HASH_SIZE(\c alg) where
382 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100383#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x02000001)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200384
Gilles Peskineea0fb492018-07-12 17:17:20 +0200385/** A secret for key derivation.
386 *
387 * The key policy determines which key derivation algorithm the key
388 * can be used for.
389 */
390#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x02000101)
391
Gilles Peskine35855962018-04-19 08:39:16 +0200392/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
393 *
394 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
395 * 32 bytes (AES-256).
396 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100397#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x04000001)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200398
Gilles Peskine35855962018-04-19 08:39:16 +0200399/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
400 *
401 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
402 * 24 bytes (3-key 3DES).
403 *
404 * Note that single DES and 2-key 3DES are weak and strongly
405 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
406 * is weak and deprecated and should only be used in legacy protocols.
407 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100408#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x04000002)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200409
Gilles Peskine35855962018-04-19 08:39:16 +0200410/** Key for an cipher, AEAD or MAC algorithm based on the
411 * Camellia block cipher. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100412#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x04000003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200413
Gilles Peskine35855962018-04-19 08:39:16 +0200414/** Key for the RC4 stream cipher.
415 *
416 * Note that RC4 is weak and deprecated and should only be used in
417 * legacy protocols. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100418#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x04000004)
419
Gilles Peskine308b91d2018-02-08 09:47:44 +0100420/** RSA public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100421#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x06010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100422/** RSA key pair (private and public key). */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100423#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x07010000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200424
Gilles Peskine06dc2632018-03-08 07:47:25 +0100425/** DSA public key. */
426#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x06020000)
427/** DSA key pair (private and public key). */
428#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x07020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200429
Gilles Peskine06dc2632018-03-08 07:47:25 +0100430#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x06030000)
431#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x07030000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100432#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200433/** Elliptic curve key pair. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100434#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
435 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200436/** Elliptic curve public key. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100437#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
438 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100439
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100440/** Whether a key type is vendor-defined. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100441#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100442 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100443
444/** Whether a key type is asymmetric: either a key pair or a public key. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100445#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
446 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100447/** Whether a key type is the public part of a key pair. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100448#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
Moran Pekerb4d0ddd2018-04-04 12:47:52 +0300449 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
450 PSA_KEY_TYPE_CATEGORY_ASYMMETRIC)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100451/** Whether a key type is a key pair containing a private part and a public
452 * part. */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100453#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
454 (((type) & (PSA_KEY_TYPE_CATEGORY_MASK | PSA_KEY_TYPE_PAIR_FLAG)) == \
455 (PSA_KEY_TYPE_CATEGORY_ASYMMETRIC | PSA_KEY_TYPE_PAIR_FLAG))
Gilles Peskine06dc2632018-03-08 07:47:25 +0100456/** The key pair type corresponding to a public key type. */
457#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
458 ((type) | PSA_KEY_TYPE_PAIR_FLAG)
459/** The public key type corresponding to a key pair type. */
460#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
461 ((type) & ~PSA_KEY_TYPE_PAIR_FLAG)
Gilles Peskined8008d62018-06-29 19:51:51 +0200462/** Whether a key type is an RSA key (pair or public-only). */
463#define PSA_KEY_TYPE_IS_RSA(type) \
Gilles Peskine3bd1a422018-07-19 11:55:51 +0200464 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
465
Gilles Peskined8008d62018-06-29 19:51:51 +0200466/** Whether a key type is an elliptic curve key (pair or public-only). */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100467#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100468 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
469 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine55728b02018-07-16 23:08:16 +0200470#define PSA_KEY_TYPE_IS_ECC_KEYPAIR(type) \
471 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
472 PSA_KEY_TYPE_ECC_KEYPAIR_BASE)
473#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
474 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
475 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100476
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200477/** The type of PSA elliptic curve identifiers. */
478typedef uint16_t psa_ecc_curve_t;
479/** Extract the curve from an elliptic curve key type. */
480#define PSA_KEY_TYPE_GET_CURVE(type) \
481 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
482 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
483 0))
484
485/* The encoding of curve identifiers is currently aligned with the
486 * TLS Supported Groups Registry (formerly known as the
487 * TLS EC Named Curve Registry)
488 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
489 * The values are defined by RFC 4492, RFC 7027 and RFC 7919. */
490#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
491#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
492#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
493#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
494#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
495#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
496#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
497#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
498#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
499#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
500#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
501#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
502#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
503#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
504#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
505#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
506#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
507#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
508#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
509#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
510#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
511#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
512#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
513#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
514#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
515#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
516#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
517#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
518#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
519#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
520#define PSA_ECC_CURVE_FFDHE_2048 ((psa_ecc_curve_t) 0x0100)
521#define PSA_ECC_CURVE_FFDHE_3072 ((psa_ecc_curve_t) 0x0101)
522#define PSA_ECC_CURVE_FFDHE_4096 ((psa_ecc_curve_t) 0x0102)
523#define PSA_ECC_CURVE_FFDHE_6144 ((psa_ecc_curve_t) 0x0103)
524#define PSA_ECC_CURVE_FFDHE_8192 ((psa_ecc_curve_t) 0x0104)
525
Gilles Peskine7e198532018-03-08 07:50:30 +0100526/** The block size of a block cipher.
527 *
528 * \param type A cipher key type (value of type #psa_key_type_t).
529 *
530 * \return The block size for a block cipher, or 1 for a stream cipher.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200531 * The return value is undefined if \p type is not a supported
Gilles Peskine35855962018-04-19 08:39:16 +0200532 * cipher key type.
533 *
534 * \note It is possible to build stream cipher algorithms on top of a block
535 * cipher, for example CTR mode (#PSA_ALG_CTR).
536 * This macro only takes the key type into account, so it cannot be
537 * used to determine the size of the data that #psa_cipher_update()
538 * might buffer for future processing in general.
Gilles Peskine7e198532018-03-08 07:50:30 +0100539 *
540 * \note This macro returns a compile-time constant if its argument is one.
541 *
542 * \warning This macro may evaluate its argument multiple times.
543 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100544#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100545 ( \
546 (type) == PSA_KEY_TYPE_AES ? 16 : \
547 (type) == PSA_KEY_TYPE_DES ? 8 : \
548 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100549 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100550 0)
551
Gilles Peskine308b91d2018-02-08 09:47:44 +0100552/** \brief Encoding of a cryptographic algorithm.
553 *
554 * For algorithms that can be applied to multiple key types, this type
555 * does not encode the key type. For example, for symmetric ciphers
556 * based on a block cipher, #psa_algorithm_t encodes the block cipher
557 * mode and the padding mode while the block cipher itself is encoded
558 * via #psa_key_type_t.
559 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100560typedef uint32_t psa_algorithm_t;
561
Gilles Peskine98f0a242018-02-06 18:57:29 +0100562#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
563#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
564#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
565#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
566#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
567#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
568#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
569#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
570#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
571#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100572
Gilles Peskine98f0a242018-02-06 18:57:29 +0100573#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
574 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200575
Gilles Peskine308b91d2018-02-08 09:47:44 +0100576/** Whether the specified algorithm is a hash algorithm.
577 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100578 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100579 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200580 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
581 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskine7e198532018-03-08 07:50:30 +0100582 * algorithm identifier.
583 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100584#define PSA_ALG_IS_HASH(alg) \
585 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200586
587/** Whether the specified algorithm is a MAC algorithm.
588 *
589 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
590 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200591 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
592 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200593 * algorithm identifier.
594 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100595#define PSA_ALG_IS_MAC(alg) \
596 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200597
598/** Whether the specified algorithm is a symmetric cipher algorithm.
599 *
600 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
601 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200602 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
603 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200604 * algorithm identifier.
605 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100606#define PSA_ALG_IS_CIPHER(alg) \
607 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200608
609/** Whether the specified algorithm is an authenticated encryption
610 * with associated data (AEAD) algorithm.
611 *
612 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
613 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200614 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
615 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200616 * algorithm identifier.
617 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100618#define PSA_ALG_IS_AEAD(alg) \
619 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200620
621/** Whether the specified algorithm is a public-key signature algorithm.
622 *
623 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
624 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200625 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
626 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200627 * algorithm identifier.
628 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100629#define PSA_ALG_IS_SIGN(alg) \
630 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200631
632/** Whether the specified algorithm is a public-key encryption algorithm.
633 *
634 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
635 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200636 * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
637 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200638 * algorithm identifier.
639 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100640#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
641 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200642
643/** Whether the specified algorithm is a key agreement algorithm.
644 *
645 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
646 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200647 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
648 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200649 * algorithm identifier.
650 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100651#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
652 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200653
654/** Whether the specified algorithm is a key derivation algorithm.
655 *
656 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
657 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200658 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
659 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200660 * algorithm identifier.
661 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100662#define PSA_ALG_IS_KEY_DERIVATION(alg) \
663 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
664
665#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
666#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
667#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
668#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100669#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
670#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskineedd76872018-07-20 17:42:05 +0200671/** SHA2-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100672#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
Gilles Peskineedd76872018-07-20 17:42:05 +0200673/** SHA2-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100674#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
Gilles Peskineedd76872018-07-20 17:42:05 +0200675/** SHA2-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100676#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
Gilles Peskineedd76872018-07-20 17:42:05 +0200677/** SHA2-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100678#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
Gilles Peskineedd76872018-07-20 17:42:05 +0200679/** SHA2-512/224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100680#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
Gilles Peskineedd76872018-07-20 17:42:05 +0200681/** SHA2-512/256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100682#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
Gilles Peskineedd76872018-07-20 17:42:05 +0200683/** SHA3-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100684#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
Gilles Peskineedd76872018-07-20 17:42:05 +0200685/** SHA3-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100686#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
Gilles Peskineedd76872018-07-20 17:42:05 +0200687/** SHA3-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100688#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
Gilles Peskineedd76872018-07-20 17:42:05 +0200689/** SHA3-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100690#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
691
Gilles Peskine8c9def32018-02-08 10:02:12 +0100692#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100693#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200694/** Macro to build an HMAC algorithm.
695 *
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200696 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
Gilles Peskine35855962018-04-19 08:39:16 +0200697 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200698 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200699 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine35855962018-04-19 08:39:16 +0200700 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200701 * \return The corresponding HMAC algorithm.
702 * \return Unspecified if \p alg is not a supported
703 * hash algorithm.
Gilles Peskine35855962018-04-19 08:39:16 +0200704 */
705#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100706 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200707
Gilles Peskine8c9def32018-02-08 10:02:12 +0100708#define PSA_ALG_HMAC_HASH(hmac_alg) \
709 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200710
711/** Whether the specified algorithm is an HMAC algorithm.
712 *
713 * HMAC is a family of MAC algorithms that are based on a hash function.
714 *
715 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
716 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200717 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
718 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200719 * algorithm identifier.
720 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100721#define PSA_ALG_IS_HMAC(alg) \
722 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
723 PSA_ALG_HMAC_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200724
Gilles Peskine8c9def32018-02-08 10:02:12 +0100725#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
726#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
727#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
728#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200729
730/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
731 *
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200732 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
733 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200734 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
735 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200736 * algorithm identifier.
737 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100738#define PSA_ALG_IS_CIPHER_MAC(alg) \
739 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
740 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100741
Gilles Peskine8c9def32018-02-08 10:02:12 +0100742#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100743#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100744#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100745#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200746
747/** Use a block cipher mode without padding.
748 *
749 * This padding mode may only be used with messages whose lengths are a
750 * whole number of blocks for the chosen block cipher.
751 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100752#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200753
Gilles Peskine98f0a242018-02-06 18:57:29 +0100754#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200755
756/** Whether the specified algorithm is a block cipher.
757 *
758 * A block cipher is a symmetric cipher that encrypts or decrypts messages
759 * by chopping them into fixed-size blocks. Processing a message requires
760 * applying a _padding mode_ to transform the message into one whose
761 * length is a whole number of blocks. To construct an algorithm
762 * identifier for a block cipher, apply a bitwise-or between the block
763 * cipher mode and the padding mode. For example, CBC with PKCS#7 padding
764 * is `PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7`.
765 *
766 * The transformation applied to each block is determined by the key type.
767 * For example, to use AES-128-CBC-PKCS7, use the algorithm above with
768 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
769 *
770 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
771 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200772 * \return 1 if \p alg is a block cipher algorithm, 0 otherwise.
773 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200774 * algorithm identifier or if it is not a symmetric cipher algorithm.
775 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100776#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
777 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
778 PSA_ALG_BLOCK_CIPHER_BASE)
779
Gilles Peskinedcd14942018-07-12 00:30:52 +0200780/** The CBC block cipher mode.
781 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100782#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100783#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
784#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
785#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200786
787#define PSA_ALG_STREAM_CIPHER_BASE ((psa_algorithm_t)0x04800000)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200788
Gilles Peskinedcd14942018-07-12 00:30:52 +0200789/** The CTR stream cipher mode.
790 *
791 * CTR is a stream cipher which is built from a block cipher. The
792 * underlying block cipher is determined by the key type. For example,
793 * to use AES-128-CTR, use this algorithm with
794 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
795 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100796#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200797
Gilles Peskinedcd14942018-07-12 00:30:52 +0200798/** The ARC4 stream cipher algorithm.
799 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100800#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100801
Gilles Peskinedcd14942018-07-12 00:30:52 +0200802/** Whether the specified algorithm is a stream cipher.
803 *
804 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
805 * by applying a bitwise-xor with a stream of bytes that is generated
806 * from a key.
807 *
808 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
809 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200810 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
811 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200812 * algorithm identifier or if it is not a symmetric cipher algorithm.
813 */
Moran Pekerbed71a22018-04-22 20:19:20 +0300814#define PSA_ALG_IS_STREAM_CIPHER(alg) \
815 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200816 PSA_ALG_STREAM_CIPHER_BASE)
Moran Pekerbed71a22018-04-22 20:19:20 +0300817
Gilles Peskine8c9def32018-02-08 10:02:12 +0100818#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
819#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100820
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200821#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
822/** RSA PKCS#1 v1.5 signature with hashing.
823 *
824 * This is the signature scheme defined by RFC 8017
825 * (PKCS#1: RSA Cryptography Specifications) under the name
826 * RSASSA-PKCS1-v1_5.
827 *
828 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200829 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200830 *
831 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
832 * \return Unspecified if \p alg is not a supported
833 * hash algorithm.
834 */
Gilles Peskinea5926232018-03-28 14:16:50 +0200835#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200836 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
837/** Raw PKCS#1 v1.5 signature.
838 *
839 * The input to this algorithm is the DigestInfo structure used by
840 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
841 * steps 3&ndash;6.
842 */
843#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
Gilles Peskinea5926232018-03-28 14:16:50 +0200844#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200845 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200846
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200847#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
848/** RSA PSS signature with hashing.
849 *
850 * This is the signature scheme defined by RFC 8017
851 * (PKCS#1: RSA Cryptography Specifications) under the name
Gilles Peskinea4d20bd2018-06-29 23:35:02 +0200852 * RSASSA-PSS, with the message generation function MGF1, and with
853 * a salt length equal to the length of the hash. The specified
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200854 * hash algorithm is used to hash the input message, to create the
855 * salted hash, and for the mask generation.
856 *
857 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200858 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200859 *
860 * \return The corresponding RSA PSS signature algorithm.
861 * \return Unspecified if \p alg is not a supported
862 * hash algorithm.
863 */
864#define PSA_ALG_RSA_PSS(hash_alg) \
865 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
866#define PSA_ALG_IS_RSA_PSS(alg) \
867 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
868
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200869#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
870/** DSA signature with hashing.
871 *
872 * This is the signature scheme defined by FIPS 186-4,
873 * with a random per-message secret number (*k*).
874 *
875 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200876 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200877 *
878 * \return The corresponding DSA signature algorithm.
879 * \return Unspecified if \p alg is not a supported
880 * hash algorithm.
881 */
882#define PSA_ALG_DSA(hash_alg) \
883 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
884#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
885#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
886#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
887 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
888#define PSA_ALG_IS_DSA(alg) \
889 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
890 PSA_ALG_DSA_BASE)
891#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
892 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +0200893#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
894 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
895#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
896 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200897
898#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
899/** ECDSA signature with hashing.
900 *
901 * This is the ECDSA signature scheme defined by ANSI X9.62,
902 * with a random per-message secret number (*k*).
903 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200904 * The representation of the signature as a byte string consists of
905 * the concatentation of the signature values *r* and *s*. Each of
906 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
907 * of the base point of the curve in octets. Each value is represented
908 * in big-endian order (most significant octet first).
909 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200910 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200911 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200912 *
913 * \return The corresponding ECDSA signature algorithm.
914 * \return Unspecified if \p alg is not a supported
915 * hash algorithm.
916 */
917#define PSA_ALG_ECDSA(hash_alg) \
918 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
919/** ECDSA signature without hashing.
920 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200921 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200922 * without specifying a hash algorithm. This algorithm may only be
923 * used to sign or verify a sequence of bytes that should be an
924 * already-calculated hash. Note that the input is padded with
925 * zeros on the left or truncated on the left as required to fit
926 * the curve size.
927 */
928#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
929#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
930/** Deterministic ECDSA signature with hashing.
931 *
932 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
933 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200934 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
935 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200936 * Note that when this algorithm is used for verification, signatures
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200937 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200938 * same private key are accepted. In other words,
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200939 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
940 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200941 *
942 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200943 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200944 *
945 * \return The corresponding deterministic ECDSA signature
946 * algorithm.
947 * \return Unspecified if \p alg is not a supported
948 * hash algorithm.
949 */
950#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
951 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
952#define PSA_ALG_IS_ECDSA(alg) \
953 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
954 PSA_ALG_ECDSA_BASE)
955#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
956 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +0200957#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
958 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
959#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
960 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200961
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200962/** Get the hash used by a hash-and-sign signature algorithm.
963 *
964 * A hash-and-sign algorithm is a signature algorithm which is
965 * composed of two phases: first a hashing phase which does not use
966 * the key and produces a hash of the input message, then a signing
967 * phase which only uses the hash and the key and not the message
968 * itself.
969 *
970 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200971 * #PSA_ALG_IS_SIGN(\p alg) is true).
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200972 *
973 * \return The underlying hash algorithm if \p alg is a hash-and-sign
974 * algorithm.
975 * \return 0 if \p alg is a signature algorithm that does not
976 * follow the hash-and-sign structure.
977 * \return Unspecified if \p alg is not a signature algorithm or
978 * if it is not supported by the implementation.
979 */
980#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200981 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
982 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
Gilles Peskine54622ae2018-06-29 22:24:24 +0200983 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200984 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
985 0)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986
Gilles Peskinedcd14942018-07-12 00:30:52 +0200987/** RSA PKCS#1 v1.5 encryption.
988 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200989#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200990
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200991#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200992/** RSA OAEP encryption.
993 *
994 * This is the encryption scheme defined by RFC 8017
995 * (PKCS#1: RSA Cryptography Specifications) under the name
996 * RSAES-OAEP, with the message generation function MGF1.
997 *
998 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
999 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1000 * for MGF1.
1001 *
1002 * \return The corresponding RSA OAEP signature algorithm.
1003 * \return Unspecified if \p alg is not a supported
1004 * hash algorithm.
1005 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001006#define PSA_ALG_RSA_OAEP(hash_alg) \
1007 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1008#define PSA_ALG_IS_RSA_OAEP(alg) \
1009 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
Gilles Peskine072ac562018-06-30 00:21:29 +02001010#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1011 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1012 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1013 0)
Gilles Peskined1e8e412018-06-07 09:49:39 +02001014
Gilles Peskinebef7f142018-07-12 17:22:21 +02001015#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100)
1016/** Macro to build an HKDF algorithm.
1017 *
1018 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1019 *
1020 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1021 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1022 *
1023 * \return The corresponding HKDF algorithm.
1024 * \return Unspecified if \p alg is not a supported
1025 * hash algorithm.
1026 */
1027#define PSA_ALG_HKDF(hash_alg) \
1028 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1029/** Whether the specified algorithm is an HKDF algorithm.
1030 *
1031 * HKDF is a family of key derivation algorithms that are based on a hash
1032 * function and the HMAC construction.
1033 *
1034 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1035 *
1036 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1037 * This macro may return either 0 or 1 if \c alg is not a supported
1038 * key derivation algorithm identifier.
1039 */
1040#define PSA_ALG_IS_HKDF(alg) \
1041 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1042#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1043 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1044
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001045/**@}*/
1046
1047/** \defgroup key_management Key management
1048 * @{
1049 */
1050
1051/**
1052 * \brief Import a key in binary format.
1053 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +01001054 * This function supports any output from psa_export_key(). Refer to the
1055 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001056 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001057 * \param key Slot where the key will be stored. This must be a
1058 * valid slot for a key of the chosen type. It must
1059 * be unoccupied.
1060 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001061 * \param[in] data Buffer containing the key data.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001062 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001063 *
Gilles Peskine28538492018-07-11 17:34:00 +02001064 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001065 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001066 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001067 * The key type or key size is not supported, either by the
1068 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001069 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +01001070 * The key slot is invalid,
1071 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +02001072 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001073 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001074 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1075 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1076 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1077 * \retval #PSA_ERROR_HARDWARE_FAILURE
1078 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001079 */
1080psa_status_t psa_import_key(psa_key_slot_t key,
1081 psa_key_type_t type,
1082 const uint8_t *data,
1083 size_t data_length);
1084
1085/**
Gilles Peskine154bd952018-04-19 08:38:16 +02001086 * \brief Destroy a key and restore the slot to its default state.
1087 *
1088 * This function destroys the content of the key slot from both volatile
1089 * memory and, if applicable, non-volatile storage. Implementations shall
1090 * make a best effort to ensure that any previous content of the slot is
1091 * unrecoverable.
1092 *
1093 * This function also erases any metadata such as policies. It returns the
1094 * specified slot to its default state.
1095 *
1096 * \param key The key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001097 *
Gilles Peskine28538492018-07-11 17:34:00 +02001098 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +02001099 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +02001100 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001101 * The slot holds content and cannot be erased because it is
1102 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskine28538492018-07-11 17:34:00 +02001103 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001104 * The specified slot number does not designate a valid slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001105 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001106 * There was an failure in communication with the cryptoprocessor.
1107 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +02001108 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001109 * The storage is corrupted. Implementations shall make a best effort
1110 * to erase key material even in this stage, however applications
1111 * should be aware that it may be impossible to guarantee that the
1112 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +02001113 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001114 * An unexpected condition which is not a storage corruption or
1115 * a communication failure occurred. The cryptoprocessor may have
1116 * been compromised.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001117 */
1118psa_status_t psa_destroy_key(psa_key_slot_t key);
1119
1120/**
1121 * \brief Get basic metadata about a key.
1122 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001123 * \param key Slot whose content is queried. This must
1124 * be an occupied key slot.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001125 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001126 * This may be a null pointer, in which case the key type
1127 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001128 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +01001129 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +01001130 * is not written.
1131 *
Gilles Peskine28538492018-07-11 17:34:00 +02001132 * \retval #PSA_SUCCESS
1133 * \retval #PSA_ERROR_EMPTY_SLOT
1134 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1135 * \retval #PSA_ERROR_HARDWARE_FAILURE
1136 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001137 */
1138psa_status_t psa_get_key_information(psa_key_slot_t key,
1139 psa_key_type_t *type,
1140 size_t *bits);
1141
1142/**
1143 * \brief Export a key in binary format.
1144 *
1145 * The output of this function can be passed to psa_import_key() to
1146 * create an equivalent object.
1147 *
1148 * If a key is created with psa_import_key() and then exported with
1149 * this function, it is not guaranteed that the resulting data is
1150 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +01001151 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001152 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001153 * For standard key types, the output format is as follows:
1154 *
1155 * - For symmetric keys (including MAC keys), the format is the
1156 * raw bytes of the key.
1157 * - For DES, the key data consists of 8 bytes. The parity bits must be
1158 * correct.
1159 * - For Triple-DES, the format is the concatenation of the
1160 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +01001161 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine2743e422018-06-27 22:57:11 +02001162 * is the non-encrypted DER representation defined by PKCS\#1 (RFC 8017)
1163 * as RSAPrivateKey.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001164 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the format
Gilles Peskine971f7062018-03-20 17:52:58 +01001165 * is the DER representation defined by RFC 5280 as SubjectPublicKeyInfo.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001166 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001167 * \param key Slot whose content is to be exported. This must
1168 * be an occupied key slot.
1169 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001170 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001171 * \param[out] data_length On success, the number of bytes
1172 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001173 *
Gilles Peskine28538492018-07-11 17:34:00 +02001174 * \retval #PSA_SUCCESS
1175 * \retval #PSA_ERROR_EMPTY_SLOT
1176 * \retval #PSA_ERROR_NOT_PERMITTED
1177 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1178 * \retval #PSA_ERROR_HARDWARE_FAILURE
1179 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001180 */
1181psa_status_t psa_export_key(psa_key_slot_t key,
1182 uint8_t *data,
1183 size_t data_size,
1184 size_t *data_length);
1185
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001186/**
1187 * \brief Export a public key or the public part of a key pair in binary format.
1188 *
1189 * The output of this function can be passed to psa_import_key() to
1190 * create an object that is equivalent to the public key.
1191 *
1192 * For standard key types, the output format is as follows:
1193 *
1194 * - For RSA keys (#PSA_KEY_TYPE_RSA_KEYPAIR or #PSA_KEY_TYPE_RSA_PUBLIC_KEY),
Moran Pekerdd4ea382018-04-03 15:30:03 +03001195 * the format is the DER representation of the public key defined by RFC 5280
Gilles Peskine971f7062018-03-20 17:52:58 +01001196 * as SubjectPublicKeyInfo.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001197 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001198 * \param key Slot whose content is to be exported. This must
1199 * be an occupied key slot.
1200 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001201 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001202 * \param[out] data_length On success, the number of bytes
1203 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001204 *
Gilles Peskine28538492018-07-11 17:34:00 +02001205 * \retval #PSA_SUCCESS
1206 * \retval #PSA_ERROR_EMPTY_SLOT
1207 * \retval #PSA_ERROR_INVALID_ARGUMENT
1208 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1209 * \retval #PSA_ERROR_HARDWARE_FAILURE
1210 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001211 */
1212psa_status_t psa_export_public_key(psa_key_slot_t key,
1213 uint8_t *data,
1214 size_t data_size,
1215 size_t *data_length);
1216
1217/**@}*/
1218
1219/** \defgroup policy Key policies
1220 * @{
1221 */
1222
1223/** \brief Encoding of permitted usage on a key. */
1224typedef uint32_t psa_key_usage_t;
1225
Gilles Peskine7e198532018-03-08 07:50:30 +01001226/** Whether the key may be exported.
1227 *
1228 * A public key or the public part of a key pair may always be exported
1229 * regardless of the value of this permission flag.
1230 *
1231 * If a key does not have export permission, implementations shall not
1232 * allow the key to be exported in plain form from the cryptoprocessor,
1233 * whether through psa_export_key() or through a proprietary interface.
1234 * The key may however be exportable in a wrapped form, i.e. in a form
1235 * where it is encrypted by another key.
1236 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001237#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1238
Gilles Peskine7e198532018-03-08 07:50:30 +01001239/** Whether the key may be used to encrypt a message.
1240 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001241 * This flag allows the key to be used for a symmetric encryption operation,
1242 * for an AEAD encryption-and-authentication operation,
1243 * or for an asymmetric encryption operation,
1244 * if otherwise permitted by the key's type and policy.
1245 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001246 * For a key pair, this concerns the public key.
1247 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001248#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +01001249
1250/** Whether the key may be used to decrypt a message.
1251 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001252 * This flag allows the key to be used for a symmetric decryption operation,
1253 * for an AEAD decryption-and-verification operation,
1254 * or for an asymmetric decryption operation,
1255 * if otherwise permitted by the key's type and policy.
1256 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001257 * For a key pair, this concerns the private key.
1258 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001259#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +01001260
1261/** Whether the key may be used to sign a message.
1262 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001263 * This flag allows the key to be used for a MAC calculation operation
1264 * or for an asymmetric signature operation,
1265 * if otherwise permitted by the key's type and policy.
1266 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001267 * For a key pair, this concerns the private key.
1268 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001269#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +01001270
1271/** Whether the key may be used to verify a message signature.
1272 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001273 * This flag allows the key to be used for a MAC verification operation
1274 * or for an asymmetric signature verification operation,
1275 * if otherwise permitted by by the key's type and policy.
1276 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001277 * For a key pair, this concerns the public key.
1278 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001279#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1280
Gilles Peskineea0fb492018-07-12 17:17:20 +02001281/** Whether the key may be used to derive other keys.
1282 */
1283#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1284
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001285/** The type of the key policy data structure.
1286 *
1287 * This is an implementation-defined \c struct. Applications should not
1288 * make any assumptions about the content of this structure except
1289 * as directed by the documentation of a specific implementation. */
1290typedef struct psa_key_policy_s psa_key_policy_t;
1291
1292/** \brief Initialize a key policy structure to a default that forbids all
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001293 * usage of the key.
1294 *
1295 * \param[out] policy The policy object to initialize.
1296 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001297void psa_key_policy_init(psa_key_policy_t *policy);
1298
Gilles Peskine7e198532018-03-08 07:50:30 +01001299/** \brief Set the standard fields of a policy structure.
1300 *
1301 * Note that this function does not make any consistency check of the
1302 * parameters. The values are only checked when applying the policy to
1303 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001304 *
1305 * \param[out] policy The policy object to modify.
1306 * \param usage The permitted uses for the key.
1307 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +01001308 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001309void psa_key_policy_set_usage(psa_key_policy_t *policy,
1310 psa_key_usage_t usage,
1311 psa_algorithm_t alg);
1312
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001313/** \brief Retrieve the usage field of a policy structure.
1314 *
1315 * \param[in] policy The policy object to query.
1316 *
1317 * \return The permitted uses for a key with this policy.
1318 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001319psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001320
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001321/** \brief Retrieve the algorithm field of a policy structure.
1322 *
1323 * \param[in] policy The policy object to query.
1324 *
1325 * \return The permitted algorithm for a key with this policy.
1326 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001327psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001328
1329/** \brief Set the usage policy on a key slot.
1330 *
1331 * This function must be called on an empty key slot, before importing,
1332 * generating or creating a key in the slot. Changing the policy of an
1333 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +01001334 *
1335 * Implementations may set restrictions on supported key policies
1336 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001337 *
1338 * \param key The key slot whose policy is to be changed.
1339 * \param[in] policy The policy object to query.
1340 *
1341 * \retval #PSA_SUCCESS
1342 * \retval #PSA_ERROR_OCCUPIED_SLOT
1343 * \retval #PSA_ERROR_NOT_SUPPORTED
1344 * \retval #PSA_ERROR_INVALID_ARGUMENT
1345 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1346 * \retval #PSA_ERROR_HARDWARE_FAILURE
1347 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001348 */
1349psa_status_t psa_set_key_policy(psa_key_slot_t key,
1350 const psa_key_policy_t *policy);
1351
Gilles Peskine7e198532018-03-08 07:50:30 +01001352/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001353 *
1354 * \param key The key slot whose policy is being queried.
1355 * \param[out] policy On success, the key's policy.
1356 *
1357 * \retval #PSA_SUCCESS
1358 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1359 * \retval #PSA_ERROR_HARDWARE_FAILURE
1360 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7e198532018-03-08 07:50:30 +01001361 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001362psa_status_t psa_get_key_policy(psa_key_slot_t key,
1363 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +01001364
1365/**@}*/
1366
Gilles Peskine609b6a52018-03-03 21:31:50 +01001367/** \defgroup persistence Key lifetime
1368 * @{
1369 */
1370
1371/** Encoding of key lifetimes.
1372 */
1373typedef uint32_t psa_key_lifetime_t;
1374
1375/** A volatile key slot retains its content as long as the application is
1376 * running. It is guaranteed to be erased on a power reset.
1377 */
1378#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1379
1380/** A persistent key slot retains its content as long as it is not explicitly
1381 * destroyed.
1382 */
1383#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1384
1385/** A write-once key slot may not be modified once a key has been set.
1386 * It will retain its content as long as the device remains operational.
1387 */
1388#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
1389
Gilles Peskined393e182018-03-08 07:49:16 +01001390/** \brief Retrieve the lifetime of a key slot.
1391 *
1392 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001393 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001394 * \param key Slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001395 * \param[out] lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001396 *
Gilles Peskine28538492018-07-11 17:34:00 +02001397 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001398 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001399 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -07001400 * The key slot is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001401 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1402 * \retval #PSA_ERROR_HARDWARE_FAILURE
1403 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001404 */
Gilles Peskine609b6a52018-03-03 21:31:50 +01001405psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1406 psa_key_lifetime_t *lifetime);
1407
Gilles Peskined393e182018-03-08 07:49:16 +01001408/** \brief Change the lifetime of a key slot.
1409 *
1410 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +01001411 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +01001412 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001413 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001414 * \param key Slot whose lifetime is to be changed.
1415 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001416 *
Gilles Peskine28538492018-07-11 17:34:00 +02001417 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001418 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001419 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603804cd712018-03-20 22:44:08 +02001420 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -07001421 * or the lifetime value is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001422 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001423 * The implementation does not support the specified lifetime value,
1424 * at least for the specified key slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001425 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001426 * The slot contains a key, and the implementation does not support
1427 * changing the lifetime of an occupied slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001428 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1429 * \retval #PSA_ERROR_HARDWARE_FAILURE
1430 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001431 */
1432psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -07001433 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +01001434
Gilles Peskine609b6a52018-03-03 21:31:50 +01001435/**@}*/
1436
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001437/** \defgroup hash Message digests
1438 * @{
1439 */
1440
Gilles Peskine308b91d2018-02-08 09:47:44 +01001441/** The type of the state data structure for multipart hash operations.
1442 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001443 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001444 * make any assumptions about the content of this structure except
1445 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001446typedef struct psa_hash_operation_s psa_hash_operation_t;
1447
Gilles Peskine308b91d2018-02-08 09:47:44 +01001448/** The size of the output of psa_hash_finish(), in bytes.
1449 *
1450 * This is also the hash size that psa_hash_verify() expects.
1451 *
1452 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001453 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
Gilles Peskinebe42f312018-07-13 14:38:15 +02001454 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
Gilles Peskine35855962018-04-19 08:39:16 +02001455 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001456 *
1457 * \return The hash size for the specified hash algorithm.
1458 * If the hash algorithm is not recognized, return 0.
1459 * An implementation may return either 0 or the correct size
1460 * for a hash algorithm that it recognizes, but does not support.
1461 */
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001462#define PSA_HASH_SIZE(alg) \
1463 ( \
1464 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD2 ? 16 : \
1465 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD4 ? 16 : \
1466 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD5 ? 16 : \
1467 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
1468 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
1469 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
1470 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
1471 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
1472 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
1473 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
1474 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
1475 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
1476 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
1477 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
1478 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001479 0)
1480
Gilles Peskine308b91d2018-02-08 09:47:44 +01001481/** Start a multipart hash operation.
1482 *
1483 * The sequence of operations to calculate a hash (message digest)
1484 * is as follows:
1485 * -# Allocate an operation object which will be passed to all the functions
1486 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001487 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001488 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001489 * of the message each time. The hash that is calculated is the hash
1490 * of the concatenation of these messages in order.
1491 * -# To calculate the hash, call psa_hash_finish().
1492 * To compare the hash with an expected value, call psa_hash_verify().
1493 *
1494 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001495 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001496 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001497 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001498 * eventually terminate the operation. The following events terminate an
1499 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001500 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001501 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001502 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001503 * \param[out] operation The operation object to use.
1504 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1505 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001506 *
Gilles Peskine28538492018-07-11 17:34:00 +02001507 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001508 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001509 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001510 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001511 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1512 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1513 * \retval #PSA_ERROR_HARDWARE_FAILURE
1514 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001515 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001516psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001517 psa_algorithm_t alg);
1518
Gilles Peskine308b91d2018-02-08 09:47:44 +01001519/** Add a message fragment to a multipart hash operation.
1520 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001521 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001522 *
1523 * If this function returns an error status, the operation becomes inactive.
1524 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001525 * \param[in,out] operation Active hash operation.
1526 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001527 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001528 *
Gilles Peskine28538492018-07-11 17:34:00 +02001529 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001530 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001531 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001532 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001533 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1534 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1535 * \retval #PSA_ERROR_HARDWARE_FAILURE
1536 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001537 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001538psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1539 const uint8_t *input,
1540 size_t input_length);
1541
Gilles Peskine308b91d2018-02-08 09:47:44 +01001542/** Finish the calculation of the hash of a message.
1543 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001544 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001545 * This function calculates the hash of the message formed by concatenating
1546 * the inputs passed to preceding calls to psa_hash_update().
1547 *
1548 * When this function returns, the operation becomes inactive.
1549 *
1550 * \warning Applications should not call this function if they expect
1551 * a specific value for the hash. Call psa_hash_verify() instead.
1552 * Beware that comparing integrity or authenticity data such as
1553 * hash values with a function such as \c memcmp is risky
1554 * because the time taken by the comparison may leak information
1555 * about the hashed data which could allow an attacker to guess
1556 * a valid hash and thereby bypass security controls.
1557 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001558 * \param[in,out] operation Active hash operation.
1559 * \param[out] hash Buffer where the hash is to be written.
1560 * \param hash_size Size of the \p hash buffer in bytes.
1561 * \param[out] hash_length On success, the number of bytes
1562 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001563 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001564 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001565 *
Gilles Peskine28538492018-07-11 17:34:00 +02001566 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001567 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001568 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001569 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001570 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001571 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001572 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001573 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001574 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1575 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1576 * \retval #PSA_ERROR_HARDWARE_FAILURE
1577 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001578 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001579psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1580 uint8_t *hash,
1581 size_t hash_size,
1582 size_t *hash_length);
1583
Gilles Peskine308b91d2018-02-08 09:47:44 +01001584/** Finish the calculation of the hash of a message and compare it with
1585 * an expected value.
1586 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001587 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001588 * This function calculates the hash of the message formed by concatenating
1589 * the inputs passed to preceding calls to psa_hash_update(). It then
1590 * compares the calculated hash with the expected hash passed as a
1591 * parameter to this function.
1592 *
1593 * When this function returns, the operation becomes inactive.
1594 *
Gilles Peskine19067982018-03-20 17:54:53 +01001595 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001596 * comparison between the actual hash and the expected hash is performed
1597 * in constant time.
1598 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001599 * \param[in,out] operation Active hash operation.
1600 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001601 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001602 *
Gilles Peskine28538492018-07-11 17:34:00 +02001603 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001604 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001605 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001606 * The hash of the message was calculated successfully, but it
1607 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001608 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001609 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001610 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1611 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1612 * \retval #PSA_ERROR_HARDWARE_FAILURE
1613 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001614 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001615psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1616 const uint8_t *hash,
1617 size_t hash_length);
1618
Gilles Peskine308b91d2018-02-08 09:47:44 +01001619/** Abort a hash operation.
1620 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001621 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001622 * \p operation structure itself. Once aborted, the operation object
1623 * can be reused for another operation by calling
1624 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001625 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001626 * You may call this function any time after the operation object has
1627 * been initialized by any of the following methods:
1628 * - A call to psa_hash_setup(), whether it succeeds or not.
1629 * - Initializing the \c struct to all-bits-zero.
1630 * - Initializing the \c struct to logical zeros, e.g.
1631 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001632 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001633 * In particular, calling psa_hash_abort() after the operation has been
1634 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1635 * psa_hash_verify() is safe and has no effect.
1636 *
1637 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001638 *
Gilles Peskine28538492018-07-11 17:34:00 +02001639 * \retval #PSA_SUCCESS
1640 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001641 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001642 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1643 * \retval #PSA_ERROR_HARDWARE_FAILURE
1644 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001645 */
1646psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001647
1648/**@}*/
1649
Gilles Peskine8c9def32018-02-08 10:02:12 +01001650/** \defgroup MAC Message authentication codes
1651 * @{
1652 */
1653
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001654/** The type of the state data structure for multipart MAC operations.
1655 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001656 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001657 * make any assumptions about the content of this structure except
1658 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001659typedef struct psa_mac_operation_s psa_mac_operation_t;
1660
Gilles Peskine89167cb2018-07-08 20:12:23 +02001661/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001662 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001663 * This function sets up the calculation of the MAC
1664 * (message authentication code) of a byte string.
1665 * To verify the MAC of a message against an
1666 * expected value, use psa_mac_verify_setup() instead.
1667 *
1668 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001669 * -# Allocate an operation object which will be passed to all the functions
1670 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001671 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001672 * The key remains associated with the operation even if the content
1673 * of the key slot changes.
1674 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1675 * of the message each time. The MAC that is calculated is the MAC
1676 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001677 * -# At the end of the message, call psa_mac_sign_finish() to finish
1678 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001679 *
1680 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +02001681 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001682 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001683 * After a successful call to psa_mac_sign_setup(), the application must
1684 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001685 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001686 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001687 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001688 * \param[out] operation The operation object to use.
1689 * \param key Slot containing the key to use for the operation.
1690 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1691 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001692 *
Gilles Peskine28538492018-07-11 17:34:00 +02001693 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001694 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001695 * \retval #PSA_ERROR_EMPTY_SLOT
1696 * \retval #PSA_ERROR_NOT_PERMITTED
1697 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001698 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001699 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001700 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001701 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1702 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1703 * \retval #PSA_ERROR_HARDWARE_FAILURE
1704 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001705 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001706psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1707 psa_key_slot_t key,
1708 psa_algorithm_t alg);
1709
1710/** Start a multipart MAC verification operation.
1711 *
1712 * This function sets up the verification of the MAC
1713 * (message authentication code) of a byte string against an expected value.
1714 *
1715 * The sequence of operations to verify a MAC is as follows:
1716 * -# Allocate an operation object which will be passed to all the functions
1717 * listed here.
1718 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1719 * The key remains associated with the operation even if the content
1720 * of the key slot changes.
1721 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1722 * of the message each time. The MAC that is calculated is the MAC
1723 * of the concatenation of these messages in order.
1724 * -# At the end of the message, call psa_mac_verify_finish() to finish
1725 * calculating the actual MAC of the message and verify it against
1726 * the expected value.
1727 *
1728 * The application may call psa_mac_abort() at any time after the operation
1729 * has been initialized with psa_mac_verify_setup().
1730 *
1731 * After a successful call to psa_mac_verify_setup(), the application must
1732 * eventually terminate the operation through one of the following methods:
1733 * - A failed call to psa_mac_update().
1734 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1735 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001736 * \param[out] operation The operation object to use.
1737 * \param key Slot containing the key to use for the operation.
1738 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1739 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001740 *
Gilles Peskine28538492018-07-11 17:34:00 +02001741 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001742 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001743 * \retval #PSA_ERROR_EMPTY_SLOT
1744 * \retval #PSA_ERROR_NOT_PERMITTED
1745 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001746 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001747 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001748 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001749 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1750 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1751 * \retval #PSA_ERROR_HARDWARE_FAILURE
1752 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001753 */
1754psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
1755 psa_key_slot_t key,
1756 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001757
Gilles Peskinedcd14942018-07-12 00:30:52 +02001758/** Add a message fragment to a multipart MAC operation.
1759 *
1760 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1761 * before calling this function.
1762 *
1763 * If this function returns an error status, the operation becomes inactive.
1764 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001765 * \param[in,out] operation Active MAC operation.
1766 * \param[in] input Buffer containing the message fragment to add to
1767 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001768 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001769 *
1770 * \retval #PSA_SUCCESS
1771 * Success.
1772 * \retval #PSA_ERROR_BAD_STATE
1773 * The operation state is not valid (not started, or already completed).
1774 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1775 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1776 * \retval #PSA_ERROR_HARDWARE_FAILURE
1777 * \retval #PSA_ERROR_TAMPERING_DETECTED
1778 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001779psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1780 const uint8_t *input,
1781 size_t input_length);
1782
Gilles Peskinedcd14942018-07-12 00:30:52 +02001783/** Finish the calculation of the MAC of a message.
1784 *
1785 * The application must call psa_mac_sign_setup() before calling this function.
1786 * This function calculates the MAC of the message formed by concatenating
1787 * the inputs passed to preceding calls to psa_mac_update().
1788 *
1789 * When this function returns, the operation becomes inactive.
1790 *
1791 * \warning Applications should not call this function if they expect
1792 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1793 * Beware that comparing integrity or authenticity data such as
1794 * MAC values with a function such as \c memcmp is risky
1795 * because the time taken by the comparison may leak information
1796 * about the MAC value which could allow an attacker to guess
1797 * a valid MAC and thereby bypass security controls.
1798 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001799 * \param[in,out] operation Active MAC operation.
1800 * \param[out] mac Buffer where the MAC value is to be written.
1801 * \param mac_size Size of the \p mac buffer in bytes.
1802 * \param[out] mac_length On success, the number of bytes
1803 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001804 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001805 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001806 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001807 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001808 *
1809 * \retval #PSA_SUCCESS
1810 * Success.
1811 * \retval #PSA_ERROR_BAD_STATE
1812 * The operation state is not valid (not started, or already completed).
1813 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001814 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001815 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1816 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1817 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1818 * \retval #PSA_ERROR_HARDWARE_FAILURE
1819 * \retval #PSA_ERROR_TAMPERING_DETECTED
1820 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001821psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1822 uint8_t *mac,
1823 size_t mac_size,
1824 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001825
Gilles Peskinedcd14942018-07-12 00:30:52 +02001826/** Finish the calculation of the MAC of a message and compare it with
1827 * an expected value.
1828 *
1829 * The application must call psa_mac_verify_setup() before calling this function.
1830 * This function calculates the MAC of the message formed by concatenating
1831 * the inputs passed to preceding calls to psa_mac_update(). It then
1832 * compares the calculated MAC with the expected MAC passed as a
1833 * parameter to this function.
1834 *
1835 * When this function returns, the operation becomes inactive.
1836 *
1837 * \note Implementations shall make the best effort to ensure that the
1838 * comparison between the actual MAC and the expected MAC is performed
1839 * in constant time.
1840 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001841 * \param[in,out] operation Active MAC operation.
1842 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001843 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001844 *
1845 * \retval #PSA_SUCCESS
1846 * The expected MAC is identical to the actual MAC of the message.
1847 * \retval #PSA_ERROR_INVALID_SIGNATURE
1848 * The MAC of the message was calculated successfully, but it
1849 * differs from the expected MAC.
1850 * \retval #PSA_ERROR_BAD_STATE
1851 * The operation state is not valid (not started, or already completed).
1852 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1853 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1854 * \retval #PSA_ERROR_HARDWARE_FAILURE
1855 * \retval #PSA_ERROR_TAMPERING_DETECTED
1856 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001857psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1858 const uint8_t *mac,
1859 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001860
Gilles Peskinedcd14942018-07-12 00:30:52 +02001861/** Abort a MAC operation.
1862 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001863 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001864 * \p operation structure itself. Once aborted, the operation object
1865 * can be reused for another operation by calling
1866 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001867 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001868 * You may call this function any time after the operation object has
1869 * been initialized by any of the following methods:
1870 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1871 * it succeeds or not.
1872 * - Initializing the \c struct to all-bits-zero.
1873 * - Initializing the \c struct to logical zeros, e.g.
1874 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001875 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001876 * In particular, calling psa_mac_abort() after the operation has been
1877 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1878 * psa_mac_verify_finish() is safe and has no effect.
1879 *
1880 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001881 *
1882 * \retval #PSA_SUCCESS
1883 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001884 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001885 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1886 * \retval #PSA_ERROR_HARDWARE_FAILURE
1887 * \retval #PSA_ERROR_TAMPERING_DETECTED
1888 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001889psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1890
1891/**@}*/
1892
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001893/** \defgroup cipher Symmetric ciphers
1894 * @{
1895 */
1896
1897/** The type of the state data structure for multipart cipher operations.
1898 *
1899 * This is an implementation-defined \c struct. Applications should not
1900 * make any assumptions about the content of this structure except
1901 * as directed by the documentation of a specific implementation. */
1902typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1903
1904/** Set the key for a multipart symmetric encryption operation.
1905 *
1906 * The sequence of operations to encrypt a message with a symmetric cipher
1907 * is as follows:
1908 * -# Allocate an operation object which will be passed to all the functions
1909 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001910 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001911 * The key remains associated with the operation even if the content
1912 * of the key slot changes.
Gilles Peskinefe119512018-07-08 21:39:34 +02001913 * -# Call either psa_encrypt_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001914 * generate or set the IV (initialization vector). You should use
1915 * psa_encrypt_generate_iv() unless the protocol you are implementing
1916 * requires a specific IV value.
1917 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1918 * of the message each time.
1919 * -# Call psa_cipher_finish().
1920 *
1921 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001922 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001923 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001924 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001925 * eventually terminate the operation. The following events terminate an
1926 * operation:
Gilles Peskinefe119512018-07-08 21:39:34 +02001927 * - A failed call to psa_encrypt_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001928 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001929 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001930 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001931 * \param[out] operation The operation object to use.
1932 * \param key Slot containing the key to use for the operation.
1933 * \param alg The cipher algorithm to compute
1934 * (\c PSA_ALG_XXX value such that
1935 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001936 *
Gilles Peskine28538492018-07-11 17:34:00 +02001937 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001938 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001939 * \retval #PSA_ERROR_EMPTY_SLOT
1940 * \retval #PSA_ERROR_NOT_PERMITTED
1941 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001942 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001943 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001944 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001945 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1946 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1947 * \retval #PSA_ERROR_HARDWARE_FAILURE
1948 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001949 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001950psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
1951 psa_key_slot_t key,
1952 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001953
1954/** Set the key for a multipart symmetric decryption operation.
1955 *
1956 * The sequence of operations to decrypt a message with a symmetric cipher
1957 * is as follows:
1958 * -# Allocate an operation object which will be passed to all the functions
1959 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001960 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001961 * The key remains associated with the operation even if the content
1962 * of the key slot changes.
1963 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1964 * decryption. If the IV is prepended to the ciphertext, you can call
1965 * psa_cipher_update() on a buffer containing the IV followed by the
1966 * beginning of the message.
1967 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1968 * of the message each time.
1969 * -# Call psa_cipher_finish().
1970 *
1971 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001972 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001973 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001974 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001975 * eventually terminate the operation. The following events terminate an
1976 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001977 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001978 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001979 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001980 * \param[out] operation The operation object to use.
1981 * \param key Slot containing the key to use for the operation.
1982 * \param alg The cipher algorithm to compute
1983 * (\c PSA_ALG_XXX value such that
1984 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001985 *
Gilles Peskine28538492018-07-11 17:34:00 +02001986 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001987 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001988 * \retval #PSA_ERROR_EMPTY_SLOT
1989 * \retval #PSA_ERROR_NOT_PERMITTED
1990 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001991 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001992 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001993 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001994 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1995 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1996 * \retval #PSA_ERROR_HARDWARE_FAILURE
1997 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001998 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001999psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
2000 psa_key_slot_t key,
2001 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002002
Gilles Peskinedcd14942018-07-12 00:30:52 +02002003/** Generate an IV for a symmetric encryption operation.
2004 *
2005 * This function generates a random IV (initialization vector), nonce
2006 * or initial counter value for the encryption operation as appropriate
2007 * for the chosen algorithm, key type and key size.
2008 *
2009 * The application must call psa_cipher_encrypt_setup() before
2010 * calling this function.
2011 *
2012 * If this function returns an error status, the operation becomes inactive.
2013 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002014 * \param[in,out] operation Active cipher operation.
2015 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002016 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002017 * \param[out] iv_length On success, the number of bytes of the
2018 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002019 *
2020 * \retval #PSA_SUCCESS
2021 * Success.
2022 * \retval #PSA_ERROR_BAD_STATE
2023 * The operation state is not valid (not started, or IV already set).
2024 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002025 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002026 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2027 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2028 * \retval #PSA_ERROR_HARDWARE_FAILURE
2029 * \retval #PSA_ERROR_TAMPERING_DETECTED
2030 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002031psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
2032 unsigned char *iv,
2033 size_t iv_size,
2034 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002035
Gilles Peskinedcd14942018-07-12 00:30:52 +02002036/** Set the IV for a symmetric encryption or decryption operation.
2037 *
2038 * This function sets the random IV (initialization vector), nonce
2039 * or initial counter value for the encryption or decryption operation.
2040 *
2041 * The application must call psa_cipher_encrypt_setup() before
2042 * calling this function.
2043 *
2044 * If this function returns an error status, the operation becomes inactive.
2045 *
2046 * \note When encrypting, applications should use psa_cipher_generate_iv()
2047 * instead of this function, unless implementing a protocol that requires
2048 * a non-random IV.
2049 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002050 * \param[in,out] operation Active cipher operation.
2051 * \param[in] iv Buffer containing the IV to use.
2052 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002053 *
2054 * \retval #PSA_SUCCESS
2055 * Success.
2056 * \retval #PSA_ERROR_BAD_STATE
2057 * The operation state is not valid (not started, or IV already set).
2058 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002059 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02002060 * or the chosen algorithm does not use an IV.
2061 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2062 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2063 * \retval #PSA_ERROR_HARDWARE_FAILURE
2064 * \retval #PSA_ERROR_TAMPERING_DETECTED
2065 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002066psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
2067 const unsigned char *iv,
2068 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002069
Gilles Peskinedcd14942018-07-12 00:30:52 +02002070/** Encrypt or decrypt a message fragment in an active cipher operation.
2071 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02002072 * Before calling this function, you must:
2073 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
2074 * The choice of setup function determines whether this function
2075 * encrypts or decrypts its input.
2076 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
2077 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002078 *
2079 * If this function returns an error status, the operation becomes inactive.
2080 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002081 * \param[in,out] operation Active cipher operation.
2082 * \param[in] input Buffer containing the message fragment to
2083 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002084 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002085 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002086 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002087 * \param[out] output_length On success, the number of bytes
2088 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002089 *
2090 * \retval #PSA_SUCCESS
2091 * Success.
2092 * \retval #PSA_ERROR_BAD_STATE
2093 * The operation state is not valid (not started, IV required but
2094 * not set, or already completed).
2095 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2096 * The size of the \p output buffer is too small.
2097 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2098 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2099 * \retval #PSA_ERROR_HARDWARE_FAILURE
2100 * \retval #PSA_ERROR_TAMPERING_DETECTED
2101 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002102psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2103 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002104 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002105 unsigned char *output,
2106 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002107 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002108
Gilles Peskinedcd14942018-07-12 00:30:52 +02002109/** Finish encrypting or decrypting a message in a cipher operation.
2110 *
2111 * The application must call psa_cipher_encrypt_setup() or
2112 * psa_cipher_decrypt_setup() before calling this function. The choice
2113 * of setup function determines whether this function encrypts or
2114 * decrypts its input.
2115 *
2116 * This function finishes the encryption or decryption of the message
2117 * formed by concatenating the inputs passed to preceding calls to
2118 * psa_cipher_update().
2119 *
2120 * When this function returns, the operation becomes inactive.
2121 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002122 * \param[in,out] operation Active cipher operation.
2123 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002124 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002125 * \param[out] output_length On success, the number of bytes
2126 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002127 *
2128 * \retval #PSA_SUCCESS
2129 * Success.
2130 * \retval #PSA_ERROR_BAD_STATE
2131 * The operation state is not valid (not started, IV required but
2132 * not set, or already completed).
2133 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2134 * The size of the \p output buffer is too small.
2135 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2136 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2137 * \retval #PSA_ERROR_HARDWARE_FAILURE
2138 * \retval #PSA_ERROR_TAMPERING_DETECTED
2139 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002140psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002141 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002142 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002143 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002144
Gilles Peskinedcd14942018-07-12 00:30:52 +02002145/** Abort a cipher operation.
2146 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002147 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002148 * \p operation structure itself. Once aborted, the operation object
2149 * can be reused for another operation by calling
2150 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002151 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002152 * You may call this function any time after the operation object has
2153 * been initialized by any of the following methods:
2154 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2155 * whether it succeeds or not.
2156 * - Initializing the \c struct to all-bits-zero.
2157 * - Initializing the \c struct to logical zeros, e.g.
2158 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002159 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002160 * In particular, calling psa_cipher_abort() after the operation has been
2161 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2162 * is safe and has no effect.
2163 *
2164 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002165 *
2166 * \retval #PSA_SUCCESS
2167 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002168 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002169 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2170 * \retval #PSA_ERROR_HARDWARE_FAILURE
2171 * \retval #PSA_ERROR_TAMPERING_DETECTED
2172 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002173psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2174
2175/**@}*/
2176
Gilles Peskine3b555712018-03-03 21:27:57 +01002177/** \defgroup aead Authenticated encryption with associated data (AEAD)
2178 * @{
2179 */
2180
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002181/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01002182 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002183 * \param alg An AEAD algorithm
2184 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002185 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002186 *
2187 * \return The tag size for the specified algorithm.
2188 * If the AEAD algorithm does not have an identified
2189 * tag that can be distinguished from the rest of
2190 * the ciphertext, return 0.
2191 * If the AEAD algorithm is not recognized, return 0.
2192 * An implementation may return either 0 or a
2193 * correct size for an AEAD algorithm that it
2194 * recognizes, but does not support.
2195 */
2196#define PSA_AEAD_TAG_SIZE(alg) \
2197 ((alg) == PSA_ALG_GCM ? 16 : \
2198 (alg) == PSA_ALG_CCM ? 16 : \
2199 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01002200
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002201/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002202 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002203 * \param key Slot containing the key to use.
2204 * \param alg The AEAD algorithm to compute
2205 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002206 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002207 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002208 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002209 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002210 * but not encrypted.
2211 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002212 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002213 * encrypted.
2214 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002215 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002216 * encrypted data. The additional data is not
2217 * part of this output. For algorithms where the
2218 * encrypted data and the authentication tag
2219 * are defined as separate outputs, the
2220 * authentication tag is appended to the
2221 * encrypted data.
2222 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2223 * This must be at least
2224 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2225 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002226 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002227 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002228 *
Gilles Peskine28538492018-07-11 17:34:00 +02002229 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002230 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002231 * \retval #PSA_ERROR_EMPTY_SLOT
2232 * \retval #PSA_ERROR_NOT_PERMITTED
2233 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002234 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002235 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002236 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002237 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2238 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2239 * \retval #PSA_ERROR_HARDWARE_FAILURE
2240 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01002241 */
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002242psa_status_t psa_aead_encrypt(psa_key_slot_t key,
2243 psa_algorithm_t alg,
2244 const uint8_t *nonce,
2245 size_t nonce_length,
2246 const uint8_t *additional_data,
2247 size_t additional_data_length,
2248 const uint8_t *plaintext,
2249 size_t plaintext_length,
2250 uint8_t *ciphertext,
2251 size_t ciphertext_size,
2252 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002253
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002254/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002255 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002256 * \param key Slot containing the key to use.
2257 * \param alg The AEAD algorithm to compute
2258 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002259 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002260 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002261 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002262 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002263 * but not encrypted.
2264 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002265 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002266 * encrypted. For algorithms where the
2267 * encrypted data and the authentication tag
2268 * are defined as separate inputs, the buffer
2269 * must contain the encrypted data followed
2270 * by the authentication tag.
2271 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002272 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002273 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2274 * This must be at least
2275 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2276 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002277 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03002278 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002279 *
Gilles Peskine28538492018-07-11 17:34:00 +02002280 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002281 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002282 * \retval #PSA_ERROR_EMPTY_SLOT
2283 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002284 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002285 * \retval #PSA_ERROR_NOT_PERMITTED
2286 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002287 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002288 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002289 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002290 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2291 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2292 * \retval #PSA_ERROR_HARDWARE_FAILURE
2293 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01002294 */
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002295psa_status_t psa_aead_decrypt(psa_key_slot_t key,
2296 psa_algorithm_t alg,
2297 const uint8_t *nonce,
2298 size_t nonce_length,
2299 const uint8_t *additional_data,
2300 size_t additional_data_length,
2301 const uint8_t *ciphertext,
2302 size_t ciphertext_length,
2303 uint8_t *plaintext,
2304 size_t plaintext_size,
2305 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002306
2307/**@}*/
2308
Gilles Peskine20035e32018-02-03 22:44:14 +01002309/** \defgroup asymmetric Asymmetric cryptography
2310 * @{
2311 */
2312
2313/**
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002314 * \brief ECDSA signature size for a given curve bit size
Gilles Peskine0189e752018-02-03 23:57:22 +01002315 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002316 * \param curve_bits Curve size in bits.
2317 * \return Signature size in bytes.
Gilles Peskine0189e752018-02-03 23:57:22 +01002318 *
2319 * \note This macro returns a compile-time constant if its argument is one.
Gilles Peskine0189e752018-02-03 23:57:22 +01002320 */
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002321#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
2322 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskine0189e752018-02-03 23:57:22 +01002323
Gilles Peskine0189e752018-02-03 23:57:22 +01002324/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002325 * \brief Sign a hash or short message with a private key.
2326 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002327 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002328 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002329 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2330 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2331 * to determine the hash algorithm to use.
2332 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002333 * \param key Key slot containing an asymmetric key pair.
2334 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002335 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002336 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002337 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002338 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002339 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002340 * \param[out] signature_length On success, the number of bytes
2341 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002342 *
Gilles Peskine28538492018-07-11 17:34:00 +02002343 * \retval #PSA_SUCCESS
2344 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002345 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002346 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002347 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002348 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002349 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002350 * \retval #PSA_ERROR_NOT_SUPPORTED
2351 * \retval #PSA_ERROR_INVALID_ARGUMENT
2352 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2353 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2354 * \retval #PSA_ERROR_HARDWARE_FAILURE
2355 * \retval #PSA_ERROR_TAMPERING_DETECTED
2356 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01002357 */
2358psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
2359 psa_algorithm_t alg,
2360 const uint8_t *hash,
2361 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002362 uint8_t *signature,
2363 size_t signature_size,
2364 size_t *signature_length);
2365
2366/**
2367 * \brief Verify the signature a hash or short message using a public key.
2368 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002369 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002370 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002371 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2372 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2373 * to determine the hash algorithm to use.
2374 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01002375 * \param key Key slot containing a public key or an
2376 * asymmetric key pair.
2377 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002378 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002379 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002380 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002381 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002382 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002383 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002384 *
Gilles Peskine28538492018-07-11 17:34:00 +02002385 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002386 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002387 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002388 * The calculation was perfomed successfully, but the passed
2389 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002390 * \retval #PSA_ERROR_NOT_SUPPORTED
2391 * \retval #PSA_ERROR_INVALID_ARGUMENT
2392 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2393 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2394 * \retval #PSA_ERROR_HARDWARE_FAILURE
2395 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01002396 */
2397psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
2398 psa_algorithm_t alg,
2399 const uint8_t *hash,
2400 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002401 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002402 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002403
Gilles Peskine723feff2018-05-31 20:08:13 +02002404#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
Gilles Peskine072ac562018-06-30 00:21:29 +02002405 (PSA_ALG_IS_RSA_OAEP(alg) ? \
2406 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskine723feff2018-05-31 20:08:13 +02002407 11 /*PKCS#1v1.5*/)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002408
2409/**
2410 * \brief Encrypt a short message with a public key.
2411 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002412 * \param key Key slot containing a public key or an
2413 * asymmetric key pair.
2414 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002415 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002416 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002417 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002418 * \param[in] salt A salt or label, if supported by the
2419 * encryption algorithm.
2420 * If the algorithm does not support a
2421 * salt, pass \c NULL.
2422 * If the algorithm supports an optional
2423 * salt and you do not want to pass a salt,
2424 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002425 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002426 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2427 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002428 * \param salt_length Size of the \p salt buffer in bytes.
2429 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002430 * \param[out] output Buffer where the encrypted message is to
2431 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002432 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002433 * \param[out] output_length On success, the number of bytes
2434 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002435 *
Gilles Peskine28538492018-07-11 17:34:00 +02002436 * \retval #PSA_SUCCESS
2437 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002438 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002439 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002440 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002441 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002442 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002443 * \retval #PSA_ERROR_NOT_SUPPORTED
2444 * \retval #PSA_ERROR_INVALID_ARGUMENT
2445 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2446 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2447 * \retval #PSA_ERROR_HARDWARE_FAILURE
2448 * \retval #PSA_ERROR_TAMPERING_DETECTED
2449 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002450 */
2451psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
2452 psa_algorithm_t alg,
2453 const uint8_t *input,
2454 size_t input_length,
2455 const uint8_t *salt,
2456 size_t salt_length,
2457 uint8_t *output,
2458 size_t output_size,
2459 size_t *output_length);
2460
2461/**
2462 * \brief Decrypt a short message with a private key.
2463 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002464 * \param key Key slot containing an asymmetric key pair.
2465 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002466 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002467 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002468 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002469 * \param[in] salt A salt or label, if supported by the
2470 * encryption algorithm.
2471 * If the algorithm does not support a
2472 * salt, pass \c NULL.
2473 * If the algorithm supports an optional
2474 * salt and you do not want to pass a salt,
2475 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002476 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002477 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2478 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002479 * \param salt_length Size of the \p salt buffer in bytes.
2480 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002481 * \param[out] output Buffer where the decrypted message is to
2482 * be written.
2483 * \param output_size Size of the \c output buffer in bytes.
2484 * \param[out] output_length On success, the number of bytes
2485 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002486 *
Gilles Peskine28538492018-07-11 17:34:00 +02002487 * \retval #PSA_SUCCESS
2488 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002489 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002490 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002491 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002492 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002493 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002494 * \retval #PSA_ERROR_NOT_SUPPORTED
2495 * \retval #PSA_ERROR_INVALID_ARGUMENT
2496 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2497 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2498 * \retval #PSA_ERROR_HARDWARE_FAILURE
2499 * \retval #PSA_ERROR_TAMPERING_DETECTED
2500 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2501 * \retval #PSA_ERROR_INVALID_PADDING
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002502 */
2503psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
2504 psa_algorithm_t alg,
2505 const uint8_t *input,
2506 size_t input_length,
2507 const uint8_t *salt,
2508 size_t salt_length,
2509 uint8_t *output,
2510 size_t output_size,
2511 size_t *output_length);
2512
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002513/**@}*/
2514
Gilles Peskineedd76872018-07-20 17:42:05 +02002515/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002516 * @{
2517 */
2518
2519/** The type of the state data structure for generators.
2520 *
2521 * Before calling any function on a generator, the application must
2522 * initialize it by any of the following means:
2523 * - Set the structure to all-bits-zero, for example:
2524 * \code
2525 * psa_crypto_generator_t generator;
2526 * memset(&generator, 0, sizeof(generator));
2527 * \endcode
2528 * - Initialize the structure to logical zero values, for example:
2529 * \code
2530 * psa_crypto_generator_t generator = {0};
2531 * \endcode
2532 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2533 * for example:
2534 * \code
2535 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2536 * \endcode
2537 * - Assign the result of the function psa_crypto_generator_init()
2538 * to the structure, for example:
2539 * \code
2540 * psa_crypto_generator_t generator;
2541 * generator = psa_crypto_generator_init();
2542 * \endcode
2543 *
2544 * This is an implementation-defined \c struct. Applications should not
2545 * make any assumptions about the content of this structure except
2546 * as directed by the documentation of a specific implementation.
2547 */
2548typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2549
2550/** \def PSA_CRYPTO_GENERATOR_INIT
2551 *
2552 * This macro returns a suitable initializer for a generator object
2553 * of type #psa_crypto_generator_t.
2554 */
2555#ifdef __DOXYGEN_ONLY__
2556/* This is an example definition for documentation purposes.
2557 * Implementations should define a suitable value in `crypto_struct.h`.
2558 */
2559#define PSA_CRYPTO_GENERATOR_INIT {0}
2560#endif
2561
2562/** Return an initial value for a generator object.
2563 */
2564static psa_crypto_generator_t psa_crypto_generator_init(void);
2565
2566/** Retrieve the current capacity of a generator.
2567 *
2568 * The capacity of a generator is the maximum number of bytes that it can
2569 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2570 *
2571 * \param[in] generator The generator to query.
2572 * \param[out] capacity On success, the capacity of the generator.
2573 *
2574 * \retval PSA_SUCCESS
2575 * \retval PSA_ERROR_BAD_STATE
2576 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2577 */
2578psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2579 size_t *capacity);
2580
2581/** Read some data from a generator.
2582 *
2583 * This function reads and returns a sequence of bytes from a generator.
2584 * The data that is read is discarded from the generator. The generator's
2585 * capacity is decreased by the number of bytes read.
2586 *
2587 * \param[in,out] generator The generator object to read from.
2588 * \param[out] output Buffer where the generator output will be
2589 * written.
2590 * \param output_length Number of bytes to output.
2591 *
2592 * \retval PSA_SUCCESS
2593 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2594 * There were fewer than \p output_length bytes
2595 * in the generator. Note that in this case, no
2596 * output is written to the output buffer.
2597 * The generator's capacity is set to 0, thus
2598 * subsequent calls to this function will not
2599 * succeed, even with a smaller output buffer.
2600 * \retval PSA_ERROR_BAD_STATE
2601 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2602 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2603 * \retval PSA_ERROR_HARDWARE_FAILURE
2604 * \retval PSA_ERROR_TAMPERING_DETECTED
2605 */
2606psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2607 uint8_t *output,
2608 size_t output_length);
2609
2610/** Create a symmetric key from data read from a generator.
2611 *
2612 * This function reads a sequence of bytes from a generator and imports
2613 * these bytes as a key.
2614 * The data that is read is discarded from the generator. The generator's
2615 * capacity is decreased by the number of bytes read.
2616 *
2617 * This function is equivalent to calling #psa_generator_read and
2618 * passing the resulting output to #psa_import_key, but
2619 * if the implementation provides an isolation boundary then
2620 * the key material is not exposed outside the isolation boundary.
2621 *
2622 * \param key Slot where the key will be stored. This must be a
2623 * valid slot for a key of the chosen type. It must
2624 * be unoccupied.
2625 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2626 * This must be a symmetric key type.
2627 * \param bits Key size in bits.
2628 * \param[in,out] generator The generator object to read from.
2629 *
2630 * \retval PSA_SUCCESS
2631 * Success.
2632 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2633 * There were fewer than \p output_length bytes
2634 * in the generator. Note that in this case, no
2635 * output is written to the output buffer.
2636 * The generator's capacity is set to 0, thus
2637 * subsequent calls to this function will not
2638 * succeed, even with a smaller output buffer.
2639 * \retval PSA_ERROR_NOT_SUPPORTED
2640 * The key type or key size is not supported, either by the
2641 * implementation in general or in this particular slot.
2642 * \retval PSA_ERROR_BAD_STATE
2643 * \retval PSA_ERROR_INVALID_ARGUMENT
2644 * The key slot is invalid.
2645 * \retval PSA_ERROR_OCCUPIED_SLOT
2646 * There is already a key in the specified slot.
2647 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2648 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
2649 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2650 * \retval PSA_ERROR_HARDWARE_FAILURE
2651 * \retval PSA_ERROR_TAMPERING_DETECTED
2652 */
2653psa_status_t psa_generator_import_key(psa_key_slot_t key,
2654 psa_key_type_t type,
2655 size_t bits,
2656 psa_crypto_generator_t *generator);
2657
2658/** Abort a generator.
2659 *
2660 * Once a generator has been aborted, its capacity is zero.
2661 * Aborting a generator frees all associated resources except for the
2662 * \c generator structure itself.
2663 *
2664 * This function may be called at any time as long as the generator
2665 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2666 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2667 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2668 * on a generator that has not been set up.
2669 *
2670 * Once aborted, the generator object may be called.
2671 *
2672 * \param[in,out] generator The generator to abort.
2673 *
2674 * \retval PSA_SUCCESS
2675 * \retval PSA_ERROR_BAD_STATE
2676 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2677 * \retval PSA_ERROR_HARDWARE_FAILURE
2678 * \retval PSA_ERROR_TAMPERING_DETECTED
2679 */
2680psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2681
2682/**@}*/
2683
Gilles Peskineea0fb492018-07-12 17:17:20 +02002684/** \defgroup derivation Key derivation
2685 * @{
2686 */
2687
2688/** Set up a key derivation operation.
2689 *
2690 * A key derivation algorithm takes three inputs: a secret input \p key and
2691 * two non-secret inputs \p label and p salt.
2692 * The result of this function is a byte generator which can
2693 * be used to produce keys and other cryptographic material.
2694 *
2695 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002696 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2697 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002698 *
2699 * \param[in,out] generator The generator object to set up. It must
2700 * have been initialized to .
2701 * \param key Slot containing the secret key to use.
2702 * \param alg The key derivation algorithm to compute
2703 * (\c PSA_ALG_XXX value such that
2704 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2705 * \param[in] salt Salt to use.
2706 * \param salt_length Size of the \p salt buffer in bytes.
2707 * \param[in] label Label to use.
2708 * \param label_length Size of the \p label buffer in bytes.
2709 * \param capacity The maximum number of bytes that the
2710 * generator will be able to provide.
2711 *
2712 * \retval #PSA_SUCCESS
2713 * Success.
2714 * \retval #PSA_ERROR_EMPTY_SLOT
2715 * \retval #PSA_ERROR_NOT_PERMITTED
2716 * \retval #PSA_ERROR_INVALID_ARGUMENT
2717 * \c key is not compatible with \c alg,
2718 * or \p capacity is too large for the specified algorithm and key.
2719 * \retval #PSA_ERROR_NOT_SUPPORTED
2720 * \c alg is not supported or is not a key derivation algorithm.
2721 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2722 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2723 * \retval #PSA_ERROR_HARDWARE_FAILURE
2724 * \retval #PSA_ERROR_TAMPERING_DETECTED
2725 */
2726psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
2727 psa_key_type_t key,
2728 psa_algorithm_t alg,
2729 const uint8_t *salt,
2730 size_t salt_length,
2731 const uint8_t *label,
2732 size_t label_length,
2733 size_t capacity);
2734
2735/**@}*/
2736
Gilles Peskineedd76872018-07-20 17:42:05 +02002737/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002738 * @{
2739 */
2740
2741/**
2742 * \brief Generate random bytes.
2743 *
2744 * \warning This function **can** fail! Callers MUST check the return status
2745 * and MUST NOT use the content of the output buffer if the return
2746 * status is not #PSA_SUCCESS.
2747 *
2748 * \note To generate a key, use psa_generate_key() instead.
2749 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002750 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002751 * \param output_size Number of bytes to generate and output.
2752 *
Gilles Peskine28538492018-07-11 17:34:00 +02002753 * \retval #PSA_SUCCESS
2754 * \retval #PSA_ERROR_NOT_SUPPORTED
2755 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2756 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2757 * \retval #PSA_ERROR_HARDWARE_FAILURE
2758 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002759 */
2760psa_status_t psa_generate_random(uint8_t *output,
2761 size_t output_size);
2762
Gilles Peskine4c317f42018-07-12 01:24:09 +02002763/** Extra parameters for RSA key generation.
2764 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002765 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002766 * parameter to psa_generate_key().
2767 */
2768typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002769 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002770} psa_generate_key_extra_rsa;
2771
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002772/**
2773 * \brief Generate a key or key pair.
2774 *
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002775 * \param key Slot where the key will be stored. This must be a
2776 * valid slot for a key of the chosen type. It must
2777 * be unoccupied.
2778 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2779 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002780 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002781 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002782 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002783 * default parameters. Implementation that support
2784 * the generation of vendor-specific key types
2785 * that allow extra parameters shall document
2786 * the format of these extra parameters and
2787 * the default values. For standard parameters,
2788 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002789 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002790 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2791 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002792 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002793 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2794 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002795 * - For an RSA key (\p type is
2796 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2797 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002798 * specifying the public exponent. The
2799 * default public exponent used when \p extra
2800 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002801 * \param extra_size Size of the buffer that \p extra
2802 * points to, in bytes. Note that if \p extra is
2803 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002804 *
Gilles Peskine28538492018-07-11 17:34:00 +02002805 * \retval #PSA_SUCCESS
2806 * \retval #PSA_ERROR_NOT_SUPPORTED
2807 * \retval #PSA_ERROR_INVALID_ARGUMENT
2808 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2809 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2810 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2811 * \retval #PSA_ERROR_HARDWARE_FAILURE
2812 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002813 */
2814psa_status_t psa_generate_key(psa_key_slot_t key,
2815 psa_key_type_t type,
2816 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002817 const void *extra,
2818 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002819
2820/**@}*/
2821
Gilles Peskinee59236f2018-01-27 23:32:46 +01002822#ifdef __cplusplus
2823}
2824#endif
2825
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002826/* The file "crypto_sizes.h" contains definitions for size calculation
2827 * macros whose definitions are implementation-specific. */
2828#include "crypto_sizes.h"
2829
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002830/* The file "crypto_struct.h" contains definitions for
2831 * implementation-specific structs that are declared above. */
2832#include "crypto_struct.h"
2833
2834/* The file "crypto_extra.h" contains vendor-specific definitions. This
2835 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002836#include "crypto_extra.h"
2837
2838#endif /* PSA_CRYPTO_H */