blob: d976d7b18294ba2d35c05be974500244050a1904 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010039/** \brief Key slot number.
40 *
41 * This type represents key slots. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043 * 0 is not a valid key slot number. The meaning of other values is
44 * implementation dependent.
45 *
46 * At any given point in time, each key slot either contains a
47 * cryptographic object, or is empty. Key slots are persistent:
48 * once set, the cryptographic object remains in the key slot until
49 * explicitly destroyed.
50 */
51typedef _unsigned_integral_type_ psa_key_slot_t;
52
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010053/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010054#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010055
Gilles Peskinee59236f2018-01-27 23:32:46 +010056#ifdef __cplusplus
57extern "C" {
58#endif
59
60/** \defgroup basic Basic definitions
61 * @{
62 */
63
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020064#if defined(PSA_SUCCESS)
65/* If PSA_SUCCESS is defined, assume that PSA crypto is being used
66 * together with PSA IPC, which also defines the identifier
67 * PSA_SUCCESS. We must not define PSA_SUCCESS ourselves in that case;
68 * the other error code names don't clash. Also define psa_status_t as
69 * an alias for the type used by PSA IPC. This is a temporary hack
mohammad160313f43942018-08-05 12:09:44 +030070 * until we unify error reporting in PSA IPC and PSA crypto.
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020071 *
72 * Note that psa_defs.h must be included before this header!
73 */
74typedef psa_error_t psa_status_t;
75
76#else /* defined(PSA_SUCCESS) */
77
Gilles Peskinee59236f2018-01-27 23:32:46 +010078/**
79 * \brief Function return status.
80 *
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020081 * This is either #PSA_SUCCESS (which is zero), indicating success,
82 * or a nonzero value indicating that an error occurred. Errors are
83 * encoded as one of the \c PSA_ERROR_xxx values defined here.
Gilles Peskinee59236f2018-01-27 23:32:46 +010084 */
itayzafrirc2a79762018-06-18 16:20:16 +030085typedef int32_t psa_status_t;
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020086
itayzafrirc2a79762018-06-18 16:20:16 +030087/** The action was completed successfully. */
88#define PSA_SUCCESS ((psa_status_t)0)
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020089
90#endif /* !defined(PSA_SUCCESS) */
itayzafrirc2a79762018-06-18 16:20:16 +030091
itayzafrirf26dbfc2018-08-01 16:09:08 +030092/** An error occurred that does not correspond to any defined
93 * failure cause.
94 *
95 * Implementations may use this error code if none of the other standard
96 * error codes are applicable. */
97#define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)1)
98
itayzafrirc2a79762018-06-18 16:20:16 +030099/** The requested operation or a parameter is not supported
100 * by this implementation.
101 *
102 * Implementations should return this error code when an enumeration
103 * parameter such as a key type, algorithm, etc. is not recognized.
104 * If a combination of parameters is recognized and identified as
105 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300106#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)2)
itayzafrirc2a79762018-06-18 16:20:16 +0300107
108/** The requested action is denied by a policy.
109 *
110 * Implementations should return this error code when the parameters
111 * are recognized as valid and supported, and a policy explicitly
112 * denies the requested operation.
113 *
114 * If a subset of the parameters of a function call identify a
115 * forbidden operation, and another subset of the parameters are
116 * not valid or not supported, it is unspecified whether the function
117 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
118 * #PSA_ERROR_INVALID_ARGUMENT. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300119#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)3)
itayzafrirc2a79762018-06-18 16:20:16 +0300120
121/** An output buffer is too small.
122 *
Gilles Peskinebe42f312018-07-13 14:38:15 +0200123 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
itayzafrirc2a79762018-06-18 16:20:16 +0300124 * description to determine a sufficient buffer size.
125 *
126 * Implementations should preferably return this error code only
127 * in cases when performing the operation with a larger output
128 * buffer would succeed. However implementations may return this
129 * error if a function has invalid or unsupported parameters in addition
130 * to the parameters that determine the necessary output buffer size. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300131#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)4)
itayzafrirc2a79762018-06-18 16:20:16 +0300132
133/** A slot is occupied, but must be empty to carry out the
134 * requested action.
135 *
136 * If the slot number is invalid (i.e. the requested action could
137 * not be performed even after erasing the slot's content),
138 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300139#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)5)
itayzafrirc2a79762018-06-18 16:20:16 +0300140
141/** A slot is empty, but must be occupied to carry out the
142 * requested action.
143 *
144 * If the slot number is invalid (i.e. the requested action could
145 * not be performed even after creating appropriate content in the slot),
146 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300147#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)6)
itayzafrirc2a79762018-06-18 16:20:16 +0300148
149/** The requested action cannot be performed in the current state.
150 *
151 * Multipart operations return this error when one of the
152 * functions is called out of sequence. Refer to the function
153 * descriptions for permitted sequencing of functions.
154 *
155 * Implementations shall not return this error code to indicate
156 * that a key slot is occupied when it needs to be free or vice versa,
157 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
158 * as applicable. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300159#define PSA_ERROR_BAD_STATE ((psa_status_t)7)
itayzafrirc2a79762018-06-18 16:20:16 +0300160
161/** The parameters passed to the function are invalid.
162 *
163 * Implementations may return this error any time a parameter or
164 * combination of parameters are recognized as invalid.
165 *
166 * Implementations shall not return this error code to indicate
167 * that a key slot is occupied when it needs to be free or vice versa,
168 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
169 * as applicable. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300170#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)8)
itayzafrirc2a79762018-06-18 16:20:16 +0300171
172/** There is not enough runtime memory.
173 *
174 * If the action is carried out across multiple security realms, this
175 * error can refer to available memory in any of the security realms. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300176#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)9)
itayzafrirc2a79762018-06-18 16:20:16 +0300177
178/** There is not enough persistent storage.
179 *
180 * Functions that modify the key storage return this error code if
181 * there is insufficient storage space on the host media. In addition,
182 * many functions that do not otherwise access storage may return this
183 * error code if the implementation requires a mandatory log entry for
184 * the requested action and the log storage space is full. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300185#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)10)
itayzafrirc2a79762018-06-18 16:20:16 +0300186
187/** There was a communication failure inside the implementation.
188 *
189 * This can indicate a communication failure between the application
190 * and an external cryptoprocessor or between the cryptoprocessor and
191 * an external volatile or persistent memory. A communication failure
192 * may be transient or permanent depending on the cause.
193 *
194 * \warning If a function returns this error, it is undetermined
195 * whether the requested action has completed or not. Implementations
196 * should return #PSA_SUCCESS on successful completion whenver
197 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
198 * if the requested action was completed successfully in an external
199 * cryptoprocessor but there was a breakdown of communication before
200 * the cryptoprocessor could report the status to the application.
201 */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300202#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)11)
itayzafrirc2a79762018-06-18 16:20:16 +0300203
204/** There was a storage failure that may have led to data loss.
205 *
206 * This error indicates that some persistent storage is corrupted.
207 * It should not be used for a corruption of volatile memory
208 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
209 * between the cryptoprocessor and its external storage (use
210 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
211 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
212 *
213 * Note that a storage failure does not indicate that any data that was
214 * previously read is invalid. However this previously read data may no
215 * longer be readable from storage.
216 *
217 * When a storage failure occurs, it is no longer possible to ensure
218 * the global integrity of the keystore. Depending on the global
219 * integrity guarantees offered by the implementation, access to other
220 * data may or may not fail even if the data is still readable but
221 * its integrity canont be guaranteed.
222 *
223 * Implementations should only use this error code to report a
224 * permanent storage corruption. However application writers should
225 * keep in mind that transient errors while reading the storage may be
226 * reported using this error code. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300227#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)12)
itayzafrirc2a79762018-06-18 16:20:16 +0300228
229/** A hardware failure was detected.
230 *
231 * A hardware failure may be transient or permanent depending on the
232 * cause. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300233#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)13)
itayzafrirc2a79762018-06-18 16:20:16 +0300234
235/** A tampering attempt was detected.
236 *
237 * If an application receives this error code, there is no guarantee
238 * that previously accessed or computed data was correct and remains
239 * confidential. Applications should not perform any security function
240 * and should enter a safe failure state.
241 *
242 * Implementations may return this error code if they detect an invalid
243 * state that cannot happen during normal operation and that indicates
244 * that the implementation's security guarantees no longer hold. Depending
245 * on the implementation architecture and on its security and safety goals,
246 * the implementation may forcibly terminate the application.
247 *
248 * This error code is intended as a last resort when a security breach
249 * is detected and it is unsure whether the keystore data is still
250 * protected. Implementations shall only return this error code
251 * to report an alarm from a tampering detector, to indicate that
252 * the confidentiality of stored data can no longer be guaranteed,
253 * or to indicate that the integrity of previously returned data is now
254 * considered compromised. Implementations shall not use this error code
255 * to indicate a hardware failure that merely makes it impossible to
256 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
257 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
258 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
259 * instead).
260 *
261 * This error indicates an attack against the application. Implementations
262 * shall not return this error code as a consequence of the behavior of
263 * the application itself. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300264#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)14)
itayzafrirc2a79762018-06-18 16:20:16 +0300265
266/** There is not enough entropy to generate random data needed
267 * for the requested action.
268 *
269 * This error indicates a failure of a hardware random generator.
270 * Application writers should note that this error can be returned not
271 * only by functions whose purpose is to generate random data, such
272 * as key, IV or nonce generation, but also by functions that execute
273 * an algorithm with a randomized result, as well as functions that
274 * use randomization of intermediate computations as a countermeasure
275 * to certain attacks.
276 *
277 * Implementations should avoid returning this error after psa_crypto_init()
278 * has succeeded. Implementations should generate sufficient
279 * entropy during initialization and subsequently use a cryptographically
280 * secure pseudorandom generator (PRNG). However implementations may return
281 * this error at any time if a policy requires the PRNG to be reseeded
282 * during normal operation. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300283#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)15)
itayzafrirc2a79762018-06-18 16:20:16 +0300284
285/** The signature, MAC or hash is incorrect.
286 *
287 * Verification functions return this error if the verification
288 * calculations completed successfully, and the value to be verified
289 * was determined to be incorrect.
290 *
291 * If the value to verify has an invalid size, implementations may return
292 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300293#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)16)
itayzafrirc2a79762018-06-18 16:20:16 +0300294
295/** The decrypted padding is incorrect.
296 *
297 * \warning In some protocols, when decrypting data, it is essential that
298 * the behavior of the application does not depend on whether the padding
299 * is correct, down to precise timing. Applications should prefer
300 * protocols that use authenticated encryption rather than plain
301 * encryption. If the application must perform a decryption of
302 * unauthenticated data, the application writer should take care not
303 * to reveal whether the padding is invalid.
304 *
305 * Implementations should strive to make valid and invalid padding
306 * as close as possible to indistinguishable to an external observer.
307 * In particular, the timing of a decryption operation should not
308 * depend on the validity of the padding. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300309#define PSA_ERROR_INVALID_PADDING ((psa_status_t)17)
itayzafrirc2a79762018-06-18 16:20:16 +0300310
Gilles Peskineeab56e42018-07-12 17:12:33 +0200311/** The generator has insufficient capacity left.
312 *
313 * Once a function returns this error, attempts to read from the
314 * generator will always return this error. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300315#define PSA_ERROR_INSUFFICIENT_CAPACITY ((psa_status_t)18)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100316
317/**
318 * \brief Library initialization.
319 *
320 * Applications must call this function before calling any other
321 * function in this module.
322 *
323 * Applications may call this function more than once. Once a call
324 * succeeds, subsequent calls are guaranteed to succeed.
325 *
itayzafrir18617092018-09-16 12:22:41 +0300326 * If the application calls other functions before calling psa_crypto_init(),
327 * the behavior is undefined. Implementations are encouraged to either perform
328 * the operation as if the library had been initialized or to return
329 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
330 * implementations should not return a success status if the lack of
331 * initialization may have security implications, for example due to improper
332 * seeding of the random number generator.
333 *
Gilles Peskine28538492018-07-11 17:34:00 +0200334 * \retval #PSA_SUCCESS
335 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
336 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
337 * \retval #PSA_ERROR_HARDWARE_FAILURE
338 * \retval #PSA_ERROR_TAMPERING_DETECTED
339 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100340 */
341psa_status_t psa_crypto_init(void);
342
Gilles Peskine2905a7a2018-03-07 16:39:31 +0100343#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
344#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Gilles Peskine0189e752018-02-03 23:57:22 +0100345
Gilles Peskinee59236f2018-01-27 23:32:46 +0100346/**@}*/
347
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100348/** \defgroup crypto_types Key and algorithm types
349 * @{
350 */
351
Gilles Peskine308b91d2018-02-08 09:47:44 +0100352/** \brief Encoding of a key type.
353 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100354typedef uint32_t psa_key_type_t;
355
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100356/** An invalid key type value.
357 *
358 * Zero is not the encoding of any key type.
359 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100360#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100361
362/** Vendor-defined flag
363 *
364 * Key types defined by this standard will never have the
365 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
366 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
367 * respect the bitwise structure used by standard encodings whenever practical.
368 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100369#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100370
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200371#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x70000000)
372#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x40000000)
373#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x50000000)
374#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x60000000)
375#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x70000000)
376
377#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x10000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200378
Gilles Peskinee8779742018-08-10 16:10:56 +0200379/** Whether a key type is vendor-defined. */
380#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
381 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
382
383/** Whether a key type is an unstructured array of bytes.
384 *
385 * This encompasses both symmetric keys and non-key data.
386 */
387#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
388 (((type) & PSA_KEY_TYPE_CATEGORY_MASK & ~(psa_key_type_t)0x10000000) == \
389 PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
390
391/** Whether a key type is asymmetric: either a key pair or a public key. */
392#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
393 (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
394 & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
395 PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
396/** Whether a key type is the public part of a key pair. */
397#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
398 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
399/** Whether a key type is a key pair containing a private part and a public
400 * part. */
401#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
402 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
403/** The key pair type corresponding to a public key type.
404 *
405 * You may also pass a key pair type as \p type, it will be left unchanged.
406 *
407 * \param type A public key type or key pair type.
408 *
409 * \return The corresponding key pair type.
410 * If \p type is not a public key or a key pair,
411 * the return value is undefined.
412 */
413#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
414 ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
415/** The public key type corresponding to a key pair type.
416 *
417 * You may also pass a key pair type as \p type, it will be left unchanged.
418 *
419 * \param type A public key type or key pair type.
420 *
421 * \return The corresponding public key type.
422 * If \p type is not a public key or a key pair,
423 * the return value is undefined.
424 */
425#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
426 ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
427/** Whether a key type is an RSA key (pair or public-only). */
428#define PSA_KEY_TYPE_IS_RSA(type) \
429 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
430
Gilles Peskine35855962018-04-19 08:39:16 +0200431/** Raw data.
432 *
433 * A "key" of this type cannot be used for any cryptographic operation.
434 * Applications may use this type to store arbitrary data in the keystore. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200435#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x50000001)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100436
Gilles Peskine35855962018-04-19 08:39:16 +0200437/** HMAC key.
438 *
439 * The key policy determines which underlying hash algorithm the key can be
440 * used for.
441 *
442 * HMAC keys should generally have the same size as the underlying hash.
Gilles Peskinebe42f312018-07-13 14:38:15 +0200443 * This size can be calculated with #PSA_HASH_SIZE(\c alg) where
444 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200445#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x51000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200446
Gilles Peskineea0fb492018-07-12 17:17:20 +0200447/** A secret for key derivation.
448 *
449 * The key policy determines which key derivation algorithm the key
450 * can be used for.
451 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200452#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x52000000)
Gilles Peskineea0fb492018-07-12 17:17:20 +0200453
Gilles Peskine35855962018-04-19 08:39:16 +0200454/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
455 *
456 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
457 * 32 bytes (AES-256).
458 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200459#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x40000001)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200460
Gilles Peskine35855962018-04-19 08:39:16 +0200461/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
462 *
463 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
464 * 24 bytes (3-key 3DES).
465 *
466 * Note that single DES and 2-key 3DES are weak and strongly
467 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
468 * is weak and deprecated and should only be used in legacy protocols.
469 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200470#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x40000002)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200471
Gilles Peskine35855962018-04-19 08:39:16 +0200472/** Key for an cipher, AEAD or MAC algorithm based on the
473 * Camellia block cipher. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200474#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x40000003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200475
Gilles Peskine35855962018-04-19 08:39:16 +0200476/** Key for the RC4 stream cipher.
477 *
478 * Note that RC4 is weak and deprecated and should only be used in
479 * legacy protocols. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200480#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x40000004)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100481
Gilles Peskine308b91d2018-02-08 09:47:44 +0100482/** RSA public key. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200483#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x60010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100484/** RSA key pair (private and public key). */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200485#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x70010000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200486
Gilles Peskine06dc2632018-03-08 07:47:25 +0100487/** DSA public key. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200488#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x60020000)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100489/** DSA key pair (private and public key). */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200490#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x70020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200491
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200492#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x60030000)
493#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x70030000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100494#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200495/** Elliptic curve key pair. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100496#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
497 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200498/** Elliptic curve public key. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100499#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
500 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100501
Gilles Peskined8008d62018-06-29 19:51:51 +0200502/** Whether a key type is an elliptic curve key (pair or public-only). */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100503#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100504 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
505 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine55728b02018-07-16 23:08:16 +0200506#define PSA_KEY_TYPE_IS_ECC_KEYPAIR(type) \
507 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
508 PSA_KEY_TYPE_ECC_KEYPAIR_BASE)
509#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
510 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
511 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100512
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200513/** The type of PSA elliptic curve identifiers. */
514typedef uint16_t psa_ecc_curve_t;
515/** Extract the curve from an elliptic curve key type. */
516#define PSA_KEY_TYPE_GET_CURVE(type) \
517 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
518 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
519 0))
520
521/* The encoding of curve identifiers is currently aligned with the
522 * TLS Supported Groups Registry (formerly known as the
523 * TLS EC Named Curve Registry)
524 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
525 * The values are defined by RFC 4492, RFC 7027 and RFC 7919. */
526#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
527#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
528#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
529#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
530#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
531#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
532#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
533#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
534#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
535#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
536#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
537#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
538#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
539#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
540#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
541#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
542#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
543#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
544#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
545#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
546#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
547#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
548#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
549#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
550#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
551#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
552#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
553#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
554#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
555#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
556#define PSA_ECC_CURVE_FFDHE_2048 ((psa_ecc_curve_t) 0x0100)
557#define PSA_ECC_CURVE_FFDHE_3072 ((psa_ecc_curve_t) 0x0101)
558#define PSA_ECC_CURVE_FFDHE_4096 ((psa_ecc_curve_t) 0x0102)
559#define PSA_ECC_CURVE_FFDHE_6144 ((psa_ecc_curve_t) 0x0103)
560#define PSA_ECC_CURVE_FFDHE_8192 ((psa_ecc_curve_t) 0x0104)
561
Gilles Peskine7e198532018-03-08 07:50:30 +0100562/** The block size of a block cipher.
563 *
564 * \param type A cipher key type (value of type #psa_key_type_t).
565 *
566 * \return The block size for a block cipher, or 1 for a stream cipher.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200567 * The return value is undefined if \p type is not a supported
Gilles Peskine35855962018-04-19 08:39:16 +0200568 * cipher key type.
569 *
570 * \note It is possible to build stream cipher algorithms on top of a block
571 * cipher, for example CTR mode (#PSA_ALG_CTR).
572 * This macro only takes the key type into account, so it cannot be
573 * used to determine the size of the data that #psa_cipher_update()
574 * might buffer for future processing in general.
Gilles Peskine7e198532018-03-08 07:50:30 +0100575 *
576 * \note This macro returns a compile-time constant if its argument is one.
577 *
578 * \warning This macro may evaluate its argument multiple times.
579 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100580#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100581 ( \
582 (type) == PSA_KEY_TYPE_AES ? 16 : \
583 (type) == PSA_KEY_TYPE_DES ? 8 : \
584 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100585 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100586 0)
587
Gilles Peskine308b91d2018-02-08 09:47:44 +0100588/** \brief Encoding of a cryptographic algorithm.
589 *
590 * For algorithms that can be applied to multiple key types, this type
591 * does not encode the key type. For example, for symmetric ciphers
592 * based on a block cipher, #psa_algorithm_t encodes the block cipher
593 * mode and the padding mode while the block cipher itself is encoded
594 * via #psa_key_type_t.
595 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100596typedef uint32_t psa_algorithm_t;
597
Gilles Peskine98f0a242018-02-06 18:57:29 +0100598#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
599#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
600#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
601#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
602#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
603#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
604#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
605#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
606#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
607#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100608
Gilles Peskine98f0a242018-02-06 18:57:29 +0100609#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
610 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200611
Gilles Peskine308b91d2018-02-08 09:47:44 +0100612/** Whether the specified algorithm is a hash algorithm.
613 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100614 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100615 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200616 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
617 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskine7e198532018-03-08 07:50:30 +0100618 * algorithm identifier.
619 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100620#define PSA_ALG_IS_HASH(alg) \
621 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200622
623/** Whether the specified algorithm is a MAC algorithm.
624 *
625 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
626 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200627 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
628 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200629 * algorithm identifier.
630 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100631#define PSA_ALG_IS_MAC(alg) \
632 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200633
634/** Whether the specified algorithm is a symmetric cipher algorithm.
635 *
636 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
637 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200638 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
639 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200640 * algorithm identifier.
641 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100642#define PSA_ALG_IS_CIPHER(alg) \
643 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200644
645/** Whether the specified algorithm is an authenticated encryption
646 * with associated data (AEAD) algorithm.
647 *
648 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
649 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200650 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
651 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200652 * algorithm identifier.
653 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100654#define PSA_ALG_IS_AEAD(alg) \
655 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200656
657/** Whether the specified algorithm is a public-key signature algorithm.
658 *
659 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
660 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200661 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
662 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200663 * algorithm identifier.
664 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100665#define PSA_ALG_IS_SIGN(alg) \
666 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200667
668/** Whether the specified algorithm is a public-key encryption algorithm.
669 *
670 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
671 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200672 * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
673 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200674 * algorithm identifier.
675 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100676#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
677 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200678
679/** Whether the specified algorithm is a key agreement algorithm.
680 *
681 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
682 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200683 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
684 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200685 * algorithm identifier.
686 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100687#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
688 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200689
690/** Whether the specified algorithm is a key derivation algorithm.
691 *
692 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
693 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200694 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
695 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200696 * algorithm identifier.
697 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100698#define PSA_ALG_IS_KEY_DERIVATION(alg) \
699 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
700
701#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
702#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
703#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
704#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100705#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
706#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskineedd76872018-07-20 17:42:05 +0200707/** SHA2-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100708#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
Gilles Peskineedd76872018-07-20 17:42:05 +0200709/** SHA2-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100710#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
Gilles Peskineedd76872018-07-20 17:42:05 +0200711/** SHA2-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100712#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
Gilles Peskineedd76872018-07-20 17:42:05 +0200713/** SHA2-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100714#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
Gilles Peskineedd76872018-07-20 17:42:05 +0200715/** SHA2-512/224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100716#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
Gilles Peskineedd76872018-07-20 17:42:05 +0200717/** SHA2-512/256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100718#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
Gilles Peskineedd76872018-07-20 17:42:05 +0200719/** SHA3-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100720#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
Gilles Peskineedd76872018-07-20 17:42:05 +0200721/** SHA3-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100722#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
Gilles Peskineedd76872018-07-20 17:42:05 +0200723/** SHA3-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100724#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
Gilles Peskineedd76872018-07-20 17:42:05 +0200725/** SHA3-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100726#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
727
Gilles Peskine8c9def32018-02-08 10:02:12 +0100728#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100729#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200730/** Macro to build an HMAC algorithm.
731 *
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200732 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
Gilles Peskine35855962018-04-19 08:39:16 +0200733 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200734 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200735 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine35855962018-04-19 08:39:16 +0200736 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200737 * \return The corresponding HMAC algorithm.
738 * \return Unspecified if \p alg is not a supported
739 * hash algorithm.
Gilles Peskine35855962018-04-19 08:39:16 +0200740 */
741#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100742 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200743
Gilles Peskine8c9def32018-02-08 10:02:12 +0100744#define PSA_ALG_HMAC_HASH(hmac_alg) \
745 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200746
747/** Whether the specified algorithm is an HMAC algorithm.
748 *
749 * HMAC is a family of MAC algorithms that are based on a hash function.
750 *
751 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
752 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200753 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
754 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200755 * algorithm identifier.
756 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100757#define PSA_ALG_IS_HMAC(alg) \
758 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
759 PSA_ALG_HMAC_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200760
Gilles Peskine8c9def32018-02-08 10:02:12 +0100761#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
762#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
763#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
764#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200765
766/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
767 *
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200768 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
769 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200770 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
771 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200772 * algorithm identifier.
773 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100774#define PSA_ALG_IS_CIPHER_MAC(alg) \
775 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
776 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100777
Gilles Peskine8c9def32018-02-08 10:02:12 +0100778#define PSA_ALG_CIPHER_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100779#define PSA_ALG_BLOCK_CIPHER_BASE ((psa_algorithm_t)0x04000000)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100780#define PSA_ALG_BLOCK_CIPHER_MODE_MASK ((psa_algorithm_t)0x000000ff)
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100781#define PSA_ALG_BLOCK_CIPHER_PADDING_MASK ((psa_algorithm_t)0x003f0000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200782
783/** Use a block cipher mode without padding.
784 *
785 * This padding mode may only be used with messages whose lengths are a
786 * whole number of blocks for the chosen block cipher.
787 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +0100788#define PSA_ALG_BLOCK_CIPHER_PAD_NONE ((psa_algorithm_t)0x00000000)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200789
Gilles Peskine98f0a242018-02-06 18:57:29 +0100790#define PSA_ALG_BLOCK_CIPHER_PAD_PKCS7 ((psa_algorithm_t)0x00010000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200791
792/** Whether the specified algorithm is a block cipher.
793 *
794 * A block cipher is a symmetric cipher that encrypts or decrypts messages
795 * by chopping them into fixed-size blocks. Processing a message requires
796 * applying a _padding mode_ to transform the message into one whose
797 * length is a whole number of blocks. To construct an algorithm
798 * identifier for a block cipher, apply a bitwise-or between the block
799 * cipher mode and the padding mode. For example, CBC with PKCS#7 padding
800 * is `PSA_ALG_CBC_BASE | PSA_ALG_BLOCK_CIPHER_PAD_PKCS7`.
801 *
802 * The transformation applied to each block is determined by the key type.
803 * For example, to use AES-128-CBC-PKCS7, use the algorithm above with
804 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
805 *
806 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
807 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200808 * \return 1 if \p alg is a block cipher algorithm, 0 otherwise.
809 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200810 * algorithm identifier or if it is not a symmetric cipher algorithm.
811 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100812#define PSA_ALG_IS_BLOCK_CIPHER(alg) \
813 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
814 PSA_ALG_BLOCK_CIPHER_BASE)
815
Gilles Peskinedcd14942018-07-12 00:30:52 +0200816/** The CBC block cipher mode.
817 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100818#define PSA_ALG_CBC_BASE ((psa_algorithm_t)0x04000001)
Gilles Peskine8c9def32018-02-08 10:02:12 +0100819#define PSA_ALG_CFB_BASE ((psa_algorithm_t)0x04000002)
820#define PSA_ALG_OFB_BASE ((psa_algorithm_t)0x04000003)
821#define PSA_ALG_XTS_BASE ((psa_algorithm_t)0x04000004)
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200822
823#define PSA_ALG_STREAM_CIPHER_BASE ((psa_algorithm_t)0x04800000)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200824
Gilles Peskinedcd14942018-07-12 00:30:52 +0200825/** The CTR stream cipher mode.
826 *
827 * CTR is a stream cipher which is built from a block cipher. The
828 * underlying block cipher is determined by the key type. For example,
829 * to use AES-128-CTR, use this algorithm with
830 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
831 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100832#define PSA_ALG_CTR ((psa_algorithm_t)0x04800001)
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200833
Gilles Peskinedcd14942018-07-12 00:30:52 +0200834/** The ARC4 stream cipher algorithm.
835 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100836#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100837
Gilles Peskinedcd14942018-07-12 00:30:52 +0200838/** Whether the specified algorithm is a stream cipher.
839 *
840 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
841 * by applying a bitwise-xor with a stream of bytes that is generated
842 * from a key.
843 *
844 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
845 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200846 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
847 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200848 * algorithm identifier or if it is not a symmetric cipher algorithm.
849 */
Moran Pekerbed71a22018-04-22 20:19:20 +0300850#define PSA_ALG_IS_STREAM_CIPHER(alg) \
851 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_SUBCATEGORY_MASK)) == \
Gilles Peskine5d1888e2018-07-12 00:32:42 +0200852 PSA_ALG_STREAM_CIPHER_BASE)
Moran Pekerbed71a22018-04-22 20:19:20 +0300853
Gilles Peskine8c9def32018-02-08 10:02:12 +0100854#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
855#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100856
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200857#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
858/** RSA PKCS#1 v1.5 signature with hashing.
859 *
860 * This is the signature scheme defined by RFC 8017
861 * (PKCS#1: RSA Cryptography Specifications) under the name
862 * RSASSA-PKCS1-v1_5.
863 *
864 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200865 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200866 *
867 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
868 * \return Unspecified if \p alg is not a supported
869 * hash algorithm.
870 */
Gilles Peskinea5926232018-03-28 14:16:50 +0200871#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200872 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
873/** Raw PKCS#1 v1.5 signature.
874 *
875 * The input to this algorithm is the DigestInfo structure used by
876 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
877 * steps 3&ndash;6.
878 */
879#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
Gilles Peskinea5926232018-03-28 14:16:50 +0200880#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200881 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200882
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200883#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
884/** RSA PSS signature with hashing.
885 *
886 * This is the signature scheme defined by RFC 8017
887 * (PKCS#1: RSA Cryptography Specifications) under the name
Gilles Peskinea4d20bd2018-06-29 23:35:02 +0200888 * RSASSA-PSS, with the message generation function MGF1, and with
889 * a salt length equal to the length of the hash. The specified
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200890 * hash algorithm is used to hash the input message, to create the
891 * salted hash, and for the mask generation.
892 *
893 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200894 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200895 *
896 * \return The corresponding RSA PSS signature algorithm.
897 * \return Unspecified if \p alg is not a supported
898 * hash algorithm.
899 */
900#define PSA_ALG_RSA_PSS(hash_alg) \
901 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
902#define PSA_ALG_IS_RSA_PSS(alg) \
903 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
904
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200905#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
906/** DSA signature with hashing.
907 *
908 * This is the signature scheme defined by FIPS 186-4,
909 * with a random per-message secret number (*k*).
910 *
911 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200912 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200913 *
914 * \return The corresponding DSA signature algorithm.
915 * \return Unspecified if \p alg is not a supported
916 * hash algorithm.
917 */
918#define PSA_ALG_DSA(hash_alg) \
919 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
920#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
921#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
922#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
923 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
924#define PSA_ALG_IS_DSA(alg) \
925 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
926 PSA_ALG_DSA_BASE)
927#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
928 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +0200929#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
930 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
931#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
932 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200933
934#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
935/** ECDSA signature with hashing.
936 *
937 * This is the ECDSA signature scheme defined by ANSI X9.62,
938 * with a random per-message secret number (*k*).
939 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200940 * The representation of the signature as a byte string consists of
941 * the concatentation of the signature values *r* and *s*. Each of
942 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
943 * of the base point of the curve in octets. Each value is represented
944 * in big-endian order (most significant octet first).
945 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200946 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200947 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200948 *
949 * \return The corresponding ECDSA signature algorithm.
950 * \return Unspecified if \p alg is not a supported
951 * hash algorithm.
952 */
953#define PSA_ALG_ECDSA(hash_alg) \
954 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
955/** ECDSA signature without hashing.
956 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200957 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200958 * without specifying a hash algorithm. This algorithm may only be
959 * used to sign or verify a sequence of bytes that should be an
960 * already-calculated hash. Note that the input is padded with
961 * zeros on the left or truncated on the left as required to fit
962 * the curve size.
963 */
964#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
965#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
966/** Deterministic ECDSA signature with hashing.
967 *
968 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
969 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200970 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
971 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200972 * Note that when this algorithm is used for verification, signatures
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200973 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200974 * same private key are accepted. In other words,
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200975 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
976 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200977 *
978 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200979 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200980 *
981 * \return The corresponding deterministic ECDSA signature
982 * algorithm.
983 * \return Unspecified if \p alg is not a supported
984 * hash algorithm.
985 */
986#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
987 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
988#define PSA_ALG_IS_ECDSA(alg) \
989 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
990 PSA_ALG_ECDSA_BASE)
991#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
992 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +0200993#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
994 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
995#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
996 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200997
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200998/** Get the hash used by a hash-and-sign signature algorithm.
999 *
1000 * A hash-and-sign algorithm is a signature algorithm which is
1001 * composed of two phases: first a hashing phase which does not use
1002 * the key and produces a hash of the input message, then a signing
1003 * phase which only uses the hash and the key and not the message
1004 * itself.
1005 *
1006 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001007 * #PSA_ALG_IS_SIGN(\p alg) is true).
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001008 *
1009 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1010 * algorithm.
1011 * \return 0 if \p alg is a signature algorithm that does not
1012 * follow the hash-and-sign structure.
1013 * \return Unspecified if \p alg is not a signature algorithm or
1014 * if it is not supported by the implementation.
1015 */
1016#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001017 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
1018 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
Gilles Peskine54622ae2018-06-29 22:24:24 +02001019 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001020 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1021 0)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022
Gilles Peskinedcd14942018-07-12 00:30:52 +02001023/** RSA PKCS#1 v1.5 encryption.
1024 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001025#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001026
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001027#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001028/** RSA OAEP encryption.
1029 *
1030 * This is the encryption scheme defined by RFC 8017
1031 * (PKCS#1: RSA Cryptography Specifications) under the name
1032 * RSAES-OAEP, with the message generation function MGF1.
1033 *
1034 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1035 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1036 * for MGF1.
1037 *
1038 * \return The corresponding RSA OAEP signature algorithm.
1039 * \return Unspecified if \p alg is not a supported
1040 * hash algorithm.
1041 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001042#define PSA_ALG_RSA_OAEP(hash_alg) \
1043 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1044#define PSA_ALG_IS_RSA_OAEP(alg) \
1045 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
Gilles Peskine072ac562018-06-30 00:21:29 +02001046#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1047 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1048 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1049 0)
Gilles Peskined1e8e412018-06-07 09:49:39 +02001050
Gilles Peskinebef7f142018-07-12 17:22:21 +02001051#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100)
1052/** Macro to build an HKDF algorithm.
1053 *
1054 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1055 *
1056 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1057 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1058 *
1059 * \return The corresponding HKDF algorithm.
1060 * \return Unspecified if \p alg is not a supported
1061 * hash algorithm.
1062 */
1063#define PSA_ALG_HKDF(hash_alg) \
1064 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1065/** Whether the specified algorithm is an HKDF algorithm.
1066 *
1067 * HKDF is a family of key derivation algorithms that are based on a hash
1068 * function and the HMAC construction.
1069 *
1070 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1071 *
1072 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1073 * This macro may return either 0 or 1 if \c alg is not a supported
1074 * key derivation algorithm identifier.
1075 */
1076#define PSA_ALG_IS_HKDF(alg) \
1077 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1078#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1079 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1080
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001081/**@}*/
1082
1083/** \defgroup key_management Key management
1084 * @{
1085 */
1086
1087/**
1088 * \brief Import a key in binary format.
1089 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +01001090 * This function supports any output from psa_export_key(). Refer to the
1091 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001092 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001093 * \param key Slot where the key will be stored. This must be a
1094 * valid slot for a key of the chosen type. It must
1095 * be unoccupied.
1096 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001097 * \param[in] data Buffer containing the key data.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001098 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001099 *
Gilles Peskine28538492018-07-11 17:34:00 +02001100 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001101 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001102 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001103 * The key type or key size is not supported, either by the
1104 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001105 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +01001106 * The key slot is invalid,
1107 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +02001108 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001109 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001110 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1111 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1112 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1113 * \retval #PSA_ERROR_HARDWARE_FAILURE
1114 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001115 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001116 * The library has not been previously initialized by psa_crypto_init().
1117 * It is implementation-dependent whether a failure to initialize
1118 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001119 */
1120psa_status_t psa_import_key(psa_key_slot_t key,
1121 psa_key_type_t type,
1122 const uint8_t *data,
1123 size_t data_length);
1124
1125/**
Gilles Peskine154bd952018-04-19 08:38:16 +02001126 * \brief Destroy a key and restore the slot to its default state.
1127 *
1128 * This function destroys the content of the key slot from both volatile
1129 * memory and, if applicable, non-volatile storage. Implementations shall
1130 * make a best effort to ensure that any previous content of the slot is
1131 * unrecoverable.
1132 *
1133 * This function also erases any metadata such as policies. It returns the
1134 * specified slot to its default state.
1135 *
1136 * \param key The key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001137 *
Gilles Peskine28538492018-07-11 17:34:00 +02001138 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +02001139 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +02001140 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001141 * The slot holds content and cannot be erased because it is
1142 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskine28538492018-07-11 17:34:00 +02001143 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001144 * The specified slot number does not designate a valid slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001145 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001146 * There was an failure in communication with the cryptoprocessor.
1147 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +02001148 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001149 * The storage is corrupted. Implementations shall make a best effort
1150 * to erase key material even in this stage, however applications
1151 * should be aware that it may be impossible to guarantee that the
1152 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +02001153 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001154 * An unexpected condition which is not a storage corruption or
1155 * a communication failure occurred. The cryptoprocessor may have
1156 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001157 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001158 * The library has not been previously initialized by psa_crypto_init().
1159 * It is implementation-dependent whether a failure to initialize
1160 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001161 */
1162psa_status_t psa_destroy_key(psa_key_slot_t key);
1163
1164/**
1165 * \brief Get basic metadata about a key.
1166 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001167 * \param key Slot whose content is queried. This must
1168 * be an occupied key slot.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001169 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001170 * This may be a null pointer, in which case the key type
1171 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001172 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +01001173 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +01001174 * is not written.
1175 *
Gilles Peskine28538492018-07-11 17:34:00 +02001176 * \retval #PSA_SUCCESS
1177 * \retval #PSA_ERROR_EMPTY_SLOT
1178 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1179 * \retval #PSA_ERROR_HARDWARE_FAILURE
1180 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001181 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001182 * The library has not been previously initialized by psa_crypto_init().
1183 * It is implementation-dependent whether a failure to initialize
1184 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001185 */
1186psa_status_t psa_get_key_information(psa_key_slot_t key,
1187 psa_key_type_t *type,
1188 size_t *bits);
1189
1190/**
1191 * \brief Export a key in binary format.
1192 *
1193 * The output of this function can be passed to psa_import_key() to
1194 * create an equivalent object.
1195 *
1196 * If a key is created with psa_import_key() and then exported with
1197 * this function, it is not guaranteed that the resulting data is
1198 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +01001199 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001200 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001201 * For standard key types, the output format is as follows:
1202 *
1203 * - For symmetric keys (including MAC keys), the format is the
1204 * raw bytes of the key.
1205 * - For DES, the key data consists of 8 bytes. The parity bits must be
1206 * correct.
1207 * - For Triple-DES, the format is the concatenation of the
1208 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +01001209 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001210 * is the non-encrypted DER encoding of the representation defined by
1211 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
1212 * ```
1213 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001214 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001215 * modulus INTEGER, -- n
1216 * publicExponent INTEGER, -- e
1217 * privateExponent INTEGER, -- d
1218 * prime1 INTEGER, -- p
1219 * prime2 INTEGER, -- q
1220 * exponent1 INTEGER, -- d mod (p-1)
1221 * exponent2 INTEGER, -- d mod (q-1)
1222 * coefficient INTEGER, -- (inverse of q) mod p
1223 * }
1224 * ```
1225 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
1226 * is the non-encrypted DER encoding of the representation used by
Gilles Peskinec6290c02018-08-13 17:24:59 +02001227 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001228 * ```
1229 * DSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001230 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001231 * prime INTEGER, -- p
1232 * subprime INTEGER, -- q
1233 * generator INTEGER, -- g
1234 * public INTEGER, -- y
1235 * private INTEGER, -- x
1236 * }
1237 * ```
1238 * - For elliptic curve key pairs (key types for which
1239 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is the
1240 * non-encrypted DER encoding of the representation defined by RFC 5915 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001241 * `ECPrivateKey`, version 1. The `ECParameters` field must be a
1242 * `namedCurve` OID as specified in RFC 5480 &sect;2.1.1.1. The public key
1243 * must be present and must be an `ECPoint` in the same format
1244 * (uncompressed variant) an ECC public key of the
1245 * corresponding type exported with psa_export_public_key().
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001246 * ```
1247 * ECPrivateKey ::= SEQUENCE {
1248 * version INTEGER, -- must be 1
1249 * privateKey OCTET STRING,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001250 * -- `ceiling(log2(n)/8)`-byte string, big endian,
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001251 * -- where n is the order of the curve.
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001252 * parameters [0] IMPLICIT ECParameters {{ namedCurve }}, -- mandatory
1253 * publicKey [1] IMPLICIT BIT STRING -- mandatory
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001254 * }
1255 * ```
1256 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
1257 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001258 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001259 * \param key Slot whose content is to be exported. This must
1260 * be an occupied key slot.
1261 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001262 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001263 * \param[out] data_length On success, the number of bytes
1264 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001265 *
Gilles Peskine28538492018-07-11 17:34:00 +02001266 * \retval #PSA_SUCCESS
1267 * \retval #PSA_ERROR_EMPTY_SLOT
1268 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +01001269 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +02001270 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1271 * The size of the \p data buffer is too small. You can determine a
1272 * sufficient buffer size by calling
1273 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
1274 * where \c type is the key type
1275 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +02001276 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1277 * \retval #PSA_ERROR_HARDWARE_FAILURE
1278 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001279 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001280 * The library has not been previously initialized by psa_crypto_init().
1281 * It is implementation-dependent whether a failure to initialize
1282 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001283 */
1284psa_status_t psa_export_key(psa_key_slot_t key,
1285 uint8_t *data,
1286 size_t data_size,
1287 size_t *data_length);
1288
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001289/**
1290 * \brief Export a public key or the public part of a key pair in binary format.
1291 *
1292 * The output of this function can be passed to psa_import_key() to
1293 * create an object that is equivalent to the public key.
1294 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001295 * The format is the DER representation defined by RFC 5280 as
1296 * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
1297 * specified below.
1298 * ```
1299 * SubjectPublicKeyInfo ::= SEQUENCE {
1300 * algorithm AlgorithmIdentifier,
1301 * subjectPublicKey BIT STRING }
1302 * AlgorithmIdentifier ::= SEQUENCE {
1303 * algorithm OBJECT IDENTIFIER,
1304 * parameters ANY DEFINED BY algorithm OPTIONAL }
1305 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001306 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001307 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY),
1308 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.1 as
1309 * `RSAPublicKey`,
1310 * with the OID `rsaEncryption`,
1311 * and with the parameters `NULL`.
1312 * ```
1313 * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
1314 * rsadsi(113549) pkcs(1) 1 }
1315 * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 }
1316 *
1317 * RSAPublicKey ::= SEQUENCE {
1318 * modulus INTEGER, -- n
1319 * publicExponent INTEGER } -- e
1320 * ```
1321 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
1322 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
1323 * `DSAPublicKey`,
1324 * with the OID `id-dsa`,
1325 * and with the parameters `DSS-Parms`.
1326 * ```
1327 * id-dsa OBJECT IDENTIFIER ::= {
1328 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
1329 *
1330 * Dss-Parms ::= SEQUENCE {
1331 * p INTEGER,
1332 * q INTEGER,
1333 * g INTEGER }
1334 * DSAPublicKey ::= INTEGER -- public key, Y
1335 * ```
1336 * - For elliptic curve public keys (key types for which
1337 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true),
1338 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.5 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001339 * `ECPoint`, which contains the uncompressed
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001340 * representation defined by SEC1 &sect;2.3.3.
1341 * The OID is `id-ecPublicKey`,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001342 * and the parameters must be given as a `namedCurve` OID as specified in
Gilles Peskinec6290c02018-08-13 17:24:59 +02001343 * RFC 5480 &sect;2.1.1.1 or other applicable standards.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001344 * ```
1345 * ansi-X9-62 OBJECT IDENTIFIER ::=
1346 * { iso(1) member-body(2) us(840) 10045 }
1347 * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 }
1348 * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 }
1349 *
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001350 * ECPoint ::= ...
1351 * -- first 8 bits: 0x04;
1352 * -- then x_P as an n-bit string, big endian;
1353 * -- then y_P as a n-bit string, big endian,
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001354 * -- where n is the order of the curve.
1355 *
1356 * EcpkParameters ::= CHOICE { -- other choices are not allowed
1357 * namedCurve OBJECT IDENTIFIER }
1358 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001359 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001360 * \param key Slot whose content is to be exported. This must
1361 * be an occupied key slot.
1362 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001363 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001364 * \param[out] data_length On success, the number of bytes
1365 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001366 *
Gilles Peskine28538492018-07-11 17:34:00 +02001367 * \retval #PSA_SUCCESS
1368 * \retval #PSA_ERROR_EMPTY_SLOT
1369 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +02001370 * The key is neither a public key nor a key pair.
1371 * \retval #PSA_ERROR_NOT_SUPPORTED
1372 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1373 * The size of the \p data buffer is too small. You can determine a
1374 * sufficient buffer size by calling
1375 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
1376 * where \c type is the key type
1377 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +02001378 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1379 * \retval #PSA_ERROR_HARDWARE_FAILURE
1380 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001381 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001382 * The library has not been previously initialized by psa_crypto_init().
1383 * It is implementation-dependent whether a failure to initialize
1384 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001385 */
1386psa_status_t psa_export_public_key(psa_key_slot_t key,
1387 uint8_t *data,
1388 size_t data_size,
1389 size_t *data_length);
1390
1391/**@}*/
1392
1393/** \defgroup policy Key policies
1394 * @{
1395 */
1396
1397/** \brief Encoding of permitted usage on a key. */
1398typedef uint32_t psa_key_usage_t;
1399
Gilles Peskine7e198532018-03-08 07:50:30 +01001400/** Whether the key may be exported.
1401 *
1402 * A public key or the public part of a key pair may always be exported
1403 * regardless of the value of this permission flag.
1404 *
1405 * If a key does not have export permission, implementations shall not
1406 * allow the key to be exported in plain form from the cryptoprocessor,
1407 * whether through psa_export_key() or through a proprietary interface.
1408 * The key may however be exportable in a wrapped form, i.e. in a form
1409 * where it is encrypted by another key.
1410 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001411#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1412
Gilles Peskine7e198532018-03-08 07:50:30 +01001413/** Whether the key may be used to encrypt a message.
1414 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001415 * This flag allows the key to be used for a symmetric encryption operation,
1416 * for an AEAD encryption-and-authentication operation,
1417 * or for an asymmetric encryption operation,
1418 * if otherwise permitted by the key's type and policy.
1419 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001420 * For a key pair, this concerns the public key.
1421 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001422#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +01001423
1424/** Whether the key may be used to decrypt a message.
1425 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001426 * This flag allows the key to be used for a symmetric decryption operation,
1427 * for an AEAD decryption-and-verification operation,
1428 * or for an asymmetric decryption operation,
1429 * if otherwise permitted by the key's type and policy.
1430 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001431 * For a key pair, this concerns the private key.
1432 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001433#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +01001434
1435/** Whether the key may be used to sign a message.
1436 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001437 * This flag allows the key to be used for a MAC calculation operation
1438 * or for an asymmetric signature operation,
1439 * if otherwise permitted by the key's type and policy.
1440 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001441 * For a key pair, this concerns the private key.
1442 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001443#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +01001444
1445/** Whether the key may be used to verify a message signature.
1446 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001447 * This flag allows the key to be used for a MAC verification operation
1448 * or for an asymmetric signature verification operation,
1449 * if otherwise permitted by by the key's type and policy.
1450 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001451 * For a key pair, this concerns the public key.
1452 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001453#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1454
Gilles Peskineea0fb492018-07-12 17:17:20 +02001455/** Whether the key may be used to derive other keys.
1456 */
1457#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1458
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001459/** The type of the key policy data structure.
1460 *
1461 * This is an implementation-defined \c struct. Applications should not
1462 * make any assumptions about the content of this structure except
1463 * as directed by the documentation of a specific implementation. */
1464typedef struct psa_key_policy_s psa_key_policy_t;
1465
1466/** \brief Initialize a key policy structure to a default that forbids all
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001467 * usage of the key.
1468 *
1469 * \param[out] policy The policy object to initialize.
1470 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001471void psa_key_policy_init(psa_key_policy_t *policy);
1472
Gilles Peskine7e198532018-03-08 07:50:30 +01001473/** \brief Set the standard fields of a policy structure.
1474 *
1475 * Note that this function does not make any consistency check of the
1476 * parameters. The values are only checked when applying the policy to
1477 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001478 *
1479 * \param[out] policy The policy object to modify.
1480 * \param usage The permitted uses for the key.
1481 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +01001482 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001483void psa_key_policy_set_usage(psa_key_policy_t *policy,
1484 psa_key_usage_t usage,
1485 psa_algorithm_t alg);
1486
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001487/** \brief Retrieve the usage field of a policy structure.
1488 *
1489 * \param[in] policy The policy object to query.
1490 *
1491 * \return The permitted uses for a key with this policy.
1492 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001493psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001494
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001495/** \brief Retrieve the algorithm field of a policy structure.
1496 *
1497 * \param[in] policy The policy object to query.
1498 *
1499 * \return The permitted algorithm for a key with this policy.
1500 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001501psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001502
1503/** \brief Set the usage policy on a key slot.
1504 *
1505 * This function must be called on an empty key slot, before importing,
1506 * generating or creating a key in the slot. Changing the policy of an
1507 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +01001508 *
1509 * Implementations may set restrictions on supported key policies
1510 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001511 *
1512 * \param key The key slot whose policy is to be changed.
1513 * \param[in] policy The policy object to query.
1514 *
1515 * \retval #PSA_SUCCESS
1516 * \retval #PSA_ERROR_OCCUPIED_SLOT
1517 * \retval #PSA_ERROR_NOT_SUPPORTED
1518 * \retval #PSA_ERROR_INVALID_ARGUMENT
1519 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1520 * \retval #PSA_ERROR_HARDWARE_FAILURE
1521 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001522 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001523 * The library has not been previously initialized by psa_crypto_init().
1524 * It is implementation-dependent whether a failure to initialize
1525 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001526 */
1527psa_status_t psa_set_key_policy(psa_key_slot_t key,
1528 const psa_key_policy_t *policy);
1529
Gilles Peskine7e198532018-03-08 07:50:30 +01001530/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001531 *
1532 * \param key The key slot whose policy is being queried.
1533 * \param[out] policy On success, the key's policy.
1534 *
1535 * \retval #PSA_SUCCESS
1536 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1537 * \retval #PSA_ERROR_HARDWARE_FAILURE
1538 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001539 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001540 * The library has not been previously initialized by psa_crypto_init().
1541 * It is implementation-dependent whether a failure to initialize
1542 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +01001543 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001544psa_status_t psa_get_key_policy(psa_key_slot_t key,
1545 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +01001546
1547/**@}*/
1548
Gilles Peskine609b6a52018-03-03 21:31:50 +01001549/** \defgroup persistence Key lifetime
1550 * @{
1551 */
1552
1553/** Encoding of key lifetimes.
1554 */
1555typedef uint32_t psa_key_lifetime_t;
1556
1557/** A volatile key slot retains its content as long as the application is
1558 * running. It is guaranteed to be erased on a power reset.
1559 */
1560#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1561
1562/** A persistent key slot retains its content as long as it is not explicitly
1563 * destroyed.
1564 */
1565#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1566
1567/** A write-once key slot may not be modified once a key has been set.
1568 * It will retain its content as long as the device remains operational.
1569 */
1570#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
1571
Gilles Peskined393e182018-03-08 07:49:16 +01001572/** \brief Retrieve the lifetime of a key slot.
1573 *
1574 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001575 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001576 * \param key Slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001577 * \param[out] lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001578 *
Gilles Peskine28538492018-07-11 17:34:00 +02001579 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001580 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001581 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -07001582 * The key slot is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001583 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1584 * \retval #PSA_ERROR_HARDWARE_FAILURE
1585 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001586 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001587 * The library has not been previously initialized by psa_crypto_init().
1588 * It is implementation-dependent whether a failure to initialize
1589 * results in this error code.
Gilles Peskined393e182018-03-08 07:49:16 +01001590 */
Gilles Peskine609b6a52018-03-03 21:31:50 +01001591psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1592 psa_key_lifetime_t *lifetime);
1593
Gilles Peskined393e182018-03-08 07:49:16 +01001594/** \brief Change the lifetime of a key slot.
1595 *
1596 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +01001597 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +01001598 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001599 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001600 * \param key Slot whose lifetime is to be changed.
1601 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001602 *
Gilles Peskine28538492018-07-11 17:34:00 +02001603 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001604 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001605 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603804cd712018-03-20 22:44:08 +02001606 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -07001607 * or the lifetime value is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001608 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001609 * The implementation does not support the specified lifetime value,
1610 * at least for the specified key slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001611 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001612 * The slot contains a key, and the implementation does not support
1613 * changing the lifetime of an occupied slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001614 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1615 * \retval #PSA_ERROR_HARDWARE_FAILURE
1616 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001617 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001618 * The library has not been previously initialized by psa_crypto_init().
1619 * It is implementation-dependent whether a failure to initialize
1620 * results in this error code.
Gilles Peskined393e182018-03-08 07:49:16 +01001621 */
1622psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -07001623 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +01001624
Gilles Peskine609b6a52018-03-03 21:31:50 +01001625/**@}*/
1626
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001627/** \defgroup hash Message digests
1628 * @{
1629 */
1630
Gilles Peskine308b91d2018-02-08 09:47:44 +01001631/** The type of the state data structure for multipart hash operations.
1632 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001633 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001634 * make any assumptions about the content of this structure except
1635 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001636typedef struct psa_hash_operation_s psa_hash_operation_t;
1637
Gilles Peskine308b91d2018-02-08 09:47:44 +01001638/** The size of the output of psa_hash_finish(), in bytes.
1639 *
1640 * This is also the hash size that psa_hash_verify() expects.
1641 *
1642 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001643 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
Gilles Peskinebe42f312018-07-13 14:38:15 +02001644 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
Gilles Peskine35855962018-04-19 08:39:16 +02001645 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001646 *
1647 * \return The hash size for the specified hash algorithm.
1648 * If the hash algorithm is not recognized, return 0.
1649 * An implementation may return either 0 or the correct size
1650 * for a hash algorithm that it recognizes, but does not support.
1651 */
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001652#define PSA_HASH_SIZE(alg) \
1653 ( \
1654 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD2 ? 16 : \
1655 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD4 ? 16 : \
1656 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD5 ? 16 : \
1657 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
1658 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
1659 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
1660 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
1661 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
1662 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
1663 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
1664 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
1665 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
1666 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
1667 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
1668 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001669 0)
1670
Gilles Peskine308b91d2018-02-08 09:47:44 +01001671/** Start a multipart hash operation.
1672 *
1673 * The sequence of operations to calculate a hash (message digest)
1674 * is as follows:
1675 * -# Allocate an operation object which will be passed to all the functions
1676 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001677 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001678 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001679 * of the message each time. The hash that is calculated is the hash
1680 * of the concatenation of these messages in order.
1681 * -# To calculate the hash, call psa_hash_finish().
1682 * To compare the hash with an expected value, call psa_hash_verify().
1683 *
1684 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001685 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001686 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001687 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001688 * eventually terminate the operation. The following events terminate an
1689 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001690 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001691 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001692 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001693 * \param[out] operation The operation object to use.
1694 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1695 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001696 *
Gilles Peskine28538492018-07-11 17:34:00 +02001697 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001698 * Success.
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 hash 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 Peskine308b91d2018-02-08 09:47:44 +01001705 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001706psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001707 psa_algorithm_t alg);
1708
Gilles Peskine308b91d2018-02-08 09:47:44 +01001709/** Add a message fragment to a multipart hash operation.
1710 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001711 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001712 *
1713 * If this function returns an error status, the operation becomes inactive.
1714 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001715 * \param[in,out] operation Active hash operation.
1716 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001717 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001718 *
Gilles Peskine28538492018-07-11 17:34:00 +02001719 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001720 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001721 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001722 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001723 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1724 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1725 * \retval #PSA_ERROR_HARDWARE_FAILURE
1726 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001727 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001728psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1729 const uint8_t *input,
1730 size_t input_length);
1731
Gilles Peskine308b91d2018-02-08 09:47:44 +01001732/** Finish the calculation of the hash of a message.
1733 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001734 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001735 * This function calculates the hash of the message formed by concatenating
1736 * the inputs passed to preceding calls to psa_hash_update().
1737 *
1738 * When this function returns, the operation becomes inactive.
1739 *
1740 * \warning Applications should not call this function if they expect
1741 * a specific value for the hash. Call psa_hash_verify() instead.
1742 * Beware that comparing integrity or authenticity data such as
1743 * hash values with a function such as \c memcmp is risky
1744 * because the time taken by the comparison may leak information
1745 * about the hashed data which could allow an attacker to guess
1746 * a valid hash and thereby bypass security controls.
1747 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001748 * \param[in,out] operation Active hash operation.
1749 * \param[out] hash Buffer where the hash is to be written.
1750 * \param hash_size Size of the \p hash buffer in bytes.
1751 * \param[out] hash_length On success, the number of bytes
1752 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001753 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001754 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001755 *
Gilles Peskine28538492018-07-11 17:34:00 +02001756 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001757 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001758 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001759 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001760 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001761 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001762 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001763 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001764 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1765 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1766 * \retval #PSA_ERROR_HARDWARE_FAILURE
1767 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001768 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001769psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1770 uint8_t *hash,
1771 size_t hash_size,
1772 size_t *hash_length);
1773
Gilles Peskine308b91d2018-02-08 09:47:44 +01001774/** Finish the calculation of the hash of a message and compare it with
1775 * an expected value.
1776 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001777 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001778 * This function calculates the hash of the message formed by concatenating
1779 * the inputs passed to preceding calls to psa_hash_update(). It then
1780 * compares the calculated hash with the expected hash passed as a
1781 * parameter to this function.
1782 *
1783 * When this function returns, the operation becomes inactive.
1784 *
Gilles Peskine19067982018-03-20 17:54:53 +01001785 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001786 * comparison between the actual hash and the expected hash is performed
1787 * in constant time.
1788 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001789 * \param[in,out] operation Active hash operation.
1790 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001791 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001792 *
Gilles Peskine28538492018-07-11 17:34:00 +02001793 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001794 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001795 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001796 * The hash of the message was calculated successfully, but it
1797 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001798 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001799 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001800 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1801 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1802 * \retval #PSA_ERROR_HARDWARE_FAILURE
1803 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001804 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001805psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1806 const uint8_t *hash,
1807 size_t hash_length);
1808
Gilles Peskine308b91d2018-02-08 09:47:44 +01001809/** Abort a hash operation.
1810 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001811 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001812 * \p operation structure itself. Once aborted, the operation object
1813 * can be reused for another operation by calling
1814 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001815 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001816 * You may call this function any time after the operation object has
1817 * been initialized by any of the following methods:
1818 * - A call to psa_hash_setup(), whether it succeeds or not.
1819 * - Initializing the \c struct to all-bits-zero.
1820 * - Initializing the \c struct to logical zeros, e.g.
1821 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001822 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001823 * In particular, calling psa_hash_abort() after the operation has been
1824 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1825 * psa_hash_verify() is safe and has no effect.
1826 *
1827 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001828 *
Gilles Peskine28538492018-07-11 17:34:00 +02001829 * \retval #PSA_SUCCESS
1830 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001831 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001832 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1833 * \retval #PSA_ERROR_HARDWARE_FAILURE
1834 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001835 */
1836psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001837
1838/**@}*/
1839
Gilles Peskine8c9def32018-02-08 10:02:12 +01001840/** \defgroup MAC Message authentication codes
1841 * @{
1842 */
1843
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001844/** The type of the state data structure for multipart MAC operations.
1845 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001846 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001847 * make any assumptions about the content of this structure except
1848 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001849typedef struct psa_mac_operation_s psa_mac_operation_t;
1850
Gilles Peskine89167cb2018-07-08 20:12:23 +02001851/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001852 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001853 * This function sets up the calculation of the MAC
1854 * (message authentication code) of a byte string.
1855 * To verify the MAC of a message against an
1856 * expected value, use psa_mac_verify_setup() instead.
1857 *
1858 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001859 * -# Allocate an operation object which will be passed to all the functions
1860 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001861 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001862 * The key remains associated with the operation even if the content
1863 * of the key slot changes.
1864 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1865 * of the message each time. The MAC that is calculated is the MAC
1866 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001867 * -# At the end of the message, call psa_mac_sign_finish() to finish
1868 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001869 *
1870 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +02001871 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001872 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001873 * After a successful call to psa_mac_sign_setup(), the application must
1874 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001875 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001876 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001877 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001878 * \param[out] operation The operation object to use.
1879 * \param key Slot containing the key to use for the operation.
1880 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1881 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001882 *
Gilles Peskine28538492018-07-11 17:34:00 +02001883 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001884 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001885 * \retval #PSA_ERROR_EMPTY_SLOT
1886 * \retval #PSA_ERROR_NOT_PERMITTED
1887 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001888 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001889 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001890 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001891 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1892 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1893 * \retval #PSA_ERROR_HARDWARE_FAILURE
1894 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001895 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001896 * The library has not been previously initialized by psa_crypto_init().
1897 * It is implementation-dependent whether a failure to initialize
1898 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001899 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001900psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1901 psa_key_slot_t key,
1902 psa_algorithm_t alg);
1903
1904/** Start a multipart MAC verification operation.
1905 *
1906 * This function sets up the verification of the MAC
1907 * (message authentication code) of a byte string against an expected value.
1908 *
1909 * The sequence of operations to verify a MAC is as follows:
1910 * -# Allocate an operation object which will be passed to all the functions
1911 * listed here.
1912 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1913 * The key remains associated with the operation even if the content
1914 * of the key slot changes.
1915 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1916 * of the message each time. The MAC that is calculated is the MAC
1917 * of the concatenation of these messages in order.
1918 * -# At the end of the message, call psa_mac_verify_finish() to finish
1919 * calculating the actual MAC of the message and verify it against
1920 * the expected value.
1921 *
1922 * The application may call psa_mac_abort() at any time after the operation
1923 * has been initialized with psa_mac_verify_setup().
1924 *
1925 * After a successful call to psa_mac_verify_setup(), the application must
1926 * eventually terminate the operation through one of the following methods:
1927 * - A failed call to psa_mac_update().
1928 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1929 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001930 * \param[out] operation The operation object to use.
1931 * \param key Slot containing the key to use for the operation.
1932 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1933 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001934 *
Gilles Peskine28538492018-07-11 17:34:00 +02001935 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001936 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001937 * \retval #PSA_ERROR_EMPTY_SLOT
1938 * \retval #PSA_ERROR_NOT_PERMITTED
1939 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001940 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001941 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001942 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001943 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1944 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1945 * \retval #PSA_ERROR_HARDWARE_FAILURE
1946 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001947 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001948 * The library has not been previously initialized by psa_crypto_init().
1949 * It is implementation-dependent whether a failure to initialize
1950 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001951 */
1952psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
1953 psa_key_slot_t key,
1954 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001955
Gilles Peskinedcd14942018-07-12 00:30:52 +02001956/** Add a message fragment to a multipart MAC operation.
1957 *
1958 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1959 * before calling this function.
1960 *
1961 * If this function returns an error status, the operation becomes inactive.
1962 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001963 * \param[in,out] operation Active MAC operation.
1964 * \param[in] input Buffer containing the message fragment to add to
1965 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001966 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001967 *
1968 * \retval #PSA_SUCCESS
1969 * Success.
1970 * \retval #PSA_ERROR_BAD_STATE
1971 * The operation state is not valid (not started, or already completed).
1972 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1973 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1974 * \retval #PSA_ERROR_HARDWARE_FAILURE
1975 * \retval #PSA_ERROR_TAMPERING_DETECTED
1976 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001977psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1978 const uint8_t *input,
1979 size_t input_length);
1980
Gilles Peskinedcd14942018-07-12 00:30:52 +02001981/** Finish the calculation of the MAC of a message.
1982 *
1983 * The application must call psa_mac_sign_setup() before calling this function.
1984 * This function calculates the MAC of the message formed by concatenating
1985 * the inputs passed to preceding calls to psa_mac_update().
1986 *
1987 * When this function returns, the operation becomes inactive.
1988 *
1989 * \warning Applications should not call this function if they expect
1990 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1991 * Beware that comparing integrity or authenticity data such as
1992 * MAC values with a function such as \c memcmp is risky
1993 * because the time taken by the comparison may leak information
1994 * about the MAC value which could allow an attacker to guess
1995 * a valid MAC and thereby bypass security controls.
1996 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001997 * \param[in,out] operation Active MAC operation.
1998 * \param[out] mac Buffer where the MAC value is to be written.
1999 * \param mac_size Size of the \p mac buffer in bytes.
2000 * \param[out] mac_length On success, the number of bytes
2001 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002002 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02002003 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002004 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02002005 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002006 *
2007 * \retval #PSA_SUCCESS
2008 * Success.
2009 * \retval #PSA_ERROR_BAD_STATE
2010 * The operation state is not valid (not started, or already completed).
2011 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002012 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02002013 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
2014 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2015 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2016 * \retval #PSA_ERROR_HARDWARE_FAILURE
2017 * \retval #PSA_ERROR_TAMPERING_DETECTED
2018 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02002019psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2020 uint8_t *mac,
2021 size_t mac_size,
2022 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002023
Gilles Peskinedcd14942018-07-12 00:30:52 +02002024/** Finish the calculation of the MAC of a message and compare it with
2025 * an expected value.
2026 *
2027 * The application must call psa_mac_verify_setup() before calling this function.
2028 * This function calculates the MAC of the message formed by concatenating
2029 * the inputs passed to preceding calls to psa_mac_update(). It then
2030 * compares the calculated MAC with the expected MAC passed as a
2031 * parameter to this function.
2032 *
2033 * When this function returns, the operation becomes inactive.
2034 *
2035 * \note Implementations shall make the best effort to ensure that the
2036 * comparison between the actual MAC and the expected MAC is performed
2037 * in constant time.
2038 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002039 * \param[in,out] operation Active MAC operation.
2040 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002041 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002042 *
2043 * \retval #PSA_SUCCESS
2044 * The expected MAC is identical to the actual MAC of the message.
2045 * \retval #PSA_ERROR_INVALID_SIGNATURE
2046 * The MAC of the message was calculated successfully, but it
2047 * differs from the expected MAC.
2048 * \retval #PSA_ERROR_BAD_STATE
2049 * The operation state is not valid (not started, or already completed).
2050 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2051 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2052 * \retval #PSA_ERROR_HARDWARE_FAILURE
2053 * \retval #PSA_ERROR_TAMPERING_DETECTED
2054 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02002055psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2056 const uint8_t *mac,
2057 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002058
Gilles Peskinedcd14942018-07-12 00:30:52 +02002059/** Abort a MAC operation.
2060 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002061 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002062 * \p operation structure itself. Once aborted, the operation object
2063 * can be reused for another operation by calling
2064 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002065 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002066 * You may call this function any time after the operation object has
2067 * been initialized by any of the following methods:
2068 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
2069 * it succeeds or not.
2070 * - Initializing the \c struct to all-bits-zero.
2071 * - Initializing the \c struct to logical zeros, e.g.
2072 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002073 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002074 * In particular, calling psa_mac_abort() after the operation has been
2075 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
2076 * psa_mac_verify_finish() is safe and has no effect.
2077 *
2078 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002079 *
2080 * \retval #PSA_SUCCESS
2081 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002082 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002083 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2084 * \retval #PSA_ERROR_HARDWARE_FAILURE
2085 * \retval #PSA_ERROR_TAMPERING_DETECTED
2086 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002087psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
2088
2089/**@}*/
2090
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002091/** \defgroup cipher Symmetric ciphers
2092 * @{
2093 */
2094
2095/** The type of the state data structure for multipart cipher operations.
2096 *
2097 * This is an implementation-defined \c struct. Applications should not
2098 * make any assumptions about the content of this structure except
2099 * as directed by the documentation of a specific implementation. */
2100typedef struct psa_cipher_operation_s psa_cipher_operation_t;
2101
2102/** Set the key for a multipart symmetric encryption operation.
2103 *
2104 * The sequence of operations to encrypt a message with a symmetric cipher
2105 * is as follows:
2106 * -# Allocate an operation object which will be passed to all the functions
2107 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02002108 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002109 * The key remains associated with the operation even if the content
2110 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03002111 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002112 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03002113 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002114 * requires a specific IV value.
2115 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
2116 * of the message each time.
2117 * -# Call psa_cipher_finish().
2118 *
2119 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02002120 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002121 *
Gilles Peskinefe119512018-07-08 21:39:34 +02002122 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002123 * eventually terminate the operation. The following events terminate an
2124 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03002125 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002126 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002127 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002128 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002129 * \param[out] operation The operation object to use.
2130 * \param key Slot containing the key to use for the operation.
2131 * \param alg The cipher algorithm to compute
2132 * (\c PSA_ALG_XXX value such that
2133 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002134 *
Gilles Peskine28538492018-07-11 17:34:00 +02002135 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002136 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002137 * \retval #PSA_ERROR_EMPTY_SLOT
2138 * \retval #PSA_ERROR_NOT_PERMITTED
2139 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002140 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002141 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002142 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002143 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2144 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2145 * \retval #PSA_ERROR_HARDWARE_FAILURE
2146 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002147 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002148 * The library has not been previously initialized by psa_crypto_init().
2149 * It is implementation-dependent whether a failure to initialize
2150 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002151 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002152psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
2153 psa_key_slot_t key,
2154 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002155
2156/** Set the key for a multipart symmetric decryption operation.
2157 *
2158 * The sequence of operations to decrypt a message with a symmetric cipher
2159 * is as follows:
2160 * -# Allocate an operation object which will be passed to all the functions
2161 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02002162 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002163 * The key remains associated with the operation even if the content
2164 * of the key slot changes.
2165 * -# Call psa_cipher_update() with the IV (initialization vector) for the
2166 * decryption. If the IV is prepended to the ciphertext, you can call
2167 * psa_cipher_update() on a buffer containing the IV followed by the
2168 * beginning of the message.
2169 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
2170 * of the message each time.
2171 * -# Call psa_cipher_finish().
2172 *
2173 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02002174 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002175 *
Gilles Peskinefe119512018-07-08 21:39:34 +02002176 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002177 * eventually terminate the operation. The following events terminate an
2178 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002179 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002180 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002181 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002182 * \param[out] operation The operation object to use.
2183 * \param key Slot containing the key to use for the operation.
2184 * \param alg The cipher algorithm to compute
2185 * (\c PSA_ALG_XXX value such that
2186 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002187 *
Gilles Peskine28538492018-07-11 17:34:00 +02002188 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002189 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002190 * \retval #PSA_ERROR_EMPTY_SLOT
2191 * \retval #PSA_ERROR_NOT_PERMITTED
2192 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002193 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002194 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002195 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002196 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2197 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2198 * \retval #PSA_ERROR_HARDWARE_FAILURE
2199 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002200 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002201 * The library has not been previously initialized by psa_crypto_init().
2202 * It is implementation-dependent whether a failure to initialize
2203 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002204 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002205psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
2206 psa_key_slot_t key,
2207 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002208
Gilles Peskinedcd14942018-07-12 00:30:52 +02002209/** Generate an IV for a symmetric encryption operation.
2210 *
2211 * This function generates a random IV (initialization vector), nonce
2212 * or initial counter value for the encryption operation as appropriate
2213 * for the chosen algorithm, key type and key size.
2214 *
2215 * The application must call psa_cipher_encrypt_setup() before
2216 * calling this function.
2217 *
2218 * If this function returns an error status, the operation becomes inactive.
2219 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002220 * \param[in,out] operation Active cipher operation.
2221 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002222 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002223 * \param[out] iv_length On success, the number of bytes of the
2224 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002225 *
2226 * \retval #PSA_SUCCESS
2227 * Success.
2228 * \retval #PSA_ERROR_BAD_STATE
2229 * The operation state is not valid (not started, or IV already set).
2230 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002231 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002232 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2233 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2234 * \retval #PSA_ERROR_HARDWARE_FAILURE
2235 * \retval #PSA_ERROR_TAMPERING_DETECTED
2236 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002237psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
2238 unsigned char *iv,
2239 size_t iv_size,
2240 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002241
Gilles Peskinedcd14942018-07-12 00:30:52 +02002242/** Set the IV for a symmetric encryption or decryption operation.
2243 *
2244 * This function sets the random IV (initialization vector), nonce
2245 * or initial counter value for the encryption or decryption operation.
2246 *
2247 * The application must call psa_cipher_encrypt_setup() before
2248 * calling this function.
2249 *
2250 * If this function returns an error status, the operation becomes inactive.
2251 *
2252 * \note When encrypting, applications should use psa_cipher_generate_iv()
2253 * instead of this function, unless implementing a protocol that requires
2254 * a non-random IV.
2255 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002256 * \param[in,out] operation Active cipher operation.
2257 * \param[in] iv Buffer containing the IV to use.
2258 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002259 *
2260 * \retval #PSA_SUCCESS
2261 * Success.
2262 * \retval #PSA_ERROR_BAD_STATE
2263 * The operation state is not valid (not started, or IV already set).
2264 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002265 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02002266 * or the chosen algorithm does not use an IV.
2267 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2268 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2269 * \retval #PSA_ERROR_HARDWARE_FAILURE
2270 * \retval #PSA_ERROR_TAMPERING_DETECTED
2271 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002272psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
2273 const unsigned char *iv,
2274 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002275
Gilles Peskinedcd14942018-07-12 00:30:52 +02002276/** Encrypt or decrypt a message fragment in an active cipher operation.
2277 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02002278 * Before calling this function, you must:
2279 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
2280 * The choice of setup function determines whether this function
2281 * encrypts or decrypts its input.
2282 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
2283 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002284 *
2285 * If this function returns an error status, the operation becomes inactive.
2286 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002287 * \param[in,out] operation Active cipher operation.
2288 * \param[in] input Buffer containing the message fragment to
2289 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002290 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002291 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002292 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002293 * \param[out] output_length On success, the number of bytes
2294 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002295 *
2296 * \retval #PSA_SUCCESS
2297 * Success.
2298 * \retval #PSA_ERROR_BAD_STATE
2299 * The operation state is not valid (not started, IV required but
2300 * not set, or already completed).
2301 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2302 * The size of the \p output buffer is too small.
2303 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2304 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2305 * \retval #PSA_ERROR_HARDWARE_FAILURE
2306 * \retval #PSA_ERROR_TAMPERING_DETECTED
2307 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002308psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2309 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002310 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002311 unsigned char *output,
2312 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002313 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002314
Gilles Peskinedcd14942018-07-12 00:30:52 +02002315/** Finish encrypting or decrypting a message in a cipher operation.
2316 *
2317 * The application must call psa_cipher_encrypt_setup() or
2318 * psa_cipher_decrypt_setup() before calling this function. The choice
2319 * of setup function determines whether this function encrypts or
2320 * decrypts its input.
2321 *
2322 * This function finishes the encryption or decryption of the message
2323 * formed by concatenating the inputs passed to preceding calls to
2324 * psa_cipher_update().
2325 *
2326 * When this function returns, the operation becomes inactive.
2327 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002328 * \param[in,out] operation Active cipher operation.
2329 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002330 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002331 * \param[out] output_length On success, the number of bytes
2332 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002333 *
2334 * \retval #PSA_SUCCESS
2335 * Success.
2336 * \retval #PSA_ERROR_BAD_STATE
2337 * The operation state is not valid (not started, IV required but
2338 * not set, or already completed).
2339 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2340 * The size of the \p output buffer is too small.
2341 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2342 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2343 * \retval #PSA_ERROR_HARDWARE_FAILURE
2344 * \retval #PSA_ERROR_TAMPERING_DETECTED
2345 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002346psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002347 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002348 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002349 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002350
Gilles Peskinedcd14942018-07-12 00:30:52 +02002351/** Abort a cipher operation.
2352 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002353 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002354 * \p operation structure itself. Once aborted, the operation object
2355 * can be reused for another operation by calling
2356 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002357 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002358 * You may call this function any time after the operation object has
2359 * been initialized by any of the following methods:
2360 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2361 * whether it succeeds or not.
2362 * - Initializing the \c struct to all-bits-zero.
2363 * - Initializing the \c struct to logical zeros, e.g.
2364 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002365 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002366 * In particular, calling psa_cipher_abort() after the operation has been
2367 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2368 * is safe and has no effect.
2369 *
2370 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002371 *
2372 * \retval #PSA_SUCCESS
2373 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002374 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002375 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2376 * \retval #PSA_ERROR_HARDWARE_FAILURE
2377 * \retval #PSA_ERROR_TAMPERING_DETECTED
2378 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002379psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2380
2381/**@}*/
2382
Gilles Peskine3b555712018-03-03 21:27:57 +01002383/** \defgroup aead Authenticated encryption with associated data (AEAD)
2384 * @{
2385 */
2386
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002387/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01002388 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002389 * \param alg An AEAD algorithm
2390 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002391 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002392 *
2393 * \return The tag size for the specified algorithm.
2394 * If the AEAD algorithm does not have an identified
2395 * tag that can be distinguished from the rest of
2396 * the ciphertext, return 0.
2397 * If the AEAD algorithm is not recognized, return 0.
2398 * An implementation may return either 0 or a
2399 * correct size for an AEAD algorithm that it
2400 * recognizes, but does not support.
2401 */
2402#define PSA_AEAD_TAG_SIZE(alg) \
2403 ((alg) == PSA_ALG_GCM ? 16 : \
2404 (alg) == PSA_ALG_CCM ? 16 : \
2405 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01002406
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002407/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002408 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002409 * \param key Slot containing the key to use.
2410 * \param alg The AEAD algorithm to compute
2411 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002412 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002413 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002414 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002415 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002416 * but not encrypted.
2417 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002418 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002419 * encrypted.
2420 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002421 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002422 * encrypted data. The additional data is not
2423 * part of this output. For algorithms where the
2424 * encrypted data and the authentication tag
2425 * are defined as separate outputs, the
2426 * authentication tag is appended to the
2427 * encrypted data.
2428 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2429 * This must be at least
2430 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2431 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002432 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002433 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002434 *
Gilles Peskine28538492018-07-11 17:34:00 +02002435 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002436 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002437 * \retval #PSA_ERROR_EMPTY_SLOT
2438 * \retval #PSA_ERROR_NOT_PERMITTED
2439 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002440 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002441 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002442 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002443 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2444 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2445 * \retval #PSA_ERROR_HARDWARE_FAILURE
2446 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002447 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002448 * The library has not been previously initialized by psa_crypto_init().
2449 * It is implementation-dependent whether a failure to initialize
2450 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002451 */
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002452psa_status_t psa_aead_encrypt(psa_key_slot_t key,
2453 psa_algorithm_t alg,
2454 const uint8_t *nonce,
2455 size_t nonce_length,
2456 const uint8_t *additional_data,
2457 size_t additional_data_length,
2458 const uint8_t *plaintext,
2459 size_t plaintext_length,
2460 uint8_t *ciphertext,
2461 size_t ciphertext_size,
2462 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002463
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002464/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002465 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002466 * \param key Slot containing the key to use.
2467 * \param alg The AEAD algorithm to compute
2468 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002469 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002470 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002471 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002472 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002473 * but not encrypted.
2474 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002475 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002476 * encrypted. For algorithms where the
2477 * encrypted data and the authentication tag
2478 * are defined as separate inputs, the buffer
2479 * must contain the encrypted data followed
2480 * by the authentication tag.
2481 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002482 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002483 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2484 * This must be at least
2485 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2486 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002487 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03002488 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002489 *
Gilles Peskine28538492018-07-11 17:34:00 +02002490 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002491 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002492 * \retval #PSA_ERROR_EMPTY_SLOT
2493 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002494 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002495 * \retval #PSA_ERROR_NOT_PERMITTED
2496 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002497 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002498 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002499 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002500 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2501 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2502 * \retval #PSA_ERROR_HARDWARE_FAILURE
2503 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002504 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002505 * The library has not been previously initialized by psa_crypto_init().
2506 * It is implementation-dependent whether a failure to initialize
2507 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002508 */
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002509psa_status_t psa_aead_decrypt(psa_key_slot_t key,
2510 psa_algorithm_t alg,
2511 const uint8_t *nonce,
2512 size_t nonce_length,
2513 const uint8_t *additional_data,
2514 size_t additional_data_length,
2515 const uint8_t *ciphertext,
2516 size_t ciphertext_length,
2517 uint8_t *plaintext,
2518 size_t plaintext_size,
2519 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002520
2521/**@}*/
2522
Gilles Peskine20035e32018-02-03 22:44:14 +01002523/** \defgroup asymmetric Asymmetric cryptography
2524 * @{
2525 */
2526
2527/**
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002528 * \brief ECDSA signature size for a given curve bit size
Gilles Peskine0189e752018-02-03 23:57:22 +01002529 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002530 * \param curve_bits Curve size in bits.
2531 * \return Signature size in bytes.
Gilles Peskine0189e752018-02-03 23:57:22 +01002532 *
2533 * \note This macro returns a compile-time constant if its argument is one.
Gilles Peskine0189e752018-02-03 23:57:22 +01002534 */
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002535#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
2536 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskine0189e752018-02-03 23:57:22 +01002537
Gilles Peskine0189e752018-02-03 23:57:22 +01002538/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002539 * \brief Sign a hash or short message with a private key.
2540 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002541 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002542 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002543 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2544 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2545 * to determine the hash algorithm to use.
2546 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002547 * \param key Key slot containing an asymmetric key pair.
2548 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002549 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002550 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002551 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002552 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002553 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002554 * \param[out] signature_length On success, the number of bytes
2555 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002556 *
Gilles Peskine28538492018-07-11 17:34:00 +02002557 * \retval #PSA_SUCCESS
2558 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002559 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002560 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002561 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002562 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002563 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002564 * \retval #PSA_ERROR_NOT_SUPPORTED
2565 * \retval #PSA_ERROR_INVALID_ARGUMENT
2566 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2567 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2568 * \retval #PSA_ERROR_HARDWARE_FAILURE
2569 * \retval #PSA_ERROR_TAMPERING_DETECTED
2570 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002571 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002572 * The library has not been previously initialized by psa_crypto_init().
2573 * It is implementation-dependent whether a failure to initialize
2574 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002575 */
2576psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
2577 psa_algorithm_t alg,
2578 const uint8_t *hash,
2579 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002580 uint8_t *signature,
2581 size_t signature_size,
2582 size_t *signature_length);
2583
2584/**
2585 * \brief Verify the signature a hash or short message using a public key.
2586 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002587 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002588 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002589 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2590 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2591 * to determine the hash algorithm to use.
2592 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01002593 * \param key Key slot containing a public key or an
2594 * asymmetric key pair.
2595 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002596 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002597 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002598 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002599 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002600 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002601 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002602 *
Gilles Peskine28538492018-07-11 17:34:00 +02002603 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002604 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002605 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002606 * The calculation was perfomed successfully, but the passed
2607 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002608 * \retval #PSA_ERROR_NOT_SUPPORTED
2609 * \retval #PSA_ERROR_INVALID_ARGUMENT
2610 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2611 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2612 * \retval #PSA_ERROR_HARDWARE_FAILURE
2613 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002614 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002615 * The library has not been previously initialized by psa_crypto_init().
2616 * It is implementation-dependent whether a failure to initialize
2617 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002618 */
2619psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
2620 psa_algorithm_t alg,
2621 const uint8_t *hash,
2622 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002623 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002624 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002625
Gilles Peskine723feff2018-05-31 20:08:13 +02002626#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
Gilles Peskine072ac562018-06-30 00:21:29 +02002627 (PSA_ALG_IS_RSA_OAEP(alg) ? \
2628 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskine723feff2018-05-31 20:08:13 +02002629 11 /*PKCS#1v1.5*/)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002630
2631/**
2632 * \brief Encrypt a short message with a public key.
2633 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002634 * \param key Key slot containing a public key or an
2635 * asymmetric key pair.
2636 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002637 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002638 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002639 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002640 * \param[in] salt A salt or label, if supported by the
2641 * encryption algorithm.
2642 * If the algorithm does not support a
2643 * salt, pass \c NULL.
2644 * If the algorithm supports an optional
2645 * salt and you do not want to pass a salt,
2646 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002647 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002648 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2649 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002650 * \param salt_length Size of the \p salt buffer in bytes.
2651 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002652 * \param[out] output Buffer where the encrypted message is to
2653 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002654 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002655 * \param[out] output_length On success, the number of bytes
2656 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002657 *
Gilles Peskine28538492018-07-11 17:34:00 +02002658 * \retval #PSA_SUCCESS
2659 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002660 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002661 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002662 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002663 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002664 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002665 * \retval #PSA_ERROR_NOT_SUPPORTED
2666 * \retval #PSA_ERROR_INVALID_ARGUMENT
2667 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2668 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2669 * \retval #PSA_ERROR_HARDWARE_FAILURE
2670 * \retval #PSA_ERROR_TAMPERING_DETECTED
2671 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002672 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002673 * The library has not been previously initialized by psa_crypto_init().
2674 * It is implementation-dependent whether a failure to initialize
2675 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002676 */
2677psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
2678 psa_algorithm_t alg,
2679 const uint8_t *input,
2680 size_t input_length,
2681 const uint8_t *salt,
2682 size_t salt_length,
2683 uint8_t *output,
2684 size_t output_size,
2685 size_t *output_length);
2686
2687/**
2688 * \brief Decrypt a short message with a private key.
2689 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002690 * \param key Key slot containing an asymmetric key pair.
2691 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002692 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002693 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002694 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002695 * \param[in] salt A salt or label, if supported by the
2696 * encryption algorithm.
2697 * If the algorithm does not support a
2698 * salt, pass \c NULL.
2699 * If the algorithm supports an optional
2700 * salt and you do not want to pass a salt,
2701 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002702 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002703 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2704 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002705 * \param salt_length Size of the \p salt buffer in bytes.
2706 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002707 * \param[out] output Buffer where the decrypted message is to
2708 * be written.
2709 * \param output_size Size of the \c output buffer in bytes.
2710 * \param[out] output_length On success, the number of bytes
2711 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002712 *
Gilles Peskine28538492018-07-11 17:34:00 +02002713 * \retval #PSA_SUCCESS
2714 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002715 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002716 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002717 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002718 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002719 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002720 * \retval #PSA_ERROR_NOT_SUPPORTED
2721 * \retval #PSA_ERROR_INVALID_ARGUMENT
2722 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2723 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2724 * \retval #PSA_ERROR_HARDWARE_FAILURE
2725 * \retval #PSA_ERROR_TAMPERING_DETECTED
2726 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2727 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002728 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002729 * The library has not been previously initialized by psa_crypto_init().
2730 * It is implementation-dependent whether a failure to initialize
2731 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002732 */
2733psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
2734 psa_algorithm_t alg,
2735 const uint8_t *input,
2736 size_t input_length,
2737 const uint8_t *salt,
2738 size_t salt_length,
2739 uint8_t *output,
2740 size_t output_size,
2741 size_t *output_length);
2742
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002743/**@}*/
2744
Gilles Peskineedd76872018-07-20 17:42:05 +02002745/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002746 * @{
2747 */
2748
2749/** The type of the state data structure for generators.
2750 *
2751 * Before calling any function on a generator, the application must
2752 * initialize it by any of the following means:
2753 * - Set the structure to all-bits-zero, for example:
2754 * \code
2755 * psa_crypto_generator_t generator;
2756 * memset(&generator, 0, sizeof(generator));
2757 * \endcode
2758 * - Initialize the structure to logical zero values, for example:
2759 * \code
2760 * psa_crypto_generator_t generator = {0};
2761 * \endcode
2762 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2763 * for example:
2764 * \code
2765 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2766 * \endcode
2767 * - Assign the result of the function psa_crypto_generator_init()
2768 * to the structure, for example:
2769 * \code
2770 * psa_crypto_generator_t generator;
2771 * generator = psa_crypto_generator_init();
2772 * \endcode
2773 *
2774 * This is an implementation-defined \c struct. Applications should not
2775 * make any assumptions about the content of this structure except
2776 * as directed by the documentation of a specific implementation.
2777 */
2778typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2779
2780/** \def PSA_CRYPTO_GENERATOR_INIT
2781 *
2782 * This macro returns a suitable initializer for a generator object
2783 * of type #psa_crypto_generator_t.
2784 */
2785#ifdef __DOXYGEN_ONLY__
2786/* This is an example definition for documentation purposes.
2787 * Implementations should define a suitable value in `crypto_struct.h`.
2788 */
2789#define PSA_CRYPTO_GENERATOR_INIT {0}
2790#endif
2791
2792/** Return an initial value for a generator object.
2793 */
2794static psa_crypto_generator_t psa_crypto_generator_init(void);
2795
2796/** Retrieve the current capacity of a generator.
2797 *
2798 * The capacity of a generator is the maximum number of bytes that it can
2799 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2800 *
2801 * \param[in] generator The generator to query.
2802 * \param[out] capacity On success, the capacity of the generator.
2803 *
2804 * \retval PSA_SUCCESS
2805 * \retval PSA_ERROR_BAD_STATE
2806 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2807 */
2808psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2809 size_t *capacity);
2810
2811/** Read some data from a generator.
2812 *
2813 * This function reads and returns a sequence of bytes from a generator.
2814 * The data that is read is discarded from the generator. The generator's
2815 * capacity is decreased by the number of bytes read.
2816 *
2817 * \param[in,out] generator The generator object to read from.
2818 * \param[out] output Buffer where the generator output will be
2819 * written.
2820 * \param output_length Number of bytes to output.
2821 *
2822 * \retval PSA_SUCCESS
2823 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2824 * There were fewer than \p output_length bytes
2825 * in the generator. Note that in this case, no
2826 * output is written to the output buffer.
2827 * The generator's capacity is set to 0, thus
2828 * subsequent calls to this function will not
2829 * succeed, even with a smaller output buffer.
2830 * \retval PSA_ERROR_BAD_STATE
2831 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2832 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2833 * \retval PSA_ERROR_HARDWARE_FAILURE
2834 * \retval PSA_ERROR_TAMPERING_DETECTED
2835 */
2836psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2837 uint8_t *output,
2838 size_t output_length);
2839
2840/** Create a symmetric key from data read from a generator.
2841 *
2842 * This function reads a sequence of bytes from a generator and imports
2843 * these bytes as a key.
2844 * The data that is read is discarded from the generator. The generator's
2845 * capacity is decreased by the number of bytes read.
2846 *
2847 * This function is equivalent to calling #psa_generator_read and
2848 * passing the resulting output to #psa_import_key, but
2849 * if the implementation provides an isolation boundary then
2850 * the key material is not exposed outside the isolation boundary.
2851 *
2852 * \param key Slot where the key will be stored. This must be a
2853 * valid slot for a key of the chosen type. It must
2854 * be unoccupied.
2855 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2856 * This must be a symmetric key type.
2857 * \param bits Key size in bits.
2858 * \param[in,out] generator The generator object to read from.
2859 *
2860 * \retval PSA_SUCCESS
2861 * Success.
2862 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2863 * There were fewer than \p output_length bytes
2864 * in the generator. Note that in this case, no
2865 * output is written to the output buffer.
2866 * The generator's capacity is set to 0, thus
2867 * subsequent calls to this function will not
2868 * succeed, even with a smaller output buffer.
2869 * \retval PSA_ERROR_NOT_SUPPORTED
2870 * The key type or key size is not supported, either by the
2871 * implementation in general or in this particular slot.
2872 * \retval PSA_ERROR_BAD_STATE
2873 * \retval PSA_ERROR_INVALID_ARGUMENT
2874 * The key slot is invalid.
2875 * \retval PSA_ERROR_OCCUPIED_SLOT
2876 * There is already a key in the specified slot.
2877 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2878 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
2879 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2880 * \retval PSA_ERROR_HARDWARE_FAILURE
2881 * \retval PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002882 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002883 * The library has not been previously initialized by psa_crypto_init().
2884 * It is implementation-dependent whether a failure to initialize
2885 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002886 */
2887psa_status_t psa_generator_import_key(psa_key_slot_t key,
2888 psa_key_type_t type,
2889 size_t bits,
2890 psa_crypto_generator_t *generator);
2891
2892/** Abort a generator.
2893 *
2894 * Once a generator has been aborted, its capacity is zero.
2895 * Aborting a generator frees all associated resources except for the
2896 * \c generator structure itself.
2897 *
2898 * This function may be called at any time as long as the generator
2899 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2900 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2901 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2902 * on a generator that has not been set up.
2903 *
2904 * Once aborted, the generator object may be called.
2905 *
2906 * \param[in,out] generator The generator to abort.
2907 *
2908 * \retval PSA_SUCCESS
2909 * \retval PSA_ERROR_BAD_STATE
2910 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2911 * \retval PSA_ERROR_HARDWARE_FAILURE
2912 * \retval PSA_ERROR_TAMPERING_DETECTED
2913 */
2914psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2915
2916/**@}*/
2917
Gilles Peskineea0fb492018-07-12 17:17:20 +02002918/** \defgroup derivation Key derivation
2919 * @{
2920 */
2921
2922/** Set up a key derivation operation.
2923 *
2924 * A key derivation algorithm takes three inputs: a secret input \p key and
2925 * two non-secret inputs \p label and p salt.
2926 * The result of this function is a byte generator which can
2927 * be used to produce keys and other cryptographic material.
2928 *
2929 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002930 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2931 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002932 *
2933 * \param[in,out] generator The generator object to set up. It must
2934 * have been initialized to .
2935 * \param key Slot containing the secret key to use.
2936 * \param alg The key derivation algorithm to compute
2937 * (\c PSA_ALG_XXX value such that
2938 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2939 * \param[in] salt Salt to use.
2940 * \param salt_length Size of the \p salt buffer in bytes.
2941 * \param[in] label Label to use.
2942 * \param label_length Size of the \p label buffer in bytes.
2943 * \param capacity The maximum number of bytes that the
2944 * generator will be able to provide.
2945 *
2946 * \retval #PSA_SUCCESS
2947 * Success.
2948 * \retval #PSA_ERROR_EMPTY_SLOT
2949 * \retval #PSA_ERROR_NOT_PERMITTED
2950 * \retval #PSA_ERROR_INVALID_ARGUMENT
2951 * \c key is not compatible with \c alg,
2952 * or \p capacity is too large for the specified algorithm and key.
2953 * \retval #PSA_ERROR_NOT_SUPPORTED
2954 * \c alg is not supported or is not a key derivation algorithm.
2955 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2956 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2957 * \retval #PSA_ERROR_HARDWARE_FAILURE
2958 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002959 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002960 * The library has not been previously initialized by psa_crypto_init().
2961 * It is implementation-dependent whether a failure to initialize
2962 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002963 */
2964psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01002965 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002966 psa_algorithm_t alg,
2967 const uint8_t *salt,
2968 size_t salt_length,
2969 const uint8_t *label,
2970 size_t label_length,
2971 size_t capacity);
2972
2973/**@}*/
2974
Gilles Peskineedd76872018-07-20 17:42:05 +02002975/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002976 * @{
2977 */
2978
2979/**
2980 * \brief Generate random bytes.
2981 *
2982 * \warning This function **can** fail! Callers MUST check the return status
2983 * and MUST NOT use the content of the output buffer if the return
2984 * status is not #PSA_SUCCESS.
2985 *
2986 * \note To generate a key, use psa_generate_key() instead.
2987 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002988 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002989 * \param output_size Number of bytes to generate and output.
2990 *
Gilles Peskine28538492018-07-11 17:34:00 +02002991 * \retval #PSA_SUCCESS
2992 * \retval #PSA_ERROR_NOT_SUPPORTED
2993 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2994 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2995 * \retval #PSA_ERROR_HARDWARE_FAILURE
2996 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002997 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002998 * The library has not been previously initialized by psa_crypto_init().
2999 * It is implementation-dependent whether a failure to initialize
3000 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003001 */
3002psa_status_t psa_generate_random(uint8_t *output,
3003 size_t output_size);
3004
Gilles Peskine4c317f42018-07-12 01:24:09 +02003005/** Extra parameters for RSA key generation.
3006 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02003007 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02003008 * parameter to psa_generate_key().
3009 */
3010typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02003011 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02003012} psa_generate_key_extra_rsa;
3013
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003014/**
3015 * \brief Generate a key or key pair.
3016 *
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003017 * \param key Slot where the key will be stored. This must be a
3018 * valid slot for a key of the chosen type. It must
3019 * be unoccupied.
3020 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
3021 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02003022 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003023 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003024 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003025 * default parameters. Implementation that support
3026 * the generation of vendor-specific key types
3027 * that allow extra parameters shall document
3028 * the format of these extra parameters and
3029 * the default values. For standard parameters,
3030 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003031 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003032 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
3033 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003034 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003035 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
3036 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003037 * - For an RSA key (\p type is
3038 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3039 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003040 * specifying the public exponent. The
3041 * default public exponent used when \p extra
3042 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02003043 * \param extra_size Size of the buffer that \p extra
3044 * points to, in bytes. Note that if \p extra is
3045 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003046 *
Gilles Peskine28538492018-07-11 17:34:00 +02003047 * \retval #PSA_SUCCESS
3048 * \retval #PSA_ERROR_NOT_SUPPORTED
3049 * \retval #PSA_ERROR_INVALID_ARGUMENT
3050 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3051 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3052 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3053 * \retval #PSA_ERROR_HARDWARE_FAILURE
3054 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003055 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003056 * The library has not been previously initialized by psa_crypto_init().
3057 * It is implementation-dependent whether a failure to initialize
3058 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003059 */
3060psa_status_t psa_generate_key(psa_key_slot_t key,
3061 psa_key_type_t type,
3062 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003063 const void *extra,
3064 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003065
3066/**@}*/
3067
Gilles Peskinee59236f2018-01-27 23:32:46 +01003068#ifdef __cplusplus
3069}
3070#endif
3071
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003072/* The file "crypto_sizes.h" contains definitions for size calculation
3073 * macros whose definitions are implementation-specific. */
3074#include "crypto_sizes.h"
3075
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003076/* The file "crypto_struct.h" contains definitions for
3077 * implementation-specific structs that are declared above. */
3078#include "crypto_struct.h"
3079
3080/* The file "crypto_extra.h" contains vendor-specific definitions. This
3081 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003082#include "crypto_extra.h"
3083
3084#endif /* PSA_CRYPTO_H */