blob: ff299362ee9cdee06c7f20443ce0aaec23ab1a46 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010039/** \brief Key slot number.
40 *
41 * This type represents key slots. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043 * 0 is not a valid key slot number. The meaning of other values is
44 * implementation dependent.
45 *
46 * At any given point in time, each key slot either contains a
47 * cryptographic object, or is empty. Key slots are persistent:
48 * once set, the cryptographic object remains in the key slot until
49 * explicitly destroyed.
50 */
51typedef _unsigned_integral_type_ psa_key_slot_t;
52
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010053/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010054#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010055
Gilles Peskinee59236f2018-01-27 23:32:46 +010056#ifdef __cplusplus
57extern "C" {
58#endif
59
60/** \defgroup basic Basic definitions
61 * @{
62 */
63
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020064#if defined(PSA_SUCCESS)
65/* If PSA_SUCCESS is defined, assume that PSA crypto is being used
66 * together with PSA IPC, which also defines the identifier
67 * PSA_SUCCESS. We must not define PSA_SUCCESS ourselves in that case;
68 * the other error code names don't clash. Also define psa_status_t as
69 * an alias for the type used by PSA IPC. This is a temporary hack
mohammad160313f43942018-08-05 12:09:44 +030070 * until we unify error reporting in PSA IPC and PSA crypto.
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020071 *
72 * Note that psa_defs.h must be included before this header!
73 */
74typedef psa_error_t psa_status_t;
75
76#else /* defined(PSA_SUCCESS) */
77
Gilles Peskinee59236f2018-01-27 23:32:46 +010078/**
79 * \brief Function return status.
80 *
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020081 * This is either #PSA_SUCCESS (which is zero), indicating success,
82 * or a nonzero value indicating that an error occurred. Errors are
83 * encoded as one of the \c PSA_ERROR_xxx values defined here.
Gilles Peskinee59236f2018-01-27 23:32:46 +010084 */
itayzafrirc2a79762018-06-18 16:20:16 +030085typedef int32_t psa_status_t;
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020086
itayzafrirc2a79762018-06-18 16:20:16 +030087/** The action was completed successfully. */
88#define PSA_SUCCESS ((psa_status_t)0)
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020089
90#endif /* !defined(PSA_SUCCESS) */
itayzafrirc2a79762018-06-18 16:20:16 +030091
itayzafrirf26dbfc2018-08-01 16:09:08 +030092/** An error occurred that does not correspond to any defined
93 * failure cause.
94 *
95 * Implementations may use this error code if none of the other standard
96 * error codes are applicable. */
97#define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)1)
98
itayzafrirc2a79762018-06-18 16:20:16 +030099/** The requested operation or a parameter is not supported
100 * by this implementation.
101 *
102 * Implementations should return this error code when an enumeration
103 * parameter such as a key type, algorithm, etc. is not recognized.
104 * If a combination of parameters is recognized and identified as
105 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300106#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)2)
itayzafrirc2a79762018-06-18 16:20:16 +0300107
108/** The requested action is denied by a policy.
109 *
110 * Implementations should return this error code when the parameters
111 * are recognized as valid and supported, and a policy explicitly
112 * denies the requested operation.
113 *
114 * If a subset of the parameters of a function call identify a
115 * forbidden operation, and another subset of the parameters are
116 * not valid or not supported, it is unspecified whether the function
117 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
118 * #PSA_ERROR_INVALID_ARGUMENT. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300119#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)3)
itayzafrirc2a79762018-06-18 16:20:16 +0300120
121/** An output buffer is too small.
122 *
Gilles Peskinebe42f312018-07-13 14:38:15 +0200123 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
itayzafrirc2a79762018-06-18 16:20:16 +0300124 * description to determine a sufficient buffer size.
125 *
126 * Implementations should preferably return this error code only
127 * in cases when performing the operation with a larger output
128 * buffer would succeed. However implementations may return this
129 * error if a function has invalid or unsupported parameters in addition
130 * to the parameters that determine the necessary output buffer size. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300131#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)4)
itayzafrirc2a79762018-06-18 16:20:16 +0300132
133/** A slot is occupied, but must be empty to carry out the
134 * requested action.
135 *
136 * If the slot number is invalid (i.e. the requested action could
137 * not be performed even after erasing the slot's content),
138 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300139#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)5)
itayzafrirc2a79762018-06-18 16:20:16 +0300140
141/** A slot is empty, but must be occupied to carry out the
142 * requested action.
143 *
144 * If the slot number is invalid (i.e. the requested action could
145 * not be performed even after creating appropriate content in the slot),
146 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300147#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)6)
itayzafrirc2a79762018-06-18 16:20:16 +0300148
149/** The requested action cannot be performed in the current state.
150 *
151 * Multipart operations return this error when one of the
152 * functions is called out of sequence. Refer to the function
153 * descriptions for permitted sequencing of functions.
154 *
155 * Implementations shall not return this error code to indicate
156 * that a key slot is occupied when it needs to be free or vice versa,
157 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
158 * as applicable. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300159#define PSA_ERROR_BAD_STATE ((psa_status_t)7)
itayzafrirc2a79762018-06-18 16:20:16 +0300160
161/** The parameters passed to the function are invalid.
162 *
163 * Implementations may return this error any time a parameter or
164 * combination of parameters are recognized as invalid.
165 *
166 * Implementations shall not return this error code to indicate
167 * that a key slot is occupied when it needs to be free or vice versa,
168 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
169 * as applicable. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300170#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)8)
itayzafrirc2a79762018-06-18 16:20:16 +0300171
172/** There is not enough runtime memory.
173 *
174 * If the action is carried out across multiple security realms, this
175 * error can refer to available memory in any of the security realms. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300176#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)9)
itayzafrirc2a79762018-06-18 16:20:16 +0300177
178/** There is not enough persistent storage.
179 *
180 * Functions that modify the key storage return this error code if
181 * there is insufficient storage space on the host media. In addition,
182 * many functions that do not otherwise access storage may return this
183 * error code if the implementation requires a mandatory log entry for
184 * the requested action and the log storage space is full. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300185#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)10)
itayzafrirc2a79762018-06-18 16:20:16 +0300186
187/** There was a communication failure inside the implementation.
188 *
189 * This can indicate a communication failure between the application
190 * and an external cryptoprocessor or between the cryptoprocessor and
191 * an external volatile or persistent memory. A communication failure
192 * may be transient or permanent depending on the cause.
193 *
194 * \warning If a function returns this error, it is undetermined
195 * whether the requested action has completed or not. Implementations
196 * should return #PSA_SUCCESS on successful completion whenver
197 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
198 * if the requested action was completed successfully in an external
199 * cryptoprocessor but there was a breakdown of communication before
200 * the cryptoprocessor could report the status to the application.
201 */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300202#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)11)
itayzafrirc2a79762018-06-18 16:20:16 +0300203
204/** There was a storage failure that may have led to data loss.
205 *
206 * This error indicates that some persistent storage is corrupted.
207 * It should not be used for a corruption of volatile memory
208 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
209 * between the cryptoprocessor and its external storage (use
210 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
211 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
212 *
213 * Note that a storage failure does not indicate that any data that was
214 * previously read is invalid. However this previously read data may no
215 * longer be readable from storage.
216 *
217 * When a storage failure occurs, it is no longer possible to ensure
218 * the global integrity of the keystore. Depending on the global
219 * integrity guarantees offered by the implementation, access to other
220 * data may or may not fail even if the data is still readable but
221 * its integrity canont be guaranteed.
222 *
223 * Implementations should only use this error code to report a
224 * permanent storage corruption. However application writers should
225 * keep in mind that transient errors while reading the storage may be
226 * reported using this error code. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300227#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)12)
itayzafrirc2a79762018-06-18 16:20:16 +0300228
229/** A hardware failure was detected.
230 *
231 * A hardware failure may be transient or permanent depending on the
232 * cause. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300233#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)13)
itayzafrirc2a79762018-06-18 16:20:16 +0300234
235/** A tampering attempt was detected.
236 *
237 * If an application receives this error code, there is no guarantee
238 * that previously accessed or computed data was correct and remains
239 * confidential. Applications should not perform any security function
240 * and should enter a safe failure state.
241 *
242 * Implementations may return this error code if they detect an invalid
243 * state that cannot happen during normal operation and that indicates
244 * that the implementation's security guarantees no longer hold. Depending
245 * on the implementation architecture and on its security and safety goals,
246 * the implementation may forcibly terminate the application.
247 *
248 * This error code is intended as a last resort when a security breach
249 * is detected and it is unsure whether the keystore data is still
250 * protected. Implementations shall only return this error code
251 * to report an alarm from a tampering detector, to indicate that
252 * the confidentiality of stored data can no longer be guaranteed,
253 * or to indicate that the integrity of previously returned data is now
254 * considered compromised. Implementations shall not use this error code
255 * to indicate a hardware failure that merely makes it impossible to
256 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
257 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
258 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
259 * instead).
260 *
261 * This error indicates an attack against the application. Implementations
262 * shall not return this error code as a consequence of the behavior of
263 * the application itself. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300264#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)14)
itayzafrirc2a79762018-06-18 16:20:16 +0300265
266/** There is not enough entropy to generate random data needed
267 * for the requested action.
268 *
269 * This error indicates a failure of a hardware random generator.
270 * Application writers should note that this error can be returned not
271 * only by functions whose purpose is to generate random data, such
272 * as key, IV or nonce generation, but also by functions that execute
273 * an algorithm with a randomized result, as well as functions that
274 * use randomization of intermediate computations as a countermeasure
275 * to certain attacks.
276 *
277 * Implementations should avoid returning this error after psa_crypto_init()
278 * has succeeded. Implementations should generate sufficient
279 * entropy during initialization and subsequently use a cryptographically
280 * secure pseudorandom generator (PRNG). However implementations may return
281 * this error at any time if a policy requires the PRNG to be reseeded
282 * during normal operation. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300283#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)15)
itayzafrirc2a79762018-06-18 16:20:16 +0300284
285/** The signature, MAC or hash is incorrect.
286 *
287 * Verification functions return this error if the verification
288 * calculations completed successfully, and the value to be verified
289 * was determined to be incorrect.
290 *
291 * If the value to verify has an invalid size, implementations may return
292 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300293#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)16)
itayzafrirc2a79762018-06-18 16:20:16 +0300294
295/** The decrypted padding is incorrect.
296 *
297 * \warning In some protocols, when decrypting data, it is essential that
298 * the behavior of the application does not depend on whether the padding
299 * is correct, down to precise timing. Applications should prefer
300 * protocols that use authenticated encryption rather than plain
301 * encryption. If the application must perform a decryption of
302 * unauthenticated data, the application writer should take care not
303 * to reveal whether the padding is invalid.
304 *
305 * Implementations should strive to make valid and invalid padding
306 * as close as possible to indistinguishable to an external observer.
307 * In particular, the timing of a decryption operation should not
308 * depend on the validity of the padding. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300309#define PSA_ERROR_INVALID_PADDING ((psa_status_t)17)
itayzafrirc2a79762018-06-18 16:20:16 +0300310
Gilles Peskineeab56e42018-07-12 17:12:33 +0200311/** The generator has insufficient capacity left.
312 *
313 * Once a function returns this error, attempts to read from the
314 * generator will always return this error. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300315#define PSA_ERROR_INSUFFICIENT_CAPACITY ((psa_status_t)18)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100316
317/**
318 * \brief Library initialization.
319 *
320 * Applications must call this function before calling any other
321 * function in this module.
322 *
323 * Applications may call this function more than once. Once a call
324 * succeeds, subsequent calls are guaranteed to succeed.
325 *
Gilles Peskine28538492018-07-11 17:34:00 +0200326 * \retval #PSA_SUCCESS
327 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
328 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
329 * \retval #PSA_ERROR_HARDWARE_FAILURE
330 * \retval #PSA_ERROR_TAMPERING_DETECTED
331 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100332 */
333psa_status_t psa_crypto_init(void);
334
Gilles Peskine2905a7a2018-03-07 16:39:31 +0100335#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
336#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Gilles Peskine0189e752018-02-03 23:57:22 +0100337
Gilles Peskinee59236f2018-01-27 23:32:46 +0100338/**@}*/
339
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100340/** \defgroup crypto_types Key and algorithm types
341 * @{
342 */
343
Gilles Peskine308b91d2018-02-08 09:47:44 +0100344/** \brief Encoding of a key type.
345 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100346typedef uint32_t psa_key_type_t;
347
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100348/** An invalid key type value.
349 *
350 * Zero is not the encoding of any key type.
351 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100352#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100353
354/** Vendor-defined flag
355 *
356 * Key types defined by this standard will never have the
357 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
358 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
359 * respect the bitwise structure used by standard encodings whenever practical.
360 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100361#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100362
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200363#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x70000000)
364#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x40000000)
365#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x50000000)
366#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x60000000)
367#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x70000000)
368
369#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x10000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200370
Gilles Peskinee8779742018-08-10 16:10:56 +0200371/** Whether a key type is vendor-defined. */
372#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
373 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
374
375/** Whether a key type is an unstructured array of bytes.
376 *
377 * This encompasses both symmetric keys and non-key data.
378 */
379#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
380 (((type) & PSA_KEY_TYPE_CATEGORY_MASK & ~(psa_key_type_t)0x10000000) == \
381 PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
382
383/** Whether a key type is asymmetric: either a key pair or a public key. */
384#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
385 (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
386 & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
387 PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
388/** Whether a key type is the public part of a key pair. */
389#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
390 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
391/** Whether a key type is a key pair containing a private part and a public
392 * part. */
393#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
394 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
395/** The key pair type corresponding to a public key type.
396 *
397 * You may also pass a key pair type as \p type, it will be left unchanged.
398 *
399 * \param type A public key type or key pair type.
400 *
401 * \return The corresponding key pair type.
402 * If \p type is not a public key or a key pair,
403 * the return value is undefined.
404 */
405#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
406 ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
407/** The public key type corresponding to a key pair type.
408 *
409 * You may also pass a key pair type as \p type, it will be left unchanged.
410 *
411 * \param type A public key type or key pair type.
412 *
413 * \return The corresponding public key type.
414 * If \p type is not a public key or a key pair,
415 * the return value is undefined.
416 */
417#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
418 ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
Gilles Peskinee8779742018-08-10 16:10:56 +0200419
Gilles Peskine35855962018-04-19 08:39:16 +0200420/** Raw data.
421 *
422 * A "key" of this type cannot be used for any cryptographic operation.
423 * Applications may use this type to store arbitrary data in the keystore. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200424#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x50000001)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100425
Gilles Peskine35855962018-04-19 08:39:16 +0200426/** HMAC key.
427 *
428 * The key policy determines which underlying hash algorithm the key can be
429 * used for.
430 *
431 * HMAC keys should generally have the same size as the underlying hash.
Gilles Peskinebe42f312018-07-13 14:38:15 +0200432 * This size can be calculated with #PSA_HASH_SIZE(\c alg) where
433 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200434#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x51000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200435
Gilles Peskineea0fb492018-07-12 17:17:20 +0200436/** A secret for key derivation.
437 *
438 * The key policy determines which key derivation algorithm the key
439 * can be used for.
440 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200441#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x52000000)
Gilles Peskineea0fb492018-07-12 17:17:20 +0200442
Gilles Peskine35855962018-04-19 08:39:16 +0200443/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
444 *
445 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
446 * 32 bytes (AES-256).
447 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200448#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x40000001)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200449
Gilles Peskine35855962018-04-19 08:39:16 +0200450/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
451 *
452 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
453 * 24 bytes (3-key 3DES).
454 *
455 * Note that single DES and 2-key 3DES are weak and strongly
456 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
457 * is weak and deprecated and should only be used in legacy protocols.
458 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200459#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x40000002)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200460
Gilles Peskine35855962018-04-19 08:39:16 +0200461/** Key for an cipher, AEAD or MAC algorithm based on the
462 * Camellia block cipher. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200463#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x40000003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200464
Gilles Peskine35855962018-04-19 08:39:16 +0200465/** Key for the RC4 stream cipher.
466 *
467 * Note that RC4 is weak and deprecated and should only be used in
468 * legacy protocols. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200469#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x40000004)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100470
Gilles Peskine308b91d2018-02-08 09:47:44 +0100471/** RSA public key. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200472#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x60010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100473/** RSA key pair (private and public key). */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200474#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x70010000)
Gilles Peskine583b55d2018-08-22 18:21:32 +0200475/** Whether a key type is an RSA key (pair or public-only). */
476#define PSA_KEY_TYPE_IS_RSA(type) \
477 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200478
Gilles Peskine06dc2632018-03-08 07:47:25 +0100479/** DSA public key. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200480#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x60020000)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100481/** DSA key pair (private and public key). */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200482#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x70020000)
Gilles Peskine583b55d2018-08-22 18:21:32 +0200483/** Whether a key type is an DSA key (pair or public-only). */
484#define PSA_KEY_TYPE_IS_DSA(type) \
485 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200486
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200487#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x60030000)
488#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x70030000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100489#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200490/** Elliptic curve key pair. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100491#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
492 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200493/** Elliptic curve public key. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100494#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
495 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100496
Gilles Peskined8008d62018-06-29 19:51:51 +0200497/** Whether a key type is an elliptic curve key (pair or public-only). */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100498#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100499 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
500 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine55728b02018-07-16 23:08:16 +0200501#define PSA_KEY_TYPE_IS_ECC_KEYPAIR(type) \
502 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
503 PSA_KEY_TYPE_ECC_KEYPAIR_BASE)
504#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
505 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
506 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200508/** The type of PSA elliptic curve identifiers. */
509typedef uint16_t psa_ecc_curve_t;
510/** Extract the curve from an elliptic curve key type. */
511#define PSA_KEY_TYPE_GET_CURVE(type) \
512 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
513 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
514 0))
515
516/* The encoding of curve identifiers is currently aligned with the
517 * TLS Supported Groups Registry (formerly known as the
518 * TLS EC Named Curve Registry)
519 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
Gilles Peskine70ce2c62018-08-22 18:21:57 +0200520 * The values are defined by RFC 8422 and RFC 7027. */
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200521#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
522#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
523#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
524#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
525#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
526#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
527#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
528#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
529#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
530#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
531#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
532#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
533#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
534#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
535#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
536#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
537#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
538#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
539#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
540#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
541#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
542#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
543#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
544#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
545#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
546#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
547#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
548#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
549#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
550#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200551
Gilles Peskine7e198532018-03-08 07:50:30 +0100552/** The block size of a block cipher.
553 *
554 * \param type A cipher key type (value of type #psa_key_type_t).
555 *
556 * \return The block size for a block cipher, or 1 for a stream cipher.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200557 * The return value is undefined if \p type is not a supported
Gilles Peskine35855962018-04-19 08:39:16 +0200558 * cipher key type.
559 *
560 * \note It is possible to build stream cipher algorithms on top of a block
561 * cipher, for example CTR mode (#PSA_ALG_CTR).
562 * This macro only takes the key type into account, so it cannot be
563 * used to determine the size of the data that #psa_cipher_update()
564 * might buffer for future processing in general.
Gilles Peskine7e198532018-03-08 07:50:30 +0100565 *
566 * \note This macro returns a compile-time constant if its argument is one.
567 *
568 * \warning This macro may evaluate its argument multiple times.
569 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100570#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100571 ( \
572 (type) == PSA_KEY_TYPE_AES ? 16 : \
573 (type) == PSA_KEY_TYPE_DES ? 8 : \
574 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100575 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100576 0)
577
Gilles Peskine308b91d2018-02-08 09:47:44 +0100578/** \brief Encoding of a cryptographic algorithm.
579 *
580 * For algorithms that can be applied to multiple key types, this type
581 * does not encode the key type. For example, for symmetric ciphers
582 * based on a block cipher, #psa_algorithm_t encodes the block cipher
583 * mode and the padding mode while the block cipher itself is encoded
584 * via #psa_key_type_t.
585 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100586typedef uint32_t psa_algorithm_t;
587
Gilles Peskine98f0a242018-02-06 18:57:29 +0100588#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
589#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
590#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
591#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
592#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
593#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
594#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
595#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
596#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
597#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100598
Gilles Peskine98f0a242018-02-06 18:57:29 +0100599#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
600 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200601
Gilles Peskine308b91d2018-02-08 09:47:44 +0100602/** Whether the specified algorithm is a hash algorithm.
603 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100604 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100605 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200606 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
607 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskine7e198532018-03-08 07:50:30 +0100608 * algorithm identifier.
609 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100610#define PSA_ALG_IS_HASH(alg) \
611 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200612
613/** Whether the specified algorithm is a MAC algorithm.
614 *
615 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
616 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200617 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
618 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200619 * algorithm identifier.
620 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100621#define PSA_ALG_IS_MAC(alg) \
622 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200623
624/** Whether the specified algorithm is a symmetric cipher algorithm.
625 *
626 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
627 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200628 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
629 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200630 * algorithm identifier.
631 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100632#define PSA_ALG_IS_CIPHER(alg) \
633 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200634
635/** Whether the specified algorithm is an authenticated encryption
636 * with associated data (AEAD) algorithm.
637 *
638 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
639 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200640 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
641 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200642 * algorithm identifier.
643 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100644#define PSA_ALG_IS_AEAD(alg) \
645 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200646
647/** Whether the specified algorithm is a public-key signature algorithm.
648 *
649 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
650 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200651 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
652 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200653 * algorithm identifier.
654 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100655#define PSA_ALG_IS_SIGN(alg) \
656 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200657
658/** Whether the specified algorithm is a public-key encryption algorithm.
659 *
660 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
661 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200662 * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
663 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200664 * algorithm identifier.
665 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100666#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
667 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200668
669/** Whether the specified algorithm is a key agreement algorithm.
670 *
671 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
672 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200673 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
674 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200675 * algorithm identifier.
676 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100677#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
678 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200679
680/** Whether the specified algorithm is a key derivation algorithm.
681 *
682 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
683 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200684 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
685 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200686 * algorithm identifier.
687 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100688#define PSA_ALG_IS_KEY_DERIVATION(alg) \
689 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
690
691#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
692#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
693#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
694#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100695#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
696#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskineedd76872018-07-20 17:42:05 +0200697/** SHA2-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100698#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
Gilles Peskineedd76872018-07-20 17:42:05 +0200699/** SHA2-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100700#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
Gilles Peskineedd76872018-07-20 17:42:05 +0200701/** SHA2-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100702#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
Gilles Peskineedd76872018-07-20 17:42:05 +0200703/** SHA2-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100704#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
Gilles Peskineedd76872018-07-20 17:42:05 +0200705/** SHA2-512/224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100706#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
Gilles Peskineedd76872018-07-20 17:42:05 +0200707/** SHA2-512/256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100708#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
Gilles Peskineedd76872018-07-20 17:42:05 +0200709/** SHA3-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100710#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
Gilles Peskineedd76872018-07-20 17:42:05 +0200711/** SHA3-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100712#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
Gilles Peskineedd76872018-07-20 17:42:05 +0200713/** SHA3-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100714#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
Gilles Peskineedd76872018-07-20 17:42:05 +0200715/** SHA3-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100716#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
717
Gilles Peskine8c9def32018-02-08 10:02:12 +0100718#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100719#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200720/** Macro to build an HMAC algorithm.
721 *
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200722 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
Gilles Peskine35855962018-04-19 08:39:16 +0200723 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200724 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200725 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine35855962018-04-19 08:39:16 +0200726 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200727 * \return The corresponding HMAC algorithm.
728 * \return Unspecified if \p alg is not a supported
729 * hash algorithm.
Gilles Peskine35855962018-04-19 08:39:16 +0200730 */
731#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100732 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200733
Gilles Peskine8c9def32018-02-08 10:02:12 +0100734#define PSA_ALG_HMAC_HASH(hmac_alg) \
735 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200736
737/** Whether the specified algorithm is an HMAC algorithm.
738 *
739 * HMAC is a family of MAC algorithms that are based on a hash function.
740 *
741 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
742 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200743 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
744 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200745 * algorithm identifier.
746 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100747#define PSA_ALG_IS_HMAC(alg) \
748 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
749 PSA_ALG_HMAC_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200750
Gilles Peskine8c9def32018-02-08 10:02:12 +0100751#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
752#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
753#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
754#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200755
756/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
757 *
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200758 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
759 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200760 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
761 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200762 * algorithm identifier.
763 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100764#define PSA_ALG_IS_CIPHER_MAC(alg) \
765 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
766 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100767
Gilles Peskinedaea26f2018-08-21 14:02:45 +0200768#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
769#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100770
Gilles Peskinedcd14942018-07-12 00:30:52 +0200771/** Whether the specified algorithm is a stream cipher.
772 *
773 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
774 * by applying a bitwise-xor with a stream of bytes that is generated
775 * from a key.
776 *
777 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
778 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200779 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
780 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200781 * algorithm identifier or if it is not a symmetric cipher algorithm.
782 */
Moran Pekerbed71a22018-04-22 20:19:20 +0300783#define PSA_ALG_IS_STREAM_CIPHER(alg) \
Gilles Peskinedaea26f2018-08-21 14:02:45 +0200784 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
785 (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
786
787/** The ARC4 stream cipher algorithm.
788 */
789#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800001)
790
791/** The CTR stream cipher mode.
792 *
793 * CTR is a stream cipher which is built from a block cipher.
794 * The underlying block cipher is determined by the key type.
795 * For example, to use AES-128-CTR, use this algorithm with
796 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
797 */
798#define PSA_ALG_CTR ((psa_algorithm_t)0x04c00001)
799
800#define PSA_ALG_CFB ((psa_algorithm_t)0x04c00002)
801
802#define PSA_ALG_OFB ((psa_algorithm_t)0x04c00003)
803
804/** The XTS cipher mode.
805 *
806 * XTS is a cipher mode which is built from a block cipher. It requires at
807 * least one full block of input, but beyond this minimum the input
808 * does not need to be a whole number of blocks.
809 */
810#define PSA_ALG_XTS ((psa_algorithm_t)0x044000ff)
811
812/** The CBC block cipher chaining mode, with no padding.
813 *
814 * The underlying block cipher is determined by the key type.
815 *
816 * This symmetric cipher mode can only be used with messages whose lengths
817 * are whole number of blocks for the chosen block cipher.
818 */
819#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04600100)
820
821/** The CBC block cipher chaining mode with PKCS#7 padding.
822 *
823 * The underlying block cipher is determined by the key type.
824 *
825 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
826 */
827#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04600101)
Moran Pekerbed71a22018-04-22 20:19:20 +0300828
Gilles Peskine8c9def32018-02-08 10:02:12 +0100829#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
830#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100831
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200832#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
833/** RSA PKCS#1 v1.5 signature with hashing.
834 *
835 * This is the signature scheme defined by RFC 8017
836 * (PKCS#1: RSA Cryptography Specifications) under the name
837 * RSASSA-PKCS1-v1_5.
838 *
839 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200840 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200841 *
842 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
843 * \return Unspecified if \p alg is not a supported
844 * hash algorithm.
845 */
Gilles Peskinea5926232018-03-28 14:16:50 +0200846#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200847 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
848/** Raw PKCS#1 v1.5 signature.
849 *
850 * The input to this algorithm is the DigestInfo structure used by
851 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
852 * steps 3&ndash;6.
853 */
854#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
Gilles Peskinea5926232018-03-28 14:16:50 +0200855#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200856 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200857
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200858#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
859/** RSA PSS signature with hashing.
860 *
861 * This is the signature scheme defined by RFC 8017
862 * (PKCS#1: RSA Cryptography Specifications) under the name
Gilles Peskinea4d20bd2018-06-29 23:35:02 +0200863 * RSASSA-PSS, with the message generation function MGF1, and with
864 * a salt length equal to the length of the hash. The specified
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200865 * hash algorithm is used to hash the input message, to create the
866 * salted hash, and for the mask generation.
867 *
868 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200869 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200870 *
871 * \return The corresponding RSA PSS signature algorithm.
872 * \return Unspecified if \p alg is not a supported
873 * hash algorithm.
874 */
875#define PSA_ALG_RSA_PSS(hash_alg) \
876 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
877#define PSA_ALG_IS_RSA_PSS(alg) \
878 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
879
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200880#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
881/** DSA signature with hashing.
882 *
883 * This is the signature scheme defined by FIPS 186-4,
884 * with a random per-message secret number (*k*).
885 *
886 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200887 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200888 *
889 * \return The corresponding DSA signature algorithm.
890 * \return Unspecified if \p alg is not a supported
891 * hash algorithm.
892 */
893#define PSA_ALG_DSA(hash_alg) \
894 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
895#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
896#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
897#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
898 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
899#define PSA_ALG_IS_DSA(alg) \
900 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
901 PSA_ALG_DSA_BASE)
902#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
903 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +0200904#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
905 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
906#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
907 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200908
909#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
910/** ECDSA signature with hashing.
911 *
912 * This is the ECDSA signature scheme defined by ANSI X9.62,
913 * with a random per-message secret number (*k*).
914 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200915 * The representation of the signature as a byte string consists of
916 * the concatentation of the signature values *r* and *s*. Each of
917 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
918 * of the base point of the curve in octets. Each value is represented
919 * in big-endian order (most significant octet first).
920 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200921 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200922 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200923 *
924 * \return The corresponding ECDSA signature algorithm.
925 * \return Unspecified if \p alg is not a supported
926 * hash algorithm.
927 */
928#define PSA_ALG_ECDSA(hash_alg) \
929 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
930/** ECDSA signature without hashing.
931 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200932 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200933 * without specifying a hash algorithm. This algorithm may only be
934 * used to sign or verify a sequence of bytes that should be an
935 * already-calculated hash. Note that the input is padded with
936 * zeros on the left or truncated on the left as required to fit
937 * the curve size.
938 */
939#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
940#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
941/** Deterministic ECDSA signature with hashing.
942 *
943 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
944 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200945 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
946 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200947 * Note that when this algorithm is used for verification, signatures
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200948 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200949 * same private key are accepted. In other words,
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200950 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
951 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200952 *
953 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200954 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200955 *
956 * \return The corresponding deterministic ECDSA signature
957 * algorithm.
958 * \return Unspecified if \p alg is not a supported
959 * hash algorithm.
960 */
961#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
962 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
963#define PSA_ALG_IS_ECDSA(alg) \
964 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
965 PSA_ALG_ECDSA_BASE)
966#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
967 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +0200968#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
969 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
970#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
971 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200972
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200973/** Get the hash used by a hash-and-sign signature algorithm.
974 *
975 * A hash-and-sign algorithm is a signature algorithm which is
976 * composed of two phases: first a hashing phase which does not use
977 * the key and produces a hash of the input message, then a signing
978 * phase which only uses the hash and the key and not the message
979 * itself.
980 *
981 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200982 * #PSA_ALG_IS_SIGN(\p alg) is true).
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200983 *
984 * \return The underlying hash algorithm if \p alg is a hash-and-sign
985 * algorithm.
986 * \return 0 if \p alg is a signature algorithm that does not
987 * follow the hash-and-sign structure.
988 * \return Unspecified if \p alg is not a signature algorithm or
989 * if it is not supported by the implementation.
990 */
991#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200992 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
993 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
Gilles Peskine54622ae2018-06-29 22:24:24 +0200994 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200995 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
996 0)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100997
Gilles Peskinedcd14942018-07-12 00:30:52 +0200998/** RSA PKCS#1 v1.5 encryption.
999 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001000#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001001
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001002#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001003/** RSA OAEP encryption.
1004 *
1005 * This is the encryption scheme defined by RFC 8017
1006 * (PKCS#1: RSA Cryptography Specifications) under the name
1007 * RSAES-OAEP, with the message generation function MGF1.
1008 *
1009 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1010 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1011 * for MGF1.
1012 *
1013 * \return The corresponding RSA OAEP signature algorithm.
1014 * \return Unspecified if \p alg is not a supported
1015 * hash algorithm.
1016 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001017#define PSA_ALG_RSA_OAEP(hash_alg) \
1018 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1019#define PSA_ALG_IS_RSA_OAEP(alg) \
1020 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
Gilles Peskine072ac562018-06-30 00:21:29 +02001021#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1022 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1023 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1024 0)
Gilles Peskined1e8e412018-06-07 09:49:39 +02001025
Gilles Peskinebef7f142018-07-12 17:22:21 +02001026#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100)
1027/** Macro to build an HKDF algorithm.
1028 *
1029 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1030 *
1031 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1032 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1033 *
1034 * \return The corresponding HKDF algorithm.
1035 * \return Unspecified if \p alg is not a supported
1036 * hash algorithm.
1037 */
1038#define PSA_ALG_HKDF(hash_alg) \
1039 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1040/** Whether the specified algorithm is an HKDF algorithm.
1041 *
1042 * HKDF is a family of key derivation algorithms that are based on a hash
1043 * function and the HMAC construction.
1044 *
1045 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1046 *
1047 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1048 * This macro may return either 0 or 1 if \c alg is not a supported
1049 * key derivation algorithm identifier.
1050 */
1051#define PSA_ALG_IS_HKDF(alg) \
1052 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1053#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1054 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1055
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001056/**@}*/
1057
1058/** \defgroup key_management Key management
1059 * @{
1060 */
1061
1062/**
1063 * \brief Import a key in binary format.
1064 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +01001065 * This function supports any output from psa_export_key(). Refer to the
1066 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001067 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001068 * \param key Slot where the key will be stored. This must be a
1069 * valid slot for a key of the chosen type. It must
1070 * be unoccupied.
1071 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001072 * \param[in] data Buffer containing the key data.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001073 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001074 *
Gilles Peskine28538492018-07-11 17:34:00 +02001075 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001076 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001077 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001078 * The key type or key size is not supported, either by the
1079 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001080 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +01001081 * The key slot is invalid,
1082 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +02001083 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001084 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001085 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1086 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1087 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1088 * \retval #PSA_ERROR_HARDWARE_FAILURE
1089 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001090 */
1091psa_status_t psa_import_key(psa_key_slot_t key,
1092 psa_key_type_t type,
1093 const uint8_t *data,
1094 size_t data_length);
1095
1096/**
Gilles Peskine154bd952018-04-19 08:38:16 +02001097 * \brief Destroy a key and restore the slot to its default state.
1098 *
1099 * This function destroys the content of the key slot from both volatile
1100 * memory and, if applicable, non-volatile storage. Implementations shall
1101 * make a best effort to ensure that any previous content of the slot is
1102 * unrecoverable.
1103 *
1104 * This function also erases any metadata such as policies. It returns the
1105 * specified slot to its default state.
1106 *
1107 * \param key The key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001108 *
Gilles Peskine28538492018-07-11 17:34:00 +02001109 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +02001110 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +02001111 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001112 * The slot holds content and cannot be erased because it is
1113 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskine28538492018-07-11 17:34:00 +02001114 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001115 * The specified slot number does not designate a valid slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001116 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001117 * There was an failure in communication with the cryptoprocessor.
1118 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +02001119 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001120 * The storage is corrupted. Implementations shall make a best effort
1121 * to erase key material even in this stage, however applications
1122 * should be aware that it may be impossible to guarantee that the
1123 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +02001124 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001125 * An unexpected condition which is not a storage corruption or
1126 * a communication failure occurred. The cryptoprocessor may have
1127 * been compromised.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001128 */
1129psa_status_t psa_destroy_key(psa_key_slot_t key);
1130
1131/**
1132 * \brief Get basic metadata about a key.
1133 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001134 * \param key Slot whose content is queried. This must
1135 * be an occupied key slot.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001136 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001137 * This may be a null pointer, in which case the key type
1138 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001139 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +01001140 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +01001141 * is not written.
1142 *
Gilles Peskine28538492018-07-11 17:34:00 +02001143 * \retval #PSA_SUCCESS
1144 * \retval #PSA_ERROR_EMPTY_SLOT
1145 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1146 * \retval #PSA_ERROR_HARDWARE_FAILURE
1147 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001148 */
1149psa_status_t psa_get_key_information(psa_key_slot_t key,
1150 psa_key_type_t *type,
1151 size_t *bits);
1152
1153/**
1154 * \brief Export a key in binary format.
1155 *
1156 * The output of this function can be passed to psa_import_key() to
1157 * create an equivalent object.
1158 *
1159 * If a key is created with psa_import_key() and then exported with
1160 * this function, it is not guaranteed that the resulting data is
1161 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +01001162 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001163 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001164 * For standard key types, the output format is as follows:
1165 *
1166 * - For symmetric keys (including MAC keys), the format is the
1167 * raw bytes of the key.
1168 * - For DES, the key data consists of 8 bytes. The parity bits must be
1169 * correct.
1170 * - For Triple-DES, the format is the concatenation of the
1171 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +01001172 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001173 * is the non-encrypted DER encoding of the representation defined by
1174 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
1175 * ```
1176 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001177 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001178 * modulus INTEGER, -- n
1179 * publicExponent INTEGER, -- e
1180 * privateExponent INTEGER, -- d
1181 * prime1 INTEGER, -- p
1182 * prime2 INTEGER, -- q
1183 * exponent1 INTEGER, -- d mod (p-1)
1184 * exponent2 INTEGER, -- d mod (q-1)
1185 * coefficient INTEGER, -- (inverse of q) mod p
1186 * }
1187 * ```
1188 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
1189 * is the non-encrypted DER encoding of the representation used by
Gilles Peskinec6290c02018-08-13 17:24:59 +02001190 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001191 * ```
1192 * DSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001193 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001194 * prime INTEGER, -- p
1195 * subprime INTEGER, -- q
1196 * generator INTEGER, -- g
1197 * public INTEGER, -- y
1198 * private INTEGER, -- x
1199 * }
1200 * ```
1201 * - For elliptic curve key pairs (key types for which
1202 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is the
1203 * non-encrypted DER encoding of the representation defined by RFC 5915 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001204 * `ECPrivateKey`, version 1. The `ECParameters` field must be a
1205 * `namedCurve` OID as specified in RFC 5480 &sect;2.1.1.1. The public key
1206 * must be present and must be an `ECPoint` in the same format
1207 * (uncompressed variant) an ECC public key of the
1208 * corresponding type exported with psa_export_public_key().
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001209 * ```
1210 * ECPrivateKey ::= SEQUENCE {
1211 * version INTEGER, -- must be 1
1212 * privateKey OCTET STRING,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001213 * -- `ceiling(log2(n)/8)`-byte string, big endian,
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001214 * -- where n is the order of the curve.
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001215 * parameters [0] IMPLICIT ECParameters {{ namedCurve }}, -- mandatory
1216 * publicKey [1] IMPLICIT BIT STRING -- mandatory
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001217 * }
1218 * ```
1219 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
1220 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001221 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001222 * \param key Slot whose content is to be exported. This must
1223 * be an occupied key slot.
1224 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001225 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001226 * \param[out] data_length On success, the number of bytes
1227 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001228 *
Gilles Peskine28538492018-07-11 17:34:00 +02001229 * \retval #PSA_SUCCESS
1230 * \retval #PSA_ERROR_EMPTY_SLOT
1231 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +01001232 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +02001233 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1234 * The size of the \p data buffer is too small. You can determine a
1235 * sufficient buffer size by calling
1236 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
1237 * where \c type is the key type
1238 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +02001239 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1240 * \retval #PSA_ERROR_HARDWARE_FAILURE
1241 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001242 */
1243psa_status_t psa_export_key(psa_key_slot_t key,
1244 uint8_t *data,
1245 size_t data_size,
1246 size_t *data_length);
1247
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001248/**
1249 * \brief Export a public key or the public part of a key pair in binary format.
1250 *
1251 * The output of this function can be passed to psa_import_key() to
1252 * create an object that is equivalent to the public key.
1253 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001254 * The format is the DER representation defined by RFC 5280 as
1255 * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
1256 * specified below.
1257 * ```
1258 * SubjectPublicKeyInfo ::= SEQUENCE {
1259 * algorithm AlgorithmIdentifier,
1260 * subjectPublicKey BIT STRING }
1261 * AlgorithmIdentifier ::= SEQUENCE {
1262 * algorithm OBJECT IDENTIFIER,
1263 * parameters ANY DEFINED BY algorithm OPTIONAL }
1264 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001265 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001266 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY),
1267 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.1 as
1268 * `RSAPublicKey`,
1269 * with the OID `rsaEncryption`,
1270 * and with the parameters `NULL`.
1271 * ```
1272 * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
1273 * rsadsi(113549) pkcs(1) 1 }
1274 * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 }
1275 *
1276 * RSAPublicKey ::= SEQUENCE {
1277 * modulus INTEGER, -- n
1278 * publicExponent INTEGER } -- e
1279 * ```
1280 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
1281 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
1282 * `DSAPublicKey`,
1283 * with the OID `id-dsa`,
1284 * and with the parameters `DSS-Parms`.
1285 * ```
1286 * id-dsa OBJECT IDENTIFIER ::= {
1287 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
1288 *
1289 * Dss-Parms ::= SEQUENCE {
1290 * p INTEGER,
1291 * q INTEGER,
1292 * g INTEGER }
1293 * DSAPublicKey ::= INTEGER -- public key, Y
1294 * ```
1295 * - For elliptic curve public keys (key types for which
1296 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true),
1297 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.5 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001298 * `ECPoint`, which contains the uncompressed
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001299 * representation defined by SEC1 &sect;2.3.3.
1300 * The OID is `id-ecPublicKey`,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001301 * and the parameters must be given as a `namedCurve` OID as specified in
Gilles Peskinec6290c02018-08-13 17:24:59 +02001302 * RFC 5480 &sect;2.1.1.1 or other applicable standards.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001303 * ```
1304 * ansi-X9-62 OBJECT IDENTIFIER ::=
1305 * { iso(1) member-body(2) us(840) 10045 }
1306 * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 }
1307 * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 }
1308 *
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001309 * ECPoint ::= ...
1310 * -- first 8 bits: 0x04;
1311 * -- then x_P as an n-bit string, big endian;
1312 * -- then y_P as a n-bit string, big endian,
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001313 * -- where n is the order of the curve.
1314 *
1315 * EcpkParameters ::= CHOICE { -- other choices are not allowed
1316 * namedCurve OBJECT IDENTIFIER }
1317 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001318 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001319 * \param key Slot whose content is to be exported. This must
1320 * be an occupied key slot.
1321 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001322 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001323 * \param[out] data_length On success, the number of bytes
1324 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001325 *
Gilles Peskine28538492018-07-11 17:34:00 +02001326 * \retval #PSA_SUCCESS
1327 * \retval #PSA_ERROR_EMPTY_SLOT
1328 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +02001329 * The key is neither a public key nor a key pair.
1330 * \retval #PSA_ERROR_NOT_SUPPORTED
1331 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1332 * The size of the \p data buffer is too small. You can determine a
1333 * sufficient buffer size by calling
1334 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
1335 * where \c type is the key type
1336 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +02001337 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1338 * \retval #PSA_ERROR_HARDWARE_FAILURE
1339 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001340 */
1341psa_status_t psa_export_public_key(psa_key_slot_t key,
1342 uint8_t *data,
1343 size_t data_size,
1344 size_t *data_length);
1345
1346/**@}*/
1347
1348/** \defgroup policy Key policies
1349 * @{
1350 */
1351
1352/** \brief Encoding of permitted usage on a key. */
1353typedef uint32_t psa_key_usage_t;
1354
Gilles Peskine7e198532018-03-08 07:50:30 +01001355/** Whether the key may be exported.
1356 *
1357 * A public key or the public part of a key pair may always be exported
1358 * regardless of the value of this permission flag.
1359 *
1360 * If a key does not have export permission, implementations shall not
1361 * allow the key to be exported in plain form from the cryptoprocessor,
1362 * whether through psa_export_key() or through a proprietary interface.
1363 * The key may however be exportable in a wrapped form, i.e. in a form
1364 * where it is encrypted by another key.
1365 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001366#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1367
Gilles Peskine7e198532018-03-08 07:50:30 +01001368/** Whether the key may be used to encrypt a message.
1369 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001370 * This flag allows the key to be used for a symmetric encryption operation,
1371 * for an AEAD encryption-and-authentication operation,
1372 * or for an asymmetric encryption operation,
1373 * if otherwise permitted by the key's type and policy.
1374 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001375 * For a key pair, this concerns the public key.
1376 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001377#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +01001378
1379/** Whether the key may be used to decrypt a message.
1380 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001381 * This flag allows the key to be used for a symmetric decryption operation,
1382 * for an AEAD decryption-and-verification operation,
1383 * or for an asymmetric decryption operation,
1384 * if otherwise permitted by the key's type and policy.
1385 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001386 * For a key pair, this concerns the private key.
1387 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001388#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +01001389
1390/** Whether the key may be used to sign a message.
1391 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001392 * This flag allows the key to be used for a MAC calculation operation
1393 * or for an asymmetric signature operation,
1394 * if otherwise permitted by the key's type and policy.
1395 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001396 * For a key pair, this concerns the private key.
1397 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001398#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +01001399
1400/** Whether the key may be used to verify a message signature.
1401 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001402 * This flag allows the key to be used for a MAC verification operation
1403 * or for an asymmetric signature verification operation,
1404 * if otherwise permitted by by the key's type and policy.
1405 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001406 * For a key pair, this concerns the public key.
1407 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001408#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1409
Gilles Peskineea0fb492018-07-12 17:17:20 +02001410/** Whether the key may be used to derive other keys.
1411 */
1412#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1413
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001414/** The type of the key policy data structure.
1415 *
1416 * This is an implementation-defined \c struct. Applications should not
1417 * make any assumptions about the content of this structure except
1418 * as directed by the documentation of a specific implementation. */
1419typedef struct psa_key_policy_s psa_key_policy_t;
1420
1421/** \brief Initialize a key policy structure to a default that forbids all
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001422 * usage of the key.
1423 *
1424 * \param[out] policy The policy object to initialize.
1425 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001426void psa_key_policy_init(psa_key_policy_t *policy);
1427
Gilles Peskine7e198532018-03-08 07:50:30 +01001428/** \brief Set the standard fields of a policy structure.
1429 *
1430 * Note that this function does not make any consistency check of the
1431 * parameters. The values are only checked when applying the policy to
1432 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001433 *
1434 * \param[out] policy The policy object to modify.
1435 * \param usage The permitted uses for the key.
1436 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +01001437 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001438void psa_key_policy_set_usage(psa_key_policy_t *policy,
1439 psa_key_usage_t usage,
1440 psa_algorithm_t alg);
1441
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001442/** \brief Retrieve the usage field of a policy structure.
1443 *
1444 * \param[in] policy The policy object to query.
1445 *
1446 * \return The permitted uses for a key with this policy.
1447 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001448psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001449
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001450/** \brief Retrieve the algorithm field of a policy structure.
1451 *
1452 * \param[in] policy The policy object to query.
1453 *
1454 * \return The permitted algorithm for a key with this policy.
1455 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001456psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001457
1458/** \brief Set the usage policy on a key slot.
1459 *
1460 * This function must be called on an empty key slot, before importing,
1461 * generating or creating a key in the slot. Changing the policy of an
1462 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +01001463 *
1464 * Implementations may set restrictions on supported key policies
1465 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001466 *
1467 * \param key The key slot whose policy is to be changed.
1468 * \param[in] policy The policy object to query.
1469 *
1470 * \retval #PSA_SUCCESS
1471 * \retval #PSA_ERROR_OCCUPIED_SLOT
1472 * \retval #PSA_ERROR_NOT_SUPPORTED
1473 * \retval #PSA_ERROR_INVALID_ARGUMENT
1474 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1475 * \retval #PSA_ERROR_HARDWARE_FAILURE
1476 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001477 */
1478psa_status_t psa_set_key_policy(psa_key_slot_t key,
1479 const psa_key_policy_t *policy);
1480
Gilles Peskine7e198532018-03-08 07:50:30 +01001481/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001482 *
1483 * \param key The key slot whose policy is being queried.
1484 * \param[out] policy On success, the key's policy.
1485 *
1486 * \retval #PSA_SUCCESS
1487 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1488 * \retval #PSA_ERROR_HARDWARE_FAILURE
1489 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7e198532018-03-08 07:50:30 +01001490 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001491psa_status_t psa_get_key_policy(psa_key_slot_t key,
1492 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +01001493
1494/**@}*/
1495
Gilles Peskine609b6a52018-03-03 21:31:50 +01001496/** \defgroup persistence Key lifetime
1497 * @{
1498 */
1499
1500/** Encoding of key lifetimes.
1501 */
1502typedef uint32_t psa_key_lifetime_t;
1503
1504/** A volatile key slot retains its content as long as the application is
1505 * running. It is guaranteed to be erased on a power reset.
1506 */
1507#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1508
1509/** A persistent key slot retains its content as long as it is not explicitly
1510 * destroyed.
1511 */
1512#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1513
1514/** A write-once key slot may not be modified once a key has been set.
1515 * It will retain its content as long as the device remains operational.
1516 */
1517#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
1518
Gilles Peskined393e182018-03-08 07:49:16 +01001519/** \brief Retrieve the lifetime of a key slot.
1520 *
1521 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001522 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001523 * \param key Slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001524 * \param[out] lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001525 *
Gilles Peskine28538492018-07-11 17:34:00 +02001526 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001527 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001528 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -07001529 * The key slot is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001530 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1531 * \retval #PSA_ERROR_HARDWARE_FAILURE
1532 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001533 */
Gilles Peskine609b6a52018-03-03 21:31:50 +01001534psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1535 psa_key_lifetime_t *lifetime);
1536
Gilles Peskined393e182018-03-08 07:49:16 +01001537/** \brief Change the lifetime of a key slot.
1538 *
1539 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +01001540 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +01001541 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001542 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001543 * \param key Slot whose lifetime is to be changed.
1544 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001545 *
Gilles Peskine28538492018-07-11 17:34:00 +02001546 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001547 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001548 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603804cd712018-03-20 22:44:08 +02001549 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -07001550 * or the lifetime value is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001551 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001552 * The implementation does not support the specified lifetime value,
1553 * at least for the specified key slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001554 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001555 * The slot contains a key, and the implementation does not support
1556 * changing the lifetime of an occupied slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001557 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1558 * \retval #PSA_ERROR_HARDWARE_FAILURE
1559 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskined393e182018-03-08 07:49:16 +01001560 */
1561psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -07001562 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +01001563
Gilles Peskine609b6a52018-03-03 21:31:50 +01001564/**@}*/
1565
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001566/** \defgroup hash Message digests
1567 * @{
1568 */
1569
Gilles Peskine308b91d2018-02-08 09:47:44 +01001570/** The type of the state data structure for multipart hash operations.
1571 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001572 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001573 * make any assumptions about the content of this structure except
1574 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001575typedef struct psa_hash_operation_s psa_hash_operation_t;
1576
Gilles Peskine308b91d2018-02-08 09:47:44 +01001577/** The size of the output of psa_hash_finish(), in bytes.
1578 *
1579 * This is also the hash size that psa_hash_verify() expects.
1580 *
1581 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001582 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
Gilles Peskinebe42f312018-07-13 14:38:15 +02001583 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
Gilles Peskine35855962018-04-19 08:39:16 +02001584 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001585 *
1586 * \return The hash size for the specified hash algorithm.
1587 * If the hash algorithm is not recognized, return 0.
1588 * An implementation may return either 0 or the correct size
1589 * for a hash algorithm that it recognizes, but does not support.
1590 */
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001591#define PSA_HASH_SIZE(alg) \
1592 ( \
1593 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD2 ? 16 : \
1594 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD4 ? 16 : \
1595 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_MD5 ? 16 : \
1596 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
1597 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
1598 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
1599 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
1600 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
1601 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
1602 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
1603 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
1604 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
1605 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
1606 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
1607 PSA_ALG_HMAC_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001608 0)
1609
Gilles Peskine308b91d2018-02-08 09:47:44 +01001610/** Start a multipart hash operation.
1611 *
1612 * The sequence of operations to calculate a hash (message digest)
1613 * is as follows:
1614 * -# Allocate an operation object which will be passed to all the functions
1615 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001616 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001617 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001618 * of the message each time. The hash that is calculated is the hash
1619 * of the concatenation of these messages in order.
1620 * -# To calculate the hash, call psa_hash_finish().
1621 * To compare the hash with an expected value, call psa_hash_verify().
1622 *
1623 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001624 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001625 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001626 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001627 * eventually terminate the operation. The following events terminate an
1628 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001629 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001630 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001631 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001632 * \param[out] operation The operation object to use.
1633 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1634 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001635 *
Gilles Peskine28538492018-07-11 17:34:00 +02001636 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001637 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001638 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001639 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001640 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1641 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1642 * \retval #PSA_ERROR_HARDWARE_FAILURE
1643 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001644 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001645psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001646 psa_algorithm_t alg);
1647
Gilles Peskine308b91d2018-02-08 09:47:44 +01001648/** Add a message fragment to a multipart hash operation.
1649 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001650 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001651 *
1652 * If this function returns an error status, the operation becomes inactive.
1653 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001654 * \param[in,out] operation Active hash operation.
1655 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001656 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001657 *
Gilles Peskine28538492018-07-11 17:34:00 +02001658 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001659 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001660 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001661 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001662 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1663 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1664 * \retval #PSA_ERROR_HARDWARE_FAILURE
1665 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001666 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001667psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1668 const uint8_t *input,
1669 size_t input_length);
1670
Gilles Peskine308b91d2018-02-08 09:47:44 +01001671/** Finish the calculation of the hash of a message.
1672 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001673 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001674 * This function calculates the hash of the message formed by concatenating
1675 * the inputs passed to preceding calls to psa_hash_update().
1676 *
1677 * When this function returns, the operation becomes inactive.
1678 *
1679 * \warning Applications should not call this function if they expect
1680 * a specific value for the hash. Call psa_hash_verify() instead.
1681 * Beware that comparing integrity or authenticity data such as
1682 * hash values with a function such as \c memcmp is risky
1683 * because the time taken by the comparison may leak information
1684 * about the hashed data which could allow an attacker to guess
1685 * a valid hash and thereby bypass security controls.
1686 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001687 * \param[in,out] operation Active hash operation.
1688 * \param[out] hash Buffer where the hash is to be written.
1689 * \param hash_size Size of the \p hash buffer in bytes.
1690 * \param[out] hash_length On success, the number of bytes
1691 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001692 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001693 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001694 *
Gilles Peskine28538492018-07-11 17:34:00 +02001695 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001696 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001697 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001698 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001699 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001700 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001701 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001702 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001703 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1704 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1705 * \retval #PSA_ERROR_HARDWARE_FAILURE
1706 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001707 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001708psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1709 uint8_t *hash,
1710 size_t hash_size,
1711 size_t *hash_length);
1712
Gilles Peskine308b91d2018-02-08 09:47:44 +01001713/** Finish the calculation of the hash of a message and compare it with
1714 * an expected value.
1715 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001716 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001717 * This function calculates the hash of the message formed by concatenating
1718 * the inputs passed to preceding calls to psa_hash_update(). It then
1719 * compares the calculated hash with the expected hash passed as a
1720 * parameter to this function.
1721 *
1722 * When this function returns, the operation becomes inactive.
1723 *
Gilles Peskine19067982018-03-20 17:54:53 +01001724 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001725 * comparison between the actual hash and the expected hash is performed
1726 * in constant time.
1727 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001728 * \param[in,out] operation Active hash operation.
1729 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001730 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001731 *
Gilles Peskine28538492018-07-11 17:34:00 +02001732 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001733 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001734 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001735 * The hash of the message was calculated successfully, but it
1736 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001737 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001738 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001739 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1740 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1741 * \retval #PSA_ERROR_HARDWARE_FAILURE
1742 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001743 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001744psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1745 const uint8_t *hash,
1746 size_t hash_length);
1747
Gilles Peskine308b91d2018-02-08 09:47:44 +01001748/** Abort a hash operation.
1749 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001750 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001751 * \p operation structure itself. Once aborted, the operation object
1752 * can be reused for another operation by calling
1753 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001754 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001755 * You may call this function any time after the operation object has
1756 * been initialized by any of the following methods:
1757 * - A call to psa_hash_setup(), whether it succeeds or not.
1758 * - Initializing the \c struct to all-bits-zero.
1759 * - Initializing the \c struct to logical zeros, e.g.
1760 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001761 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001762 * In particular, calling psa_hash_abort() after the operation has been
1763 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1764 * psa_hash_verify() is safe and has no effect.
1765 *
1766 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001767 *
Gilles Peskine28538492018-07-11 17:34:00 +02001768 * \retval #PSA_SUCCESS
1769 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001770 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001771 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1772 * \retval #PSA_ERROR_HARDWARE_FAILURE
1773 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001774 */
1775psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001776
1777/**@}*/
1778
Gilles Peskine8c9def32018-02-08 10:02:12 +01001779/** \defgroup MAC Message authentication codes
1780 * @{
1781 */
1782
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001783/** The type of the state data structure for multipart MAC operations.
1784 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001785 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001786 * make any assumptions about the content of this structure except
1787 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001788typedef struct psa_mac_operation_s psa_mac_operation_t;
1789
Gilles Peskine89167cb2018-07-08 20:12:23 +02001790/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001791 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001792 * This function sets up the calculation of the MAC
1793 * (message authentication code) of a byte string.
1794 * To verify the MAC of a message against an
1795 * expected value, use psa_mac_verify_setup() instead.
1796 *
1797 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001798 * -# Allocate an operation object which will be passed to all the functions
1799 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001800 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001801 * The key remains associated with the operation even if the content
1802 * of the key slot changes.
1803 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1804 * of the message each time. The MAC that is calculated is the MAC
1805 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001806 * -# At the end of the message, call psa_mac_sign_finish() to finish
1807 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001808 *
1809 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +02001810 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001811 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001812 * After a successful call to psa_mac_sign_setup(), the application must
1813 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001814 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001815 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001816 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001817 * \param[out] operation The operation object to use.
1818 * \param key Slot containing the key to use for the operation.
1819 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1820 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001821 *
Gilles Peskine28538492018-07-11 17:34:00 +02001822 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001823 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001824 * \retval #PSA_ERROR_EMPTY_SLOT
1825 * \retval #PSA_ERROR_NOT_PERMITTED
1826 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001827 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001828 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001829 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001830 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1831 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1832 * \retval #PSA_ERROR_HARDWARE_FAILURE
1833 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001834 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001835psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1836 psa_key_slot_t key,
1837 psa_algorithm_t alg);
1838
1839/** Start a multipart MAC verification operation.
1840 *
1841 * This function sets up the verification of the MAC
1842 * (message authentication code) of a byte string against an expected value.
1843 *
1844 * The sequence of operations to verify a MAC is as follows:
1845 * -# Allocate an operation object which will be passed to all the functions
1846 * listed here.
1847 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1848 * The key remains associated with the operation even if the content
1849 * of the key slot changes.
1850 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1851 * of the message each time. The MAC that is calculated is the MAC
1852 * of the concatenation of these messages in order.
1853 * -# At the end of the message, call psa_mac_verify_finish() to finish
1854 * calculating the actual MAC of the message and verify it against
1855 * the expected value.
1856 *
1857 * The application may call psa_mac_abort() at any time after the operation
1858 * has been initialized with psa_mac_verify_setup().
1859 *
1860 * After a successful call to psa_mac_verify_setup(), the application must
1861 * eventually terminate the operation through one of the following methods:
1862 * - A failed call to psa_mac_update().
1863 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1864 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001865 * \param[out] operation The operation object to use.
1866 * \param key Slot containing the key to use for the operation.
1867 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1868 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001869 *
Gilles Peskine28538492018-07-11 17:34:00 +02001870 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001871 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001872 * \retval #PSA_ERROR_EMPTY_SLOT
1873 * \retval #PSA_ERROR_NOT_PERMITTED
1874 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001875 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001876 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001877 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001878 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1879 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1880 * \retval #PSA_ERROR_HARDWARE_FAILURE
1881 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001882 */
1883psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
1884 psa_key_slot_t key,
1885 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001886
Gilles Peskinedcd14942018-07-12 00:30:52 +02001887/** Add a message fragment to a multipart MAC operation.
1888 *
1889 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1890 * before calling this function.
1891 *
1892 * If this function returns an error status, the operation becomes inactive.
1893 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001894 * \param[in,out] operation Active MAC operation.
1895 * \param[in] input Buffer containing the message fragment to add to
1896 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001897 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001898 *
1899 * \retval #PSA_SUCCESS
1900 * Success.
1901 * \retval #PSA_ERROR_BAD_STATE
1902 * The operation state is not valid (not started, or already completed).
1903 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1904 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1905 * \retval #PSA_ERROR_HARDWARE_FAILURE
1906 * \retval #PSA_ERROR_TAMPERING_DETECTED
1907 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001908psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1909 const uint8_t *input,
1910 size_t input_length);
1911
Gilles Peskinedcd14942018-07-12 00:30:52 +02001912/** Finish the calculation of the MAC of a message.
1913 *
1914 * The application must call psa_mac_sign_setup() before calling this function.
1915 * This function calculates the MAC of the message formed by concatenating
1916 * the inputs passed to preceding calls to psa_mac_update().
1917 *
1918 * When this function returns, the operation becomes inactive.
1919 *
1920 * \warning Applications should not call this function if they expect
1921 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1922 * Beware that comparing integrity or authenticity data such as
1923 * MAC values with a function such as \c memcmp is risky
1924 * because the time taken by the comparison may leak information
1925 * about the MAC value which could allow an attacker to guess
1926 * a valid MAC and thereby bypass security controls.
1927 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001928 * \param[in,out] operation Active MAC operation.
1929 * \param[out] mac Buffer where the MAC value is to be written.
1930 * \param mac_size Size of the \p mac buffer in bytes.
1931 * \param[out] mac_length On success, the number of bytes
1932 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001933 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001934 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001935 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001936 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001937 *
1938 * \retval #PSA_SUCCESS
1939 * Success.
1940 * \retval #PSA_ERROR_BAD_STATE
1941 * The operation state is not valid (not started, or already completed).
1942 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001943 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001944 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1945 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1946 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1947 * \retval #PSA_ERROR_HARDWARE_FAILURE
1948 * \retval #PSA_ERROR_TAMPERING_DETECTED
1949 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001950psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1951 uint8_t *mac,
1952 size_t mac_size,
1953 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001954
Gilles Peskinedcd14942018-07-12 00:30:52 +02001955/** Finish the calculation of the MAC of a message and compare it with
1956 * an expected value.
1957 *
1958 * The application must call psa_mac_verify_setup() before calling this function.
1959 * This function calculates the MAC of the message formed by concatenating
1960 * the inputs passed to preceding calls to psa_mac_update(). It then
1961 * compares the calculated MAC with the expected MAC passed as a
1962 * parameter to this function.
1963 *
1964 * When this function returns, the operation becomes inactive.
1965 *
1966 * \note Implementations shall make the best effort to ensure that the
1967 * comparison between the actual MAC and the expected MAC is performed
1968 * in constant time.
1969 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001970 * \param[in,out] operation Active MAC operation.
1971 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001972 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001973 *
1974 * \retval #PSA_SUCCESS
1975 * The expected MAC is identical to the actual MAC of the message.
1976 * \retval #PSA_ERROR_INVALID_SIGNATURE
1977 * The MAC of the message was calculated successfully, but it
1978 * differs from the expected MAC.
1979 * \retval #PSA_ERROR_BAD_STATE
1980 * The operation state is not valid (not started, or already completed).
1981 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1982 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1983 * \retval #PSA_ERROR_HARDWARE_FAILURE
1984 * \retval #PSA_ERROR_TAMPERING_DETECTED
1985 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001986psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1987 const uint8_t *mac,
1988 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001989
Gilles Peskinedcd14942018-07-12 00:30:52 +02001990/** Abort a MAC operation.
1991 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001992 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001993 * \p operation structure itself. Once aborted, the operation object
1994 * can be reused for another operation by calling
1995 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001996 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001997 * You may call this function any time after the operation object has
1998 * been initialized by any of the following methods:
1999 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
2000 * it succeeds or not.
2001 * - Initializing the \c struct to all-bits-zero.
2002 * - Initializing the \c struct to logical zeros, e.g.
2003 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002004 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002005 * In particular, calling psa_mac_abort() after the operation has been
2006 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
2007 * psa_mac_verify_finish() is safe and has no effect.
2008 *
2009 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002010 *
2011 * \retval #PSA_SUCCESS
2012 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002013 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002014 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2015 * \retval #PSA_ERROR_HARDWARE_FAILURE
2016 * \retval #PSA_ERROR_TAMPERING_DETECTED
2017 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002018psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
2019
2020/**@}*/
2021
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002022/** \defgroup cipher Symmetric ciphers
2023 * @{
2024 */
2025
2026/** The type of the state data structure for multipart cipher operations.
2027 *
2028 * This is an implementation-defined \c struct. Applications should not
2029 * make any assumptions about the content of this structure except
2030 * as directed by the documentation of a specific implementation. */
2031typedef struct psa_cipher_operation_s psa_cipher_operation_t;
2032
2033/** Set the key for a multipart symmetric encryption operation.
2034 *
2035 * The sequence of operations to encrypt a message with a symmetric cipher
2036 * is as follows:
2037 * -# Allocate an operation object which will be passed to all the functions
2038 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02002039 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002040 * The key remains associated with the operation even if the content
2041 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03002042 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002043 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03002044 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002045 * requires a specific IV value.
2046 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
2047 * of the message each time.
2048 * -# Call psa_cipher_finish().
2049 *
2050 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02002051 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002052 *
Gilles Peskinefe119512018-07-08 21:39:34 +02002053 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002054 * eventually terminate the operation. The following events terminate an
2055 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03002056 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002057 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002058 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002059 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002060 * \param[out] operation The operation object to use.
2061 * \param key Slot containing the key to use for the operation.
2062 * \param alg The cipher algorithm to compute
2063 * (\c PSA_ALG_XXX value such that
2064 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002065 *
Gilles Peskine28538492018-07-11 17:34:00 +02002066 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002067 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002068 * \retval #PSA_ERROR_EMPTY_SLOT
2069 * \retval #PSA_ERROR_NOT_PERMITTED
2070 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002071 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002072 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002073 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002074 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2075 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2076 * \retval #PSA_ERROR_HARDWARE_FAILURE
2077 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002078 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002079psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
2080 psa_key_slot_t key,
2081 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002082
2083/** Set the key for a multipart symmetric decryption operation.
2084 *
2085 * The sequence of operations to decrypt a message with a symmetric cipher
2086 * is as follows:
2087 * -# Allocate an operation object which will be passed to all the functions
2088 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02002089 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002090 * The key remains associated with the operation even if the content
2091 * of the key slot changes.
2092 * -# Call psa_cipher_update() with the IV (initialization vector) for the
2093 * decryption. If the IV is prepended to the ciphertext, you can call
2094 * psa_cipher_update() on a buffer containing the IV followed by the
2095 * beginning of the message.
2096 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
2097 * of the message each time.
2098 * -# Call psa_cipher_finish().
2099 *
2100 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02002101 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002102 *
Gilles Peskinefe119512018-07-08 21:39:34 +02002103 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002104 * eventually terminate the operation. The following events terminate an
2105 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002106 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002107 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002108 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002109 * \param[out] operation The operation object to use.
2110 * \param key Slot containing the key to use for the operation.
2111 * \param alg The cipher algorithm to compute
2112 * (\c PSA_ALG_XXX value such that
2113 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002114 *
Gilles Peskine28538492018-07-11 17:34:00 +02002115 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002116 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002117 * \retval #PSA_ERROR_EMPTY_SLOT
2118 * \retval #PSA_ERROR_NOT_PERMITTED
2119 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002120 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002121 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002122 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002123 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2124 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2125 * \retval #PSA_ERROR_HARDWARE_FAILURE
2126 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002127 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002128psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
2129 psa_key_slot_t key,
2130 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002131
Gilles Peskinedcd14942018-07-12 00:30:52 +02002132/** Generate an IV for a symmetric encryption operation.
2133 *
2134 * This function generates a random IV (initialization vector), nonce
2135 * or initial counter value for the encryption operation as appropriate
2136 * for the chosen algorithm, key type and key size.
2137 *
2138 * The application must call psa_cipher_encrypt_setup() before
2139 * calling this function.
2140 *
2141 * If this function returns an error status, the operation becomes inactive.
2142 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002143 * \param[in,out] operation Active cipher operation.
2144 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002145 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002146 * \param[out] iv_length On success, the number of bytes of the
2147 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002148 *
2149 * \retval #PSA_SUCCESS
2150 * Success.
2151 * \retval #PSA_ERROR_BAD_STATE
2152 * The operation state is not valid (not started, or IV already set).
2153 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002154 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002155 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2156 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2157 * \retval #PSA_ERROR_HARDWARE_FAILURE
2158 * \retval #PSA_ERROR_TAMPERING_DETECTED
2159 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002160psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
2161 unsigned char *iv,
2162 size_t iv_size,
2163 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002164
Gilles Peskinedcd14942018-07-12 00:30:52 +02002165/** Set the IV for a symmetric encryption or decryption operation.
2166 *
2167 * This function sets the random IV (initialization vector), nonce
2168 * or initial counter value for the encryption or decryption operation.
2169 *
2170 * The application must call psa_cipher_encrypt_setup() before
2171 * calling this function.
2172 *
2173 * If this function returns an error status, the operation becomes inactive.
2174 *
2175 * \note When encrypting, applications should use psa_cipher_generate_iv()
2176 * instead of this function, unless implementing a protocol that requires
2177 * a non-random IV.
2178 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002179 * \param[in,out] operation Active cipher operation.
2180 * \param[in] iv Buffer containing the IV to use.
2181 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002182 *
2183 * \retval #PSA_SUCCESS
2184 * Success.
2185 * \retval #PSA_ERROR_BAD_STATE
2186 * The operation state is not valid (not started, or IV already set).
2187 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002188 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02002189 * or the chosen algorithm does not use an IV.
2190 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2191 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2192 * \retval #PSA_ERROR_HARDWARE_FAILURE
2193 * \retval #PSA_ERROR_TAMPERING_DETECTED
2194 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002195psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
2196 const unsigned char *iv,
2197 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002198
Gilles Peskinedcd14942018-07-12 00:30:52 +02002199/** Encrypt or decrypt a message fragment in an active cipher operation.
2200 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02002201 * Before calling this function, you must:
2202 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
2203 * The choice of setup function determines whether this function
2204 * encrypts or decrypts its input.
2205 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
2206 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002207 *
2208 * If this function returns an error status, the operation becomes inactive.
2209 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002210 * \param[in,out] operation Active cipher operation.
2211 * \param[in] input Buffer containing the message fragment to
2212 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002213 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002214 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002215 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002216 * \param[out] output_length On success, the number of bytes
2217 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002218 *
2219 * \retval #PSA_SUCCESS
2220 * Success.
2221 * \retval #PSA_ERROR_BAD_STATE
2222 * The operation state is not valid (not started, IV required but
2223 * not set, or already completed).
2224 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2225 * The size of the \p output buffer is too small.
2226 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2227 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2228 * \retval #PSA_ERROR_HARDWARE_FAILURE
2229 * \retval #PSA_ERROR_TAMPERING_DETECTED
2230 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002231psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2232 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002233 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002234 unsigned char *output,
2235 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002236 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002237
Gilles Peskinedcd14942018-07-12 00:30:52 +02002238/** Finish encrypting or decrypting a message in a cipher operation.
2239 *
2240 * The application must call psa_cipher_encrypt_setup() or
2241 * psa_cipher_decrypt_setup() before calling this function. The choice
2242 * of setup function determines whether this function encrypts or
2243 * decrypts its input.
2244 *
2245 * This function finishes the encryption or decryption of the message
2246 * formed by concatenating the inputs passed to preceding calls to
2247 * psa_cipher_update().
2248 *
2249 * When this function returns, the operation becomes inactive.
2250 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002251 * \param[in,out] operation Active cipher operation.
2252 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002253 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002254 * \param[out] output_length On success, the number of bytes
2255 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002256 *
2257 * \retval #PSA_SUCCESS
2258 * Success.
2259 * \retval #PSA_ERROR_BAD_STATE
2260 * The operation state is not valid (not started, IV required but
2261 * not set, or already completed).
2262 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2263 * The size of the \p output buffer is too small.
2264 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2265 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2266 * \retval #PSA_ERROR_HARDWARE_FAILURE
2267 * \retval #PSA_ERROR_TAMPERING_DETECTED
2268 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002269psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002270 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002271 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002272 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002273
Gilles Peskinedcd14942018-07-12 00:30:52 +02002274/** Abort a cipher operation.
2275 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002276 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002277 * \p operation structure itself. Once aborted, the operation object
2278 * can be reused for another operation by calling
2279 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002280 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002281 * You may call this function any time after the operation object has
2282 * been initialized by any of the following methods:
2283 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2284 * whether it succeeds or not.
2285 * - Initializing the \c struct to all-bits-zero.
2286 * - Initializing the \c struct to logical zeros, e.g.
2287 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002288 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002289 * In particular, calling psa_cipher_abort() after the operation has been
2290 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2291 * is safe and has no effect.
2292 *
2293 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002294 *
2295 * \retval #PSA_SUCCESS
2296 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002297 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002298 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2299 * \retval #PSA_ERROR_HARDWARE_FAILURE
2300 * \retval #PSA_ERROR_TAMPERING_DETECTED
2301 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002302psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2303
2304/**@}*/
2305
Gilles Peskine3b555712018-03-03 21:27:57 +01002306/** \defgroup aead Authenticated encryption with associated data (AEAD)
2307 * @{
2308 */
2309
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002310/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01002311 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002312 * \param alg An AEAD algorithm
2313 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002314 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002315 *
2316 * \return The tag size for the specified algorithm.
2317 * If the AEAD algorithm does not have an identified
2318 * tag that can be distinguished from the rest of
2319 * the ciphertext, return 0.
2320 * If the AEAD algorithm is not recognized, return 0.
2321 * An implementation may return either 0 or a
2322 * correct size for an AEAD algorithm that it
2323 * recognizes, but does not support.
2324 */
2325#define PSA_AEAD_TAG_SIZE(alg) \
2326 ((alg) == PSA_ALG_GCM ? 16 : \
2327 (alg) == PSA_ALG_CCM ? 16 : \
2328 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01002329
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002330/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002331 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002332 * \param key Slot containing the key to use.
2333 * \param alg The AEAD algorithm to compute
2334 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002335 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002336 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002337 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002338 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002339 * but not encrypted.
2340 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002341 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002342 * encrypted.
2343 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002344 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002345 * encrypted data. The additional data is not
2346 * part of this output. For algorithms where the
2347 * encrypted data and the authentication tag
2348 * are defined as separate outputs, the
2349 * authentication tag is appended to the
2350 * encrypted data.
2351 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2352 * This must be at least
2353 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2354 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002355 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002356 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002357 *
Gilles Peskine28538492018-07-11 17:34:00 +02002358 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002359 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002360 * \retval #PSA_ERROR_EMPTY_SLOT
2361 * \retval #PSA_ERROR_NOT_PERMITTED
2362 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002363 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002364 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002365 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002366 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2367 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2368 * \retval #PSA_ERROR_HARDWARE_FAILURE
2369 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01002370 */
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002371psa_status_t psa_aead_encrypt(psa_key_slot_t key,
2372 psa_algorithm_t alg,
2373 const uint8_t *nonce,
2374 size_t nonce_length,
2375 const uint8_t *additional_data,
2376 size_t additional_data_length,
2377 const uint8_t *plaintext,
2378 size_t plaintext_length,
2379 uint8_t *ciphertext,
2380 size_t ciphertext_size,
2381 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002382
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002383/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002384 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002385 * \param key Slot containing the key to use.
2386 * \param alg The AEAD algorithm to compute
2387 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002388 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002389 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002390 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002391 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002392 * but not encrypted.
2393 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002394 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002395 * encrypted. For algorithms where the
2396 * encrypted data and the authentication tag
2397 * are defined as separate inputs, the buffer
2398 * must contain the encrypted data followed
2399 * by the authentication tag.
2400 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002401 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002402 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2403 * This must be at least
2404 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2405 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002406 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03002407 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002408 *
Gilles Peskine28538492018-07-11 17:34:00 +02002409 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002410 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002411 * \retval #PSA_ERROR_EMPTY_SLOT
2412 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002413 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002414 * \retval #PSA_ERROR_NOT_PERMITTED
2415 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002416 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002417 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002418 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002419 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2420 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2421 * \retval #PSA_ERROR_HARDWARE_FAILURE
2422 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine3b555712018-03-03 21:27:57 +01002423 */
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002424psa_status_t psa_aead_decrypt(psa_key_slot_t key,
2425 psa_algorithm_t alg,
2426 const uint8_t *nonce,
2427 size_t nonce_length,
2428 const uint8_t *additional_data,
2429 size_t additional_data_length,
2430 const uint8_t *ciphertext,
2431 size_t ciphertext_length,
2432 uint8_t *plaintext,
2433 size_t plaintext_size,
2434 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002435
2436/**@}*/
2437
Gilles Peskine20035e32018-02-03 22:44:14 +01002438/** \defgroup asymmetric Asymmetric cryptography
2439 * @{
2440 */
2441
2442/**
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002443 * \brief ECDSA signature size for a given curve bit size
Gilles Peskine0189e752018-02-03 23:57:22 +01002444 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002445 * \param curve_bits Curve size in bits.
2446 * \return Signature size in bytes.
Gilles Peskine0189e752018-02-03 23:57:22 +01002447 *
2448 * \note This macro returns a compile-time constant if its argument is one.
Gilles Peskine0189e752018-02-03 23:57:22 +01002449 */
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002450#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
2451 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskine0189e752018-02-03 23:57:22 +01002452
Gilles Peskine0189e752018-02-03 23:57:22 +01002453/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002454 * \brief Sign a hash or short message with a private key.
2455 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002456 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002457 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002458 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2459 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2460 * to determine the hash algorithm to use.
2461 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002462 * \param key Key slot containing an asymmetric key pair.
2463 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002464 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002465 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002466 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002467 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002468 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002469 * \param[out] signature_length On success, the number of bytes
2470 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002471 *
Gilles Peskine28538492018-07-11 17:34:00 +02002472 * \retval #PSA_SUCCESS
2473 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002474 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002475 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002476 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002477 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002478 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002479 * \retval #PSA_ERROR_NOT_SUPPORTED
2480 * \retval #PSA_ERROR_INVALID_ARGUMENT
2481 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2482 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2483 * \retval #PSA_ERROR_HARDWARE_FAILURE
2484 * \retval #PSA_ERROR_TAMPERING_DETECTED
2485 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine20035e32018-02-03 22:44:14 +01002486 */
2487psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
2488 psa_algorithm_t alg,
2489 const uint8_t *hash,
2490 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002491 uint8_t *signature,
2492 size_t signature_size,
2493 size_t *signature_length);
2494
2495/**
2496 * \brief Verify the signature a hash or short message using a public key.
2497 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002498 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002499 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002500 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2501 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2502 * to determine the hash algorithm to use.
2503 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01002504 * \param key Key slot containing a public key or an
2505 * asymmetric key pair.
2506 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002507 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002508 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002509 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002510 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002511 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002512 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002513 *
Gilles Peskine28538492018-07-11 17:34:00 +02002514 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002515 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002516 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002517 * The calculation was perfomed successfully, but the passed
2518 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002519 * \retval #PSA_ERROR_NOT_SUPPORTED
2520 * \retval #PSA_ERROR_INVALID_ARGUMENT
2521 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2522 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2523 * \retval #PSA_ERROR_HARDWARE_FAILURE
2524 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine20035e32018-02-03 22:44:14 +01002525 */
2526psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
2527 psa_algorithm_t alg,
2528 const uint8_t *hash,
2529 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002530 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002531 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002532
Gilles Peskine723feff2018-05-31 20:08:13 +02002533#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
Gilles Peskine072ac562018-06-30 00:21:29 +02002534 (PSA_ALG_IS_RSA_OAEP(alg) ? \
2535 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskine723feff2018-05-31 20:08:13 +02002536 11 /*PKCS#1v1.5*/)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002537
2538/**
2539 * \brief Encrypt a short message with a public key.
2540 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002541 * \param key Key slot containing a public key or an
2542 * asymmetric key pair.
2543 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002544 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002545 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002546 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002547 * \param[in] salt A salt or label, if supported by the
2548 * encryption algorithm.
2549 * If the algorithm does not support a
2550 * salt, pass \c NULL.
2551 * If the algorithm supports an optional
2552 * salt and you do not want to pass a salt,
2553 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002554 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002555 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2556 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002557 * \param salt_length Size of the \p salt buffer in bytes.
2558 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002559 * \param[out] output Buffer where the encrypted message is to
2560 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002561 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002562 * \param[out] output_length On success, the number of bytes
2563 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002564 *
Gilles Peskine28538492018-07-11 17:34:00 +02002565 * \retval #PSA_SUCCESS
2566 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002567 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002568 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002569 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002570 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002571 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002572 * \retval #PSA_ERROR_NOT_SUPPORTED
2573 * \retval #PSA_ERROR_INVALID_ARGUMENT
2574 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2575 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2576 * \retval #PSA_ERROR_HARDWARE_FAILURE
2577 * \retval #PSA_ERROR_TAMPERING_DETECTED
2578 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002579 */
2580psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
2581 psa_algorithm_t alg,
2582 const uint8_t *input,
2583 size_t input_length,
2584 const uint8_t *salt,
2585 size_t salt_length,
2586 uint8_t *output,
2587 size_t output_size,
2588 size_t *output_length);
2589
2590/**
2591 * \brief Decrypt a short message with a private key.
2592 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002593 * \param key Key slot containing an asymmetric key pair.
2594 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002595 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002596 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002597 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002598 * \param[in] salt A salt or label, if supported by the
2599 * encryption algorithm.
2600 * If the algorithm does not support a
2601 * salt, pass \c NULL.
2602 * If the algorithm supports an optional
2603 * salt and you do not want to pass a salt,
2604 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002605 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002606 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2607 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002608 * \param salt_length Size of the \p salt buffer in bytes.
2609 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002610 * \param[out] output Buffer where the decrypted message is to
2611 * be written.
2612 * \param output_size Size of the \c output buffer in bytes.
2613 * \param[out] output_length On success, the number of bytes
2614 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002615 *
Gilles Peskine28538492018-07-11 17:34:00 +02002616 * \retval #PSA_SUCCESS
2617 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002618 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002619 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002620 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002621 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002622 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002623 * \retval #PSA_ERROR_NOT_SUPPORTED
2624 * \retval #PSA_ERROR_INVALID_ARGUMENT
2625 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2626 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2627 * \retval #PSA_ERROR_HARDWARE_FAILURE
2628 * \retval #PSA_ERROR_TAMPERING_DETECTED
2629 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2630 * \retval #PSA_ERROR_INVALID_PADDING
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002631 */
2632psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
2633 psa_algorithm_t alg,
2634 const uint8_t *input,
2635 size_t input_length,
2636 const uint8_t *salt,
2637 size_t salt_length,
2638 uint8_t *output,
2639 size_t output_size,
2640 size_t *output_length);
2641
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002642/**@}*/
2643
Gilles Peskineedd76872018-07-20 17:42:05 +02002644/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002645 * @{
2646 */
2647
2648/** The type of the state data structure for generators.
2649 *
2650 * Before calling any function on a generator, the application must
2651 * initialize it by any of the following means:
2652 * - Set the structure to all-bits-zero, for example:
2653 * \code
2654 * psa_crypto_generator_t generator;
2655 * memset(&generator, 0, sizeof(generator));
2656 * \endcode
2657 * - Initialize the structure to logical zero values, for example:
2658 * \code
2659 * psa_crypto_generator_t generator = {0};
2660 * \endcode
2661 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2662 * for example:
2663 * \code
2664 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2665 * \endcode
2666 * - Assign the result of the function psa_crypto_generator_init()
2667 * to the structure, for example:
2668 * \code
2669 * psa_crypto_generator_t generator;
2670 * generator = psa_crypto_generator_init();
2671 * \endcode
2672 *
2673 * This is an implementation-defined \c struct. Applications should not
2674 * make any assumptions about the content of this structure except
2675 * as directed by the documentation of a specific implementation.
2676 */
2677typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2678
2679/** \def PSA_CRYPTO_GENERATOR_INIT
2680 *
2681 * This macro returns a suitable initializer for a generator object
2682 * of type #psa_crypto_generator_t.
2683 */
2684#ifdef __DOXYGEN_ONLY__
2685/* This is an example definition for documentation purposes.
2686 * Implementations should define a suitable value in `crypto_struct.h`.
2687 */
2688#define PSA_CRYPTO_GENERATOR_INIT {0}
2689#endif
2690
2691/** Return an initial value for a generator object.
2692 */
2693static psa_crypto_generator_t psa_crypto_generator_init(void);
2694
2695/** Retrieve the current capacity of a generator.
2696 *
2697 * The capacity of a generator is the maximum number of bytes that it can
2698 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2699 *
2700 * \param[in] generator The generator to query.
2701 * \param[out] capacity On success, the capacity of the generator.
2702 *
2703 * \retval PSA_SUCCESS
2704 * \retval PSA_ERROR_BAD_STATE
2705 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2706 */
2707psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2708 size_t *capacity);
2709
2710/** Read some data from a generator.
2711 *
2712 * This function reads and returns a sequence of bytes from a generator.
2713 * The data that is read is discarded from the generator. The generator's
2714 * capacity is decreased by the number of bytes read.
2715 *
2716 * \param[in,out] generator The generator object to read from.
2717 * \param[out] output Buffer where the generator output will be
2718 * written.
2719 * \param output_length Number of bytes to output.
2720 *
2721 * \retval PSA_SUCCESS
2722 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2723 * There were fewer than \p output_length bytes
2724 * in the generator. Note that in this case, no
2725 * output is written to the output buffer.
2726 * The generator's capacity is set to 0, thus
2727 * subsequent calls to this function will not
2728 * succeed, even with a smaller output buffer.
2729 * \retval PSA_ERROR_BAD_STATE
2730 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2731 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2732 * \retval PSA_ERROR_HARDWARE_FAILURE
2733 * \retval PSA_ERROR_TAMPERING_DETECTED
2734 */
2735psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2736 uint8_t *output,
2737 size_t output_length);
2738
2739/** Create a symmetric key from data read from a generator.
2740 *
2741 * This function reads a sequence of bytes from a generator and imports
2742 * these bytes as a key.
2743 * The data that is read is discarded from the generator. The generator's
2744 * capacity is decreased by the number of bytes read.
2745 *
2746 * This function is equivalent to calling #psa_generator_read and
2747 * passing the resulting output to #psa_import_key, but
2748 * if the implementation provides an isolation boundary then
2749 * the key material is not exposed outside the isolation boundary.
2750 *
2751 * \param key Slot where the key will be stored. This must be a
2752 * valid slot for a key of the chosen type. It must
2753 * be unoccupied.
2754 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2755 * This must be a symmetric key type.
2756 * \param bits Key size in bits.
2757 * \param[in,out] generator The generator object to read from.
2758 *
2759 * \retval PSA_SUCCESS
2760 * Success.
2761 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2762 * There were fewer than \p output_length bytes
2763 * in the generator. Note that in this case, no
2764 * output is written to the output buffer.
2765 * The generator's capacity is set to 0, thus
2766 * subsequent calls to this function will not
2767 * succeed, even with a smaller output buffer.
2768 * \retval PSA_ERROR_NOT_SUPPORTED
2769 * The key type or key size is not supported, either by the
2770 * implementation in general or in this particular slot.
2771 * \retval PSA_ERROR_BAD_STATE
2772 * \retval PSA_ERROR_INVALID_ARGUMENT
2773 * The key slot is invalid.
2774 * \retval PSA_ERROR_OCCUPIED_SLOT
2775 * There is already a key in the specified slot.
2776 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2777 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
2778 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2779 * \retval PSA_ERROR_HARDWARE_FAILURE
2780 * \retval PSA_ERROR_TAMPERING_DETECTED
2781 */
2782psa_status_t psa_generator_import_key(psa_key_slot_t key,
2783 psa_key_type_t type,
2784 size_t bits,
2785 psa_crypto_generator_t *generator);
2786
2787/** Abort a generator.
2788 *
2789 * Once a generator has been aborted, its capacity is zero.
2790 * Aborting a generator frees all associated resources except for the
2791 * \c generator structure itself.
2792 *
2793 * This function may be called at any time as long as the generator
2794 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2795 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2796 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2797 * on a generator that has not been set up.
2798 *
2799 * Once aborted, the generator object may be called.
2800 *
2801 * \param[in,out] generator The generator to abort.
2802 *
2803 * \retval PSA_SUCCESS
2804 * \retval PSA_ERROR_BAD_STATE
2805 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2806 * \retval PSA_ERROR_HARDWARE_FAILURE
2807 * \retval PSA_ERROR_TAMPERING_DETECTED
2808 */
2809psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2810
2811/**@}*/
2812
Gilles Peskineea0fb492018-07-12 17:17:20 +02002813/** \defgroup derivation Key derivation
2814 * @{
2815 */
2816
2817/** Set up a key derivation operation.
2818 *
2819 * A key derivation algorithm takes three inputs: a secret input \p key and
2820 * two non-secret inputs \p label and p salt.
2821 * The result of this function is a byte generator which can
2822 * be used to produce keys and other cryptographic material.
2823 *
2824 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002825 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2826 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002827 *
2828 * \param[in,out] generator The generator object to set up. It must
2829 * have been initialized to .
2830 * \param key Slot containing the secret key to use.
2831 * \param alg The key derivation algorithm to compute
2832 * (\c PSA_ALG_XXX value such that
2833 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2834 * \param[in] salt Salt to use.
2835 * \param salt_length Size of the \p salt buffer in bytes.
2836 * \param[in] label Label to use.
2837 * \param label_length Size of the \p label buffer in bytes.
2838 * \param capacity The maximum number of bytes that the
2839 * generator will be able to provide.
2840 *
2841 * \retval #PSA_SUCCESS
2842 * Success.
2843 * \retval #PSA_ERROR_EMPTY_SLOT
2844 * \retval #PSA_ERROR_NOT_PERMITTED
2845 * \retval #PSA_ERROR_INVALID_ARGUMENT
2846 * \c key is not compatible with \c alg,
2847 * or \p capacity is too large for the specified algorithm and key.
2848 * \retval #PSA_ERROR_NOT_SUPPORTED
2849 * \c alg is not supported or is not a key derivation algorithm.
2850 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2851 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2852 * \retval #PSA_ERROR_HARDWARE_FAILURE
2853 * \retval #PSA_ERROR_TAMPERING_DETECTED
2854 */
2855psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01002856 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002857 psa_algorithm_t alg,
2858 const uint8_t *salt,
2859 size_t salt_length,
2860 const uint8_t *label,
2861 size_t label_length,
2862 size_t capacity);
2863
2864/**@}*/
2865
Gilles Peskineedd76872018-07-20 17:42:05 +02002866/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002867 * @{
2868 */
2869
2870/**
2871 * \brief Generate random bytes.
2872 *
2873 * \warning This function **can** fail! Callers MUST check the return status
2874 * and MUST NOT use the content of the output buffer if the return
2875 * status is not #PSA_SUCCESS.
2876 *
2877 * \note To generate a key, use psa_generate_key() instead.
2878 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002879 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002880 * \param output_size Number of bytes to generate and output.
2881 *
Gilles Peskine28538492018-07-11 17:34:00 +02002882 * \retval #PSA_SUCCESS
2883 * \retval #PSA_ERROR_NOT_SUPPORTED
2884 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2885 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2886 * \retval #PSA_ERROR_HARDWARE_FAILURE
2887 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002888 */
2889psa_status_t psa_generate_random(uint8_t *output,
2890 size_t output_size);
2891
Gilles Peskine4c317f42018-07-12 01:24:09 +02002892/** Extra parameters for RSA key generation.
2893 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002894 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002895 * parameter to psa_generate_key().
2896 */
2897typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002898 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002899} psa_generate_key_extra_rsa;
2900
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002901/**
2902 * \brief Generate a key or key pair.
2903 *
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002904 * \param key Slot where the key will be stored. This must be a
2905 * valid slot for a key of the chosen type. It must
2906 * be unoccupied.
2907 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2908 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002909 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002910 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002911 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002912 * default parameters. Implementation that support
2913 * the generation of vendor-specific key types
2914 * that allow extra parameters shall document
2915 * the format of these extra parameters and
2916 * the default values. For standard parameters,
2917 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002918 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002919 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2920 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002921 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002922 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2923 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002924 * - For an RSA key (\p type is
2925 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2926 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002927 * specifying the public exponent. The
2928 * default public exponent used when \p extra
2929 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002930 * \param extra_size Size of the buffer that \p extra
2931 * points to, in bytes. Note that if \p extra is
2932 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002933 *
Gilles Peskine28538492018-07-11 17:34:00 +02002934 * \retval #PSA_SUCCESS
2935 * \retval #PSA_ERROR_NOT_SUPPORTED
2936 * \retval #PSA_ERROR_INVALID_ARGUMENT
2937 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2938 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2939 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2940 * \retval #PSA_ERROR_HARDWARE_FAILURE
2941 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002942 */
2943psa_status_t psa_generate_key(psa_key_slot_t key,
2944 psa_key_type_t type,
2945 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002946 const void *extra,
2947 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002948
2949/**@}*/
2950
Gilles Peskinee59236f2018-01-27 23:32:46 +01002951#ifdef __cplusplus
2952}
2953#endif
2954
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002955/* The file "crypto_sizes.h" contains definitions for size calculation
2956 * macros whose definitions are implementation-specific. */
2957#include "crypto_sizes.h"
2958
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002959/* The file "crypto_struct.h" contains definitions for
2960 * implementation-specific structs that are declared above. */
2961#include "crypto_struct.h"
2962
2963/* The file "crypto_extra.h" contains vendor-specific definitions. This
2964 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002965#include "crypto_extra.h"
2966
2967#endif /* PSA_CRYPTO_H */