blob: 62d392017fceec1f310ab32ece3f4411268a45aa [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 Peskine8c9def32018-02-08 10:02:12 +0100759#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
760#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
761#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
762#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200763
764/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
765 *
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200766 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
767 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200768 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
769 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200770 * algorithm identifier.
771 */
Gilles Peskine9df2dc82018-08-22 18:24:17 +0200772#define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100773 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
774 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100775
Gilles Peskinedaea26f2018-08-21 14:02:45 +0200776#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
777#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100778
Gilles Peskinedcd14942018-07-12 00:30:52 +0200779/** Whether the specified algorithm is a stream cipher.
780 *
781 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
782 * by applying a bitwise-xor with a stream of bytes that is generated
783 * from a key.
784 *
785 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
786 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200787 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
788 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200789 * algorithm identifier or if it is not a symmetric cipher algorithm.
790 */
Moran Pekerbed71a22018-04-22 20:19:20 +0300791#define PSA_ALG_IS_STREAM_CIPHER(alg) \
Gilles Peskinedaea26f2018-08-21 14:02:45 +0200792 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
793 (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
794
795/** The ARC4 stream cipher algorithm.
796 */
797#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800001)
798
799/** The CTR stream cipher mode.
800 *
801 * CTR is a stream cipher which is built from a block cipher.
802 * The underlying block cipher is determined by the key type.
803 * For example, to use AES-128-CTR, use this algorithm with
804 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
805 */
806#define PSA_ALG_CTR ((psa_algorithm_t)0x04c00001)
807
808#define PSA_ALG_CFB ((psa_algorithm_t)0x04c00002)
809
810#define PSA_ALG_OFB ((psa_algorithm_t)0x04c00003)
811
812/** The XTS cipher mode.
813 *
814 * XTS is a cipher mode which is built from a block cipher. It requires at
815 * least one full block of input, but beyond this minimum the input
816 * does not need to be a whole number of blocks.
817 */
818#define PSA_ALG_XTS ((psa_algorithm_t)0x044000ff)
819
820/** The CBC block cipher chaining mode, with no padding.
821 *
822 * The underlying block cipher is determined by the key type.
823 *
824 * This symmetric cipher mode can only be used with messages whose lengths
825 * are whole number of blocks for the chosen block cipher.
826 */
827#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04600100)
828
829/** The CBC block cipher chaining mode with PKCS#7 padding.
830 *
831 * The underlying block cipher is determined by the key type.
832 *
833 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
834 */
835#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04600101)
Moran Pekerbed71a22018-04-22 20:19:20 +0300836
Gilles Peskine8c9def32018-02-08 10:02:12 +0100837#define PSA_ALG_CCM ((psa_algorithm_t)0x06000001)
838#define PSA_ALG_GCM ((psa_algorithm_t)0x06000002)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100839
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200840#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
841/** RSA PKCS#1 v1.5 signature with hashing.
842 *
843 * This is the signature scheme defined by RFC 8017
844 * (PKCS#1: RSA Cryptography Specifications) under the name
845 * RSASSA-PKCS1-v1_5.
846 *
847 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200848 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200849 *
850 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
851 * \return Unspecified if \p alg is not a supported
852 * hash algorithm.
853 */
Gilles Peskinea5926232018-03-28 14:16:50 +0200854#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200855 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
856/** Raw PKCS#1 v1.5 signature.
857 *
858 * The input to this algorithm is the DigestInfo structure used by
859 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
860 * steps 3&ndash;6.
861 */
862#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
Gilles Peskinea5926232018-03-28 14:16:50 +0200863#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200864 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200865
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200866#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
867/** RSA PSS signature with hashing.
868 *
869 * This is the signature scheme defined by RFC 8017
870 * (PKCS#1: RSA Cryptography Specifications) under the name
Gilles Peskinea4d20bd2018-06-29 23:35:02 +0200871 * RSASSA-PSS, with the message generation function MGF1, and with
872 * a salt length equal to the length of the hash. The specified
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200873 * hash algorithm is used to hash the input message, to create the
874 * salted hash, and for the mask generation.
875 *
876 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200877 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200878 *
879 * \return The corresponding RSA PSS signature algorithm.
880 * \return Unspecified if \p alg is not a supported
881 * hash algorithm.
882 */
883#define PSA_ALG_RSA_PSS(hash_alg) \
884 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
885#define PSA_ALG_IS_RSA_PSS(alg) \
886 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
887
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200888#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
889/** DSA signature with hashing.
890 *
891 * This is the signature scheme defined by FIPS 186-4,
892 * with a random per-message secret number (*k*).
893 *
894 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200895 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200896 *
897 * \return The corresponding DSA signature algorithm.
898 * \return Unspecified if \p alg is not a supported
899 * hash algorithm.
900 */
901#define PSA_ALG_DSA(hash_alg) \
902 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
903#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
904#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
905#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
906 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
907#define PSA_ALG_IS_DSA(alg) \
908 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
909 PSA_ALG_DSA_BASE)
910#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
911 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +0200912#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
913 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
914#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
915 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200916
917#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
918/** ECDSA signature with hashing.
919 *
920 * This is the ECDSA signature scheme defined by ANSI X9.62,
921 * with a random per-message secret number (*k*).
922 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200923 * The representation of the signature as a byte string consists of
924 * the concatentation of the signature values *r* and *s*. Each of
925 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
926 * of the base point of the curve in octets. Each value is represented
927 * in big-endian order (most significant octet first).
928 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200929 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200930 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200931 *
932 * \return The corresponding ECDSA signature algorithm.
933 * \return Unspecified if \p alg is not a supported
934 * hash algorithm.
935 */
936#define PSA_ALG_ECDSA(hash_alg) \
937 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
938/** ECDSA signature without hashing.
939 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200940 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200941 * without specifying a hash algorithm. This algorithm may only be
942 * used to sign or verify a sequence of bytes that should be an
943 * already-calculated hash. Note that the input is padded with
944 * zeros on the left or truncated on the left as required to fit
945 * the curve size.
946 */
947#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
948#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
949/** Deterministic ECDSA signature with hashing.
950 *
951 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
952 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +0200953 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
954 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200955 * Note that when this algorithm is used for verification, signatures
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200956 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200957 * same private key are accepted. In other words,
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200958 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
959 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200960 *
961 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200962 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200963 *
964 * \return The corresponding deterministic ECDSA signature
965 * algorithm.
966 * \return Unspecified if \p alg is not a supported
967 * hash algorithm.
968 */
969#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
970 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
971#define PSA_ALG_IS_ECDSA(alg) \
972 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
973 PSA_ALG_ECDSA_BASE)
974#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
975 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +0200976#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
977 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
978#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
979 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +0200980
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200981/** Get the hash used by a hash-and-sign signature algorithm.
982 *
983 * A hash-and-sign algorithm is a signature algorithm which is
984 * composed of two phases: first a hashing phase which does not use
985 * the key and produces a hash of the input message, then a signing
986 * phase which only uses the hash and the key and not the message
987 * itself.
988 *
989 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200990 * #PSA_ALG_IS_SIGN(\p alg) is true).
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200991 *
992 * \return The underlying hash algorithm if \p alg is a hash-and-sign
993 * algorithm.
994 * \return 0 if \p alg is a signature algorithm that does not
995 * follow the hash-and-sign structure.
996 * \return Unspecified if \p alg is not a signature algorithm or
997 * if it is not supported by the implementation.
998 */
999#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001000 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
1001 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
Gilles Peskine54622ae2018-06-29 22:24:24 +02001002 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001003 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1004 0)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001005
Gilles Peskinedcd14942018-07-12 00:30:52 +02001006/** RSA PKCS#1 v1.5 encryption.
1007 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001008#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001009
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001010#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001011/** RSA OAEP encryption.
1012 *
1013 * This is the encryption scheme defined by RFC 8017
1014 * (PKCS#1: RSA Cryptography Specifications) under the name
1015 * RSAES-OAEP, with the message generation function MGF1.
1016 *
1017 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1018 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1019 * for MGF1.
1020 *
1021 * \return The corresponding RSA OAEP signature algorithm.
1022 * \return Unspecified if \p alg is not a supported
1023 * hash algorithm.
1024 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001025#define PSA_ALG_RSA_OAEP(hash_alg) \
1026 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1027#define PSA_ALG_IS_RSA_OAEP(alg) \
1028 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
Gilles Peskine072ac562018-06-30 00:21:29 +02001029#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1030 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1031 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1032 0)
Gilles Peskined1e8e412018-06-07 09:49:39 +02001033
Gilles Peskinebef7f142018-07-12 17:22:21 +02001034#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100)
1035/** Macro to build an HKDF algorithm.
1036 *
1037 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1038 *
1039 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1040 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1041 *
1042 * \return The corresponding HKDF algorithm.
1043 * \return Unspecified if \p alg is not a supported
1044 * hash algorithm.
1045 */
1046#define PSA_ALG_HKDF(hash_alg) \
1047 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1048/** Whether the specified algorithm is an HKDF algorithm.
1049 *
1050 * HKDF is a family of key derivation algorithms that are based on a hash
1051 * function and the HMAC construction.
1052 *
1053 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1054 *
1055 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1056 * This macro may return either 0 or 1 if \c alg is not a supported
1057 * key derivation algorithm identifier.
1058 */
1059#define PSA_ALG_IS_HKDF(alg) \
1060 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1061#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1062 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1063
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001064/**@}*/
1065
1066/** \defgroup key_management Key management
1067 * @{
1068 */
1069
1070/**
1071 * \brief Import a key in binary format.
1072 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +01001073 * This function supports any output from psa_export_key(). Refer to the
1074 * documentation of psa_export_key() for the format for each key type.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001075 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001076 * \param key Slot where the key will be stored. This must be a
1077 * valid slot for a key of the chosen type. It must
1078 * be unoccupied.
1079 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001080 * \param[in] data Buffer containing the key data.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001081 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082 *
Gilles Peskine28538492018-07-11 17:34:00 +02001083 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001084 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001085 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001086 * The key type or key size is not supported, either by the
1087 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001088 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +01001089 * The key slot is invalid,
1090 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +02001091 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001092 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001093 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1094 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1095 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1096 * \retval #PSA_ERROR_HARDWARE_FAILURE
1097 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001098 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001099 * The library has not been previously initialized by psa_crypto_init().
1100 * It is implementation-dependent whether a failure to initialize
1101 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001102 */
1103psa_status_t psa_import_key(psa_key_slot_t key,
1104 psa_key_type_t type,
1105 const uint8_t *data,
1106 size_t data_length);
1107
1108/**
Gilles Peskine154bd952018-04-19 08:38:16 +02001109 * \brief Destroy a key and restore the slot to its default state.
1110 *
1111 * This function destroys the content of the key slot from both volatile
1112 * memory and, if applicable, non-volatile storage. Implementations shall
1113 * make a best effort to ensure that any previous content of the slot is
1114 * unrecoverable.
1115 *
1116 * This function also erases any metadata such as policies. It returns the
1117 * specified slot to its default state.
1118 *
1119 * \param key The key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001120 *
Gilles Peskine28538492018-07-11 17:34:00 +02001121 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +02001122 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +02001123 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001124 * The slot holds content and cannot be erased because it is
1125 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskine28538492018-07-11 17:34:00 +02001126 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001127 * The specified slot number does not designate a valid slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001128 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001129 * There was an failure in communication with the cryptoprocessor.
1130 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +02001131 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001132 * The storage is corrupted. Implementations shall make a best effort
1133 * to erase key material even in this stage, however applications
1134 * should be aware that it may be impossible to guarantee that the
1135 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +02001136 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001137 * An unexpected condition which is not a storage corruption or
1138 * a communication failure occurred. The cryptoprocessor may have
1139 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001140 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001141 * The library has not been previously initialized by psa_crypto_init().
1142 * It is implementation-dependent whether a failure to initialize
1143 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001144 */
1145psa_status_t psa_destroy_key(psa_key_slot_t key);
1146
1147/**
1148 * \brief Get basic metadata about a key.
1149 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001150 * \param key Slot whose content is queried. This must
1151 * be an occupied key slot.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001152 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001153 * This may be a null pointer, in which case the key type
1154 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001155 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +01001156 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +01001157 * is not written.
1158 *
Gilles Peskine28538492018-07-11 17:34:00 +02001159 * \retval #PSA_SUCCESS
1160 * \retval #PSA_ERROR_EMPTY_SLOT
1161 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1162 * \retval #PSA_ERROR_HARDWARE_FAILURE
1163 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001164 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001165 * The library has not been previously initialized by psa_crypto_init().
1166 * It is implementation-dependent whether a failure to initialize
1167 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001168 */
1169psa_status_t psa_get_key_information(psa_key_slot_t key,
1170 psa_key_type_t *type,
1171 size_t *bits);
1172
1173/**
1174 * \brief Export a key in binary format.
1175 *
1176 * The output of this function can be passed to psa_import_key() to
1177 * create an equivalent object.
1178 *
1179 * If a key is created with psa_import_key() and then exported with
1180 * this function, it is not guaranteed that the resulting data is
1181 * identical: the implementation may choose a different representation
Gilles Peskine92b30732018-03-03 21:29:30 +01001182 * of the same key if the format permits it.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001183 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001184 * For standard key types, the output format is as follows:
1185 *
1186 * - For symmetric keys (including MAC keys), the format is the
1187 * raw bytes of the key.
1188 * - For DES, the key data consists of 8 bytes. The parity bits must be
1189 * correct.
1190 * - For Triple-DES, the format is the concatenation of the
1191 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +01001192 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001193 * is the non-encrypted DER encoding of the representation defined by
1194 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
1195 * ```
1196 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001197 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001198 * modulus INTEGER, -- n
1199 * publicExponent INTEGER, -- e
1200 * privateExponent INTEGER, -- d
1201 * prime1 INTEGER, -- p
1202 * prime2 INTEGER, -- q
1203 * exponent1 INTEGER, -- d mod (p-1)
1204 * exponent2 INTEGER, -- d mod (q-1)
1205 * coefficient INTEGER, -- (inverse of q) mod p
1206 * }
1207 * ```
1208 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
1209 * is the non-encrypted DER encoding of the representation used by
Gilles Peskinec6290c02018-08-13 17:24:59 +02001210 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001211 * ```
1212 * DSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001213 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001214 * prime INTEGER, -- p
1215 * subprime INTEGER, -- q
1216 * generator INTEGER, -- g
1217 * public INTEGER, -- y
1218 * private INTEGER, -- x
1219 * }
1220 * ```
1221 * - For elliptic curve key pairs (key types for which
1222 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is the
1223 * non-encrypted DER encoding of the representation defined by RFC 5915 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001224 * `ECPrivateKey`, version 1. The `ECParameters` field must be a
1225 * `namedCurve` OID as specified in RFC 5480 &sect;2.1.1.1. The public key
1226 * must be present and must be an `ECPoint` in the same format
1227 * (uncompressed variant) an ECC public key of the
1228 * corresponding type exported with psa_export_public_key().
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001229 * ```
1230 * ECPrivateKey ::= SEQUENCE {
1231 * version INTEGER, -- must be 1
1232 * privateKey OCTET STRING,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001233 * -- `ceiling(log2(n)/8)`-byte string, big endian,
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001234 * -- where n is the order of the curve.
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001235 * parameters [0] IMPLICIT ECParameters {{ namedCurve }}, -- mandatory
1236 * publicKey [1] IMPLICIT BIT STRING -- mandatory
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001237 * }
1238 * ```
1239 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
1240 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001241 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001242 * \param key Slot whose content is to be exported. This must
1243 * be an occupied key slot.
1244 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001245 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001246 * \param[out] data_length On success, the number of bytes
1247 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001248 *
Gilles Peskine28538492018-07-11 17:34:00 +02001249 * \retval #PSA_SUCCESS
1250 * \retval #PSA_ERROR_EMPTY_SLOT
1251 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +01001252 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +02001253 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1254 * The size of the \p data buffer is too small. You can determine a
1255 * sufficient buffer size by calling
1256 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
1257 * where \c type is the key type
1258 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +02001259 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1260 * \retval #PSA_ERROR_HARDWARE_FAILURE
1261 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001262 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001263 * The library has not been previously initialized by psa_crypto_init().
1264 * It is implementation-dependent whether a failure to initialize
1265 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001266 */
1267psa_status_t psa_export_key(psa_key_slot_t key,
1268 uint8_t *data,
1269 size_t data_size,
1270 size_t *data_length);
1271
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001272/**
1273 * \brief Export a public key or the public part of a key pair in binary format.
1274 *
1275 * The output of this function can be passed to psa_import_key() to
1276 * create an object that is equivalent to the public key.
1277 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001278 * The format is the DER representation defined by RFC 5280 as
1279 * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
1280 * specified below.
1281 * ```
1282 * SubjectPublicKeyInfo ::= SEQUENCE {
1283 * algorithm AlgorithmIdentifier,
1284 * subjectPublicKey BIT STRING }
1285 * AlgorithmIdentifier ::= SEQUENCE {
1286 * algorithm OBJECT IDENTIFIER,
1287 * parameters ANY DEFINED BY algorithm OPTIONAL }
1288 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001289 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001290 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY),
1291 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.1 as
1292 * `RSAPublicKey`,
1293 * with the OID `rsaEncryption`,
1294 * and with the parameters `NULL`.
1295 * ```
1296 * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
1297 * rsadsi(113549) pkcs(1) 1 }
1298 * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 }
1299 *
1300 * RSAPublicKey ::= SEQUENCE {
1301 * modulus INTEGER, -- n
1302 * publicExponent INTEGER } -- e
1303 * ```
1304 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
1305 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
1306 * `DSAPublicKey`,
1307 * with the OID `id-dsa`,
1308 * and with the parameters `DSS-Parms`.
1309 * ```
1310 * id-dsa OBJECT IDENTIFIER ::= {
1311 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
1312 *
1313 * Dss-Parms ::= SEQUENCE {
1314 * p INTEGER,
1315 * q INTEGER,
1316 * g INTEGER }
1317 * DSAPublicKey ::= INTEGER -- public key, Y
1318 * ```
1319 * - For elliptic curve public keys (key types for which
1320 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true),
1321 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.5 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001322 * `ECPoint`, which contains the uncompressed
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001323 * representation defined by SEC1 &sect;2.3.3.
1324 * The OID is `id-ecPublicKey`,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001325 * and the parameters must be given as a `namedCurve` OID as specified in
Gilles Peskinec6290c02018-08-13 17:24:59 +02001326 * RFC 5480 &sect;2.1.1.1 or other applicable standards.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001327 * ```
1328 * ansi-X9-62 OBJECT IDENTIFIER ::=
1329 * { iso(1) member-body(2) us(840) 10045 }
1330 * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 }
1331 * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 }
1332 *
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001333 * ECPoint ::= ...
1334 * -- first 8 bits: 0x04;
1335 * -- then x_P as an n-bit string, big endian;
1336 * -- then y_P as a n-bit string, big endian,
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001337 * -- where n is the order of the curve.
1338 *
1339 * EcpkParameters ::= CHOICE { -- other choices are not allowed
1340 * namedCurve OBJECT IDENTIFIER }
1341 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001342 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001343 * \param key Slot whose content is to be exported. This must
1344 * be an occupied key slot.
1345 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001346 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001347 * \param[out] data_length On success, the number of bytes
1348 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001349 *
Gilles Peskine28538492018-07-11 17:34:00 +02001350 * \retval #PSA_SUCCESS
1351 * \retval #PSA_ERROR_EMPTY_SLOT
1352 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +02001353 * The key is neither a public key nor a key pair.
1354 * \retval #PSA_ERROR_NOT_SUPPORTED
1355 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1356 * The size of the \p data buffer is too small. You can determine a
1357 * sufficient buffer size by calling
1358 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
1359 * where \c type is the key type
1360 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +02001361 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1362 * \retval #PSA_ERROR_HARDWARE_FAILURE
1363 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001364 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001365 * The library has not been previously initialized by psa_crypto_init().
1366 * It is implementation-dependent whether a failure to initialize
1367 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001368 */
1369psa_status_t psa_export_public_key(psa_key_slot_t key,
1370 uint8_t *data,
1371 size_t data_size,
1372 size_t *data_length);
1373
1374/**@}*/
1375
1376/** \defgroup policy Key policies
1377 * @{
1378 */
1379
1380/** \brief Encoding of permitted usage on a key. */
1381typedef uint32_t psa_key_usage_t;
1382
Gilles Peskine7e198532018-03-08 07:50:30 +01001383/** Whether the key may be exported.
1384 *
1385 * A public key or the public part of a key pair may always be exported
1386 * regardless of the value of this permission flag.
1387 *
1388 * If a key does not have export permission, implementations shall not
1389 * allow the key to be exported in plain form from the cryptoprocessor,
1390 * whether through psa_export_key() or through a proprietary interface.
1391 * The key may however be exportable in a wrapped form, i.e. in a form
1392 * where it is encrypted by another key.
1393 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001394#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1395
Gilles Peskine7e198532018-03-08 07:50:30 +01001396/** Whether the key may be used to encrypt a message.
1397 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001398 * This flag allows the key to be used for a symmetric encryption operation,
1399 * for an AEAD encryption-and-authentication operation,
1400 * or for an asymmetric encryption operation,
1401 * if otherwise permitted by the key's type and policy.
1402 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001403 * For a key pair, this concerns the public key.
1404 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001405#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +01001406
1407/** Whether the key may be used to decrypt a message.
1408 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001409 * This flag allows the key to be used for a symmetric decryption operation,
1410 * for an AEAD decryption-and-verification operation,
1411 * or for an asymmetric decryption operation,
1412 * if otherwise permitted by the key's type and policy.
1413 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001414 * For a key pair, this concerns the private key.
1415 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001416#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +01001417
1418/** Whether the key may be used to sign a message.
1419 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001420 * This flag allows the key to be used for a MAC calculation operation
1421 * or for an asymmetric signature operation,
1422 * if otherwise permitted by the key's type and policy.
1423 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001424 * For a key pair, this concerns the private key.
1425 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001426#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +01001427
1428/** Whether the key may be used to verify a message signature.
1429 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001430 * This flag allows the key to be used for a MAC verification operation
1431 * or for an asymmetric signature verification operation,
1432 * if otherwise permitted by by the key's type and policy.
1433 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001434 * For a key pair, this concerns the public key.
1435 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001436#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1437
Gilles Peskineea0fb492018-07-12 17:17:20 +02001438/** Whether the key may be used to derive other keys.
1439 */
1440#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1441
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001442/** The type of the key policy data structure.
1443 *
1444 * This is an implementation-defined \c struct. Applications should not
1445 * make any assumptions about the content of this structure except
1446 * as directed by the documentation of a specific implementation. */
1447typedef struct psa_key_policy_s psa_key_policy_t;
1448
1449/** \brief Initialize a key policy structure to a default that forbids all
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001450 * usage of the key.
1451 *
1452 * \param[out] policy The policy object to initialize.
1453 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001454void psa_key_policy_init(psa_key_policy_t *policy);
1455
Gilles Peskine7e198532018-03-08 07:50:30 +01001456/** \brief Set the standard fields of a policy structure.
1457 *
1458 * Note that this function does not make any consistency check of the
1459 * parameters. The values are only checked when applying the policy to
1460 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001461 *
1462 * \param[out] policy The policy object to modify.
1463 * \param usage The permitted uses for the key.
1464 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +01001465 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001466void psa_key_policy_set_usage(psa_key_policy_t *policy,
1467 psa_key_usage_t usage,
1468 psa_algorithm_t alg);
1469
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001470/** \brief Retrieve the usage field of a policy structure.
1471 *
1472 * \param[in] policy The policy object to query.
1473 *
1474 * \return The permitted uses for a key with this policy.
1475 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001476psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001477
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001478/** \brief Retrieve the algorithm field of a policy structure.
1479 *
1480 * \param[in] policy The policy object to query.
1481 *
1482 * \return The permitted algorithm for a key with this policy.
1483 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001484psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001485
1486/** \brief Set the usage policy on a key slot.
1487 *
1488 * This function must be called on an empty key slot, before importing,
1489 * generating or creating a key in the slot. Changing the policy of an
1490 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +01001491 *
1492 * Implementations may set restrictions on supported key policies
1493 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001494 *
1495 * \param key The key slot whose policy is to be changed.
1496 * \param[in] policy The policy object to query.
1497 *
1498 * \retval #PSA_SUCCESS
1499 * \retval #PSA_ERROR_OCCUPIED_SLOT
1500 * \retval #PSA_ERROR_NOT_SUPPORTED
1501 * \retval #PSA_ERROR_INVALID_ARGUMENT
1502 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1503 * \retval #PSA_ERROR_HARDWARE_FAILURE
1504 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001505 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001506 * The library has not been previously initialized by psa_crypto_init().
1507 * It is implementation-dependent whether a failure to initialize
1508 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001509 */
1510psa_status_t psa_set_key_policy(psa_key_slot_t key,
1511 const psa_key_policy_t *policy);
1512
Gilles Peskine7e198532018-03-08 07:50:30 +01001513/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001514 *
1515 * \param key The key slot whose policy is being queried.
1516 * \param[out] policy On success, the key's policy.
1517 *
1518 * \retval #PSA_SUCCESS
1519 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1520 * \retval #PSA_ERROR_HARDWARE_FAILURE
1521 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001522 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001523 * The library has not been previously initialized by psa_crypto_init().
1524 * It is implementation-dependent whether a failure to initialize
1525 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +01001526 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001527psa_status_t psa_get_key_policy(psa_key_slot_t key,
1528 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +01001529
1530/**@}*/
1531
Gilles Peskine609b6a52018-03-03 21:31:50 +01001532/** \defgroup persistence Key lifetime
1533 * @{
1534 */
1535
1536/** Encoding of key lifetimes.
1537 */
1538typedef uint32_t psa_key_lifetime_t;
1539
1540/** A volatile key slot retains its content as long as the application is
1541 * running. It is guaranteed to be erased on a power reset.
1542 */
1543#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1544
1545/** A persistent key slot retains its content as long as it is not explicitly
1546 * destroyed.
1547 */
1548#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1549
1550/** A write-once key slot may not be modified once a key has been set.
1551 * It will retain its content as long as the device remains operational.
1552 */
1553#define PSA_KEY_LIFETIME_WRITE_ONCE ((psa_key_lifetime_t)0x7fffffff)
1554
Gilles Peskined393e182018-03-08 07:49:16 +01001555/** \brief Retrieve the lifetime of a key slot.
1556 *
1557 * The assignment of lifetimes to slots is implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001558 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001559 * \param key Slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001560 * \param[out] lifetime On success, the lifetime value.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001561 *
Gilles Peskine28538492018-07-11 17:34:00 +02001562 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001563 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001564 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603a7d245a2018-04-17 00:40:08 -07001565 * The key slot is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001566 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1567 * \retval #PSA_ERROR_HARDWARE_FAILURE
1568 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001569 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001570 * The library has not been previously initialized by psa_crypto_init().
1571 * It is implementation-dependent whether a failure to initialize
1572 * results in this error code.
Gilles Peskined393e182018-03-08 07:49:16 +01001573 */
Gilles Peskine609b6a52018-03-03 21:31:50 +01001574psa_status_t psa_get_key_lifetime(psa_key_slot_t key,
1575 psa_key_lifetime_t *lifetime);
1576
Gilles Peskined393e182018-03-08 07:49:16 +01001577/** \brief Change the lifetime of a key slot.
1578 *
1579 * Whether the lifetime of a key slot can be changed at all, and if so
Gilles Peskine19067982018-03-20 17:54:53 +01001580 * whether the lifetime of an occupied key slot can be changed, is
Gilles Peskined393e182018-03-08 07:49:16 +01001581 * implementation-dependent.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001582 *
Gilles Peskine9bb53d72018-04-17 14:09:24 +02001583 * \param key Slot whose lifetime is to be changed.
1584 * \param lifetime The lifetime value to set for the given key slot.
Gilles Peskine8ca56022018-04-17 14:07:59 +02001585 *
Gilles Peskine28538492018-07-11 17:34:00 +02001586 * \retval #PSA_SUCCESS
mohammad1603804cd712018-03-20 22:44:08 +02001587 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001588 * \retval #PSA_ERROR_INVALID_ARGUMENT
mohammad1603804cd712018-03-20 22:44:08 +02001589 * The key slot is invalid,
mohammad1603a7d245a2018-04-17 00:40:08 -07001590 * or the lifetime value is invalid.
Gilles Peskine28538492018-07-11 17:34:00 +02001591 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001592 * The implementation does not support the specified lifetime value,
1593 * at least for the specified key slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001594 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskinef0c9dd32018-04-17 14:11:07 +02001595 * The slot contains a key, and the implementation does not support
1596 * changing the lifetime of an occupied slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001597 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1598 * \retval #PSA_ERROR_HARDWARE_FAILURE
1599 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001600 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001601 * The library has not been previously initialized by psa_crypto_init().
1602 * It is implementation-dependent whether a failure to initialize
1603 * results in this error code.
Gilles Peskined393e182018-03-08 07:49:16 +01001604 */
1605psa_status_t psa_set_key_lifetime(psa_key_slot_t key,
mohammad1603ea050092018-04-17 00:31:34 -07001606 psa_key_lifetime_t lifetime);
Gilles Peskined393e182018-03-08 07:49:16 +01001607
Gilles Peskine609b6a52018-03-03 21:31:50 +01001608/**@}*/
1609
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001610/** \defgroup hash Message digests
1611 * @{
1612 */
1613
Gilles Peskine308b91d2018-02-08 09:47:44 +01001614/** The type of the state data structure for multipart hash operations.
1615 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001616 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01001617 * make any assumptions about the content of this structure except
1618 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001619typedef struct psa_hash_operation_s psa_hash_operation_t;
1620
Gilles Peskine308b91d2018-02-08 09:47:44 +01001621/** The size of the output of psa_hash_finish(), in bytes.
1622 *
1623 * This is also the hash size that psa_hash_verify() expects.
1624 *
1625 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001626 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
Gilles Peskinebe42f312018-07-13 14:38:15 +02001627 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
Gilles Peskine35855962018-04-19 08:39:16 +02001628 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001629 *
1630 * \return The hash size for the specified hash algorithm.
1631 * If the hash algorithm is not recognized, return 0.
1632 * An implementation may return either 0 or the correct size
1633 * for a hash algorithm that it recognizes, but does not support.
1634 */
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001635#define PSA_HASH_SIZE(alg) \
1636 ( \
Gilles Peskine00709fa2018-08-22 18:25:41 +02001637 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
1638 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
1639 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
1640 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
1641 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
1642 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
1643 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
1644 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
1645 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
1646 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
1647 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
1648 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
1649 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
1650 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
1651 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001652 0)
1653
Gilles Peskine308b91d2018-02-08 09:47:44 +01001654/** Start a multipart hash operation.
1655 *
1656 * The sequence of operations to calculate a hash (message digest)
1657 * is as follows:
1658 * -# Allocate an operation object which will be passed to all the functions
1659 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001660 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001661 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01001662 * of the message each time. The hash that is calculated is the hash
1663 * of the concatenation of these messages in order.
1664 * -# To calculate the hash, call psa_hash_finish().
1665 * To compare the hash with an expected value, call psa_hash_verify().
1666 *
1667 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001668 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001669 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001670 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001671 * eventually terminate the operation. The following events terminate an
1672 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01001673 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001674 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001675 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001676 * \param[out] operation The operation object to use.
1677 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1678 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001679 *
Gilles Peskine28538492018-07-11 17:34:00 +02001680 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001681 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001682 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001683 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001684 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1685 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1686 * \retval #PSA_ERROR_HARDWARE_FAILURE
1687 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001688 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001689psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001690 psa_algorithm_t alg);
1691
Gilles Peskine308b91d2018-02-08 09:47:44 +01001692/** Add a message fragment to a multipart hash operation.
1693 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001694 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001695 *
1696 * If this function returns an error status, the operation becomes inactive.
1697 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001698 * \param[in,out] operation Active hash operation.
1699 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001700 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001701 *
Gilles Peskine28538492018-07-11 17:34:00 +02001702 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001703 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001704 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001705 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001706 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1707 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1708 * \retval #PSA_ERROR_HARDWARE_FAILURE
1709 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001710 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001711psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1712 const uint8_t *input,
1713 size_t input_length);
1714
Gilles Peskine308b91d2018-02-08 09:47:44 +01001715/** Finish the calculation of the hash of a message.
1716 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001717 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001718 * This function calculates the hash of the message formed by concatenating
1719 * the inputs passed to preceding calls to psa_hash_update().
1720 *
1721 * When this function returns, the operation becomes inactive.
1722 *
1723 * \warning Applications should not call this function if they expect
1724 * a specific value for the hash. Call psa_hash_verify() instead.
1725 * Beware that comparing integrity or authenticity data such as
1726 * hash values with a function such as \c memcmp is risky
1727 * because the time taken by the comparison may leak information
1728 * about the hashed data which could allow an attacker to guess
1729 * a valid hash and thereby bypass security controls.
1730 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001731 * \param[in,out] operation Active hash operation.
1732 * \param[out] hash Buffer where the hash is to be written.
1733 * \param hash_size Size of the \p hash buffer in bytes.
1734 * \param[out] hash_length On success, the number of bytes
1735 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001736 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001737 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001738 *
Gilles Peskine28538492018-07-11 17:34:00 +02001739 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001740 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001741 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001742 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001743 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001744 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001745 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001746 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001747 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1748 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1749 * \retval #PSA_ERROR_HARDWARE_FAILURE
1750 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001751 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001752psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1753 uint8_t *hash,
1754 size_t hash_size,
1755 size_t *hash_length);
1756
Gilles Peskine308b91d2018-02-08 09:47:44 +01001757/** Finish the calculation of the hash of a message and compare it with
1758 * an expected value.
1759 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001760 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001761 * This function calculates the hash of the message formed by concatenating
1762 * the inputs passed to preceding calls to psa_hash_update(). It then
1763 * compares the calculated hash with the expected hash passed as a
1764 * parameter to this function.
1765 *
1766 * When this function returns, the operation becomes inactive.
1767 *
Gilles Peskine19067982018-03-20 17:54:53 +01001768 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001769 * comparison between the actual hash and the expected hash is performed
1770 * in constant time.
1771 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001772 * \param[in,out] operation Active hash operation.
1773 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001774 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001775 *
Gilles Peskine28538492018-07-11 17:34:00 +02001776 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001777 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001778 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001779 * The hash of the message was calculated successfully, but it
1780 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001781 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001782 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001783 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1784 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1785 * \retval #PSA_ERROR_HARDWARE_FAILURE
1786 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001787 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001788psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1789 const uint8_t *hash,
1790 size_t hash_length);
1791
Gilles Peskine308b91d2018-02-08 09:47:44 +01001792/** Abort a hash operation.
1793 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001794 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001795 * \p operation structure itself. Once aborted, the operation object
1796 * can be reused for another operation by calling
1797 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001798 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001799 * You may call this function any time after the operation object has
1800 * been initialized by any of the following methods:
1801 * - A call to psa_hash_setup(), whether it succeeds or not.
1802 * - Initializing the \c struct to all-bits-zero.
1803 * - Initializing the \c struct to logical zeros, e.g.
1804 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001805 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001806 * In particular, calling psa_hash_abort() after the operation has been
1807 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1808 * psa_hash_verify() is safe and has no effect.
1809 *
1810 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001811 *
Gilles Peskine28538492018-07-11 17:34:00 +02001812 * \retval #PSA_SUCCESS
1813 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001814 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001815 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1816 * \retval #PSA_ERROR_HARDWARE_FAILURE
1817 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001818 */
1819psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001820
1821/**@}*/
1822
Gilles Peskine8c9def32018-02-08 10:02:12 +01001823/** \defgroup MAC Message authentication codes
1824 * @{
1825 */
1826
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001827/** The type of the state data structure for multipart MAC operations.
1828 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001829 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001830 * make any assumptions about the content of this structure except
1831 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001832typedef struct psa_mac_operation_s psa_mac_operation_t;
1833
Gilles Peskine89167cb2018-07-08 20:12:23 +02001834/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001835 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001836 * This function sets up the calculation of the MAC
1837 * (message authentication code) of a byte string.
1838 * To verify the MAC of a message against an
1839 * expected value, use psa_mac_verify_setup() instead.
1840 *
1841 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001842 * -# Allocate an operation object which will be passed to all the functions
1843 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001844 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001845 * The key remains associated with the operation even if the content
1846 * of the key slot changes.
1847 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1848 * of the message each time. The MAC that is calculated is the MAC
1849 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001850 * -# At the end of the message, call psa_mac_sign_finish() to finish
1851 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001852 *
1853 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +02001854 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001855 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001856 * After a successful call to psa_mac_sign_setup(), the application must
1857 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001858 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001859 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001860 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001861 * \param[out] operation The operation object to use.
1862 * \param key Slot containing the key to use for the operation.
1863 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1864 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001865 *
Gilles Peskine28538492018-07-11 17:34:00 +02001866 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001867 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001868 * \retval #PSA_ERROR_EMPTY_SLOT
1869 * \retval #PSA_ERROR_NOT_PERMITTED
1870 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001871 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001872 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001873 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001874 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1875 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1876 * \retval #PSA_ERROR_HARDWARE_FAILURE
1877 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001878 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001879 * The library has not been previously initialized by psa_crypto_init().
1880 * It is implementation-dependent whether a failure to initialize
1881 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001882 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001883psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
1884 psa_key_slot_t key,
1885 psa_algorithm_t alg);
1886
1887/** Start a multipart MAC verification operation.
1888 *
1889 * This function sets up the verification of the MAC
1890 * (message authentication code) of a byte string against an expected value.
1891 *
1892 * The sequence of operations to verify a MAC is as follows:
1893 * -# Allocate an operation object which will be passed to all the functions
1894 * listed here.
1895 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1896 * The key remains associated with the operation even if the content
1897 * of the key slot changes.
1898 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1899 * of the message each time. The MAC that is calculated is the MAC
1900 * of the concatenation of these messages in order.
1901 * -# At the end of the message, call psa_mac_verify_finish() to finish
1902 * calculating the actual MAC of the message and verify it against
1903 * the expected value.
1904 *
1905 * The application may call psa_mac_abort() at any time after the operation
1906 * has been initialized with psa_mac_verify_setup().
1907 *
1908 * After a successful call to psa_mac_verify_setup(), the application must
1909 * eventually terminate the operation through one of the following methods:
1910 * - A failed call to psa_mac_update().
1911 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1912 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001913 * \param[out] operation The operation object to use.
1914 * \param key Slot containing the key to use for the operation.
1915 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1916 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001917 *
Gilles Peskine28538492018-07-11 17:34:00 +02001918 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001919 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001920 * \retval #PSA_ERROR_EMPTY_SLOT
1921 * \retval #PSA_ERROR_NOT_PERMITTED
1922 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001923 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001924 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001925 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001926 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1927 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1928 * \retval #PSA_ERROR_HARDWARE_FAILURE
1929 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001930 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001931 * The library has not been previously initialized by psa_crypto_init().
1932 * It is implementation-dependent whether a failure to initialize
1933 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001934 */
1935psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
1936 psa_key_slot_t key,
1937 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001938
Gilles Peskinedcd14942018-07-12 00:30:52 +02001939/** Add a message fragment to a multipart MAC operation.
1940 *
1941 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1942 * before calling this function.
1943 *
1944 * If this function returns an error status, the operation becomes inactive.
1945 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001946 * \param[in,out] operation Active MAC operation.
1947 * \param[in] input Buffer containing the message fragment to add to
1948 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001949 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001950 *
1951 * \retval #PSA_SUCCESS
1952 * Success.
1953 * \retval #PSA_ERROR_BAD_STATE
1954 * The operation state is not valid (not started, or already completed).
1955 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1956 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1957 * \retval #PSA_ERROR_HARDWARE_FAILURE
1958 * \retval #PSA_ERROR_TAMPERING_DETECTED
1959 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001960psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1961 const uint8_t *input,
1962 size_t input_length);
1963
Gilles Peskinedcd14942018-07-12 00:30:52 +02001964/** Finish the calculation of the MAC of a message.
1965 *
1966 * The application must call psa_mac_sign_setup() before calling this function.
1967 * This function calculates the MAC of the message formed by concatenating
1968 * the inputs passed to preceding calls to psa_mac_update().
1969 *
1970 * When this function returns, the operation becomes inactive.
1971 *
1972 * \warning Applications should not call this function if they expect
1973 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1974 * Beware that comparing integrity or authenticity data such as
1975 * MAC values with a function such as \c memcmp is risky
1976 * because the time taken by the comparison may leak information
1977 * about the MAC value which could allow an attacker to guess
1978 * a valid MAC and thereby bypass security controls.
1979 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001980 * \param[in,out] operation Active MAC operation.
1981 * \param[out] mac Buffer where the MAC value is to be written.
1982 * \param mac_size Size of the \p mac buffer in bytes.
1983 * \param[out] mac_length On success, the number of bytes
1984 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001985 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001986 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001987 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001988 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001989 *
1990 * \retval #PSA_SUCCESS
1991 * Success.
1992 * \retval #PSA_ERROR_BAD_STATE
1993 * The operation state is not valid (not started, or already completed).
1994 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001995 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001996 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1997 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1998 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1999 * \retval #PSA_ERROR_HARDWARE_FAILURE
2000 * \retval #PSA_ERROR_TAMPERING_DETECTED
2001 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02002002psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2003 uint8_t *mac,
2004 size_t mac_size,
2005 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002006
Gilles Peskinedcd14942018-07-12 00:30:52 +02002007/** Finish the calculation of the MAC of a message and compare it with
2008 * an expected value.
2009 *
2010 * The application must call psa_mac_verify_setup() before calling this function.
2011 * This function calculates the MAC of the message formed by concatenating
2012 * the inputs passed to preceding calls to psa_mac_update(). It then
2013 * compares the calculated MAC with the expected MAC passed as a
2014 * parameter to this function.
2015 *
2016 * When this function returns, the operation becomes inactive.
2017 *
2018 * \note Implementations shall make the best effort to ensure that the
2019 * comparison between the actual MAC and the expected MAC is performed
2020 * in constant time.
2021 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002022 * \param[in,out] operation Active MAC operation.
2023 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002024 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002025 *
2026 * \retval #PSA_SUCCESS
2027 * The expected MAC is identical to the actual MAC of the message.
2028 * \retval #PSA_ERROR_INVALID_SIGNATURE
2029 * The MAC of the message was calculated successfully, but it
2030 * differs from the expected MAC.
2031 * \retval #PSA_ERROR_BAD_STATE
2032 * The operation state is not valid (not started, or already completed).
2033 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2034 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2035 * \retval #PSA_ERROR_HARDWARE_FAILURE
2036 * \retval #PSA_ERROR_TAMPERING_DETECTED
2037 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02002038psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2039 const uint8_t *mac,
2040 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002041
Gilles Peskinedcd14942018-07-12 00:30:52 +02002042/** Abort a MAC operation.
2043 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002044 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002045 * \p operation structure itself. Once aborted, the operation object
2046 * can be reused for another operation by calling
2047 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002048 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002049 * You may call this function any time after the operation object has
2050 * been initialized by any of the following methods:
2051 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
2052 * it succeeds or not.
2053 * - Initializing the \c struct to all-bits-zero.
2054 * - Initializing the \c struct to logical zeros, e.g.
2055 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002056 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002057 * In particular, calling psa_mac_abort() after the operation has been
2058 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
2059 * psa_mac_verify_finish() is safe and has no effect.
2060 *
2061 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002062 *
2063 * \retval #PSA_SUCCESS
2064 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002065 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002066 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2067 * \retval #PSA_ERROR_HARDWARE_FAILURE
2068 * \retval #PSA_ERROR_TAMPERING_DETECTED
2069 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002070psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
2071
2072/**@}*/
2073
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002074/** \defgroup cipher Symmetric ciphers
2075 * @{
2076 */
2077
2078/** The type of the state data structure for multipart cipher operations.
2079 *
2080 * This is an implementation-defined \c struct. Applications should not
2081 * make any assumptions about the content of this structure except
2082 * as directed by the documentation of a specific implementation. */
2083typedef struct psa_cipher_operation_s psa_cipher_operation_t;
2084
2085/** Set the key for a multipart symmetric encryption operation.
2086 *
2087 * The sequence of operations to encrypt a message with a symmetric cipher
2088 * is as follows:
2089 * -# Allocate an operation object which will be passed to all the functions
2090 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02002091 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002092 * The key remains associated with the operation even if the content
2093 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03002094 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002095 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03002096 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002097 * requires a specific IV value.
2098 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
2099 * of the message each time.
2100 * -# Call psa_cipher_finish().
2101 *
2102 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02002103 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002104 *
Gilles Peskinefe119512018-07-08 21:39:34 +02002105 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002106 * eventually terminate the operation. The following events terminate an
2107 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03002108 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002109 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002110 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002111 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002112 * \param[out] operation The operation object to use.
2113 * \param key Slot containing the key to use for the operation.
2114 * \param alg The cipher algorithm to compute
2115 * (\c PSA_ALG_XXX value such that
2116 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002117 *
Gilles Peskine28538492018-07-11 17:34:00 +02002118 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002119 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002120 * \retval #PSA_ERROR_EMPTY_SLOT
2121 * \retval #PSA_ERROR_NOT_PERMITTED
2122 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002123 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002124 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002125 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002126 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2127 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2128 * \retval #PSA_ERROR_HARDWARE_FAILURE
2129 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002130 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002131 * The library has not been previously initialized by psa_crypto_init().
2132 * It is implementation-dependent whether a failure to initialize
2133 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002134 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002135psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
2136 psa_key_slot_t key,
2137 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002138
2139/** Set the key for a multipart symmetric decryption operation.
2140 *
2141 * The sequence of operations to decrypt a message with a symmetric cipher
2142 * is as follows:
2143 * -# Allocate an operation object which will be passed to all the functions
2144 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02002145 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002146 * The key remains associated with the operation even if the content
2147 * of the key slot changes.
2148 * -# Call psa_cipher_update() with the IV (initialization vector) for the
2149 * decryption. If the IV is prepended to the ciphertext, you can call
2150 * psa_cipher_update() on a buffer containing the IV followed by the
2151 * beginning of the message.
2152 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
2153 * of the message each time.
2154 * -# Call psa_cipher_finish().
2155 *
2156 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02002157 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002158 *
Gilles Peskinefe119512018-07-08 21:39:34 +02002159 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002160 * eventually terminate the operation. The following events terminate an
2161 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002162 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002163 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002164 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002165 * \param[out] operation The operation object to use.
2166 * \param key Slot containing the key to use for the operation.
2167 * \param alg The cipher algorithm to compute
2168 * (\c PSA_ALG_XXX value such that
2169 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002170 *
Gilles Peskine28538492018-07-11 17:34:00 +02002171 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002172 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002173 * \retval #PSA_ERROR_EMPTY_SLOT
2174 * \retval #PSA_ERROR_NOT_PERMITTED
2175 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002176 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002177 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002178 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002179 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2180 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2181 * \retval #PSA_ERROR_HARDWARE_FAILURE
2182 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002183 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002184 * The library has not been previously initialized by psa_crypto_init().
2185 * It is implementation-dependent whether a failure to initialize
2186 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002187 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002188psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
2189 psa_key_slot_t key,
2190 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002191
Gilles Peskinedcd14942018-07-12 00:30:52 +02002192/** Generate an IV for a symmetric encryption operation.
2193 *
2194 * This function generates a random IV (initialization vector), nonce
2195 * or initial counter value for the encryption operation as appropriate
2196 * for the chosen algorithm, key type and key size.
2197 *
2198 * The application must call psa_cipher_encrypt_setup() before
2199 * calling this function.
2200 *
2201 * If this function returns an error status, the operation becomes inactive.
2202 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002203 * \param[in,out] operation Active cipher operation.
2204 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002205 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002206 * \param[out] iv_length On success, the number of bytes of the
2207 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002208 *
2209 * \retval #PSA_SUCCESS
2210 * Success.
2211 * \retval #PSA_ERROR_BAD_STATE
2212 * The operation state is not valid (not started, or IV already set).
2213 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002214 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002215 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2216 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2217 * \retval #PSA_ERROR_HARDWARE_FAILURE
2218 * \retval #PSA_ERROR_TAMPERING_DETECTED
2219 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002220psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
2221 unsigned char *iv,
2222 size_t iv_size,
2223 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002224
Gilles Peskinedcd14942018-07-12 00:30:52 +02002225/** Set the IV for a symmetric encryption or decryption operation.
2226 *
2227 * This function sets the random IV (initialization vector), nonce
2228 * or initial counter value for the encryption or decryption operation.
2229 *
2230 * The application must call psa_cipher_encrypt_setup() before
2231 * calling this function.
2232 *
2233 * If this function returns an error status, the operation becomes inactive.
2234 *
2235 * \note When encrypting, applications should use psa_cipher_generate_iv()
2236 * instead of this function, unless implementing a protocol that requires
2237 * a non-random IV.
2238 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002239 * \param[in,out] operation Active cipher operation.
2240 * \param[in] iv Buffer containing the IV to use.
2241 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002242 *
2243 * \retval #PSA_SUCCESS
2244 * Success.
2245 * \retval #PSA_ERROR_BAD_STATE
2246 * The operation state is not valid (not started, or IV already set).
2247 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002248 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02002249 * or the chosen algorithm does not use an IV.
2250 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2251 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2252 * \retval #PSA_ERROR_HARDWARE_FAILURE
2253 * \retval #PSA_ERROR_TAMPERING_DETECTED
2254 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002255psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
2256 const unsigned char *iv,
2257 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002258
Gilles Peskinedcd14942018-07-12 00:30:52 +02002259/** Encrypt or decrypt a message fragment in an active cipher operation.
2260 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02002261 * Before calling this function, you must:
2262 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
2263 * The choice of setup function determines whether this function
2264 * encrypts or decrypts its input.
2265 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
2266 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002267 *
2268 * If this function returns an error status, the operation becomes inactive.
2269 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002270 * \param[in,out] operation Active cipher operation.
2271 * \param[in] input Buffer containing the message fragment to
2272 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002273 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002274 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002275 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002276 * \param[out] output_length On success, the number of bytes
2277 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002278 *
2279 * \retval #PSA_SUCCESS
2280 * Success.
2281 * \retval #PSA_ERROR_BAD_STATE
2282 * The operation state is not valid (not started, IV required but
2283 * not set, or already completed).
2284 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2285 * The size of the \p output buffer is too small.
2286 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2287 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2288 * \retval #PSA_ERROR_HARDWARE_FAILURE
2289 * \retval #PSA_ERROR_TAMPERING_DETECTED
2290 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002291psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2292 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002293 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002294 unsigned char *output,
2295 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002296 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002297
Gilles Peskinedcd14942018-07-12 00:30:52 +02002298/** Finish encrypting or decrypting a message in a cipher operation.
2299 *
2300 * The application must call psa_cipher_encrypt_setup() or
2301 * psa_cipher_decrypt_setup() before calling this function. The choice
2302 * of setup function determines whether this function encrypts or
2303 * decrypts its input.
2304 *
2305 * This function finishes the encryption or decryption of the message
2306 * formed by concatenating the inputs passed to preceding calls to
2307 * psa_cipher_update().
2308 *
2309 * When this function returns, the operation becomes inactive.
2310 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002311 * \param[in,out] operation Active cipher operation.
2312 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002313 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002314 * \param[out] output_length On success, the number of bytes
2315 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002316 *
2317 * \retval #PSA_SUCCESS
2318 * Success.
2319 * \retval #PSA_ERROR_BAD_STATE
2320 * The operation state is not valid (not started, IV required but
2321 * not set, or already completed).
2322 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2323 * The size of the \p output buffer is too small.
2324 * \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 Peskine428dc5a2018-03-03 21:27:18 +01002329psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002330 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002331 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002332 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002333
Gilles Peskinedcd14942018-07-12 00:30:52 +02002334/** Abort a cipher operation.
2335 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002336 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002337 * \p operation structure itself. Once aborted, the operation object
2338 * can be reused for another operation by calling
2339 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002340 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002341 * You may call this function any time after the operation object has
2342 * been initialized by any of the following methods:
2343 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2344 * whether it succeeds or not.
2345 * - Initializing the \c struct to all-bits-zero.
2346 * - Initializing the \c struct to logical zeros, e.g.
2347 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002348 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002349 * In particular, calling psa_cipher_abort() after the operation has been
2350 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2351 * is safe and has no effect.
2352 *
2353 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002354 *
2355 * \retval #PSA_SUCCESS
2356 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002357 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002358 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2359 * \retval #PSA_ERROR_HARDWARE_FAILURE
2360 * \retval #PSA_ERROR_TAMPERING_DETECTED
2361 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002362psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2363
2364/**@}*/
2365
Gilles Peskine3b555712018-03-03 21:27:57 +01002366/** \defgroup aead Authenticated encryption with associated data (AEAD)
2367 * @{
2368 */
2369
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002370/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01002371 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002372 * \param alg An AEAD algorithm
2373 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002374 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002375 *
2376 * \return The tag size for the specified algorithm.
2377 * If the AEAD algorithm does not have an identified
2378 * tag that can be distinguished from the rest of
2379 * the ciphertext, return 0.
2380 * If the AEAD algorithm is not recognized, return 0.
2381 * An implementation may return either 0 or a
2382 * correct size for an AEAD algorithm that it
2383 * recognizes, but does not support.
2384 */
2385#define PSA_AEAD_TAG_SIZE(alg) \
2386 ((alg) == PSA_ALG_GCM ? 16 : \
2387 (alg) == PSA_ALG_CCM ? 16 : \
2388 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01002389
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002390/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002391 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002392 * \param key Slot containing the key to use.
2393 * \param alg The AEAD algorithm to compute
2394 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002395 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002396 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002397 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002398 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002399 * but not encrypted.
2400 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002401 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002402 * encrypted.
2403 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002404 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002405 * encrypted data. The additional data is not
2406 * part of this output. For algorithms where the
2407 * encrypted data and the authentication tag
2408 * are defined as separate outputs, the
2409 * authentication tag is appended to the
2410 * encrypted data.
2411 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2412 * This must be at least
2413 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2414 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002415 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002416 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002417 *
Gilles Peskine28538492018-07-11 17:34:00 +02002418 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002419 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002420 * \retval #PSA_ERROR_EMPTY_SLOT
2421 * \retval #PSA_ERROR_NOT_PERMITTED
2422 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002423 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002424 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002425 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002426 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2427 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2428 * \retval #PSA_ERROR_HARDWARE_FAILURE
2429 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002430 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002431 * The library has not been previously initialized by psa_crypto_init().
2432 * It is implementation-dependent whether a failure to initialize
2433 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002434 */
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002435psa_status_t psa_aead_encrypt(psa_key_slot_t key,
2436 psa_algorithm_t alg,
2437 const uint8_t *nonce,
2438 size_t nonce_length,
2439 const uint8_t *additional_data,
2440 size_t additional_data_length,
2441 const uint8_t *plaintext,
2442 size_t plaintext_length,
2443 uint8_t *ciphertext,
2444 size_t ciphertext_size,
2445 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002446
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002447/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002448 *
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002449 * \param key Slot containing the key to use.
2450 * \param alg The AEAD algorithm to compute
2451 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002452 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002453 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002454 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002455 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002456 * but not encrypted.
2457 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002458 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002459 * encrypted. For algorithms where the
2460 * encrypted data and the authentication tag
2461 * are defined as separate inputs, the buffer
2462 * must contain the encrypted data followed
2463 * by the authentication tag.
2464 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002465 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002466 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2467 * This must be at least
2468 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2469 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002470 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03002471 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002472 *
Gilles Peskine28538492018-07-11 17:34:00 +02002473 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002474 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002475 * \retval #PSA_ERROR_EMPTY_SLOT
2476 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002477 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002478 * \retval #PSA_ERROR_NOT_PERMITTED
2479 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002480 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002481 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002482 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002483 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2484 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2485 * \retval #PSA_ERROR_HARDWARE_FAILURE
2486 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002487 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002488 * The library has not been previously initialized by psa_crypto_init().
2489 * It is implementation-dependent whether a failure to initialize
2490 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002491 */
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002492psa_status_t psa_aead_decrypt(psa_key_slot_t key,
2493 psa_algorithm_t alg,
2494 const uint8_t *nonce,
2495 size_t nonce_length,
2496 const uint8_t *additional_data,
2497 size_t additional_data_length,
2498 const uint8_t *ciphertext,
2499 size_t ciphertext_length,
2500 uint8_t *plaintext,
2501 size_t plaintext_size,
2502 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002503
2504/**@}*/
2505
Gilles Peskine20035e32018-02-03 22:44:14 +01002506/** \defgroup asymmetric Asymmetric cryptography
2507 * @{
2508 */
2509
2510/**
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002511 * \brief ECDSA signature size for a given curve bit size
Gilles Peskine0189e752018-02-03 23:57:22 +01002512 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002513 * \param curve_bits Curve size in bits.
2514 * \return Signature size in bytes.
Gilles Peskine0189e752018-02-03 23:57:22 +01002515 *
2516 * \note This macro returns a compile-time constant if its argument is one.
Gilles Peskine0189e752018-02-03 23:57:22 +01002517 */
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002518#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
2519 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskine0189e752018-02-03 23:57:22 +01002520
Gilles Peskine0189e752018-02-03 23:57:22 +01002521/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002522 * \brief Sign a hash or short message with a private key.
2523 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002524 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002525 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002526 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2527 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2528 * to determine the hash algorithm to use.
2529 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002530 * \param key Key slot containing an asymmetric key pair.
2531 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002532 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002533 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002534 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002535 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002536 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002537 * \param[out] signature_length On success, the number of bytes
2538 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002539 *
Gilles Peskine28538492018-07-11 17:34:00 +02002540 * \retval #PSA_SUCCESS
2541 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002542 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002543 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002544 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002545 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002546 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002547 * \retval #PSA_ERROR_NOT_SUPPORTED
2548 * \retval #PSA_ERROR_INVALID_ARGUMENT
2549 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2550 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2551 * \retval #PSA_ERROR_HARDWARE_FAILURE
2552 * \retval #PSA_ERROR_TAMPERING_DETECTED
2553 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002554 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002555 * The library has not been previously initialized by psa_crypto_init().
2556 * It is implementation-dependent whether a failure to initialize
2557 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002558 */
2559psa_status_t psa_asymmetric_sign(psa_key_slot_t key,
2560 psa_algorithm_t alg,
2561 const uint8_t *hash,
2562 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002563 uint8_t *signature,
2564 size_t signature_size,
2565 size_t *signature_length);
2566
2567/**
2568 * \brief Verify the signature a hash or short message using a public key.
2569 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002570 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002571 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002572 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2573 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2574 * to determine the hash algorithm to use.
2575 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01002576 * \param key Key slot containing a public key or an
2577 * asymmetric key pair.
2578 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002579 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002580 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002581 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002582 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002583 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002584 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002585 *
Gilles Peskine28538492018-07-11 17:34:00 +02002586 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002587 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002588 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002589 * The calculation was perfomed successfully, but the passed
2590 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002591 * \retval #PSA_ERROR_NOT_SUPPORTED
2592 * \retval #PSA_ERROR_INVALID_ARGUMENT
2593 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2594 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2595 * \retval #PSA_ERROR_HARDWARE_FAILURE
2596 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002597 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002598 * The library has not been previously initialized by psa_crypto_init().
2599 * It is implementation-dependent whether a failure to initialize
2600 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002601 */
2602psa_status_t psa_asymmetric_verify(psa_key_slot_t key,
2603 psa_algorithm_t alg,
2604 const uint8_t *hash,
2605 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002606 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002607 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002608
Gilles Peskine723feff2018-05-31 20:08:13 +02002609#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
Gilles Peskine072ac562018-06-30 00:21:29 +02002610 (PSA_ALG_IS_RSA_OAEP(alg) ? \
2611 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskine723feff2018-05-31 20:08:13 +02002612 11 /*PKCS#1v1.5*/)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002613
2614/**
2615 * \brief Encrypt a short message with a public key.
2616 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002617 * \param key Key slot containing a public key or an
2618 * asymmetric key pair.
2619 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002620 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002621 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002622 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002623 * \param[in] salt A salt or label, if supported by the
2624 * encryption algorithm.
2625 * If the algorithm does not support a
2626 * salt, pass \c NULL.
2627 * If the algorithm supports an optional
2628 * salt and you do not want to pass a salt,
2629 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002630 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002631 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2632 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002633 * \param salt_length Size of the \p salt buffer in bytes.
2634 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002635 * \param[out] output Buffer where the encrypted message is to
2636 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002637 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002638 * \param[out] output_length On success, the number of bytes
2639 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002640 *
Gilles Peskine28538492018-07-11 17:34:00 +02002641 * \retval #PSA_SUCCESS
2642 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002643 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002644 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002645 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002646 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002647 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002648 * \retval #PSA_ERROR_NOT_SUPPORTED
2649 * \retval #PSA_ERROR_INVALID_ARGUMENT
2650 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2651 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2652 * \retval #PSA_ERROR_HARDWARE_FAILURE
2653 * \retval #PSA_ERROR_TAMPERING_DETECTED
2654 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002655 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002656 * The library has not been previously initialized by psa_crypto_init().
2657 * It is implementation-dependent whether a failure to initialize
2658 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002659 */
2660psa_status_t psa_asymmetric_encrypt(psa_key_slot_t key,
2661 psa_algorithm_t alg,
2662 const uint8_t *input,
2663 size_t input_length,
2664 const uint8_t *salt,
2665 size_t salt_length,
2666 uint8_t *output,
2667 size_t output_size,
2668 size_t *output_length);
2669
2670/**
2671 * \brief Decrypt a short message with a private key.
2672 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002673 * \param key Key slot containing an asymmetric key pair.
2674 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002675 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002676 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002677 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002678 * \param[in] salt A salt or label, if supported by the
2679 * encryption algorithm.
2680 * If the algorithm does not support a
2681 * salt, pass \c NULL.
2682 * If the algorithm supports an optional
2683 * salt and you do not want to pass a salt,
2684 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002685 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002686 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2687 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002688 * \param salt_length Size of the \p salt buffer in bytes.
2689 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002690 * \param[out] output Buffer where the decrypted message is to
2691 * be written.
2692 * \param output_size Size of the \c output buffer in bytes.
2693 * \param[out] output_length On success, the number of bytes
2694 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002695 *
Gilles Peskine28538492018-07-11 17:34:00 +02002696 * \retval #PSA_SUCCESS
2697 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002698 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002699 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002700 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002701 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002702 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002703 * \retval #PSA_ERROR_NOT_SUPPORTED
2704 * \retval #PSA_ERROR_INVALID_ARGUMENT
2705 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2706 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2707 * \retval #PSA_ERROR_HARDWARE_FAILURE
2708 * \retval #PSA_ERROR_TAMPERING_DETECTED
2709 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2710 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002711 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002712 * The library has not been previously initialized by psa_crypto_init().
2713 * It is implementation-dependent whether a failure to initialize
2714 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002715 */
2716psa_status_t psa_asymmetric_decrypt(psa_key_slot_t key,
2717 psa_algorithm_t alg,
2718 const uint8_t *input,
2719 size_t input_length,
2720 const uint8_t *salt,
2721 size_t salt_length,
2722 uint8_t *output,
2723 size_t output_size,
2724 size_t *output_length);
2725
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002726/**@}*/
2727
Gilles Peskineedd76872018-07-20 17:42:05 +02002728/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002729 * @{
2730 */
2731
2732/** The type of the state data structure for generators.
2733 *
2734 * Before calling any function on a generator, the application must
2735 * initialize it by any of the following means:
2736 * - Set the structure to all-bits-zero, for example:
2737 * \code
2738 * psa_crypto_generator_t generator;
2739 * memset(&generator, 0, sizeof(generator));
2740 * \endcode
2741 * - Initialize the structure to logical zero values, for example:
2742 * \code
2743 * psa_crypto_generator_t generator = {0};
2744 * \endcode
2745 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2746 * for example:
2747 * \code
2748 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2749 * \endcode
2750 * - Assign the result of the function psa_crypto_generator_init()
2751 * to the structure, for example:
2752 * \code
2753 * psa_crypto_generator_t generator;
2754 * generator = psa_crypto_generator_init();
2755 * \endcode
2756 *
2757 * This is an implementation-defined \c struct. Applications should not
2758 * make any assumptions about the content of this structure except
2759 * as directed by the documentation of a specific implementation.
2760 */
2761typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2762
2763/** \def PSA_CRYPTO_GENERATOR_INIT
2764 *
2765 * This macro returns a suitable initializer for a generator object
2766 * of type #psa_crypto_generator_t.
2767 */
2768#ifdef __DOXYGEN_ONLY__
2769/* This is an example definition for documentation purposes.
2770 * Implementations should define a suitable value in `crypto_struct.h`.
2771 */
2772#define PSA_CRYPTO_GENERATOR_INIT {0}
2773#endif
2774
2775/** Return an initial value for a generator object.
2776 */
2777static psa_crypto_generator_t psa_crypto_generator_init(void);
2778
2779/** Retrieve the current capacity of a generator.
2780 *
2781 * The capacity of a generator is the maximum number of bytes that it can
2782 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2783 *
2784 * \param[in] generator The generator to query.
2785 * \param[out] capacity On success, the capacity of the generator.
2786 *
2787 * \retval PSA_SUCCESS
2788 * \retval PSA_ERROR_BAD_STATE
2789 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2790 */
2791psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2792 size_t *capacity);
2793
2794/** Read some data from a generator.
2795 *
2796 * This function reads and returns a sequence of bytes from a generator.
2797 * The data that is read is discarded from the generator. The generator's
2798 * capacity is decreased by the number of bytes read.
2799 *
2800 * \param[in,out] generator The generator object to read from.
2801 * \param[out] output Buffer where the generator output will be
2802 * written.
2803 * \param output_length Number of bytes to output.
2804 *
2805 * \retval PSA_SUCCESS
2806 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2807 * There were fewer than \p output_length bytes
2808 * in the generator. Note that in this case, no
2809 * output is written to the output buffer.
2810 * The generator's capacity is set to 0, thus
2811 * subsequent calls to this function will not
2812 * succeed, even with a smaller output buffer.
2813 * \retval PSA_ERROR_BAD_STATE
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 */
2819psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2820 uint8_t *output,
2821 size_t output_length);
2822
2823/** Create a symmetric key from data read from a generator.
2824 *
2825 * This function reads a sequence of bytes from a generator and imports
2826 * these bytes as a key.
2827 * The data that is read is discarded from the generator. The generator's
2828 * capacity is decreased by the number of bytes read.
2829 *
2830 * This function is equivalent to calling #psa_generator_read and
2831 * passing the resulting output to #psa_import_key, but
2832 * if the implementation provides an isolation boundary then
2833 * the key material is not exposed outside the isolation boundary.
2834 *
2835 * \param key Slot where the key will be stored. This must be a
2836 * valid slot for a key of the chosen type. It must
2837 * be unoccupied.
2838 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2839 * This must be a symmetric key type.
2840 * \param bits Key size in bits.
2841 * \param[in,out] generator The generator object to read from.
2842 *
2843 * \retval PSA_SUCCESS
2844 * Success.
2845 * \retval PSA_ERROR_INSUFFICIENT_CAPACITY
2846 * There were fewer than \p output_length bytes
2847 * in the generator. Note that in this case, no
2848 * output is written to the output buffer.
2849 * The generator's capacity is set to 0, thus
2850 * subsequent calls to this function will not
2851 * succeed, even with a smaller output buffer.
2852 * \retval PSA_ERROR_NOT_SUPPORTED
2853 * The key type or key size is not supported, either by the
2854 * implementation in general or in this particular slot.
2855 * \retval PSA_ERROR_BAD_STATE
2856 * \retval PSA_ERROR_INVALID_ARGUMENT
2857 * The key slot is invalid.
2858 * \retval PSA_ERROR_OCCUPIED_SLOT
2859 * There is already a key in the specified slot.
2860 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
2861 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
2862 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2863 * \retval PSA_ERROR_HARDWARE_FAILURE
2864 * \retval PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002865 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002866 * The library has not been previously initialized by psa_crypto_init().
2867 * It is implementation-dependent whether a failure to initialize
2868 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002869 */
2870psa_status_t psa_generator_import_key(psa_key_slot_t key,
2871 psa_key_type_t type,
2872 size_t bits,
2873 psa_crypto_generator_t *generator);
2874
2875/** Abort a generator.
2876 *
2877 * Once a generator has been aborted, its capacity is zero.
2878 * Aborting a generator frees all associated resources except for the
2879 * \c generator structure itself.
2880 *
2881 * This function may be called at any time as long as the generator
2882 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2883 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2884 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2885 * on a generator that has not been set up.
2886 *
2887 * Once aborted, the generator object may be called.
2888 *
2889 * \param[in,out] generator The generator to abort.
2890 *
2891 * \retval PSA_SUCCESS
2892 * \retval PSA_ERROR_BAD_STATE
2893 * \retval PSA_ERROR_COMMUNICATION_FAILURE
2894 * \retval PSA_ERROR_HARDWARE_FAILURE
2895 * \retval PSA_ERROR_TAMPERING_DETECTED
2896 */
2897psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2898
2899/**@}*/
2900
Gilles Peskineea0fb492018-07-12 17:17:20 +02002901/** \defgroup derivation Key derivation
2902 * @{
2903 */
2904
2905/** Set up a key derivation operation.
2906 *
2907 * A key derivation algorithm takes three inputs: a secret input \p key and
2908 * two non-secret inputs \p label and p salt.
2909 * The result of this function is a byte generator which can
2910 * be used to produce keys and other cryptographic material.
2911 *
2912 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002913 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2914 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002915 *
2916 * \param[in,out] generator The generator object to set up. It must
2917 * have been initialized to .
2918 * \param key Slot containing the secret key to use.
2919 * \param alg The key derivation algorithm to compute
2920 * (\c PSA_ALG_XXX value such that
2921 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2922 * \param[in] salt Salt to use.
2923 * \param salt_length Size of the \p salt buffer in bytes.
2924 * \param[in] label Label to use.
2925 * \param label_length Size of the \p label buffer in bytes.
2926 * \param capacity The maximum number of bytes that the
2927 * generator will be able to provide.
2928 *
2929 * \retval #PSA_SUCCESS
2930 * Success.
2931 * \retval #PSA_ERROR_EMPTY_SLOT
2932 * \retval #PSA_ERROR_NOT_PERMITTED
2933 * \retval #PSA_ERROR_INVALID_ARGUMENT
2934 * \c key is not compatible with \c alg,
2935 * or \p capacity is too large for the specified algorithm and key.
2936 * \retval #PSA_ERROR_NOT_SUPPORTED
2937 * \c alg is not supported or is not a key derivation algorithm.
2938 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2939 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2940 * \retval #PSA_ERROR_HARDWARE_FAILURE
2941 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002942 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002943 * The library has not been previously initialized by psa_crypto_init().
2944 * It is implementation-dependent whether a failure to initialize
2945 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002946 */
2947psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01002948 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002949 psa_algorithm_t alg,
2950 const uint8_t *salt,
2951 size_t salt_length,
2952 const uint8_t *label,
2953 size_t label_length,
2954 size_t capacity);
2955
2956/**@}*/
2957
Gilles Peskineedd76872018-07-20 17:42:05 +02002958/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002959 * @{
2960 */
2961
2962/**
2963 * \brief Generate random bytes.
2964 *
2965 * \warning This function **can** fail! Callers MUST check the return status
2966 * and MUST NOT use the content of the output buffer if the return
2967 * status is not #PSA_SUCCESS.
2968 *
2969 * \note To generate a key, use psa_generate_key() instead.
2970 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002971 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002972 * \param output_size Number of bytes to generate and output.
2973 *
Gilles Peskine28538492018-07-11 17:34:00 +02002974 * \retval #PSA_SUCCESS
2975 * \retval #PSA_ERROR_NOT_SUPPORTED
2976 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2977 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2978 * \retval #PSA_ERROR_HARDWARE_FAILURE
2979 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002980 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002981 * The library has not been previously initialized by psa_crypto_init().
2982 * It is implementation-dependent whether a failure to initialize
2983 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002984 */
2985psa_status_t psa_generate_random(uint8_t *output,
2986 size_t output_size);
2987
Gilles Peskine4c317f42018-07-12 01:24:09 +02002988/** Extra parameters for RSA key generation.
2989 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002990 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002991 * parameter to psa_generate_key().
2992 */
2993typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002994 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002995} psa_generate_key_extra_rsa;
2996
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002997/**
2998 * \brief Generate a key or key pair.
2999 *
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003000 * \param key Slot where the key will be stored. This must be a
3001 * valid slot for a key of the chosen type. It must
3002 * be unoccupied.
3003 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
3004 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02003005 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003006 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003007 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003008 * default parameters. Implementation that support
3009 * the generation of vendor-specific key types
3010 * that allow extra parameters shall document
3011 * the format of these extra parameters and
3012 * the default values. For standard parameters,
3013 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003014 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003015 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
3016 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003017 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003018 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
3019 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003020 * - For an RSA key (\p type is
3021 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3022 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003023 * specifying the public exponent. The
3024 * default public exponent used when \p extra
3025 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02003026 * \param extra_size Size of the buffer that \p extra
3027 * points to, in bytes. Note that if \p extra is
3028 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003029 *
Gilles Peskine28538492018-07-11 17:34:00 +02003030 * \retval #PSA_SUCCESS
3031 * \retval #PSA_ERROR_NOT_SUPPORTED
3032 * \retval #PSA_ERROR_INVALID_ARGUMENT
3033 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3034 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3035 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3036 * \retval #PSA_ERROR_HARDWARE_FAILURE
3037 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003038 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003039 * The library has not been previously initialized by psa_crypto_init().
3040 * It is implementation-dependent whether a failure to initialize
3041 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003042 */
3043psa_status_t psa_generate_key(psa_key_slot_t key,
3044 psa_key_type_t type,
3045 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003046 const void *extra,
3047 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003048
3049/**@}*/
3050
Gilles Peskinee59236f2018-01-27 23:32:46 +01003051#ifdef __cplusplus
3052}
3053#endif
3054
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003055/* The file "crypto_sizes.h" contains definitions for size calculation
3056 * macros whose definitions are implementation-specific. */
3057#include "crypto_sizes.h"
3058
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003059/* The file "crypto_struct.h" contains definitions for
3060 * implementation-specific structs that are declared above. */
3061#include "crypto_struct.h"
3062
3063/* The file "crypto_extra.h" contains vendor-specific definitions. This
3064 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003065#include "crypto_extra.h"
3066
3067#endif /* PSA_CRYPTO_H */