blob: 48c971351184d834c2325070552bcdd67890c2f5 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010039/** \brief Key slot number.
40 *
41 * This type represents key slots. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043 * 0 is not a valid key slot number. The meaning of other values is
44 * implementation dependent.
45 *
46 * At any given point in time, each key slot either contains a
47 * cryptographic object, or is empty. Key slots are persistent:
48 * once set, the cryptographic object remains in the key slot until
49 * explicitly destroyed.
50 */
51typedef _unsigned_integral_type_ psa_key_slot_t;
52
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010053/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010054#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010055
Gilles Peskinee59236f2018-01-27 23:32:46 +010056#ifdef __cplusplus
57extern "C" {
58#endif
59
60/** \defgroup basic Basic definitions
61 * @{
62 */
63
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020064#if defined(PSA_SUCCESS)
65/* If PSA_SUCCESS is defined, assume that PSA crypto is being used
66 * together with PSA IPC, which also defines the identifier
67 * PSA_SUCCESS. We must not define PSA_SUCCESS ourselves in that case;
68 * the other error code names don't clash. Also define psa_status_t as
69 * an alias for the type used by PSA IPC. This is a temporary hack
mohammad160313f43942018-08-05 12:09:44 +030070 * until we unify error reporting in PSA IPC and PSA crypto.
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020071 *
72 * Note that psa_defs.h must be included before this header!
73 */
74typedef psa_error_t psa_status_t;
75
76#else /* defined(PSA_SUCCESS) */
77
Gilles Peskinee59236f2018-01-27 23:32:46 +010078/**
79 * \brief Function return status.
80 *
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020081 * This is either #PSA_SUCCESS (which is zero), indicating success,
82 * or a nonzero value indicating that an error occurred. Errors are
83 * encoded as one of the \c PSA_ERROR_xxx values defined here.
Gilles Peskinee59236f2018-01-27 23:32:46 +010084 */
itayzafrirc2a79762018-06-18 16:20:16 +030085typedef int32_t psa_status_t;
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020086
itayzafrirc2a79762018-06-18 16:20:16 +030087/** The action was completed successfully. */
88#define PSA_SUCCESS ((psa_status_t)0)
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020089
90#endif /* !defined(PSA_SUCCESS) */
itayzafrirc2a79762018-06-18 16:20:16 +030091
itayzafrirf26dbfc2018-08-01 16:09:08 +030092/** An error occurred that does not correspond to any defined
93 * failure cause.
94 *
95 * Implementations may use this error code if none of the other standard
96 * error codes are applicable. */
97#define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)1)
98
itayzafrirc2a79762018-06-18 16:20:16 +030099/** The requested operation or a parameter is not supported
100 * by this implementation.
101 *
102 * Implementations should return this error code when an enumeration
103 * parameter such as a key type, algorithm, etc. is not recognized.
104 * If a combination of parameters is recognized and identified as
105 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300106#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)2)
itayzafrirc2a79762018-06-18 16:20:16 +0300107
108/** The requested action is denied by a policy.
109 *
110 * Implementations should return this error code when the parameters
111 * are recognized as valid and supported, and a policy explicitly
112 * denies the requested operation.
113 *
114 * If a subset of the parameters of a function call identify a
115 * forbidden operation, and another subset of the parameters are
116 * not valid or not supported, it is unspecified whether the function
117 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
118 * #PSA_ERROR_INVALID_ARGUMENT. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300119#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)3)
itayzafrirc2a79762018-06-18 16:20:16 +0300120
121/** An output buffer is too small.
122 *
Gilles Peskinebe42f312018-07-13 14:38:15 +0200123 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
itayzafrirc2a79762018-06-18 16:20:16 +0300124 * description to determine a sufficient buffer size.
125 *
126 * Implementations should preferably return this error code only
127 * in cases when performing the operation with a larger output
128 * buffer would succeed. However implementations may return this
129 * error if a function has invalid or unsupported parameters in addition
130 * to the parameters that determine the necessary output buffer size. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300131#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)4)
itayzafrirc2a79762018-06-18 16:20:16 +0300132
133/** A slot is occupied, but must be empty to carry out the
134 * requested action.
135 *
136 * If the slot number is invalid (i.e. the requested action could
137 * not be performed even after erasing the slot's content),
138 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300139#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)5)
itayzafrirc2a79762018-06-18 16:20:16 +0300140
141/** A slot is empty, but must be occupied to carry out the
142 * requested action.
143 *
144 * If the slot number is invalid (i.e. the requested action could
145 * not be performed even after creating appropriate content in the slot),
146 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300147#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)6)
itayzafrirc2a79762018-06-18 16:20:16 +0300148
149/** The requested action cannot be performed in the current state.
150 *
151 * Multipart operations return this error when one of the
152 * functions is called out of sequence. Refer to the function
153 * descriptions for permitted sequencing of functions.
154 *
155 * Implementations shall not return this error code to indicate
156 * that a key slot is occupied when it needs to be free or vice versa,
157 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
158 * as applicable. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300159#define PSA_ERROR_BAD_STATE ((psa_status_t)7)
itayzafrirc2a79762018-06-18 16:20:16 +0300160
161/** The parameters passed to the function are invalid.
162 *
163 * Implementations may return this error any time a parameter or
164 * combination of parameters are recognized as invalid.
165 *
166 * Implementations shall not return this error code to indicate
167 * that a key slot is occupied when it needs to be free or vice versa,
168 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
169 * as applicable. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300170#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)8)
itayzafrirc2a79762018-06-18 16:20:16 +0300171
172/** There is not enough runtime memory.
173 *
174 * If the action is carried out across multiple security realms, this
175 * error can refer to available memory in any of the security realms. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300176#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)9)
itayzafrirc2a79762018-06-18 16:20:16 +0300177
178/** There is not enough persistent storage.
179 *
180 * Functions that modify the key storage return this error code if
181 * there is insufficient storage space on the host media. In addition,
182 * many functions that do not otherwise access storage may return this
183 * error code if the implementation requires a mandatory log entry for
184 * the requested action and the log storage space is full. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300185#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)10)
itayzafrirc2a79762018-06-18 16:20:16 +0300186
187/** There was a communication failure inside the implementation.
188 *
189 * This can indicate a communication failure between the application
190 * and an external cryptoprocessor or between the cryptoprocessor and
191 * an external volatile or persistent memory. A communication failure
192 * may be transient or permanent depending on the cause.
193 *
194 * \warning If a function returns this error, it is undetermined
195 * whether the requested action has completed or not. Implementations
196 * should return #PSA_SUCCESS on successful completion whenver
197 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
198 * if the requested action was completed successfully in an external
199 * cryptoprocessor but there was a breakdown of communication before
200 * the cryptoprocessor could report the status to the application.
201 */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300202#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)11)
itayzafrirc2a79762018-06-18 16:20:16 +0300203
204/** There was a storage failure that may have led to data loss.
205 *
206 * This error indicates that some persistent storage is corrupted.
207 * It should not be used for a corruption of volatile memory
208 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
209 * between the cryptoprocessor and its external storage (use
210 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
211 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
212 *
213 * Note that a storage failure does not indicate that any data that was
214 * previously read is invalid. However this previously read data may no
215 * longer be readable from storage.
216 *
217 * When a storage failure occurs, it is no longer possible to ensure
218 * the global integrity of the keystore. Depending on the global
219 * integrity guarantees offered by the implementation, access to other
220 * data may or may not fail even if the data is still readable but
221 * its integrity canont be guaranteed.
222 *
223 * Implementations should only use this error code to report a
224 * permanent storage corruption. However application writers should
225 * keep in mind that transient errors while reading the storage may be
226 * reported using this error code. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300227#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)12)
itayzafrirc2a79762018-06-18 16:20:16 +0300228
229/** A hardware failure was detected.
230 *
231 * A hardware failure may be transient or permanent depending on the
232 * cause. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300233#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)13)
itayzafrirc2a79762018-06-18 16:20:16 +0300234
235/** A tampering attempt was detected.
236 *
237 * If an application receives this error code, there is no guarantee
238 * that previously accessed or computed data was correct and remains
239 * confidential. Applications should not perform any security function
240 * and should enter a safe failure state.
241 *
242 * Implementations may return this error code if they detect an invalid
243 * state that cannot happen during normal operation and that indicates
244 * that the implementation's security guarantees no longer hold. Depending
245 * on the implementation architecture and on its security and safety goals,
246 * the implementation may forcibly terminate the application.
247 *
248 * This error code is intended as a last resort when a security breach
249 * is detected and it is unsure whether the keystore data is still
250 * protected. Implementations shall only return this error code
251 * to report an alarm from a tampering detector, to indicate that
252 * the confidentiality of stored data can no longer be guaranteed,
253 * or to indicate that the integrity of previously returned data is now
254 * considered compromised. Implementations shall not use this error code
255 * to indicate a hardware failure that merely makes it impossible to
256 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
257 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
258 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
259 * instead).
260 *
261 * This error indicates an attack against the application. Implementations
262 * shall not return this error code as a consequence of the behavior of
263 * the application itself. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300264#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)14)
itayzafrirc2a79762018-06-18 16:20:16 +0300265
266/** There is not enough entropy to generate random data needed
267 * for the requested action.
268 *
269 * This error indicates a failure of a hardware random generator.
270 * Application writers should note that this error can be returned not
271 * only by functions whose purpose is to generate random data, such
272 * as key, IV or nonce generation, but also by functions that execute
273 * an algorithm with a randomized result, as well as functions that
274 * use randomization of intermediate computations as a countermeasure
275 * to certain attacks.
276 *
277 * Implementations should avoid returning this error after psa_crypto_init()
278 * has succeeded. Implementations should generate sufficient
279 * entropy during initialization and subsequently use a cryptographically
280 * secure pseudorandom generator (PRNG). However implementations may return
281 * this error at any time if a policy requires the PRNG to be reseeded
282 * during normal operation. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300283#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)15)
itayzafrirc2a79762018-06-18 16:20:16 +0300284
285/** The signature, MAC or hash is incorrect.
286 *
287 * Verification functions return this error if the verification
288 * calculations completed successfully, and the value to be verified
289 * was determined to be incorrect.
290 *
291 * If the value to verify has an invalid size, implementations may return
292 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300293#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)16)
itayzafrirc2a79762018-06-18 16:20:16 +0300294
295/** The decrypted padding is incorrect.
296 *
297 * \warning In some protocols, when decrypting data, it is essential that
298 * the behavior of the application does not depend on whether the padding
299 * is correct, down to precise timing. Applications should prefer
300 * protocols that use authenticated encryption rather than plain
301 * encryption. If the application must perform a decryption of
302 * unauthenticated data, the application writer should take care not
303 * to reveal whether the padding is invalid.
304 *
305 * Implementations should strive to make valid and invalid padding
306 * as close as possible to indistinguishable to an external observer.
307 * In particular, the timing of a decryption operation should not
308 * depend on the validity of the padding. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300309#define PSA_ERROR_INVALID_PADDING ((psa_status_t)17)
itayzafrirc2a79762018-06-18 16:20:16 +0300310
Gilles Peskineeab56e42018-07-12 17:12:33 +0200311/** The generator has insufficient capacity left.
312 *
313 * Once a function returns this error, attempts to read from the
314 * generator will always return this error. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300315#define PSA_ERROR_INSUFFICIENT_CAPACITY ((psa_status_t)18)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100316
317/**
318 * \brief Library initialization.
319 *
320 * Applications must call this function before calling any other
321 * function in this module.
322 *
323 * Applications may call this function more than once. Once a call
324 * succeeds, subsequent calls are guaranteed to succeed.
325 *
itayzafrir18617092018-09-16 12:22:41 +0300326 * If the application calls other functions before calling psa_crypto_init(),
327 * the behavior is undefined. Implementations are encouraged to either perform
328 * the operation as if the library had been initialized or to return
329 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
330 * implementations should not return a success status if the lack of
331 * initialization may have security implications, for example due to improper
332 * seeding of the random number generator.
333 *
Gilles Peskine28538492018-07-11 17:34:00 +0200334 * \retval #PSA_SUCCESS
335 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
336 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
337 * \retval #PSA_ERROR_HARDWARE_FAILURE
338 * \retval #PSA_ERROR_TAMPERING_DETECTED
339 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100340 */
341psa_status_t psa_crypto_init(void);
342
Gilles Peskine2905a7a2018-03-07 16:39:31 +0100343#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
344#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Gilles Peskine0189e752018-02-03 23:57:22 +0100345
Gilles Peskinee59236f2018-01-27 23:32:46 +0100346/**@}*/
347
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100348/** \defgroup crypto_types Key and algorithm types
349 * @{
350 */
351
Gilles Peskine308b91d2018-02-08 09:47:44 +0100352/** \brief Encoding of a key type.
353 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100354typedef uint32_t psa_key_type_t;
355
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100356/** An invalid key type value.
357 *
358 * Zero is not the encoding of any key type.
359 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100360#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100361
362/** Vendor-defined flag
363 *
364 * Key types defined by this standard will never have the
365 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
366 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
367 * respect the bitwise structure used by standard encodings whenever practical.
368 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100369#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100370
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200371#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x70000000)
372#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x40000000)
373#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x50000000)
374#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x60000000)
375#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x70000000)
376
377#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x10000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200378
Gilles Peskinee8779742018-08-10 16:10:56 +0200379/** Whether a key type is vendor-defined. */
380#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
381 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
382
383/** Whether a key type is an unstructured array of bytes.
384 *
385 * This encompasses both symmetric keys and non-key data.
386 */
387#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
388 (((type) & PSA_KEY_TYPE_CATEGORY_MASK & ~(psa_key_type_t)0x10000000) == \
389 PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
390
391/** Whether a key type is asymmetric: either a key pair or a public key. */
392#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
393 (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
394 & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
395 PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
396/** Whether a key type is the public part of a key pair. */
397#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
398 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
399/** Whether a key type is a key pair containing a private part and a public
400 * part. */
401#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
402 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
403/** The key pair type corresponding to a public key type.
404 *
405 * You may also pass a key pair type as \p type, it will be left unchanged.
406 *
407 * \param type A public key type or key pair type.
408 *
409 * \return The corresponding key pair type.
410 * If \p type is not a public key or a key pair,
411 * the return value is undefined.
412 */
413#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
414 ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
415/** The public key type corresponding to a key pair type.
416 *
417 * You may also pass a key pair type as \p type, it will be left unchanged.
418 *
419 * \param type A public key type or key pair type.
420 *
421 * \return The corresponding public key type.
422 * If \p type is not a public key or a key pair,
423 * the return value is undefined.
424 */
425#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
426 ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
Gilles Peskinee8779742018-08-10 16:10:56 +0200427
Gilles Peskine35855962018-04-19 08:39:16 +0200428/** Raw data.
429 *
430 * A "key" of this type cannot be used for any cryptographic operation.
431 * Applications may use this type to store arbitrary data in the keystore. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200432#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x50000001)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100433
Gilles Peskine35855962018-04-19 08:39:16 +0200434/** HMAC key.
435 *
436 * The key policy determines which underlying hash algorithm the key can be
437 * used for.
438 *
439 * HMAC keys should generally have the same size as the underlying hash.
Gilles Peskinebe42f312018-07-13 14:38:15 +0200440 * This size can be calculated with #PSA_HASH_SIZE(\c alg) where
441 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200442#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x51000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200443
Gilles Peskineea0fb492018-07-12 17:17:20 +0200444/** A secret for key derivation.
445 *
446 * The key policy determines which key derivation algorithm the key
447 * can be used for.
448 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200449#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x52000000)
Gilles Peskineea0fb492018-07-12 17:17:20 +0200450
Gilles Peskine35855962018-04-19 08:39:16 +0200451/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
452 *
453 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
454 * 32 bytes (AES-256).
455 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200456#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x40000001)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200457
Gilles Peskine35855962018-04-19 08:39:16 +0200458/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
459 *
460 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
461 * 24 bytes (3-key 3DES).
462 *
463 * Note that single DES and 2-key 3DES are weak and strongly
464 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
465 * is weak and deprecated and should only be used in legacy protocols.
466 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200467#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x40000002)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200468
Gilles Peskine35855962018-04-19 08:39:16 +0200469/** Key for an cipher, AEAD or MAC algorithm based on the
470 * Camellia block cipher. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200471#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x40000003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200472
Gilles Peskine35855962018-04-19 08:39:16 +0200473/** Key for the RC4 stream cipher.
474 *
475 * Note that RC4 is weak and deprecated and should only be used in
476 * legacy protocols. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200477#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x40000004)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100478
Gilles Peskine308b91d2018-02-08 09:47:44 +0100479/** RSA public key. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200480#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x60010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100481/** RSA key pair (private and public key). */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200482#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x70010000)
Gilles Peskine583b55d2018-08-22 18:21:32 +0200483/** Whether a key type is an RSA key (pair or public-only). */
484#define PSA_KEY_TYPE_IS_RSA(type) \
485 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200486
Gilles Peskine06dc2632018-03-08 07:47:25 +0100487/** DSA public key. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200488#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x60020000)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100489/** DSA key pair (private and public key). */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200490#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x70020000)
Gilles Peskine583b55d2018-08-22 18:21:32 +0200491/** Whether a key type is an DSA key (pair or public-only). */
492#define PSA_KEY_TYPE_IS_DSA(type) \
493 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200494
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200495#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x60030000)
496#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x70030000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100497#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200498/** Elliptic curve key pair. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100499#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
500 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200501/** Elliptic curve public key. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100502#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
503 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100504
Gilles Peskined8008d62018-06-29 19:51:51 +0200505/** Whether a key type is an elliptic curve key (pair or public-only). */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100506#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100507 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
508 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine55728b02018-07-16 23:08:16 +0200509#define PSA_KEY_TYPE_IS_ECC_KEYPAIR(type) \
510 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
511 PSA_KEY_TYPE_ECC_KEYPAIR_BASE)
512#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
513 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
514 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200516/** The type of PSA elliptic curve identifiers. */
517typedef uint16_t psa_ecc_curve_t;
518/** Extract the curve from an elliptic curve key type. */
519#define PSA_KEY_TYPE_GET_CURVE(type) \
520 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
521 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
522 0))
523
524/* The encoding of curve identifiers is currently aligned with the
525 * TLS Supported Groups Registry (formerly known as the
526 * TLS EC Named Curve Registry)
527 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
Gilles Peskine70ce2c62018-08-22 18:21:57 +0200528 * The values are defined by RFC 8422 and RFC 7027. */
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200529#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
530#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
531#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
532#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
533#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
534#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
535#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
536#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
537#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
538#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
539#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
540#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
541#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
542#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
543#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
544#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
545#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
546#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
547#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
548#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
549#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
550#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
551#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
552#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
553#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
554#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
555#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
556#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
557#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
558#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200559
Gilles Peskine7e198532018-03-08 07:50:30 +0100560/** The block size of a block cipher.
561 *
562 * \param type A cipher key type (value of type #psa_key_type_t).
563 *
564 * \return The block size for a block cipher, or 1 for a stream cipher.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200565 * The return value is undefined if \p type is not a supported
Gilles Peskine35855962018-04-19 08:39:16 +0200566 * cipher key type.
567 *
568 * \note It is possible to build stream cipher algorithms on top of a block
569 * cipher, for example CTR mode (#PSA_ALG_CTR).
570 * This macro only takes the key type into account, so it cannot be
571 * used to determine the size of the data that #psa_cipher_update()
572 * might buffer for future processing in general.
Gilles Peskine7e198532018-03-08 07:50:30 +0100573 *
574 * \note This macro returns a compile-time constant if its argument is one.
575 *
576 * \warning This macro may evaluate its argument multiple times.
577 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100578#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100579 ( \
580 (type) == PSA_KEY_TYPE_AES ? 16 : \
581 (type) == PSA_KEY_TYPE_DES ? 8 : \
582 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100583 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100584 0)
585
Gilles Peskine308b91d2018-02-08 09:47:44 +0100586/** \brief Encoding of a cryptographic algorithm.
587 *
588 * For algorithms that can be applied to multiple key types, this type
589 * does not encode the key type. For example, for symmetric ciphers
590 * based on a block cipher, #psa_algorithm_t encodes the block cipher
591 * mode and the padding mode while the block cipher itself is encoded
592 * via #psa_key_type_t.
593 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100594typedef uint32_t psa_algorithm_t;
595
Gilles Peskine98f0a242018-02-06 18:57:29 +0100596#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
597#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
598#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
599#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
600#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
601#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
602#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
603#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
604#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
605#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100606
Gilles Peskine98f0a242018-02-06 18:57:29 +0100607#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
608 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200609
Gilles Peskine308b91d2018-02-08 09:47:44 +0100610/** Whether the specified algorithm is a hash algorithm.
611 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100612 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100613 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200614 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
615 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskine7e198532018-03-08 07:50:30 +0100616 * algorithm identifier.
617 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100618#define PSA_ALG_IS_HASH(alg) \
619 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200620
621/** Whether the specified algorithm is a MAC algorithm.
622 *
623 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
624 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200625 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
626 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200627 * algorithm identifier.
628 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100629#define PSA_ALG_IS_MAC(alg) \
630 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200631
632/** Whether the specified algorithm is a symmetric cipher algorithm.
633 *
634 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
635 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200636 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
637 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200638 * algorithm identifier.
639 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100640#define PSA_ALG_IS_CIPHER(alg) \
641 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200642
643/** Whether the specified algorithm is an authenticated encryption
644 * with associated data (AEAD) algorithm.
645 *
646 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
647 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200648 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
649 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200650 * algorithm identifier.
651 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100652#define PSA_ALG_IS_AEAD(alg) \
653 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200654
655/** Whether the specified algorithm is a public-key signature algorithm.
656 *
657 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
658 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200659 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
660 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200661 * algorithm identifier.
662 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100663#define PSA_ALG_IS_SIGN(alg) \
664 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200665
666/** Whether the specified algorithm is a public-key encryption algorithm.
667 *
668 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
669 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200670 * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
671 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200672 * algorithm identifier.
673 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100674#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
675 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200676
677/** Whether the specified algorithm is a key agreement algorithm.
678 *
679 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
680 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200681 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
682 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200683 * algorithm identifier.
684 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100685#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
686 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_AGREEMENT)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200687
688/** Whether the specified algorithm is a key derivation algorithm.
689 *
690 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
691 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200692 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
693 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200694 * algorithm identifier.
695 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100696#define PSA_ALG_IS_KEY_DERIVATION(alg) \
697 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
698
699#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
700#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
701#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
702#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100703#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
704#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskineedd76872018-07-20 17:42:05 +0200705/** SHA2-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100706#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
Gilles Peskineedd76872018-07-20 17:42:05 +0200707/** SHA2-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100708#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
Gilles Peskineedd76872018-07-20 17:42:05 +0200709/** SHA2-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100710#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
Gilles Peskineedd76872018-07-20 17:42:05 +0200711/** SHA2-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100712#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
Gilles Peskineedd76872018-07-20 17:42:05 +0200713/** SHA2-512/224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100714#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
Gilles Peskineedd76872018-07-20 17:42:05 +0200715/** SHA2-512/256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100716#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
Gilles Peskineedd76872018-07-20 17:42:05 +0200717/** SHA3-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100718#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
Gilles Peskineedd76872018-07-20 17:42:05 +0200719/** SHA3-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100720#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
Gilles Peskineedd76872018-07-20 17:42:05 +0200721/** SHA3-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100722#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
Gilles Peskineedd76872018-07-20 17:42:05 +0200723/** SHA3-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100724#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
725
Gilles Peskine8c9def32018-02-08 10:02:12 +0100726#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100727#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200728/** Macro to build an HMAC algorithm.
729 *
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200730 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
Gilles Peskine35855962018-04-19 08:39:16 +0200731 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200732 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200733 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine35855962018-04-19 08:39:16 +0200734 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200735 * \return The corresponding HMAC algorithm.
736 * \return Unspecified if \p alg is not a supported
737 * hash algorithm.
Gilles Peskine35855962018-04-19 08:39:16 +0200738 */
739#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100740 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200741
Gilles Peskine00709fa2018-08-22 18:25:41 +0200742#define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100743 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200744
745/** Whether the specified algorithm is an HMAC algorithm.
746 *
747 * HMAC is a family of MAC algorithms that are based on a hash function.
748 *
749 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
750 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200751 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
752 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200753 * algorithm identifier.
754 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100755#define PSA_ALG_IS_HMAC(alg) \
756 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
757 PSA_ALG_HMAC_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200758
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200759/* In the encoding of a MAC algorithm, the bits corresponding to
760 * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
761 * truncated. As an exception, the value 0 means the untruncated algorithm,
762 * whatever its length is. The length is encoded in 6 bits, so it can
763 * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
764 * to full length is correctly encoded as 0 and any non-trivial truncation
765 * is correctly encoded as a value between 1 and 63. */
Gilles Peskined911eb72018-08-14 15:18:45 +0200766#define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x00003f00)
767#define PSA_MAC_TRUNCATION_OFFSET 8
768
769/** Macro to build a truncated MAC algorithm.
770 *
771 * A truncated MAC algorithm is identical to the corresponding MAC
772 * algorithm except that the MAC value for the truncated algorithm
773 * consists of only the first \p mac_length bytes of the MAC value
774 * for the untruncated algorithm.
775 *
776 * \note This macro may allow constructing algorithm identifiers that
777 * are not valid, either because the specified length is larger
778 * than the untruncated MAC or because the specified length is
779 * smaller than permitted by the implementation.
780 *
781 * \note It is implementation-defined whether a truncated MAC that
782 * is truncated to the same length as the MAC of the untruncated
783 * algorithm is considered identical to the untruncated algorithm
784 * for policy comparison purposes.
785 *
786 * \param alg A MAC algorithm identifier (value of type
787 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
788 * is true). This may be a truncated or untruncated
789 * MAC algorithm.
790 * \param mac_length Desired length of the truncated MAC in bytes.
Gilles Peskine6d72ff92018-08-21 14:55:08 +0200791 * This must be at most the full length of the MAC
792 * and must be at least an implementation-specified
793 * minimum. The implementation-specified minimum
794 * shall not be zero.
Gilles Peskined911eb72018-08-14 15:18:45 +0200795 *
796 * \return The corresponding MAC algorithm with the specified
797 * length.
798 * \return Unspecified if \p alg is not a supported
799 * MAC algorithm or if \p mac_length is too small or
800 * too large for the specified MAC algorithm.
801 */
802#define PSA_ALG_TRUNCATED_MAC(alg, mac_length) \
803 (((alg) & ~PSA_ALG_MAC_TRUNCATION_MASK) | \
804 ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
805
806/** Length to which a MAC algorithm is truncated.
807 *
808 * \param alg A MAC algorithm identifier (value of type
809 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
810 * is true).
811 *
812 * \return Length of the truncated MAC in bytes.
813 * \return 0 if \p alg is a non-truncated MAC algorithm.
814 * \return Unspecified if \p alg is not a supported
815 * MAC algorithm.
816 */
817#define PSA_MAC_TRUNCATED_LENGTH(alg) \
818 (((alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
819
Gilles Peskine8c9def32018-02-08 10:02:12 +0100820#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
821#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
822#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
823#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200824
825/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
826 *
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200827 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
828 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200829 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
830 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200831 * algorithm identifier.
832 */
Gilles Peskine9df2dc82018-08-22 18:24:17 +0200833#define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100834 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
835 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100836
Gilles Peskinedaea26f2018-08-21 14:02:45 +0200837#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
838#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100839
Gilles Peskinedcd14942018-07-12 00:30:52 +0200840/** Whether the specified algorithm is a stream cipher.
841 *
842 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
843 * by applying a bitwise-xor with a stream of bytes that is generated
844 * from a key.
845 *
846 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
847 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200848 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
849 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200850 * algorithm identifier or if it is not a symmetric cipher algorithm.
851 */
Moran Pekerbed71a22018-04-22 20:19:20 +0300852#define PSA_ALG_IS_STREAM_CIPHER(alg) \
Gilles Peskinedaea26f2018-08-21 14:02:45 +0200853 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
854 (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
855
856/** The ARC4 stream cipher algorithm.
857 */
858#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800001)
859
860/** The CTR stream cipher mode.
861 *
862 * CTR is a stream cipher which is built from a block cipher.
863 * The underlying block cipher is determined by the key type.
864 * For example, to use AES-128-CTR, use this algorithm with
865 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
866 */
867#define PSA_ALG_CTR ((psa_algorithm_t)0x04c00001)
868
869#define PSA_ALG_CFB ((psa_algorithm_t)0x04c00002)
870
871#define PSA_ALG_OFB ((psa_algorithm_t)0x04c00003)
872
873/** The XTS cipher mode.
874 *
875 * XTS is a cipher mode which is built from a block cipher. It requires at
876 * least one full block of input, but beyond this minimum the input
877 * does not need to be a whole number of blocks.
878 */
879#define PSA_ALG_XTS ((psa_algorithm_t)0x044000ff)
880
881/** The CBC block cipher chaining mode, with no padding.
882 *
883 * The underlying block cipher is determined by the key type.
884 *
885 * This symmetric cipher mode can only be used with messages whose lengths
886 * are whole number of blocks for the chosen block cipher.
887 */
888#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04600100)
889
890/** The CBC block cipher chaining mode with PKCS#7 padding.
891 *
892 * The underlying block cipher is determined by the key type.
893 *
894 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
895 */
896#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04600101)
Moran Pekerbed71a22018-04-22 20:19:20 +0300897
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200898#define PSA_ALG_CCM ((psa_algorithm_t)0x06001001)
899#define PSA_ALG_GCM ((psa_algorithm_t)0x06001002)
900
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200901/* In the encoding of a AEAD algorithm, the bits corresponding to
902 * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
903 * The constants for default lengths follow this encoding.
904 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200905#define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x00003f00)
906#define PSA_AEAD_TAG_LENGTH_OFFSET 8
907
908/** Macro to build a shortened AEAD algorithm.
909 *
910 * A shortened AEAD algorithm is similar to the corresponding AEAD
911 * algorithm, but has an authentication tag that consists of fewer bytes.
912 * Depending on the algorithm, the tag length may affect the calculation
913 * of the ciphertext.
914 *
915 * \param alg A AEAD algorithm identifier (value of type
916 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p alg)
917 * is true).
Gilles Peskine31119812018-08-21 14:47:48 +0200918 * \param tag_length Desired length of the authentication tag in bytes.
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200919 *
920 * \return The corresponding AEAD algorithm with the specified
921 * length.
922 * \return Unspecified if \p alg is not a supported
923 * AEAD algorithm or if \p tag_length is not valid
924 * for the specified AEAD algorithm.
925 */
926#define PSA_ALG_AEAD_WITH_TAG_LENGTH(alg, tag_length) \
927 (((alg) & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) | \
928 ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
929 PSA_ALG_AEAD_TAG_LENGTH_MASK))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100930
Gilles Peskine70f46e12018-08-20 15:07:53 +0200931/** Calculate the corresponding AEAD algorithm with the default tag length.
932 *
933 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
934 * #PSA_ALG_IS_AEAD(\p alg) is true).
935 *
936 * \return The corresponding AEAD algorithm with the default tag length
937 * for that algorithm.
938 */
939#define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg) \
940 ( \
941 PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, PSA_ALG_CCM) \
942 PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, PSA_ALG_GCM) \
943 0)
944#define PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, ref) \
945 PSA_ALG_AEAD_WITH_TAG_LENGTH(alg, 0) == \
946 PSA_ALG_AEAD_WITH_TAG_LENGTH(ref, 0) ? \
947 ref :
948
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200949#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
950/** RSA PKCS#1 v1.5 signature with hashing.
951 *
952 * This is the signature scheme defined by RFC 8017
953 * (PKCS#1: RSA Cryptography Specifications) under the name
954 * RSASSA-PKCS1-v1_5.
955 *
956 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200957 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200958 *
959 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
960 * \return Unspecified if \p alg is not a supported
961 * hash algorithm.
962 */
Gilles Peskinea5926232018-03-28 14:16:50 +0200963#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200964 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
965/** Raw PKCS#1 v1.5 signature.
966 *
967 * The input to this algorithm is the DigestInfo structure used by
968 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
969 * steps 3&ndash;6.
970 */
971#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
Gilles Peskinea5926232018-03-28 14:16:50 +0200972#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200973 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200974
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200975#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
976/** RSA PSS signature with hashing.
977 *
978 * This is the signature scheme defined by RFC 8017
979 * (PKCS#1: RSA Cryptography Specifications) under the name
Gilles Peskinea4d20bd2018-06-29 23:35:02 +0200980 * RSASSA-PSS, with the message generation function MGF1, and with
981 * a salt length equal to the length of the hash. The specified
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200982 * hash algorithm is used to hash the input message, to create the
983 * salted hash, and for the mask generation.
984 *
985 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200986 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200987 *
988 * \return The corresponding RSA PSS signature algorithm.
989 * \return Unspecified if \p alg is not a supported
990 * hash algorithm.
991 */
992#define PSA_ALG_RSA_PSS(hash_alg) \
993 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
994#define PSA_ALG_IS_RSA_PSS(alg) \
995 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
996
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200997#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
998/** DSA signature with hashing.
999 *
1000 * This is the signature scheme defined by FIPS 186-4,
1001 * with a random per-message secret number (*k*).
1002 *
1003 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001004 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001005 *
1006 * \return The corresponding DSA signature algorithm.
1007 * \return Unspecified if \p alg is not a supported
1008 * hash algorithm.
1009 */
1010#define PSA_ALG_DSA(hash_alg) \
1011 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1012#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
1013#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
1014#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
1015 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1016#define PSA_ALG_IS_DSA(alg) \
1017 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
1018 PSA_ALG_DSA_BASE)
1019#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
1020 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +02001021#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
1022 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
1023#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
1024 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001025
1026#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
1027/** ECDSA signature with hashing.
1028 *
1029 * This is the ECDSA signature scheme defined by ANSI X9.62,
1030 * with a random per-message secret number (*k*).
1031 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001032 * The representation of the signature as a byte string consists of
1033 * the concatentation of the signature values *r* and *s*. Each of
1034 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1035 * of the base point of the curve in octets. Each value is represented
1036 * in big-endian order (most significant octet first).
1037 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001038 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001039 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001040 *
1041 * \return The corresponding ECDSA signature algorithm.
1042 * \return Unspecified if \p alg is not a supported
1043 * hash algorithm.
1044 */
1045#define PSA_ALG_ECDSA(hash_alg) \
1046 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1047/** ECDSA signature without hashing.
1048 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001049 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001050 * without specifying a hash algorithm. This algorithm may only be
1051 * used to sign or verify a sequence of bytes that should be an
1052 * already-calculated hash. Note that the input is padded with
1053 * zeros on the left or truncated on the left as required to fit
1054 * the curve size.
1055 */
1056#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
1057#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
1058/** Deterministic ECDSA signature with hashing.
1059 *
1060 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1061 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001062 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1063 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001064 * Note that when this algorithm is used for verification, signatures
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001065 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001066 * same private key are accepted. In other words,
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001067 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1068 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001069 *
1070 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001071 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001072 *
1073 * \return The corresponding deterministic ECDSA signature
1074 * algorithm.
1075 * \return Unspecified if \p alg is not a supported
1076 * hash algorithm.
1077 */
1078#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1079 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1080#define PSA_ALG_IS_ECDSA(alg) \
1081 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
1082 PSA_ALG_ECDSA_BASE)
1083#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
1084 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +02001085#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1086 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1087#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1088 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001089
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001090/** Get the hash used by a hash-and-sign signature algorithm.
1091 *
1092 * A hash-and-sign algorithm is a signature algorithm which is
1093 * composed of two phases: first a hashing phase which does not use
1094 * the key and produces a hash of the input message, then a signing
1095 * phase which only uses the hash and the key and not the message
1096 * itself.
1097 *
1098 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001099 * #PSA_ALG_IS_SIGN(\p alg) is true).
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001100 *
1101 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1102 * algorithm.
1103 * \return 0 if \p alg is a signature algorithm that does not
1104 * follow the hash-and-sign structure.
1105 * \return Unspecified if \p alg is not a signature algorithm or
1106 * if it is not supported by the implementation.
1107 */
1108#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001109 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
1110 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
Gilles Peskine54622ae2018-06-29 22:24:24 +02001111 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001112 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1113 0)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001114
Gilles Peskinedcd14942018-07-12 00:30:52 +02001115/** RSA PKCS#1 v1.5 encryption.
1116 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001117#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001118
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001119#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001120/** RSA OAEP encryption.
1121 *
1122 * This is the encryption scheme defined by RFC 8017
1123 * (PKCS#1: RSA Cryptography Specifications) under the name
1124 * RSAES-OAEP, with the message generation function MGF1.
1125 *
1126 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1127 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1128 * for MGF1.
1129 *
1130 * \return The corresponding RSA OAEP signature algorithm.
1131 * \return Unspecified if \p alg is not a supported
1132 * hash algorithm.
1133 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001134#define PSA_ALG_RSA_OAEP(hash_alg) \
1135 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1136#define PSA_ALG_IS_RSA_OAEP(alg) \
1137 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
Gilles Peskine072ac562018-06-30 00:21:29 +02001138#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1139 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1140 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1141 0)
Gilles Peskined1e8e412018-06-07 09:49:39 +02001142
Gilles Peskinebef7f142018-07-12 17:22:21 +02001143#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100)
1144/** Macro to build an HKDF algorithm.
1145 *
1146 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1147 *
1148 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1149 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1150 *
1151 * \return The corresponding HKDF algorithm.
1152 * \return Unspecified if \p alg is not a supported
1153 * hash algorithm.
1154 */
1155#define PSA_ALG_HKDF(hash_alg) \
1156 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1157/** Whether the specified algorithm is an HKDF algorithm.
1158 *
1159 * HKDF is a family of key derivation algorithms that are based on a hash
1160 * function and the HMAC construction.
1161 *
1162 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1163 *
1164 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1165 * This macro may return either 0 or 1 if \c alg is not a supported
1166 * key derivation algorithm identifier.
1167 */
1168#define PSA_ALG_IS_HKDF(alg) \
1169 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1170#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1171 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1172
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001173/**@}*/
1174
1175/** \defgroup key_management Key management
1176 * @{
1177 */
1178
1179/**
1180 * \brief Import a key in binary format.
1181 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +01001182 * This function supports any output from psa_export_key(). Refer to the
1183 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001184 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001185 * \param key Slot where the key will be stored. This must be a
1186 * valid slot for a key of the chosen type. It must
1187 * be unoccupied.
1188 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001189 * \param[in] data Buffer containing the key data.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001190 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001191 *
Gilles Peskine28538492018-07-11 17:34:00 +02001192 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001193 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001194 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001195 * The key type or key size is not supported, either by the
1196 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001197 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +01001198 * The key slot is invalid,
1199 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +02001200 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001201 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001202 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1203 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1204 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1205 * \retval #PSA_ERROR_HARDWARE_FAILURE
1206 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001207 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001208 * The library has not been previously initialized by psa_crypto_init().
1209 * It is implementation-dependent whether a failure to initialize
1210 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001211 */
1212psa_status_t psa_import_key(psa_key_slot_t key,
1213 psa_key_type_t type,
1214 const uint8_t *data,
1215 size_t data_length);
1216
1217/**
Gilles Peskine154bd952018-04-19 08:38:16 +02001218 * \brief Destroy a key and restore the slot to its default state.
1219 *
1220 * This function destroys the content of the key slot from both volatile
1221 * memory and, if applicable, non-volatile storage. Implementations shall
1222 * make a best effort to ensure that any previous content of the slot is
1223 * unrecoverable.
1224 *
1225 * This function also erases any metadata such as policies. It returns the
1226 * specified slot to its default state.
1227 *
1228 * \param key The key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001229 *
Gilles Peskine28538492018-07-11 17:34:00 +02001230 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +02001231 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +02001232 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001233 * The slot holds content and cannot be erased because it is
1234 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskine28538492018-07-11 17:34:00 +02001235 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001236 * The specified slot number does not designate a valid slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001237 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001238 * There was an failure in communication with the cryptoprocessor.
1239 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +02001240 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001241 * The storage is corrupted. Implementations shall make a best effort
1242 * to erase key material even in this stage, however applications
1243 * should be aware that it may be impossible to guarantee that the
1244 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +02001245 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001246 * An unexpected condition which is not a storage corruption or
1247 * a communication failure occurred. The cryptoprocessor may have
1248 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001249 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001250 * The library has not been previously initialized by psa_crypto_init().
1251 * It is implementation-dependent whether a failure to initialize
1252 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001253 */
1254psa_status_t psa_destroy_key(psa_key_slot_t key);
1255
1256/**
1257 * \brief Get basic metadata about a key.
1258 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001259 * \param key Slot whose content is queried. This must
1260 * be an occupied key slot.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001261 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001262 * This may be a null pointer, in which case the key type
1263 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001264 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +01001265 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +01001266 * is not written.
1267 *
Gilles Peskine28538492018-07-11 17:34:00 +02001268 * \retval #PSA_SUCCESS
1269 * \retval #PSA_ERROR_EMPTY_SLOT
1270 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1271 * \retval #PSA_ERROR_HARDWARE_FAILURE
1272 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001273 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001274 * The library has not been previously initialized by psa_crypto_init().
1275 * It is implementation-dependent whether a failure to initialize
1276 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001277 */
1278psa_status_t psa_get_key_information(psa_key_slot_t key,
1279 psa_key_type_t *type,
1280 size_t *bits);
1281
1282/**
1283 * \brief Export a key in binary format.
1284 *
1285 * The output of this function can be passed to psa_import_key() to
1286 * create an equivalent object.
1287 *
1288 * If a key is created with psa_import_key() and then exported with
1289 * this function, it is not guaranteed that the resulting data is
1290 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +01001291 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001292 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001293 * For standard key types, the output format is as follows:
1294 *
1295 * - For symmetric keys (including MAC keys), the format is the
1296 * raw bytes of the key.
1297 * - For DES, the key data consists of 8 bytes. The parity bits must be
1298 * correct.
1299 * - For Triple-DES, the format is the concatenation of the
1300 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +01001301 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001302 * is the non-encrypted DER encoding of the representation defined by
1303 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
1304 * ```
1305 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001306 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001307 * modulus INTEGER, -- n
1308 * publicExponent INTEGER, -- e
1309 * privateExponent INTEGER, -- d
1310 * prime1 INTEGER, -- p
1311 * prime2 INTEGER, -- q
1312 * exponent1 INTEGER, -- d mod (p-1)
1313 * exponent2 INTEGER, -- d mod (q-1)
1314 * coefficient INTEGER, -- (inverse of q) mod p
1315 * }
1316 * ```
1317 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
1318 * is the non-encrypted DER encoding of the representation used by
Gilles Peskinec6290c02018-08-13 17:24:59 +02001319 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001320 * ```
1321 * DSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001322 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001323 * prime INTEGER, -- p
1324 * subprime INTEGER, -- q
1325 * generator INTEGER, -- g
1326 * public INTEGER, -- y
1327 * private INTEGER, -- x
1328 * }
1329 * ```
1330 * - For elliptic curve key pairs (key types for which
1331 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is the
1332 * non-encrypted DER encoding of the representation defined by RFC 5915 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001333 * `ECPrivateKey`, version 1. The `ECParameters` field must be a
1334 * `namedCurve` OID as specified in RFC 5480 &sect;2.1.1.1. The public key
1335 * must be present and must be an `ECPoint` in the same format
1336 * (uncompressed variant) an ECC public key of the
1337 * corresponding type exported with psa_export_public_key().
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001338 * ```
1339 * ECPrivateKey ::= SEQUENCE {
1340 * version INTEGER, -- must be 1
1341 * privateKey OCTET STRING,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001342 * -- `ceiling(log2(n)/8)`-byte string, big endian,
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001343 * -- where n is the order of the curve.
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001344 * parameters [0] IMPLICIT ECParameters {{ namedCurve }}, -- mandatory
1345 * publicKey [1] IMPLICIT BIT STRING -- mandatory
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001346 * }
1347 * ```
1348 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
1349 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001350 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001351 * \param key Slot whose content is to be exported. This must
1352 * be an occupied key slot.
1353 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001354 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001355 * \param[out] data_length On success, the number of bytes
1356 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001357 *
Gilles Peskine28538492018-07-11 17:34:00 +02001358 * \retval #PSA_SUCCESS
1359 * \retval #PSA_ERROR_EMPTY_SLOT
1360 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +01001361 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +02001362 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1363 * The size of the \p data buffer is too small. You can determine a
1364 * sufficient buffer size by calling
1365 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
1366 * where \c type is the key type
1367 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +02001368 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1369 * \retval #PSA_ERROR_HARDWARE_FAILURE
1370 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001371 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001372 * The library has not been previously initialized by psa_crypto_init().
1373 * It is implementation-dependent whether a failure to initialize
1374 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001375 */
1376psa_status_t psa_export_key(psa_key_slot_t key,
1377 uint8_t *data,
1378 size_t data_size,
1379 size_t *data_length);
1380
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001381/**
1382 * \brief Export a public key or the public part of a key pair in binary format.
1383 *
1384 * The output of this function can be passed to psa_import_key() to
1385 * create an object that is equivalent to the public key.
1386 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001387 * The format is the DER representation defined by RFC 5280 as
1388 * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
1389 * specified below.
1390 * ```
1391 * SubjectPublicKeyInfo ::= SEQUENCE {
1392 * algorithm AlgorithmIdentifier,
1393 * subjectPublicKey BIT STRING }
1394 * AlgorithmIdentifier ::= SEQUENCE {
1395 * algorithm OBJECT IDENTIFIER,
1396 * parameters ANY DEFINED BY algorithm OPTIONAL }
1397 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001398 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001399 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY),
1400 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.1 as
1401 * `RSAPublicKey`,
1402 * with the OID `rsaEncryption`,
1403 * and with the parameters `NULL`.
1404 * ```
1405 * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
1406 * rsadsi(113549) pkcs(1) 1 }
1407 * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 }
1408 *
1409 * RSAPublicKey ::= SEQUENCE {
1410 * modulus INTEGER, -- n
1411 * publicExponent INTEGER } -- e
1412 * ```
1413 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
1414 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
1415 * `DSAPublicKey`,
1416 * with the OID `id-dsa`,
1417 * and with the parameters `DSS-Parms`.
1418 * ```
1419 * id-dsa OBJECT IDENTIFIER ::= {
1420 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
1421 *
1422 * Dss-Parms ::= SEQUENCE {
1423 * p INTEGER,
1424 * q INTEGER,
1425 * g INTEGER }
1426 * DSAPublicKey ::= INTEGER -- public key, Y
1427 * ```
1428 * - For elliptic curve public keys (key types for which
1429 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true),
1430 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.5 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001431 * `ECPoint`, which contains the uncompressed
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001432 * representation defined by SEC1 &sect;2.3.3.
1433 * The OID is `id-ecPublicKey`,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001434 * and the parameters must be given as a `namedCurve` OID as specified in
Gilles Peskinec6290c02018-08-13 17:24:59 +02001435 * RFC 5480 &sect;2.1.1.1 or other applicable standards.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001436 * ```
1437 * ansi-X9-62 OBJECT IDENTIFIER ::=
1438 * { iso(1) member-body(2) us(840) 10045 }
1439 * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 }
1440 * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 }
1441 *
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001442 * ECPoint ::= ...
1443 * -- first 8 bits: 0x04;
1444 * -- then x_P as an n-bit string, big endian;
1445 * -- then y_P as a n-bit string, big endian,
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001446 * -- where n is the order of the curve.
1447 *
1448 * EcpkParameters ::= CHOICE { -- other choices are not allowed
1449 * namedCurve OBJECT IDENTIFIER }
1450 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001451 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001452 * \param key Slot whose content is to be exported. This must
1453 * be an occupied key slot.
1454 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001455 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001456 * \param[out] data_length On success, the number of bytes
1457 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001458 *
Gilles Peskine28538492018-07-11 17:34:00 +02001459 * \retval #PSA_SUCCESS
1460 * \retval #PSA_ERROR_EMPTY_SLOT
1461 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +02001462 * The key is neither a public key nor a key pair.
1463 * \retval #PSA_ERROR_NOT_SUPPORTED
1464 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1465 * The size of the \p data buffer is too small. You can determine a
1466 * sufficient buffer size by calling
1467 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
1468 * where \c type is the key type
1469 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +02001470 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1471 * \retval #PSA_ERROR_HARDWARE_FAILURE
1472 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001473 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001474 * The library has not been previously initialized by psa_crypto_init().
1475 * It is implementation-dependent whether a failure to initialize
1476 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001477 */
1478psa_status_t psa_export_public_key(psa_key_slot_t key,
1479 uint8_t *data,
1480 size_t data_size,
1481 size_t *data_length);
1482
1483/**@}*/
1484
1485/** \defgroup policy Key policies
1486 * @{
1487 */
1488
1489/** \brief Encoding of permitted usage on a key. */
1490typedef uint32_t psa_key_usage_t;
1491
Gilles Peskine7e198532018-03-08 07:50:30 +01001492/** Whether the key may be exported.
1493 *
1494 * A public key or the public part of a key pair may always be exported
1495 * regardless of the value of this permission flag.
1496 *
1497 * If a key does not have export permission, implementations shall not
1498 * allow the key to be exported in plain form from the cryptoprocessor,
1499 * whether through psa_export_key() or through a proprietary interface.
1500 * The key may however be exportable in a wrapped form, i.e. in a form
1501 * where it is encrypted by another key.
1502 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001503#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1504
Gilles Peskine7e198532018-03-08 07:50:30 +01001505/** Whether the key may be used to encrypt a message.
1506 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001507 * This flag allows the key to be used for a symmetric encryption operation,
1508 * for an AEAD encryption-and-authentication operation,
1509 * or for an asymmetric encryption operation,
1510 * if otherwise permitted by the key's type and policy.
1511 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001512 * For a key pair, this concerns the public key.
1513 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001514#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +01001515
1516/** Whether the key may be used to decrypt a message.
1517 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001518 * This flag allows the key to be used for a symmetric decryption operation,
1519 * for an AEAD decryption-and-verification operation,
1520 * or for an asymmetric decryption operation,
1521 * if otherwise permitted by the key's type and policy.
1522 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001523 * For a key pair, this concerns the private key.
1524 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001525#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +01001526
1527/** Whether the key may be used to sign a message.
1528 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001529 * This flag allows the key to be used for a MAC calculation operation
1530 * or for an asymmetric signature operation,
1531 * if otherwise permitted by the key's type and policy.
1532 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001533 * For a key pair, this concerns the private key.
1534 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001535#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +01001536
1537/** Whether the key may be used to verify a message signature.
1538 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001539 * This flag allows the key to be used for a MAC verification operation
1540 * or for an asymmetric signature verification operation,
1541 * if otherwise permitted by by the key's type and policy.
1542 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001543 * For a key pair, this concerns the public key.
1544 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001545#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1546
Gilles Peskineea0fb492018-07-12 17:17:20 +02001547/** Whether the key may be used to derive other keys.
1548 */
1549#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1550
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001551/** The type of the key policy data structure.
1552 *
1553 * This is an implementation-defined \c struct. Applications should not
1554 * make any assumptions about the content of this structure except
1555 * as directed by the documentation of a specific implementation. */
1556typedef struct psa_key_policy_s psa_key_policy_t;
1557
1558/** \brief Initialize a key policy structure to a default that forbids all
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001559 * usage of the key.
1560 *
1561 * \param[out] policy The policy object to initialize.
1562 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001563void psa_key_policy_init(psa_key_policy_t *policy);
1564
Gilles Peskine7e198532018-03-08 07:50:30 +01001565/** \brief Set the standard fields of a policy structure.
1566 *
1567 * Note that this function does not make any consistency check of the
1568 * parameters. The values are only checked when applying the policy to
1569 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001570 *
1571 * \param[out] policy The policy object to modify.
1572 * \param usage The permitted uses for the key.
1573 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +01001574 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001575void psa_key_policy_set_usage(psa_key_policy_t *policy,
1576 psa_key_usage_t usage,
1577 psa_algorithm_t alg);
1578
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001579/** \brief Retrieve the usage field of a policy structure.
1580 *
1581 * \param[in] policy The policy object to query.
1582 *
1583 * \return The permitted uses for a key with this policy.
1584 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001585psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001586
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001587/** \brief Retrieve the algorithm field of a policy structure.
1588 *
1589 * \param[in] policy The policy object to query.
1590 *
1591 * \return The permitted algorithm for a key with this policy.
1592 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001593psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001594
1595/** \brief Set the usage policy on a key slot.
1596 *
1597 * This function must be called on an empty key slot, before importing,
1598 * generating or creating a key in the slot. Changing the policy of an
1599 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +01001600 *
1601 * Implementations may set restrictions on supported key policies
1602 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001603 *
1604 * \param key The key slot whose policy is to be changed.
1605 * \param[in] policy The policy object to query.
1606 *
1607 * \retval #PSA_SUCCESS
1608 * \retval #PSA_ERROR_OCCUPIED_SLOT
1609 * \retval #PSA_ERROR_NOT_SUPPORTED
1610 * \retval #PSA_ERROR_INVALID_ARGUMENT
1611 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1612 * \retval #PSA_ERROR_HARDWARE_FAILURE
1613 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001614 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001615 * The library has not been previously initialized by psa_crypto_init().
1616 * It is implementation-dependent whether a failure to initialize
1617 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001618 */
1619psa_status_t psa_set_key_policy(psa_key_slot_t key,
1620 const psa_key_policy_t *policy);
1621
Gilles Peskine7e198532018-03-08 07:50:30 +01001622/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001623 *
1624 * \param key The key slot whose policy is being queried.
1625 * \param[out] policy On success, the key's policy.
1626 *
1627 * \retval #PSA_SUCCESS
1628 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1629 * \retval #PSA_ERROR_HARDWARE_FAILURE
1630 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001631 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001632 * The library has not been previously initialized by psa_crypto_init().
1633 * It is implementation-dependent whether a failure to initialize
1634 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +01001635 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001636psa_status_t psa_get_key_policy(psa_key_slot_t key,
1637 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +01001638
1639/**@}*/
1640
Gilles Peskine609b6a52018-03-03 21:31:50 +01001641/** \defgroup persistence Key lifetime
1642 * @{
1643 */
1644
1645/** Encoding of key lifetimes.
1646 */
1647typedef uint32_t psa_key_lifetime_t;
1648
1649/** A volatile key slot retains its content as long as the application is
1650 * running. It is guaranteed to be erased on a power reset.
1651 */
1652#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1653
1654/** A persistent key slot retains its content as long as it is not explicitly
1655 * destroyed.
1656 */
1657#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1658
1659/** A write-once key slot may not be modified once a key has been set.
1660 * It will retain its content as long as the device remains operational.
1661 */
1662#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
1663
Gilles Peskined393e182018-03-08 07:49:16 +01001664/** \brief Retrieve the lifetime of a key slot.
1665 *
1666 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001667 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001668 * \param key Slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001669 * \param[out] lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001670 *
Gilles Peskine28538492018-07-11 17:34:00 +02001671 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001672 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001673 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -07001674 * The key slot is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001675 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1676 * \retval #PSA_ERROR_HARDWARE_FAILURE
1677 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001678 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001679 * The library has not been previously initialized by psa_crypto_init().
1680 * It is implementation-dependent whether a failure to initialize
1681 * results in this error code.
Gilles Peskined393e182018-03-08 07:49:16 +01001682 */
Gilles Peskine609b6a52018-03-03 21:31:50 +01001683psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1684 psa_key_lifetime_t *lifetime);
1685
Gilles Peskined393e182018-03-08 07:49:16 +01001686/** \brief Change the lifetime of a key slot.
1687 *
1688 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +01001689 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +01001690 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001691 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001692 * \param key Slot whose lifetime is to be changed.
1693 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001694 *
Gilles Peskine28538492018-07-11 17:34:00 +02001695 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001696 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001697 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603804cd712018-03-20 22:44:08 +02001698 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -07001699 * or the lifetime value is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001700 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001701 * The implementation does not support the specified lifetime value,
1702 * at least for the specified key slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001703 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001704 * The slot contains a key, and the implementation does not support
1705 * changing the lifetime of an occupied slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001706 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1707 * \retval #PSA_ERROR_HARDWARE_FAILURE
1708 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001709 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001710 * The library has not been previously initialized by psa_crypto_init().
1711 * It is implementation-dependent whether a failure to initialize
1712 * results in this error code.
Gilles Peskined393e182018-03-08 07:49:16 +01001713 */
1714psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -07001715 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +01001716
Gilles Peskine609b6a52018-03-03 21:31:50 +01001717/**@}*/
1718
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001719/** \defgroup hash Message digests
1720 * @{
1721 */
1722
Gilles Peskine308b91d2018-02-08 09:47:44 +01001723/** The type of the state data structure for multipart hash operations.
1724 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001725 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001726 * make any assumptions about the content of this structure except
1727 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001728typedef struct psa_hash_operation_s psa_hash_operation_t;
1729
Gilles Peskine308b91d2018-02-08 09:47:44 +01001730/** The size of the output of psa_hash_finish(), in bytes.
1731 *
1732 * This is also the hash size that psa_hash_verify() expects.
1733 *
1734 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001735 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
Gilles Peskinebe42f312018-07-13 14:38:15 +02001736 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
Gilles Peskine35855962018-04-19 08:39:16 +02001737 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001738 *
1739 * \return The hash size for the specified hash algorithm.
1740 * If the hash algorithm is not recognized, return 0.
1741 * An implementation may return either 0 or the correct size
1742 * for a hash algorithm that it recognizes, but does not support.
1743 */
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001744#define PSA_HASH_SIZE(alg) \
1745 ( \
Gilles Peskine00709fa2018-08-22 18:25:41 +02001746 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
1747 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
1748 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
1749 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
1750 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
1751 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
1752 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
1753 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
1754 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
1755 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
1756 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
1757 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
1758 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
1759 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
1760 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001761 0)
1762
Gilles Peskine308b91d2018-02-08 09:47:44 +01001763/** Start a multipart hash operation.
1764 *
1765 * The sequence of operations to calculate a hash (message digest)
1766 * is as follows:
1767 * -# Allocate an operation object which will be passed to all the functions
1768 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001769 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001770 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001771 * of the message each time. The hash that is calculated is the hash
1772 * of the concatenation of these messages in order.
1773 * -# To calculate the hash, call psa_hash_finish().
1774 * To compare the hash with an expected value, call psa_hash_verify().
1775 *
1776 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001777 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001778 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001779 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001780 * eventually terminate the operation. The following events terminate an
1781 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001782 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001783 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001784 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001785 * \param[out] operation The operation object to use.
1786 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1787 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001788 *
Gilles Peskine28538492018-07-11 17:34:00 +02001789 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001790 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001791 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001792 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001793 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1794 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1795 * \retval #PSA_ERROR_HARDWARE_FAILURE
1796 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001797 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001798psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001799 psa_algorithm_t alg);
1800
Gilles Peskine308b91d2018-02-08 09:47:44 +01001801/** Add a message fragment to a multipart hash operation.
1802 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001803 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001804 *
1805 * If this function returns an error status, the operation becomes inactive.
1806 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001807 * \param[in,out] operation Active hash operation.
1808 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001809 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001810 *
Gilles Peskine28538492018-07-11 17:34:00 +02001811 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001812 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001813 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001814 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001815 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1816 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1817 * \retval #PSA_ERROR_HARDWARE_FAILURE
1818 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001819 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001820psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1821 const uint8_t *input,
1822 size_t input_length);
1823
Gilles Peskine308b91d2018-02-08 09:47:44 +01001824/** Finish the calculation of the hash of a message.
1825 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001826 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001827 * This function calculates the hash of the message formed by concatenating
1828 * the inputs passed to preceding calls to psa_hash_update().
1829 *
1830 * When this function returns, the operation becomes inactive.
1831 *
1832 * \warning Applications should not call this function if they expect
1833 * a specific value for the hash. Call psa_hash_verify() instead.
1834 * Beware that comparing integrity or authenticity data such as
1835 * hash values with a function such as \c memcmp is risky
1836 * because the time taken by the comparison may leak information
1837 * about the hashed data which could allow an attacker to guess
1838 * a valid hash and thereby bypass security controls.
1839 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001840 * \param[in,out] operation Active hash operation.
1841 * \param[out] hash Buffer where the hash is to be written.
1842 * \param hash_size Size of the \p hash buffer in bytes.
1843 * \param[out] hash_length On success, the number of bytes
1844 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001845 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001846 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001847 *
Gilles Peskine28538492018-07-11 17:34:00 +02001848 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001849 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001850 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001851 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001852 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001853 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001854 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001855 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001856 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1857 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1858 * \retval #PSA_ERROR_HARDWARE_FAILURE
1859 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001860 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001861psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1862 uint8_t *hash,
1863 size_t hash_size,
1864 size_t *hash_length);
1865
Gilles Peskine308b91d2018-02-08 09:47:44 +01001866/** Finish the calculation of the hash of a message and compare it with
1867 * an expected value.
1868 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001869 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001870 * This function calculates the hash of the message formed by concatenating
1871 * the inputs passed to preceding calls to psa_hash_update(). It then
1872 * compares the calculated hash with the expected hash passed as a
1873 * parameter to this function.
1874 *
1875 * When this function returns, the operation becomes inactive.
1876 *
Gilles Peskine19067982018-03-20 17:54:53 +01001877 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001878 * comparison between the actual hash and the expected hash is performed
1879 * in constant time.
1880 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001881 * \param[in,out] operation Active hash operation.
1882 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001883 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001884 *
Gilles Peskine28538492018-07-11 17:34:00 +02001885 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001886 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001887 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001888 * The hash of the message was calculated successfully, but it
1889 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001890 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001891 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001892 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1893 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1894 * \retval #PSA_ERROR_HARDWARE_FAILURE
1895 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001896 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001897psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1898 const uint8_t *hash,
1899 size_t hash_length);
1900
Gilles Peskine308b91d2018-02-08 09:47:44 +01001901/** Abort a hash operation.
1902 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001903 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001904 * \p operation structure itself. Once aborted, the operation object
1905 * can be reused for another operation by calling
1906 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001907 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001908 * You may call this function any time after the operation object has
1909 * been initialized by any of the following methods:
1910 * - A call to psa_hash_setup(), whether it succeeds or not.
1911 * - Initializing the \c struct to all-bits-zero.
1912 * - Initializing the \c struct to logical zeros, e.g.
1913 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001914 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001915 * In particular, calling psa_hash_abort() after the operation has been
1916 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1917 * psa_hash_verify() is safe and has no effect.
1918 *
1919 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001920 *
Gilles Peskine28538492018-07-11 17:34:00 +02001921 * \retval #PSA_SUCCESS
1922 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001923 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001924 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1925 * \retval #PSA_ERROR_HARDWARE_FAILURE
1926 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001927 */
1928psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001929
1930/**@}*/
1931
Gilles Peskine8c9def32018-02-08 10:02:12 +01001932/** \defgroup MAC Message authentication codes
1933 * @{
1934 */
1935
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001936/** The type of the state data structure for multipart MAC operations.
1937 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001938 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001939 * make any assumptions about the content of this structure except
1940 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001941typedef struct psa_mac_operation_s psa_mac_operation_t;
1942
Gilles Peskine89167cb2018-07-08 20:12:23 +02001943/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001944 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001945 * This function sets up the calculation of the MAC
1946 * (message authentication code) of a byte string.
1947 * To verify the MAC of a message against an
1948 * expected value, use psa_mac_verify_setup() instead.
1949 *
1950 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001951 * -# Allocate an operation object which will be passed to all the functions
1952 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001953 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001954 * The key remains associated with the operation even if the content
1955 * of the key slot changes.
1956 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1957 * of the message each time. The MAC that is calculated is the MAC
1958 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001959 * -# At the end of the message, call psa_mac_sign_finish() to finish
1960 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001961 *
1962 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +02001963 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001964 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001965 * After a successful call to psa_mac_sign_setup(), the application must
1966 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001967 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001968 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001969 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001970 * \param[out] operation The operation object to use.
1971 * \param key Slot containing the key to use for the operation.
1972 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1973 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001974 *
Gilles Peskine28538492018-07-11 17:34:00 +02001975 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001976 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001977 * \retval #PSA_ERROR_EMPTY_SLOT
1978 * \retval #PSA_ERROR_NOT_PERMITTED
1979 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001980 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001981 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001982 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001983 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1984 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1985 * \retval #PSA_ERROR_HARDWARE_FAILURE
1986 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001987 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001988 * The library has not been previously initialized by psa_crypto_init().
1989 * It is implementation-dependent whether a failure to initialize
1990 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001991 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001992psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1993 psa_key_slot_t key,
1994 psa_algorithm_t alg);
1995
1996/** Start a multipart MAC verification operation.
1997 *
1998 * This function sets up the verification of the MAC
1999 * (message authentication code) of a byte string against an expected value.
2000 *
2001 * The sequence of operations to verify a MAC is as follows:
2002 * -# Allocate an operation object which will be passed to all the functions
2003 * listed here.
2004 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
2005 * The key remains associated with the operation even if the content
2006 * of the key slot changes.
2007 * -# Call psa_mac_update() zero, one or more times, passing a fragment
2008 * of the message each time. The MAC that is calculated is the MAC
2009 * of the concatenation of these messages in order.
2010 * -# At the end of the message, call psa_mac_verify_finish() to finish
2011 * calculating the actual MAC of the message and verify it against
2012 * the expected value.
2013 *
2014 * The application may call psa_mac_abort() at any time after the operation
2015 * has been initialized with psa_mac_verify_setup().
2016 *
2017 * After a successful call to psa_mac_verify_setup(), the application must
2018 * eventually terminate the operation through one of the following methods:
2019 * - A failed call to psa_mac_update().
2020 * - A call to psa_mac_verify_finish() or psa_mac_abort().
2021 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002022 * \param[out] operation The operation object to use.
2023 * \param key Slot containing the key to use for the operation.
2024 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
2025 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02002026 *
Gilles Peskine28538492018-07-11 17:34:00 +02002027 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02002028 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002029 * \retval #PSA_ERROR_EMPTY_SLOT
2030 * \retval #PSA_ERROR_NOT_PERMITTED
2031 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02002032 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002033 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02002034 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002035 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2036 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2037 * \retval #PSA_ERROR_HARDWARE_FAILURE
2038 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002039 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002040 * The library has not been previously initialized by psa_crypto_init().
2041 * It is implementation-dependent whether a failure to initialize
2042 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02002043 */
2044psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
2045 psa_key_slot_t key,
2046 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002047
Gilles Peskinedcd14942018-07-12 00:30:52 +02002048/** Add a message fragment to a multipart MAC operation.
2049 *
2050 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
2051 * before calling this function.
2052 *
2053 * If this function returns an error status, the operation becomes inactive.
2054 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002055 * \param[in,out] operation Active MAC operation.
2056 * \param[in] input Buffer containing the message fragment to add to
2057 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002058 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002059 *
2060 * \retval #PSA_SUCCESS
2061 * Success.
2062 * \retval #PSA_ERROR_BAD_STATE
2063 * The operation state is not valid (not started, or already completed).
2064 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2065 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2066 * \retval #PSA_ERROR_HARDWARE_FAILURE
2067 * \retval #PSA_ERROR_TAMPERING_DETECTED
2068 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002069psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2070 const uint8_t *input,
2071 size_t input_length);
2072
Gilles Peskinedcd14942018-07-12 00:30:52 +02002073/** Finish the calculation of the MAC of a message.
2074 *
2075 * The application must call psa_mac_sign_setup() before calling this function.
2076 * This function calculates the MAC of the message formed by concatenating
2077 * the inputs passed to preceding calls to psa_mac_update().
2078 *
2079 * When this function returns, the operation becomes inactive.
2080 *
2081 * \warning Applications should not call this function if they expect
2082 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
2083 * Beware that comparing integrity or authenticity data such as
2084 * MAC values with a function such as \c memcmp is risky
2085 * because the time taken by the comparison may leak information
2086 * about the MAC value which could allow an attacker to guess
2087 * a valid MAC and thereby bypass security controls.
2088 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002089 * \param[in,out] operation Active MAC operation.
2090 * \param[out] mac Buffer where the MAC value is to be written.
2091 * \param mac_size Size of the \p mac buffer in bytes.
2092 * \param[out] mac_length On success, the number of bytes
2093 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002094 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02002095 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002096 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02002097 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002098 *
2099 * \retval #PSA_SUCCESS
2100 * Success.
2101 * \retval #PSA_ERROR_BAD_STATE
2102 * The operation state is not valid (not started, or already completed).
2103 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002104 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02002105 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
2106 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2107 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2108 * \retval #PSA_ERROR_HARDWARE_FAILURE
2109 * \retval #PSA_ERROR_TAMPERING_DETECTED
2110 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02002111psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2112 uint8_t *mac,
2113 size_t mac_size,
2114 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002115
Gilles Peskinedcd14942018-07-12 00:30:52 +02002116/** Finish the calculation of the MAC of a message and compare it with
2117 * an expected value.
2118 *
2119 * The application must call psa_mac_verify_setup() before calling this function.
2120 * This function calculates the MAC of the message formed by concatenating
2121 * the inputs passed to preceding calls to psa_mac_update(). It then
2122 * compares the calculated MAC with the expected MAC passed as a
2123 * parameter to this function.
2124 *
2125 * When this function returns, the operation becomes inactive.
2126 *
2127 * \note Implementations shall make the best effort to ensure that the
2128 * comparison between the actual MAC and the expected MAC is performed
2129 * in constant time.
2130 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002131 * \param[in,out] operation Active MAC operation.
2132 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002133 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002134 *
2135 * \retval #PSA_SUCCESS
2136 * The expected MAC is identical to the actual MAC of the message.
2137 * \retval #PSA_ERROR_INVALID_SIGNATURE
2138 * The MAC of the message was calculated successfully, but it
2139 * differs from the expected MAC.
2140 * \retval #PSA_ERROR_BAD_STATE
2141 * The operation state is not valid (not started, or already completed).
2142 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2143 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2144 * \retval #PSA_ERROR_HARDWARE_FAILURE
2145 * \retval #PSA_ERROR_TAMPERING_DETECTED
2146 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02002147psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2148 const uint8_t *mac,
2149 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002150
Gilles Peskinedcd14942018-07-12 00:30:52 +02002151/** Abort a MAC operation.
2152 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002153 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002154 * \p operation structure itself. Once aborted, the operation object
2155 * can be reused for another operation by calling
2156 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002157 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002158 * You may call this function any time after the operation object has
2159 * been initialized by any of the following methods:
2160 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
2161 * it succeeds or not.
2162 * - Initializing the \c struct to all-bits-zero.
2163 * - Initializing the \c struct to logical zeros, e.g.
2164 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002165 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002166 * In particular, calling psa_mac_abort() after the operation has been
2167 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
2168 * psa_mac_verify_finish() is safe and has no effect.
2169 *
2170 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002171 *
2172 * \retval #PSA_SUCCESS
2173 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002174 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002175 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2176 * \retval #PSA_ERROR_HARDWARE_FAILURE
2177 * \retval #PSA_ERROR_TAMPERING_DETECTED
2178 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002179psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
2180
2181/**@}*/
2182
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002183/** \defgroup cipher Symmetric ciphers
2184 * @{
2185 */
2186
2187/** The type of the state data structure for multipart cipher operations.
2188 *
2189 * This is an implementation-defined \c struct. Applications should not
2190 * make any assumptions about the content of this structure except
2191 * as directed by the documentation of a specific implementation. */
2192typedef struct psa_cipher_operation_s psa_cipher_operation_t;
2193
2194/** Set the key for a multipart symmetric encryption operation.
2195 *
2196 * The sequence of operations to encrypt a message with a symmetric cipher
2197 * is as follows:
2198 * -# Allocate an operation object which will be passed to all the functions
2199 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02002200 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002201 * The key remains associated with the operation even if the content
2202 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03002203 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002204 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03002205 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002206 * requires a specific IV value.
2207 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
2208 * of the message each time.
2209 * -# Call psa_cipher_finish().
2210 *
2211 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02002212 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002213 *
Gilles Peskinefe119512018-07-08 21:39:34 +02002214 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002215 * eventually terminate the operation. The following events terminate an
2216 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03002217 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002218 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002219 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002220 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002221 * \param[out] operation The operation object to use.
2222 * \param key Slot containing the key to use for the operation.
2223 * \param alg The cipher algorithm to compute
2224 * (\c PSA_ALG_XXX value such that
2225 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002226 *
Gilles Peskine28538492018-07-11 17:34:00 +02002227 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002228 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002229 * \retval #PSA_ERROR_EMPTY_SLOT
2230 * \retval #PSA_ERROR_NOT_PERMITTED
2231 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002232 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002233 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002234 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002235 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2236 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2237 * \retval #PSA_ERROR_HARDWARE_FAILURE
2238 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002239 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002240 * The library has not been previously initialized by psa_crypto_init().
2241 * It is implementation-dependent whether a failure to initialize
2242 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002243 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002244psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
2245 psa_key_slot_t key,
2246 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002247
2248/** Set the key for a multipart symmetric decryption operation.
2249 *
2250 * The sequence of operations to decrypt a message with a symmetric cipher
2251 * is as follows:
2252 * -# Allocate an operation object which will be passed to all the functions
2253 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02002254 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002255 * The key remains associated with the operation even if the content
2256 * of the key slot changes.
2257 * -# Call psa_cipher_update() with the IV (initialization vector) for the
2258 * decryption. If the IV is prepended to the ciphertext, you can call
2259 * psa_cipher_update() on a buffer containing the IV followed by the
2260 * beginning of the message.
2261 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
2262 * of the message each time.
2263 * -# Call psa_cipher_finish().
2264 *
2265 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02002266 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002267 *
Gilles Peskinefe119512018-07-08 21:39:34 +02002268 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002269 * eventually terminate the operation. The following events terminate an
2270 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002271 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002272 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002273 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002274 * \param[out] operation The operation object to use.
2275 * \param key Slot containing the key to use for the operation.
2276 * \param alg The cipher algorithm to compute
2277 * (\c PSA_ALG_XXX value such that
2278 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002279 *
Gilles Peskine28538492018-07-11 17:34:00 +02002280 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002281 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002282 * \retval #PSA_ERROR_EMPTY_SLOT
2283 * \retval #PSA_ERROR_NOT_PERMITTED
2284 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002285 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002286 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002287 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002288 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2289 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2290 * \retval #PSA_ERROR_HARDWARE_FAILURE
2291 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002292 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002293 * The library has not been previously initialized by psa_crypto_init().
2294 * It is implementation-dependent whether a failure to initialize
2295 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002296 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002297psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
2298 psa_key_slot_t key,
2299 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002300
Gilles Peskinedcd14942018-07-12 00:30:52 +02002301/** Generate an IV for a symmetric encryption operation.
2302 *
2303 * This function generates a random IV (initialization vector), nonce
2304 * or initial counter value for the encryption operation as appropriate
2305 * for the chosen algorithm, key type and key size.
2306 *
2307 * The application must call psa_cipher_encrypt_setup() before
2308 * calling this function.
2309 *
2310 * If this function returns an error status, the operation becomes inactive.
2311 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002312 * \param[in,out] operation Active cipher operation.
2313 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002314 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002315 * \param[out] iv_length On success, the number of bytes of the
2316 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002317 *
2318 * \retval #PSA_SUCCESS
2319 * Success.
2320 * \retval #PSA_ERROR_BAD_STATE
2321 * The operation state is not valid (not started, or IV already set).
2322 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002323 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002324 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2325 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2326 * \retval #PSA_ERROR_HARDWARE_FAILURE
2327 * \retval #PSA_ERROR_TAMPERING_DETECTED
2328 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002329psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
2330 unsigned char *iv,
2331 size_t iv_size,
2332 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002333
Gilles Peskinedcd14942018-07-12 00:30:52 +02002334/** Set the IV for a symmetric encryption or decryption operation.
2335 *
2336 * This function sets the random IV (initialization vector), nonce
2337 * or initial counter value for the encryption or decryption operation.
2338 *
2339 * The application must call psa_cipher_encrypt_setup() before
2340 * calling this function.
2341 *
2342 * If this function returns an error status, the operation becomes inactive.
2343 *
2344 * \note When encrypting, applications should use psa_cipher_generate_iv()
2345 * instead of this function, unless implementing a protocol that requires
2346 * a non-random IV.
2347 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002348 * \param[in,out] operation Active cipher operation.
2349 * \param[in] iv Buffer containing the IV to use.
2350 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002351 *
2352 * \retval #PSA_SUCCESS
2353 * Success.
2354 * \retval #PSA_ERROR_BAD_STATE
2355 * The operation state is not valid (not started, or IV already set).
2356 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002357 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02002358 * or the chosen algorithm does not use an IV.
2359 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2360 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2361 * \retval #PSA_ERROR_HARDWARE_FAILURE
2362 * \retval #PSA_ERROR_TAMPERING_DETECTED
2363 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002364psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
2365 const unsigned char *iv,
2366 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002367
Gilles Peskinedcd14942018-07-12 00:30:52 +02002368/** Encrypt or decrypt a message fragment in an active cipher operation.
2369 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02002370 * Before calling this function, you must:
2371 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
2372 * The choice of setup function determines whether this function
2373 * encrypts or decrypts its input.
2374 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
2375 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002376 *
2377 * If this function returns an error status, the operation becomes inactive.
2378 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002379 * \param[in,out] operation Active cipher operation.
2380 * \param[in] input Buffer containing the message fragment to
2381 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002382 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002383 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002384 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002385 * \param[out] output_length On success, the number of bytes
2386 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002387 *
2388 * \retval #PSA_SUCCESS
2389 * Success.
2390 * \retval #PSA_ERROR_BAD_STATE
2391 * The operation state is not valid (not started, IV required but
2392 * not set, or already completed).
2393 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2394 * The size of the \p output buffer is too small.
2395 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2396 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2397 * \retval #PSA_ERROR_HARDWARE_FAILURE
2398 * \retval #PSA_ERROR_TAMPERING_DETECTED
2399 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002400psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2401 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002402 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002403 unsigned char *output,
2404 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002405 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002406
Gilles Peskinedcd14942018-07-12 00:30:52 +02002407/** Finish encrypting or decrypting a message in a cipher operation.
2408 *
2409 * The application must call psa_cipher_encrypt_setup() or
2410 * psa_cipher_decrypt_setup() before calling this function. The choice
2411 * of setup function determines whether this function encrypts or
2412 * decrypts its input.
2413 *
2414 * This function finishes the encryption or decryption of the message
2415 * formed by concatenating the inputs passed to preceding calls to
2416 * psa_cipher_update().
2417 *
2418 * When this function returns, the operation becomes inactive.
2419 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002420 * \param[in,out] operation Active cipher operation.
2421 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002422 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002423 * \param[out] output_length On success, the number of bytes
2424 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002425 *
2426 * \retval #PSA_SUCCESS
2427 * Success.
2428 * \retval #PSA_ERROR_BAD_STATE
2429 * The operation state is not valid (not started, IV required but
2430 * not set, or already completed).
2431 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2432 * The size of the \p output buffer is too small.
2433 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2434 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2435 * \retval #PSA_ERROR_HARDWARE_FAILURE
2436 * \retval #PSA_ERROR_TAMPERING_DETECTED
2437 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002438psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002439 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002440 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002441 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002442
Gilles Peskinedcd14942018-07-12 00:30:52 +02002443/** Abort a cipher operation.
2444 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002445 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002446 * \p operation structure itself. Once aborted, the operation object
2447 * can be reused for another operation by calling
2448 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002449 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002450 * You may call this function any time after the operation object has
2451 * been initialized by any of the following methods:
2452 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2453 * whether it succeeds or not.
2454 * - Initializing the \c struct to all-bits-zero.
2455 * - Initializing the \c struct to logical zeros, e.g.
2456 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002457 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002458 * In particular, calling psa_cipher_abort() after the operation has been
2459 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2460 * is safe and has no effect.
2461 *
2462 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002463 *
2464 * \retval #PSA_SUCCESS
2465 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002466 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002467 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2468 * \retval #PSA_ERROR_HARDWARE_FAILURE
2469 * \retval #PSA_ERROR_TAMPERING_DETECTED
2470 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002471psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2472
2473/**@}*/
2474
Gilles Peskine3b555712018-03-03 21:27:57 +01002475/** \defgroup aead Authenticated encryption with associated data (AEAD)
2476 * @{
2477 */
2478
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002479/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01002480 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002481 * \param alg An AEAD algorithm
2482 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002483 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002484 *
2485 * \return The tag size for the specified algorithm.
2486 * If the AEAD algorithm does not have an identified
2487 * tag that can be distinguished from the rest of
2488 * the ciphertext, return 0.
2489 * If the AEAD algorithm is not recognized, return 0.
2490 * An implementation may return either 0 or a
2491 * correct size for an AEAD algorithm that it
2492 * recognizes, but does not support.
2493 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002494#define PSA_AEAD_TAG_LENGTH(alg) \
2495 (PSA_ALG_IS_AEAD(alg) ? \
2496 (((alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> PSA_AEAD_TAG_LENGTH_OFFSET) : \
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002497 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01002498
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002499/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002500 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002501 * \param key Slot containing the key to use.
2502 * \param alg The AEAD algorithm to compute
2503 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002504 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002505 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002506 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002507 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002508 * but not encrypted.
2509 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002510 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002511 * encrypted.
2512 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002513 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002514 * encrypted data. The additional data is not
2515 * part of this output. For algorithms where the
2516 * encrypted data and the authentication tag
2517 * are defined as separate outputs, the
2518 * authentication tag is appended to the
2519 * encrypted data.
2520 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2521 * This must be at least
2522 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2523 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002524 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002525 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002526 *
Gilles Peskine28538492018-07-11 17:34:00 +02002527 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002528 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002529 * \retval #PSA_ERROR_EMPTY_SLOT
2530 * \retval #PSA_ERROR_NOT_PERMITTED
2531 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002532 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002533 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002534 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002535 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2536 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2537 * \retval #PSA_ERROR_HARDWARE_FAILURE
2538 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002539 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002540 * The library has not been previously initialized by psa_crypto_init().
2541 * It is implementation-dependent whether a failure to initialize
2542 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002543 */
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002544psa_status_t psa_aead_encrypt(psa_key_slot_t key,
2545 psa_algorithm_t alg,
2546 const uint8_t *nonce,
2547 size_t nonce_length,
2548 const uint8_t *additional_data,
2549 size_t additional_data_length,
2550 const uint8_t *plaintext,
2551 size_t plaintext_length,
2552 uint8_t *ciphertext,
2553 size_t ciphertext_size,
2554 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002555
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002556/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002557 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002558 * \param key Slot containing the key to use.
2559 * \param alg The AEAD algorithm to compute
2560 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002561 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002562 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002563 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002564 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002565 * but not encrypted.
2566 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002567 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002568 * encrypted. For algorithms where the
2569 * encrypted data and the authentication tag
2570 * are defined as separate inputs, the buffer
2571 * must contain the encrypted data followed
2572 * by the authentication tag.
2573 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002574 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002575 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2576 * This must be at least
2577 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2578 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002579 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03002580 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002581 *
Gilles Peskine28538492018-07-11 17:34:00 +02002582 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002583 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002584 * \retval #PSA_ERROR_EMPTY_SLOT
2585 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002586 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002587 * \retval #PSA_ERROR_NOT_PERMITTED
2588 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002589 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002590 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002591 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002592 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2593 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2594 * \retval #PSA_ERROR_HARDWARE_FAILURE
2595 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002596 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002597 * The library has not been previously initialized by psa_crypto_init().
2598 * It is implementation-dependent whether a failure to initialize
2599 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002600 */
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002601psa_status_t psa_aead_decrypt(psa_key_slot_t key,
2602 psa_algorithm_t alg,
2603 const uint8_t *nonce,
2604 size_t nonce_length,
2605 const uint8_t *additional_data,
2606 size_t additional_data_length,
2607 const uint8_t *ciphertext,
2608 size_t ciphertext_length,
2609 uint8_t *plaintext,
2610 size_t plaintext_size,
2611 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002612
2613/**@}*/
2614
Gilles Peskine20035e32018-02-03 22:44:14 +01002615/** \defgroup asymmetric Asymmetric cryptography
2616 * @{
2617 */
2618
2619/**
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002620 * \brief ECDSA signature size for a given curve bit size
Gilles Peskine0189e752018-02-03 23:57:22 +01002621 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002622 * \param curve_bits Curve size in bits.
2623 * \return Signature size in bytes.
Gilles Peskine0189e752018-02-03 23:57:22 +01002624 *
2625 * \note This macro returns a compile-time constant if its argument is one.
Gilles Peskine0189e752018-02-03 23:57:22 +01002626 */
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002627#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
2628 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskine0189e752018-02-03 23:57:22 +01002629
Gilles Peskine0189e752018-02-03 23:57:22 +01002630/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002631 * \brief Sign a hash or short message with a private key.
2632 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002633 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002634 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002635 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2636 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2637 * to determine the hash algorithm to use.
2638 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002639 * \param key Key slot containing an asymmetric key pair.
2640 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002641 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002642 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002643 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002644 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002645 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002646 * \param[out] signature_length On success, the number of bytes
2647 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002648 *
Gilles Peskine28538492018-07-11 17:34:00 +02002649 * \retval #PSA_SUCCESS
2650 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002651 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002652 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002653 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002654 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002655 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002656 * \retval #PSA_ERROR_NOT_SUPPORTED
2657 * \retval #PSA_ERROR_INVALID_ARGUMENT
2658 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2659 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2660 * \retval #PSA_ERROR_HARDWARE_FAILURE
2661 * \retval #PSA_ERROR_TAMPERING_DETECTED
2662 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002663 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002664 * The library has not been previously initialized by psa_crypto_init().
2665 * It is implementation-dependent whether a failure to initialize
2666 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002667 */
2668psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
2669 psa_algorithm_t alg,
2670 const uint8_t *hash,
2671 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002672 uint8_t *signature,
2673 size_t signature_size,
2674 size_t *signature_length);
2675
2676/**
2677 * \brief Verify the signature a hash or short message using a public key.
2678 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002679 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002680 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002681 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2682 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2683 * to determine the hash algorithm to use.
2684 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01002685 * \param key Key slot containing a public key or an
2686 * asymmetric key pair.
2687 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002688 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002689 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002690 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002691 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002692 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002693 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002694 *
Gilles Peskine28538492018-07-11 17:34:00 +02002695 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002696 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002697 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002698 * The calculation was perfomed successfully, but the passed
2699 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002700 * \retval #PSA_ERROR_NOT_SUPPORTED
2701 * \retval #PSA_ERROR_INVALID_ARGUMENT
2702 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2703 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2704 * \retval #PSA_ERROR_HARDWARE_FAILURE
2705 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002706 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002707 * The library has not been previously initialized by psa_crypto_init().
2708 * It is implementation-dependent whether a failure to initialize
2709 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002710 */
2711psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
2712 psa_algorithm_t alg,
2713 const uint8_t *hash,
2714 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002715 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002716 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002717
Gilles Peskine723feff2018-05-31 20:08:13 +02002718#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
Gilles Peskine072ac562018-06-30 00:21:29 +02002719 (PSA_ALG_IS_RSA_OAEP(alg) ? \
2720 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskine723feff2018-05-31 20:08:13 +02002721 11 /*PKCS#1v1.5*/)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002722
2723/**
2724 * \brief Encrypt a short message with a public key.
2725 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002726 * \param key Key slot containing a public key or an
2727 * asymmetric key pair.
2728 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002729 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002730 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002731 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002732 * \param[in] salt A salt or label, if supported by the
2733 * encryption algorithm.
2734 * If the algorithm does not support a
2735 * salt, pass \c NULL.
2736 * If the algorithm supports an optional
2737 * salt and you do not want to pass a salt,
2738 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002739 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002740 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2741 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002742 * \param salt_length Size of the \p salt buffer in bytes.
2743 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002744 * \param[out] output Buffer where the encrypted message is to
2745 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002746 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002747 * \param[out] output_length On success, the number of bytes
2748 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002749 *
Gilles Peskine28538492018-07-11 17:34:00 +02002750 * \retval #PSA_SUCCESS
2751 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002752 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002753 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002754 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002755 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002756 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002757 * \retval #PSA_ERROR_NOT_SUPPORTED
2758 * \retval #PSA_ERROR_INVALID_ARGUMENT
2759 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2760 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2761 * \retval #PSA_ERROR_HARDWARE_FAILURE
2762 * \retval #PSA_ERROR_TAMPERING_DETECTED
2763 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002764 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002765 * The library has not been previously initialized by psa_crypto_init().
2766 * It is implementation-dependent whether a failure to initialize
2767 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002768 */
2769psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
2770 psa_algorithm_t alg,
2771 const uint8_t *input,
2772 size_t input_length,
2773 const uint8_t *salt,
2774 size_t salt_length,
2775 uint8_t *output,
2776 size_t output_size,
2777 size_t *output_length);
2778
2779/**
2780 * \brief Decrypt a short message with a private key.
2781 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002782 * \param key Key slot containing an asymmetric key pair.
2783 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002784 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002785 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002786 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002787 * \param[in] salt A salt or label, if supported by the
2788 * encryption algorithm.
2789 * If the algorithm does not support a
2790 * salt, pass \c NULL.
2791 * If the algorithm supports an optional
2792 * salt and you do not want to pass a salt,
2793 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002794 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002795 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2796 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002797 * \param salt_length Size of the \p salt buffer in bytes.
2798 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002799 * \param[out] output Buffer where the decrypted message is to
2800 * be written.
2801 * \param output_size Size of the \c output buffer in bytes.
2802 * \param[out] output_length On success, the number of bytes
2803 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002804 *
Gilles Peskine28538492018-07-11 17:34:00 +02002805 * \retval #PSA_SUCCESS
2806 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002807 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002808 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002809 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002810 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002811 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002812 * \retval #PSA_ERROR_NOT_SUPPORTED
2813 * \retval #PSA_ERROR_INVALID_ARGUMENT
2814 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2815 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2816 * \retval #PSA_ERROR_HARDWARE_FAILURE
2817 * \retval #PSA_ERROR_TAMPERING_DETECTED
2818 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2819 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002820 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002821 * The library has not been previously initialized by psa_crypto_init().
2822 * It is implementation-dependent whether a failure to initialize
2823 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002824 */
2825psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
2826 psa_algorithm_t alg,
2827 const uint8_t *input,
2828 size_t input_length,
2829 const uint8_t *salt,
2830 size_t salt_length,
2831 uint8_t *output,
2832 size_t output_size,
2833 size_t *output_length);
2834
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002835/**@}*/
2836
Gilles Peskineedd76872018-07-20 17:42:05 +02002837/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002838 * @{
2839 */
2840
2841/** The type of the state data structure for generators.
2842 *
2843 * Before calling any function on a generator, the application must
2844 * initialize it by any of the following means:
2845 * - Set the structure to all-bits-zero, for example:
2846 * \code
2847 * psa_crypto_generator_t generator;
2848 * memset(&generator, 0, sizeof(generator));
2849 * \endcode
2850 * - Initialize the structure to logical zero values, for example:
2851 * \code
2852 * psa_crypto_generator_t generator = {0};
2853 * \endcode
2854 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2855 * for example:
2856 * \code
2857 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2858 * \endcode
2859 * - Assign the result of the function psa_crypto_generator_init()
2860 * to the structure, for example:
2861 * \code
2862 * psa_crypto_generator_t generator;
2863 * generator = psa_crypto_generator_init();
2864 * \endcode
2865 *
2866 * This is an implementation-defined \c struct. Applications should not
2867 * make any assumptions about the content of this structure except
2868 * as directed by the documentation of a specific implementation.
2869 */
2870typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2871
2872/** \def PSA_CRYPTO_GENERATOR_INIT
2873 *
2874 * This macro returns a suitable initializer for a generator object
2875 * of type #psa_crypto_generator_t.
2876 */
2877#ifdef __DOXYGEN_ONLY__
2878/* This is an example definition for documentation purposes.
2879 * Implementations should define a suitable value in `crypto_struct.h`.
2880 */
2881#define PSA_CRYPTO_GENERATOR_INIT {0}
2882#endif
2883
2884/** Return an initial value for a generator object.
2885 */
2886static psa_crypto_generator_t psa_crypto_generator_init(void);
2887
2888/** Retrieve the current capacity of a generator.
2889 *
2890 * The capacity of a generator is the maximum number of bytes that it can
2891 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2892 *
2893 * \param[in] generator The generator to query.
2894 * \param[out] capacity On success, the capacity of the generator.
2895 *
2896 * \retval PSA_SUCCESS
2897 * \retval PSA_ERROR_BAD_STATE
2898 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2899 */
2900psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2901 size_t *capacity);
2902
2903/** Read some data from a generator.
2904 *
2905 * This function reads and returns a sequence of bytes from a generator.
2906 * The data that is read is discarded from the generator. The generator's
2907 * capacity is decreased by the number of bytes read.
2908 *
2909 * \param[in,out] generator The generator object to read from.
2910 * \param[out] output Buffer where the generator output will be
2911 * written.
2912 * \param output_length Number of bytes to output.
2913 *
2914 * \retval PSA_SUCCESS
2915 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2916 * There were fewer than \p output_length bytes
2917 * in the generator. Note that in this case, no
2918 * output is written to the output buffer.
2919 * The generator's capacity is set to 0, thus
2920 * subsequent calls to this function will not
2921 * succeed, even with a smaller output buffer.
2922 * \retval PSA_ERROR_BAD_STATE
2923 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2924 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2925 * \retval PSA_ERROR_HARDWARE_FAILURE
2926 * \retval PSA_ERROR_TAMPERING_DETECTED
2927 */
2928psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2929 uint8_t *output,
2930 size_t output_length);
2931
2932/** Create a symmetric key from data read from a generator.
2933 *
2934 * This function reads a sequence of bytes from a generator and imports
2935 * these bytes as a key.
2936 * The data that is read is discarded from the generator. The generator's
2937 * capacity is decreased by the number of bytes read.
2938 *
2939 * This function is equivalent to calling #psa_generator_read and
2940 * passing the resulting output to #psa_import_key, but
2941 * if the implementation provides an isolation boundary then
2942 * the key material is not exposed outside the isolation boundary.
2943 *
2944 * \param key Slot where the key will be stored. This must be a
2945 * valid slot for a key of the chosen type. It must
2946 * be unoccupied.
2947 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2948 * This must be a symmetric key type.
2949 * \param bits Key size in bits.
2950 * \param[in,out] generator The generator object to read from.
2951 *
2952 * \retval PSA_SUCCESS
2953 * Success.
2954 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2955 * There were fewer than \p output_length bytes
2956 * in the generator. Note that in this case, no
2957 * output is written to the output buffer.
2958 * The generator's capacity is set to 0, thus
2959 * subsequent calls to this function will not
2960 * succeed, even with a smaller output buffer.
2961 * \retval PSA_ERROR_NOT_SUPPORTED
2962 * The key type or key size is not supported, either by the
2963 * implementation in general or in this particular slot.
2964 * \retval PSA_ERROR_BAD_STATE
2965 * \retval PSA_ERROR_INVALID_ARGUMENT
2966 * The key slot is invalid.
2967 * \retval PSA_ERROR_OCCUPIED_SLOT
2968 * There is already a key in the specified slot.
2969 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2970 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
2971 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2972 * \retval PSA_ERROR_HARDWARE_FAILURE
2973 * \retval PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002974 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002975 * The library has not been previously initialized by psa_crypto_init().
2976 * It is implementation-dependent whether a failure to initialize
2977 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002978 */
2979psa_status_t psa_generator_import_key(psa_key_slot_t key,
2980 psa_key_type_t type,
2981 size_t bits,
2982 psa_crypto_generator_t *generator);
2983
2984/** Abort a generator.
2985 *
2986 * Once a generator has been aborted, its capacity is zero.
2987 * Aborting a generator frees all associated resources except for the
2988 * \c generator structure itself.
2989 *
2990 * This function may be called at any time as long as the generator
2991 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2992 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2993 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2994 * on a generator that has not been set up.
2995 *
2996 * Once aborted, the generator object may be called.
2997 *
2998 * \param[in,out] generator The generator to abort.
2999 *
3000 * \retval PSA_SUCCESS
3001 * \retval PSA_ERROR_BAD_STATE
3002 * \retval PSA_ERROR_COMMUNICATION_FAILURE
3003 * \retval PSA_ERROR_HARDWARE_FAILURE
3004 * \retval PSA_ERROR_TAMPERING_DETECTED
3005 */
3006psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3007
3008/**@}*/
3009
Gilles Peskineea0fb492018-07-12 17:17:20 +02003010/** \defgroup derivation Key derivation
3011 * @{
3012 */
3013
3014/** Set up a key derivation operation.
3015 *
3016 * A key derivation algorithm takes three inputs: a secret input \p key and
3017 * two non-secret inputs \p label and p salt.
3018 * The result of this function is a byte generator which can
3019 * be used to produce keys and other cryptographic material.
3020 *
3021 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02003022 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
3023 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003024 *
3025 * \param[in,out] generator The generator object to set up. It must
3026 * have been initialized to .
3027 * \param key Slot containing the secret key to use.
3028 * \param alg The key derivation algorithm to compute
3029 * (\c PSA_ALG_XXX value such that
3030 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3031 * \param[in] salt Salt to use.
3032 * \param salt_length Size of the \p salt buffer in bytes.
3033 * \param[in] label Label to use.
3034 * \param label_length Size of the \p label buffer in bytes.
3035 * \param capacity The maximum number of bytes that the
3036 * generator will be able to provide.
3037 *
3038 * \retval #PSA_SUCCESS
3039 * Success.
3040 * \retval #PSA_ERROR_EMPTY_SLOT
3041 * \retval #PSA_ERROR_NOT_PERMITTED
3042 * \retval #PSA_ERROR_INVALID_ARGUMENT
3043 * \c key is not compatible with \c alg,
3044 * or \p capacity is too large for the specified algorithm and key.
3045 * \retval #PSA_ERROR_NOT_SUPPORTED
3046 * \c alg is not supported or is not a key derivation algorithm.
3047 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3048 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3049 * \retval #PSA_ERROR_HARDWARE_FAILURE
3050 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003051 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003052 * The library has not been previously initialized by psa_crypto_init().
3053 * It is implementation-dependent whether a failure to initialize
3054 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003055 */
3056psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003057 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003058 psa_algorithm_t alg,
3059 const uint8_t *salt,
3060 size_t salt_length,
3061 const uint8_t *label,
3062 size_t label_length,
3063 size_t capacity);
3064
3065/**@}*/
3066
Gilles Peskineedd76872018-07-20 17:42:05 +02003067/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003068 * @{
3069 */
3070
3071/**
3072 * \brief Generate random bytes.
3073 *
3074 * \warning This function **can** fail! Callers MUST check the return status
3075 * and MUST NOT use the content of the output buffer if the return
3076 * status is not #PSA_SUCCESS.
3077 *
3078 * \note To generate a key, use psa_generate_key() instead.
3079 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003080 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003081 * \param output_size Number of bytes to generate and output.
3082 *
Gilles Peskine28538492018-07-11 17:34:00 +02003083 * \retval #PSA_SUCCESS
3084 * \retval #PSA_ERROR_NOT_SUPPORTED
3085 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3086 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3087 * \retval #PSA_ERROR_HARDWARE_FAILURE
3088 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003089 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003090 * The library has not been previously initialized by psa_crypto_init().
3091 * It is implementation-dependent whether a failure to initialize
3092 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003093 */
3094psa_status_t psa_generate_random(uint8_t *output,
3095 size_t output_size);
3096
Gilles Peskine4c317f42018-07-12 01:24:09 +02003097/** Extra parameters for RSA key generation.
3098 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02003099 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02003100 * parameter to psa_generate_key().
3101 */
3102typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02003103 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02003104} psa_generate_key_extra_rsa;
3105
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003106/**
3107 * \brief Generate a key or key pair.
3108 *
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003109 * \param key Slot where the key will be stored. This must be a
3110 * valid slot for a key of the chosen type. It must
3111 * be unoccupied.
3112 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
3113 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02003114 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003115 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003116 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003117 * default parameters. Implementation that support
3118 * the generation of vendor-specific key types
3119 * that allow extra parameters shall document
3120 * the format of these extra parameters and
3121 * the default values. For standard parameters,
3122 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003123 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003124 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
3125 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003126 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003127 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
3128 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003129 * - For an RSA key (\p type is
3130 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3131 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003132 * specifying the public exponent. The
3133 * default public exponent used when \p extra
3134 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02003135 * \param extra_size Size of the buffer that \p extra
3136 * points to, in bytes. Note that if \p extra is
3137 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003138 *
Gilles Peskine28538492018-07-11 17:34:00 +02003139 * \retval #PSA_SUCCESS
3140 * \retval #PSA_ERROR_NOT_SUPPORTED
3141 * \retval #PSA_ERROR_INVALID_ARGUMENT
3142 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3143 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3144 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3145 * \retval #PSA_ERROR_HARDWARE_FAILURE
3146 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003147 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003148 * The library has not been previously initialized by psa_crypto_init().
3149 * It is implementation-dependent whether a failure to initialize
3150 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003151 */
3152psa_status_t psa_generate_key(psa_key_slot_t key,
3153 psa_key_type_t type,
3154 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003155 const void *extra,
3156 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003157
3158/**@}*/
3159
Gilles Peskinee59236f2018-01-27 23:32:46 +01003160#ifdef __cplusplus
3161}
3162#endif
3163
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003164/* The file "crypto_sizes.h" contains definitions for size calculation
3165 * macros whose definitions are implementation-specific. */
3166#include "crypto_sizes.h"
3167
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003168/* The file "crypto_struct.h" contains definitions for
3169 * implementation-specific structs that are declared above. */
3170#include "crypto_struct.h"
3171
3172/* The file "crypto_extra.h" contains vendor-specific definitions. This
3173 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003174#include "crypto_extra.h"
3175
3176#endif /* PSA_CRYPTO_H */