blob: 4aea9905d18aca49ccda10615952115d60726012 [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 Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044 * 0 is not a valid key slot number. The meaning of other values is
45 * implementation dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
56/** \defgroup basic Basic definitions
57 * @{
58 */
59
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020060#if defined(PSA_SUCCESS)
61/* If PSA_SUCCESS is defined, assume that PSA crypto is being used
62 * together with PSA IPC, which also defines the identifier
63 * PSA_SUCCESS. We must not define PSA_SUCCESS ourselves in that case;
64 * the other error code names don't clash. Also define psa_status_t as
65 * an alias for the type used by PSA IPC. This is a temporary hack
mohammad160313f43942018-08-05 12:09:44 +030066 * until we unify error reporting in PSA IPC and PSA crypto.
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020067 *
68 * Note that psa_defs.h must be included before this header!
69 */
70typedef psa_error_t psa_status_t;
71
72#else /* defined(PSA_SUCCESS) */
73
Gilles Peskinee59236f2018-01-27 23:32:46 +010074/**
75 * \brief Function return status.
76 *
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020077 * This is either #PSA_SUCCESS (which is zero), indicating success,
78 * or a nonzero value indicating that an error occurred. Errors are
79 * encoded as one of the \c PSA_ERROR_xxx values defined here.
Gilles Peskinee59236f2018-01-27 23:32:46 +010080 */
itayzafrirc2a79762018-06-18 16:20:16 +030081typedef int32_t psa_status_t;
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020082
itayzafrirc2a79762018-06-18 16:20:16 +030083/** The action was completed successfully. */
84#define PSA_SUCCESS ((psa_status_t)0)
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020085
86#endif /* !defined(PSA_SUCCESS) */
itayzafrirc2a79762018-06-18 16:20:16 +030087
itayzafrirf26dbfc2018-08-01 16:09:08 +030088/** An error occurred that does not correspond to any defined
89 * failure cause.
90 *
91 * Implementations may use this error code if none of the other standard
92 * error codes are applicable. */
93#define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)1)
94
itayzafrirc2a79762018-06-18 16:20:16 +030095/** The requested operation or a parameter is not supported
96 * by this implementation.
97 *
98 * Implementations should return this error code when an enumeration
99 * parameter such as a key type, algorithm, etc. is not recognized.
100 * If a combination of parameters is recognized and identified as
101 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300102#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)2)
itayzafrirc2a79762018-06-18 16:20:16 +0300103
104/** The requested action is denied by a policy.
105 *
106 * Implementations should return this error code when the parameters
107 * are recognized as valid and supported, and a policy explicitly
108 * denies the requested operation.
109 *
110 * If a subset of the parameters of a function call identify a
111 * forbidden operation, and another subset of the parameters are
112 * not valid or not supported, it is unspecified whether the function
113 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
114 * #PSA_ERROR_INVALID_ARGUMENT. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300115#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)3)
itayzafrirc2a79762018-06-18 16:20:16 +0300116
117/** An output buffer is too small.
118 *
Gilles Peskinebe42f312018-07-13 14:38:15 +0200119 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
itayzafrirc2a79762018-06-18 16:20:16 +0300120 * description to determine a sufficient buffer size.
121 *
122 * Implementations should preferably return this error code only
123 * in cases when performing the operation with a larger output
124 * buffer would succeed. However implementations may return this
125 * error if a function has invalid or unsupported parameters in addition
126 * to the parameters that determine the necessary output buffer size. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300127#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)4)
itayzafrirc2a79762018-06-18 16:20:16 +0300128
129/** A slot is occupied, but must be empty to carry out the
130 * requested action.
131 *
132 * If the slot number is invalid (i.e. the requested action could
133 * not be performed even after erasing the slot's content),
134 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300135#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)5)
itayzafrirc2a79762018-06-18 16:20:16 +0300136
137/** A slot is empty, but must be occupied to carry out the
138 * requested action.
139 *
140 * If the slot number is invalid (i.e. the requested action could
141 * not be performed even after creating appropriate content in the slot),
142 * implementations shall return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300143#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)6)
itayzafrirc2a79762018-06-18 16:20:16 +0300144
145/** The requested action cannot be performed in the current state.
146 *
147 * Multipart operations return this error when one of the
148 * functions is called out of sequence. Refer to the function
149 * descriptions for permitted sequencing of functions.
150 *
151 * Implementations shall not return this error code to indicate
152 * that a key slot is occupied when it needs to be free or vice versa,
153 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
154 * as applicable. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300155#define PSA_ERROR_BAD_STATE ((psa_status_t)7)
itayzafrirc2a79762018-06-18 16:20:16 +0300156
157/** The parameters passed to the function are invalid.
158 *
159 * Implementations may return this error any time a parameter or
160 * combination of parameters are recognized as invalid.
161 *
162 * Implementations shall not return this error code to indicate
163 * that a key slot is occupied when it needs to be free or vice versa,
164 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
165 * as applicable. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300166#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)8)
itayzafrirc2a79762018-06-18 16:20:16 +0300167
168/** There is not enough runtime memory.
169 *
170 * If the action is carried out across multiple security realms, this
171 * error can refer to available memory in any of the security realms. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300172#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)9)
itayzafrirc2a79762018-06-18 16:20:16 +0300173
174/** There is not enough persistent storage.
175 *
176 * Functions that modify the key storage return this error code if
177 * there is insufficient storage space on the host media. In addition,
178 * many functions that do not otherwise access storage may return this
179 * error code if the implementation requires a mandatory log entry for
180 * the requested action and the log storage space is full. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300181#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)10)
itayzafrirc2a79762018-06-18 16:20:16 +0300182
183/** There was a communication failure inside the implementation.
184 *
185 * This can indicate a communication failure between the application
186 * and an external cryptoprocessor or between the cryptoprocessor and
187 * an external volatile or persistent memory. A communication failure
188 * may be transient or permanent depending on the cause.
189 *
190 * \warning If a function returns this error, it is undetermined
191 * whether the requested action has completed or not. Implementations
192 * should return #PSA_SUCCESS on successful completion whenver
193 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
194 * if the requested action was completed successfully in an external
195 * cryptoprocessor but there was a breakdown of communication before
196 * the cryptoprocessor could report the status to the application.
197 */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300198#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)11)
itayzafrirc2a79762018-06-18 16:20:16 +0300199
200/** There was a storage failure that may have led to data loss.
201 *
202 * This error indicates that some persistent storage is corrupted.
203 * It should not be used for a corruption of volatile memory
204 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
205 * between the cryptoprocessor and its external storage (use
206 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
207 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
208 *
209 * Note that a storage failure does not indicate that any data that was
210 * previously read is invalid. However this previously read data may no
211 * longer be readable from storage.
212 *
213 * When a storage failure occurs, it is no longer possible to ensure
214 * the global integrity of the keystore. Depending on the global
215 * integrity guarantees offered by the implementation, access to other
216 * data may or may not fail even if the data is still readable but
217 * its integrity canont be guaranteed.
218 *
219 * Implementations should only use this error code to report a
220 * permanent storage corruption. However application writers should
221 * keep in mind that transient errors while reading the storage may be
222 * reported using this error code. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300223#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)12)
itayzafrirc2a79762018-06-18 16:20:16 +0300224
225/** A hardware failure was detected.
226 *
227 * A hardware failure may be transient or permanent depending on the
228 * cause. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300229#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)13)
itayzafrirc2a79762018-06-18 16:20:16 +0300230
231/** A tampering attempt was detected.
232 *
233 * If an application receives this error code, there is no guarantee
234 * that previously accessed or computed data was correct and remains
235 * confidential. Applications should not perform any security function
236 * and should enter a safe failure state.
237 *
238 * Implementations may return this error code if they detect an invalid
239 * state that cannot happen during normal operation and that indicates
240 * that the implementation's security guarantees no longer hold. Depending
241 * on the implementation architecture and on its security and safety goals,
242 * the implementation may forcibly terminate the application.
243 *
244 * This error code is intended as a last resort when a security breach
245 * is detected and it is unsure whether the keystore data is still
246 * protected. Implementations shall only return this error code
247 * to report an alarm from a tampering detector, to indicate that
248 * the confidentiality of stored data can no longer be guaranteed,
249 * or to indicate that the integrity of previously returned data is now
250 * considered compromised. Implementations shall not use this error code
251 * to indicate a hardware failure that merely makes it impossible to
252 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
253 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
254 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
255 * instead).
256 *
257 * This error indicates an attack against the application. Implementations
258 * shall not return this error code as a consequence of the behavior of
259 * the application itself. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300260#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)14)
itayzafrirc2a79762018-06-18 16:20:16 +0300261
262/** There is not enough entropy to generate random data needed
263 * for the requested action.
264 *
265 * This error indicates a failure of a hardware random generator.
266 * Application writers should note that this error can be returned not
267 * only by functions whose purpose is to generate random data, such
268 * as key, IV or nonce generation, but also by functions that execute
269 * an algorithm with a randomized result, as well as functions that
270 * use randomization of intermediate computations as a countermeasure
271 * to certain attacks.
272 *
273 * Implementations should avoid returning this error after psa_crypto_init()
274 * has succeeded. Implementations should generate sufficient
275 * entropy during initialization and subsequently use a cryptographically
276 * secure pseudorandom generator (PRNG). However implementations may return
277 * this error at any time if a policy requires the PRNG to be reseeded
278 * during normal operation. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300279#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)15)
itayzafrirc2a79762018-06-18 16:20:16 +0300280
281/** The signature, MAC or hash is incorrect.
282 *
283 * Verification functions return this error if the verification
284 * calculations completed successfully, and the value to be verified
285 * was determined to be incorrect.
286 *
287 * If the value to verify has an invalid size, implementations may return
288 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300289#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)16)
itayzafrirc2a79762018-06-18 16:20:16 +0300290
291/** The decrypted padding is incorrect.
292 *
293 * \warning In some protocols, when decrypting data, it is essential that
294 * the behavior of the application does not depend on whether the padding
295 * is correct, down to precise timing. Applications should prefer
296 * protocols that use authenticated encryption rather than plain
297 * encryption. If the application must perform a decryption of
298 * unauthenticated data, the application writer should take care not
299 * to reveal whether the padding is invalid.
300 *
301 * Implementations should strive to make valid and invalid padding
302 * as close as possible to indistinguishable to an external observer.
303 * In particular, the timing of a decryption operation should not
304 * depend on the validity of the padding. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300305#define PSA_ERROR_INVALID_PADDING ((psa_status_t)17)
itayzafrirc2a79762018-06-18 16:20:16 +0300306
Gilles Peskineeab56e42018-07-12 17:12:33 +0200307/** The generator has insufficient capacity left.
308 *
309 * Once a function returns this error, attempts to read from the
310 * generator will always return this error. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300311#define PSA_ERROR_INSUFFICIENT_CAPACITY ((psa_status_t)18)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100312
Gilles Peskinef535eb22018-11-30 14:08:36 +0100313/** The key handle is not valid.
314 */
315#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)19)
316
Gilles Peskinee59236f2018-01-27 23:32:46 +0100317/**
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 Peskinee8f0e3d2018-09-18 11:52:10 +0200606#define PSA_ALG_CATEGORY_KEY_SELECTION ((psa_algorithm_t)0x31000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100607
Gilles Peskine98f0a242018-02-06 18:57:29 +0100608#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
609 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200610
Gilles Peskine308b91d2018-02-08 09:47:44 +0100611/** Whether the specified algorithm is a hash algorithm.
612 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100613 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100614 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200615 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
616 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskine7e198532018-03-08 07:50:30 +0100617 * algorithm identifier.
618 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100619#define PSA_ALG_IS_HASH(alg) \
620 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200621
622/** Whether the specified algorithm is a MAC algorithm.
623 *
624 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
625 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200626 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
627 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200628 * algorithm identifier.
629 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100630#define PSA_ALG_IS_MAC(alg) \
631 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200632
633/** Whether the specified algorithm is a symmetric cipher algorithm.
634 *
635 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
636 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200637 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
638 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200639 * algorithm identifier.
640 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100641#define PSA_ALG_IS_CIPHER(alg) \
642 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200643
644/** Whether the specified algorithm is an authenticated encryption
645 * with associated data (AEAD) algorithm.
646 *
647 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
648 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200649 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
650 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200651 * algorithm identifier.
652 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100653#define PSA_ALG_IS_AEAD(alg) \
654 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200655
656/** Whether the specified algorithm is a public-key signature algorithm.
657 *
658 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
659 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200660 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
661 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200662 * algorithm identifier.
663 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100664#define PSA_ALG_IS_SIGN(alg) \
665 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200666
667/** Whether the specified algorithm is a public-key encryption algorithm.
668 *
669 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
670 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200671 * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
672 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200673 * algorithm identifier.
674 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100675#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
676 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200677
Gilles Peskinee8f0e3d2018-09-18 11:52:10 +0200678#define PSA_ALG_KEY_SELECTION_FLAG ((psa_algorithm_t)0x01000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200679/** Whether the specified algorithm is a key agreement algorithm.
680 *
681 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
682 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200683 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
684 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200685 * algorithm identifier.
686 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100687#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
Gilles Peskinee8f0e3d2018-09-18 11:52:10 +0200688 (((alg) & PSA_ALG_CATEGORY_MASK & ~PSA_ALG_KEY_SELECTION_FLAG) == \
689 PSA_ALG_CATEGORY_KEY_AGREEMENT)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200690
691/** Whether the specified algorithm is a key derivation algorithm.
692 *
693 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
694 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200695 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
696 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200697 * algorithm identifier.
698 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100699#define PSA_ALG_IS_KEY_DERIVATION(alg) \
700 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
701
Gilles Peskinee8f0e3d2018-09-18 11:52:10 +0200702/** Whether the specified algorithm is a key selection algorithm.
703 *
704 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
705 *
706 * \return 1 if \p alg is a key selection algorithm, 0 otherwise.
707 * This macro may return either 0 or 1 if \p alg is not a supported
708 * algorithm identifier.
709 */
710#define PSA_ALG_IS_KEY_SELECTION(alg) \
711 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_SELECTION)
712
Gilles Peskine98f0a242018-02-06 18:57:29 +0100713#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
714#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
715#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
716#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100717#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
718#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskineedd76872018-07-20 17:42:05 +0200719/** SHA2-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100720#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
Gilles Peskineedd76872018-07-20 17:42:05 +0200721/** SHA2-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100722#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
Gilles Peskineedd76872018-07-20 17:42:05 +0200723/** SHA2-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100724#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
Gilles Peskineedd76872018-07-20 17:42:05 +0200725/** SHA2-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100726#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
Gilles Peskineedd76872018-07-20 17:42:05 +0200727/** SHA2-512/224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100728#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
Gilles Peskineedd76872018-07-20 17:42:05 +0200729/** SHA2-512/256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100730#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
Gilles Peskineedd76872018-07-20 17:42:05 +0200731/** SHA3-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100732#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
Gilles Peskineedd76872018-07-20 17:42:05 +0200733/** SHA3-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100734#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
Gilles Peskineedd76872018-07-20 17:42:05 +0200735/** SHA3-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100736#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
Gilles Peskineedd76872018-07-20 17:42:05 +0200737/** SHA3-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100738#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
739
Gilles Peskine8c9def32018-02-08 10:02:12 +0100740#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100741#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200742/** Macro to build an HMAC algorithm.
743 *
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200744 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
Gilles Peskine35855962018-04-19 08:39:16 +0200745 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200746 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200747 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine35855962018-04-19 08:39:16 +0200748 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200749 * \return The corresponding HMAC algorithm.
750 * \return Unspecified if \p alg is not a supported
751 * hash algorithm.
Gilles Peskine35855962018-04-19 08:39:16 +0200752 */
753#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100754 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200755
Gilles Peskine00709fa2018-08-22 18:25:41 +0200756#define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100757 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200758
759/** Whether the specified algorithm is an HMAC algorithm.
760 *
761 * HMAC is a family of MAC algorithms that are based on a hash function.
762 *
763 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
764 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200765 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
766 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200767 * algorithm identifier.
768 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100769#define PSA_ALG_IS_HMAC(alg) \
770 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
771 PSA_ALG_HMAC_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200772
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200773/* In the encoding of a MAC algorithm, the bits corresponding to
774 * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
775 * truncated. As an exception, the value 0 means the untruncated algorithm,
776 * whatever its length is. The length is encoded in 6 bits, so it can
777 * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
778 * to full length is correctly encoded as 0 and any non-trivial truncation
779 * is correctly encoded as a value between 1 and 63. */
Gilles Peskined911eb72018-08-14 15:18:45 +0200780#define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x00003f00)
781#define PSA_MAC_TRUNCATION_OFFSET 8
782
783/** Macro to build a truncated MAC algorithm.
784 *
785 * A truncated MAC algorithm is identical to the corresponding MAC
786 * algorithm except that the MAC value for the truncated algorithm
787 * consists of only the first \p mac_length bytes of the MAC value
788 * for the untruncated algorithm.
789 *
790 * \note This macro may allow constructing algorithm identifiers that
791 * are not valid, either because the specified length is larger
792 * than the untruncated MAC or because the specified length is
793 * smaller than permitted by the implementation.
794 *
795 * \note It is implementation-defined whether a truncated MAC that
796 * is truncated to the same length as the MAC of the untruncated
797 * algorithm is considered identical to the untruncated algorithm
798 * for policy comparison purposes.
799 *
800 * \param alg A MAC algorithm identifier (value of type
801 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
802 * is true). This may be a truncated or untruncated
803 * MAC algorithm.
804 * \param mac_length Desired length of the truncated MAC in bytes.
Gilles Peskine6d72ff92018-08-21 14:55:08 +0200805 * This must be at most the full length of the MAC
806 * and must be at least an implementation-specified
807 * minimum. The implementation-specified minimum
808 * shall not be zero.
Gilles Peskined911eb72018-08-14 15:18:45 +0200809 *
810 * \return The corresponding MAC algorithm with the specified
811 * length.
812 * \return Unspecified if \p alg is not a supported
813 * MAC algorithm or if \p mac_length is too small or
814 * too large for the specified MAC algorithm.
815 */
816#define PSA_ALG_TRUNCATED_MAC(alg, mac_length) \
817 (((alg) & ~PSA_ALG_MAC_TRUNCATION_MASK) | \
818 ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
819
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +0200820/** Macro to build the base MAC algorithm corresponding to a truncated
821 * MAC algorithm.
822 *
823 * \param alg A MAC algorithm identifier (value of type
824 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
825 * is true). This may be a truncated or untruncated
826 * MAC algorithm.
827 *
828 * \return The corresponding base MAC algorithm.
829 * \return Unspecified if \p alg is not a supported
830 * MAC algorithm.
831 */
832#define PSA_ALG_FULL_LENGTH_MAC(alg) \
833 ((alg) & ~PSA_ALG_MAC_TRUNCATION_MASK)
834
Gilles Peskined911eb72018-08-14 15:18:45 +0200835/** Length to which a MAC algorithm is truncated.
836 *
837 * \param alg A MAC algorithm identifier (value of type
838 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
839 * is true).
840 *
841 * \return Length of the truncated MAC in bytes.
842 * \return 0 if \p alg is a non-truncated MAC algorithm.
843 * \return Unspecified if \p alg is not a supported
844 * MAC algorithm.
845 */
846#define PSA_MAC_TRUNCATED_LENGTH(alg) \
847 (((alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
848
Gilles Peskine8c9def32018-02-08 10:02:12 +0100849#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
850#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
851#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
852#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200853
854/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
855 *
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200856 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
857 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200858 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
859 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200860 * algorithm identifier.
861 */
Gilles Peskine9df2dc82018-08-22 18:24:17 +0200862#define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100863 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
864 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100865
Gilles Peskinedaea26f2018-08-21 14:02:45 +0200866#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
867#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100868
Gilles Peskinedcd14942018-07-12 00:30:52 +0200869/** Whether the specified algorithm is a stream cipher.
870 *
871 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
872 * by applying a bitwise-xor with a stream of bytes that is generated
873 * from a key.
874 *
875 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
876 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200877 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
878 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200879 * algorithm identifier or if it is not a symmetric cipher algorithm.
880 */
Moran Pekerbed71a22018-04-22 20:19:20 +0300881#define PSA_ALG_IS_STREAM_CIPHER(alg) \
Gilles Peskinedaea26f2018-08-21 14:02:45 +0200882 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
883 (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
884
885/** The ARC4 stream cipher algorithm.
886 */
887#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800001)
888
889/** The CTR stream cipher mode.
890 *
891 * CTR is a stream cipher which is built from a block cipher.
892 * The underlying block cipher is determined by the key type.
893 * For example, to use AES-128-CTR, use this algorithm with
894 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
895 */
896#define PSA_ALG_CTR ((psa_algorithm_t)0x04c00001)
897
898#define PSA_ALG_CFB ((psa_algorithm_t)0x04c00002)
899
900#define PSA_ALG_OFB ((psa_algorithm_t)0x04c00003)
901
902/** The XTS cipher mode.
903 *
904 * XTS is a cipher mode which is built from a block cipher. It requires at
905 * least one full block of input, but beyond this minimum the input
906 * does not need to be a whole number of blocks.
907 */
908#define PSA_ALG_XTS ((psa_algorithm_t)0x044000ff)
909
910/** The CBC block cipher chaining mode, with no padding.
911 *
912 * The underlying block cipher is determined by the key type.
913 *
914 * This symmetric cipher mode can only be used with messages whose lengths
915 * are whole number of blocks for the chosen block cipher.
916 */
917#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04600100)
918
919/** The CBC block cipher chaining mode with PKCS#7 padding.
920 *
921 * The underlying block cipher is determined by the key type.
922 *
923 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
924 */
925#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04600101)
Moran Pekerbed71a22018-04-22 20:19:20 +0300926
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200927#define PSA_ALG_CCM ((psa_algorithm_t)0x06001001)
928#define PSA_ALG_GCM ((psa_algorithm_t)0x06001002)
929
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200930/* In the encoding of a AEAD algorithm, the bits corresponding to
931 * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
932 * The constants for default lengths follow this encoding.
933 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200934#define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x00003f00)
935#define PSA_AEAD_TAG_LENGTH_OFFSET 8
936
937/** Macro to build a shortened AEAD algorithm.
938 *
939 * A shortened AEAD algorithm is similar to the corresponding AEAD
940 * algorithm, but has an authentication tag that consists of fewer bytes.
941 * Depending on the algorithm, the tag length may affect the calculation
942 * of the ciphertext.
943 *
944 * \param alg A AEAD algorithm identifier (value of type
945 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p alg)
946 * is true).
Gilles Peskine31119812018-08-21 14:47:48 +0200947 * \param tag_length Desired length of the authentication tag in bytes.
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200948 *
949 * \return The corresponding AEAD algorithm with the specified
950 * length.
951 * \return Unspecified if \p alg is not a supported
952 * AEAD algorithm or if \p tag_length is not valid
953 * for the specified AEAD algorithm.
954 */
955#define PSA_ALG_AEAD_WITH_TAG_LENGTH(alg, tag_length) \
956 (((alg) & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) | \
957 ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
958 PSA_ALG_AEAD_TAG_LENGTH_MASK))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100959
Gilles Peskine70f46e12018-08-20 15:07:53 +0200960/** Calculate the corresponding AEAD algorithm with the default tag length.
961 *
962 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
963 * #PSA_ALG_IS_AEAD(\p alg) is true).
964 *
965 * \return The corresponding AEAD algorithm with the default tag length
966 * for that algorithm.
967 */
968#define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg) \
969 ( \
970 PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, PSA_ALG_CCM) \
971 PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, PSA_ALG_GCM) \
972 0)
973#define PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, ref) \
974 PSA_ALG_AEAD_WITH_TAG_LENGTH(alg, 0) == \
975 PSA_ALG_AEAD_WITH_TAG_LENGTH(ref, 0) ? \
976 ref :
977
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200978#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
979/** RSA PKCS#1 v1.5 signature with hashing.
980 *
981 * This is the signature scheme defined by RFC 8017
982 * (PKCS#1: RSA Cryptography Specifications) under the name
983 * RSASSA-PKCS1-v1_5.
984 *
985 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200986 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200987 *
988 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
989 * \return Unspecified if \p alg is not a supported
990 * hash algorithm.
991 */
Gilles Peskinea5926232018-03-28 14:16:50 +0200992#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200993 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
994/** Raw PKCS#1 v1.5 signature.
995 *
996 * The input to this algorithm is the DigestInfo structure used by
997 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
998 * steps 3&ndash;6.
999 */
1000#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
Gilles Peskinea5926232018-03-28 14:16:50 +02001001#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001002 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001003
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001004#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
1005/** RSA PSS signature with hashing.
1006 *
1007 * This is the signature scheme defined by RFC 8017
1008 * (PKCS#1: RSA Cryptography Specifications) under the name
Gilles Peskinea4d20bd2018-06-29 23:35:02 +02001009 * RSASSA-PSS, with the message generation function MGF1, and with
1010 * a salt length equal to the length of the hash. The specified
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001011 * hash algorithm is used to hash the input message, to create the
1012 * salted hash, and for the mask generation.
1013 *
1014 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001015 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001016 *
1017 * \return The corresponding RSA PSS signature algorithm.
1018 * \return Unspecified if \p alg is not a supported
1019 * hash algorithm.
1020 */
1021#define PSA_ALG_RSA_PSS(hash_alg) \
1022 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1023#define PSA_ALG_IS_RSA_PSS(alg) \
1024 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
1025
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001026#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
1027/** DSA signature with hashing.
1028 *
1029 * This is the signature scheme defined by FIPS 186-4,
1030 * with a random per-message secret number (*k*).
1031 *
1032 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001033 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001034 *
1035 * \return The corresponding DSA signature algorithm.
1036 * \return Unspecified if \p alg is not a supported
1037 * hash algorithm.
1038 */
1039#define PSA_ALG_DSA(hash_alg) \
1040 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1041#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
1042#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
1043#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
1044 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1045#define PSA_ALG_IS_DSA(alg) \
1046 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
1047 PSA_ALG_DSA_BASE)
1048#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
1049 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +02001050#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
1051 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
1052#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
1053 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001054
1055#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
1056/** ECDSA signature with hashing.
1057 *
1058 * This is the ECDSA signature scheme defined by ANSI X9.62,
1059 * with a random per-message secret number (*k*).
1060 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001061 * The representation of the signature as a byte string consists of
1062 * the concatentation of the signature values *r* and *s*. Each of
1063 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1064 * of the base point of the curve in octets. Each value is represented
1065 * in big-endian order (most significant octet first).
1066 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001067 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001068 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001069 *
1070 * \return The corresponding ECDSA signature algorithm.
1071 * \return Unspecified if \p alg is not a supported
1072 * hash algorithm.
1073 */
1074#define PSA_ALG_ECDSA(hash_alg) \
1075 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1076/** ECDSA signature without hashing.
1077 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001078 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001079 * without specifying a hash algorithm. This algorithm may only be
1080 * used to sign or verify a sequence of bytes that should be an
1081 * already-calculated hash. Note that the input is padded with
1082 * zeros on the left or truncated on the left as required to fit
1083 * the curve size.
1084 */
1085#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
1086#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
1087/** Deterministic ECDSA signature with hashing.
1088 *
1089 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1090 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001091 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1092 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001093 * Note that when this algorithm is used for verification, signatures
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001094 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001095 * same private key are accepted. In other words,
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001096 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1097 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001098 *
1099 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001100 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001101 *
1102 * \return The corresponding deterministic ECDSA signature
1103 * algorithm.
1104 * \return Unspecified if \p alg is not a supported
1105 * hash algorithm.
1106 */
1107#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1108 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1109#define PSA_ALG_IS_ECDSA(alg) \
1110 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
1111 PSA_ALG_ECDSA_BASE)
1112#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
1113 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +02001114#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1115 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1116#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1117 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001118
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001119/** Get the hash used by a hash-and-sign signature algorithm.
1120 *
1121 * A hash-and-sign algorithm is a signature algorithm which is
1122 * composed of two phases: first a hashing phase which does not use
1123 * the key and produces a hash of the input message, then a signing
1124 * phase which only uses the hash and the key and not the message
1125 * itself.
1126 *
1127 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001128 * #PSA_ALG_IS_SIGN(\p alg) is true).
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001129 *
1130 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1131 * algorithm.
1132 * \return 0 if \p alg is a signature algorithm that does not
1133 * follow the hash-and-sign structure.
1134 * \return Unspecified if \p alg is not a signature algorithm or
1135 * if it is not supported by the implementation.
1136 */
1137#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001138 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
1139 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
Gilles Peskine54622ae2018-06-29 22:24:24 +02001140 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001141 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1142 0)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001143
Gilles Peskinedcd14942018-07-12 00:30:52 +02001144/** RSA PKCS#1 v1.5 encryption.
1145 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001146#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001147
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001148#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001149/** RSA OAEP encryption.
1150 *
1151 * This is the encryption scheme defined by RFC 8017
1152 * (PKCS#1: RSA Cryptography Specifications) under the name
1153 * RSAES-OAEP, with the message generation function MGF1.
1154 *
1155 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1156 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1157 * for MGF1.
1158 *
1159 * \return The corresponding RSA OAEP signature algorithm.
1160 * \return Unspecified if \p alg is not a supported
1161 * hash algorithm.
1162 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001163#define PSA_ALG_RSA_OAEP(hash_alg) \
1164 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1165#define PSA_ALG_IS_RSA_OAEP(alg) \
1166 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
Gilles Peskine072ac562018-06-30 00:21:29 +02001167#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1168 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1169 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1170 0)
Gilles Peskined1e8e412018-06-07 09:49:39 +02001171
Gilles Peskinebef7f142018-07-12 17:22:21 +02001172#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100)
1173/** Macro to build an HKDF algorithm.
1174 *
1175 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1176 *
1177 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1178 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1179 *
1180 * \return The corresponding HKDF algorithm.
1181 * \return Unspecified if \p alg is not a supported
1182 * hash algorithm.
1183 */
1184#define PSA_ALG_HKDF(hash_alg) \
1185 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1186/** Whether the specified algorithm is an HKDF algorithm.
1187 *
1188 * HKDF is a family of key derivation algorithms that are based on a hash
1189 * function and the HMAC construction.
1190 *
1191 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1192 *
1193 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1194 * This macro may return either 0 or 1 if \c alg is not a supported
1195 * key derivation algorithm identifier.
1196 */
1197#define PSA_ALG_IS_HKDF(alg) \
1198 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1199#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1200 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1201
Hanno Becker79250c22018-10-09 17:32:46 +01001202#define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t)0x30000200)
1203/** Macro to build a TLS-1.2 PRF algorithm.
1204 *
Hanno Becker2255a362018-11-16 16:05:13 +00001205 * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1206 * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1207 * used with either SHA-256 or SHA-384.
1208 *
1209 * For the application to TLS-1.2, the salt and label arguments passed
1210 * to psa_key_derivation() are what's called 'seed' and 'label' in RFC 5246,
1211 * respectively. For example, for TLS key expansion, the salt is the
1212 * concatenation of ServerHello.Random + ClientHello.Random,
1213 * while the label is "key expansion".
1214 *
Hanno Becker79250c22018-10-09 17:32:46 +01001215 * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the
1216 * TLS 1.2 PRF using HMAC-SHA-256.
1217 *
1218 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1219 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1220 *
1221 * \return The corresponding TLS-1.2 PRF algorithm.
1222 * \return Unspecified if \p alg is not a supported
1223 * hash algorithm.
1224 */
1225#define PSA_ALG_TLS12_PRF(hash_alg) \
1226 (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1227
1228/** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1229 *
Hanno Becker79250c22018-10-09 17:32:46 +01001230 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1231 *
1232 * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1233 * This macro may return either 0 or 1 if \c alg is not a supported
1234 * key derivation algorithm identifier.
1235 */
1236#define PSA_ALG_IS_TLS12_PRF(alg) \
1237 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1238#define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1239 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1240
Hanno Becker8dbfca42018-10-12 11:56:55 +01001241#define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t)0x30000300)
1242/** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1243 *
Hanno Becker2255a362018-11-16 16:05:13 +00001244 * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1245 * from the PreSharedKey (PSK) through the application of padding
1246 * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1247 * The latter is based on HMAC and can be used with either SHA-256
1248 * or SHA-384.
1249 *
1250 * For the application to TLS-1.2, the salt passed to psa_key_derivation()
1251 * (and forwarded to the TLS-1.2 PRF) is the concatenation of the
1252 * ClientHello.Random + ServerHello.Random, while the label is "master secret"
1253 * or "extended master secret".
1254 *
Hanno Becker8dbfca42018-10-12 11:56:55 +01001255 * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the
1256 * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
1257 *
1258 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1259 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1260 *
1261 * \return The corresponding TLS-1.2 PSK to MS algorithm.
1262 * \return Unspecified if \p alg is not a supported
1263 * hash algorithm.
1264 */
1265#define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
1266 (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1267
1268/** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
1269 *
Hanno Becker8dbfca42018-10-12 11:56:55 +01001270 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1271 *
1272 * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
1273 * This macro may return either 0 or 1 if \c alg is not a supported
1274 * key derivation algorithm identifier.
1275 */
1276#define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
1277 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
1278#define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
1279 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1280
Gilles Peskinee8f0e3d2018-09-18 11:52:10 +02001281#define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t)0x010fffff)
1282
1283/** Use a shared secret as is.
1284 *
1285 * Specify this algorithm as the selection component of a key agreement
1286 * to use the raw result of the key agreement as key material.
1287 *
1288 * \warning The raw result of a key agreement algorithm such as finite-field
1289 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
1290 * not be used directly as key material. It can however be used as the secret
1291 * input in a key derivation algorithm.
1292 */
1293#define PSA_ALG_SELECT_RAW ((psa_algorithm_t)0x31000001)
1294
1295#define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
1296 (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
1297
1298#define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
1299 ((alg) & ~PSA_ALG_KEY_DERIVATION_MASK)
Gilles Peskine93098fd2018-09-18 11:54:43 +02001300
1301#define PSA_ALG_FFDH_BASE ((psa_algorithm_t)0x22100000)
1302/** The Diffie-Hellman key agreement algorithm.
1303 *
Gilles Peskine2607bca2018-10-25 22:21:03 +02001304 * This algorithm combines the finite-field Diffie-Hellman (DH) key
1305 * agreement, also known as Diffie-Hellman-Merkle (DHM) key agreement,
1306 * to produce a shared secret from a private key and the peer's
Gilles Peskine93098fd2018-09-18 11:54:43 +02001307 * public key, with a key selection or key derivation algorithm to produce
1308 * one or more shared keys and other shared cryptographic material.
1309 *
Gilles Peskine99d02592018-11-15 17:47:25 +01001310 * The shared secret produced by key agreement and passed as input to the
1311 * derivation or selection algorithm \p kdf_alg is the shared secret
1312 * `g^{ab}` in big-endian format.
1313 * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
1314 * in bits.
Gilles Peskine79dd6222018-10-25 22:22:11 +02001315 *
Gilles Peskine93098fd2018-09-18 11:54:43 +02001316 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1317 * that #PSA_ALG_IS_KEY_DERIVATION(\p hash_alg) is true)
1318 * or a key selection algorithm (\c PSA_ALG_XXX value such
Gilles Peskine19643c52018-11-16 16:45:02 +01001319 * that #PSA_ALG_IS_KEY_SELECTION(\p hash_alg) is true).
Gilles Peskine93098fd2018-09-18 11:54:43 +02001320 *
1321 * \return The Diffie-Hellman algorithm with the specified
1322 * selection or derivation algorithm.
1323 */
1324#define PSA_ALG_FFDH(kdf_alg) \
1325 (PSA_ALG_FFDH_BASE | ((kdf_alg) & PSA_ALG_KEY_DERIVATION_MASK))
1326/** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
1327 *
1328 * This includes every supported key selection or key agreement algorithm
1329 * for the output of the Diffie-Hellman calculation.
1330 *
1331 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1332 *
1333 * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
1334 * This macro may return either 0 or 1 if \c alg is not a supported
1335 * key agreement algorithm identifier.
1336 */
1337#define PSA_ALG_IS_FFDH(alg) \
1338 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH_BASE)
1339
1340#define PSA_ALG_ECDH_BASE ((psa_algorithm_t)0x22200000)
Gilles Peskine2607bca2018-10-25 22:21:03 +02001341/** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
Gilles Peskine93098fd2018-09-18 11:54:43 +02001342 *
1343 * This algorithm combines the elliptic curve Diffie-Hellman key
1344 * agreement to produce a shared secret from a private key and the peer's
1345 * public key, with a key selection or key derivation algorithm to produce
1346 * one or more shared keys and other shared cryptographic material.
1347 *
Gilles Peskine7b5b4a02018-11-14 21:05:10 +01001348 * The shared secret produced by key agreement and passed as input to the
1349 * derivation or selection algorithm \p kdf_alg is the x-coordinate of
Gilles Peskine6c6a0232018-11-15 17:44:43 +01001350 * the shared secret point. It is always `ceiling(m / 8)` bytes long where
1351 * `m` is the bit size associated with the curve, i.e. the bit size of the
1352 * order of the curve's coordinate field. When `m` is not a multiple of 8,
Gilles Peskine7b5b4a02018-11-14 21:05:10 +01001353 * the byte containing the most significant bit of the shared secret
1354 * is padded with zero bits. The byte order is either little-endian
1355 * or big-endian depending on the curve type.
1356 *
1357 * - For Montgomery curves (curve types `PSA_ECC_CURVE_CURVEXXX`),
1358 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1359 * in little-endian byte order.
1360 * The bit size is 448 for Curve448 and 255 for Curve25519.
1361 * - For Weierstrass curves over prime fields (curve types
1362 * `PSA_ECC_CURVE_SECPXXX` and `PSA_ECC_CURVE_BRAINPOOL_PXXX`),
1363 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1364 * in big-endian byte order.
Gilles Peskine6c6a0232018-11-15 17:44:43 +01001365 * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
Gilles Peskine7b5b4a02018-11-14 21:05:10 +01001366 * - For Weierstrass curves over binary fields (curve types
1367 * `PSA_ECC_CURVE_SECTXXX`),
1368 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1369 * in big-endian byte order.
Gilles Peskine6c6a0232018-11-15 17:44:43 +01001370 * The bit size is `m` for the field `F_{2^m}`.
Gilles Peskine79dd6222018-10-25 22:22:11 +02001371 *
Gilles Peskine93098fd2018-09-18 11:54:43 +02001372 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1373 * that #PSA_ALG_IS_KEY_DERIVATION(\p hash_alg) is true)
1374 * or a selection algorithm (\c PSA_ALG_XXX value such
1375 * that #PSA_ALG_IS_KEY_SELECTION(\p hash_alg) is true).
1376 *
1377 * \return The Diffie-Hellman algorithm with the specified
1378 * selection or derivation algorithm.
1379 */
1380#define PSA_ALG_ECDH(kdf_alg) \
1381 (PSA_ALG_ECDH_BASE | ((kdf_alg) & PSA_ALG_KEY_DERIVATION_MASK))
1382/** Whether the specified algorithm is an elliptic curve Diffie-Hellman
1383 * algorithm.
1384 *
1385 * This includes every supported key selection or key agreement algorithm
1386 * for the output of the Diffie-Hellman calculation.
1387 *
1388 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1389 *
1390 * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
1391 * 0 otherwise.
1392 * This macro may return either 0 or 1 if \c alg is not a supported
1393 * key agreement algorithm identifier.
1394 */
1395#define PSA_ALG_IS_ECDH(alg) \
1396 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH_BASE)
1397
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001398/**@}*/
1399
1400/** \defgroup key_management Key management
1401 * @{
1402 */
1403
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001404/** Encoding of key lifetimes.
1405 */
1406typedef uint32_t psa_key_lifetime_t;
1407
1408/** Encoding of identifiers of persistent keys.
1409 */
1410typedef uint32_t psa_key_id_t;
1411
1412/** A volatile key slot retains its content as long as the application is
1413 * running. It is guaranteed to be erased on a power reset.
1414 */
1415#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1416
1417/** A persistent key slot retains its content as long as it is not explicitly
1418 * destroyed.
1419 */
1420#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1421
Gilles Peskineae32aac2018-11-30 14:39:32 +01001422/** \brief Retrieve the lifetime of an open key.
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001423 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001424 * \param handle Handle to query.
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001425 * \param[out] lifetime On success, the lifetime value.
1426 *
1427 * \retval #PSA_SUCCESS
1428 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001429 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001430 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1431 * \retval #PSA_ERROR_HARDWARE_FAILURE
1432 * \retval #PSA_ERROR_TAMPERING_DETECTED
1433 * \retval #PSA_ERROR_BAD_STATE
1434 * The library has not been previously initialized by psa_crypto_init().
1435 * It is implementation-dependent whether a failure to initialize
1436 * results in this error code.
1437 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001438psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001439 psa_key_lifetime_t *lifetime);
1440
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001441
Gilles Peskinef535eb22018-11-30 14:08:36 +01001442/** Allocate a key slot for a transient key, i.e. a key which is only stored
1443 * in volatile memory.
1444 *
1445 * The allocated key slot and its handle remain valid until the
1446 * application calls psa_close_key() or psa_destroy_key() or until the
1447 * application terminates.
1448 *
1449 * This function takes a key type and maximum size as arguments so that
1450 * the implementation can reserve a corresponding amount of memory.
1451 * Implementations are not required to enforce this limit: if the application
1452 * later tries to create a larger key or a key of a different type, it
1453 * is implementation-defined whether this may succeed.
1454 *
1455 * \param type The type of key that the slot will contain.
1456 * \param max_bits The maximum key size that the slot will contain.
1457 * \param[out] handle On success, a handle to a volatile key slot.
1458 *
1459 * \retval #PSA_SUCCESS
1460 * Success. The application can now use the value of `*handle`
1461 * to access the newly allocated key slot.
1462 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1463 * There was not enough memory, or the maximum number of key slots
1464 * has been reached.
1465 * \retval #PSA_ERROR_INVALID_ARGUMENT
1466 * This implementation does not support this key type.
1467 */
1468
1469psa_status_t psa_allocate_key(psa_key_type_t type,
1470 size_t max_bits,
1471 psa_key_handle_t *handle);
1472
1473/** Open a handle to an existing persistent key.
1474 *
1475 * Open a handle to a key which was previously created with psa_create_key().
1476 *
1477 * \param lifetime The lifetime of the key. This designates a storage
1478 * area where the key material is stored. This must not
1479 * be #PSA_KEY_LIFETIME_VOLATILE.
1480 * \param id The persistent identifier of the key.
1481 * \param[out] handle On success, a handle to a key slot which contains
1482 * the data and metadata loaded from the specified
1483 * persistent location.
1484 *
1485 * \retval #PSA_SUCCESS
1486 * Success. The application can now use the value of `*handle`
1487 * to access the newly allocated key slot.
1488 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1489 * \retval #PSA_ERROR_EMPTY_SLOT
1490 * \retval #PSA_ERROR_INVALID_ARGUMENT
1491 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
1492 * \retval #PSA_ERROR_INVALID_ARGUMENT
1493 * \p id is invalid for the specified lifetime.
1494 * \retval #PSA_ERROR_NOT_SUPPORTED
1495 * \p lifetime is not supported.
1496 * \retval #PSA_ERROR_NOT_PERMITTED
1497 * The specified key exists, but the application does not have the
1498 * permission to access it. Note that this specification does not
1499 * define any way to create such a key, but it may be possible
1500 * through implementation-specific means.
1501 */
1502psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
1503 psa_key_id_t id,
1504 psa_key_handle_t *handle);
1505
1506/** Create a new persistent key slot.
1507 *
1508 * Create a new persistent key slot and return a handle to it. The handle
1509 * remains valid until the application calls psa_close_key() or terminates.
1510 * The application can open the key again with psa_open_key() until it
1511 * removes the key by calling psa_destroy_key().
1512 *
1513 * \param lifetime The lifetime of the key. This designates a storage
1514 * area where the key material is stored. This must not
1515 * be #PSA_KEY_LIFETIME_VOLATILE.
1516 * \param id The persistent identifier of the key.
1517 * \param type The type of key that the slot will contain.
1518 * \param max_bits The maximum key size that the slot will contain.
1519 * \param[out] handle On success, a handle to the newly created key slot.
1520 * When key material is later created in this key slot,
1521 * it will be saved to the specified persistent location.
1522 *
1523 * \retval #PSA_SUCCESS
1524 * Success. The application can now use the value of `*handle`
1525 * to access the newly allocated key slot.
1526 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1527 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1528 * \retval #PSA_ERROR_OCCUPIED_SLOT
1529 * There is already a key with the identifier \p id in the storage
1530 * area designated by \p lifetime.
1531 * \retval #PSA_ERROR_INVALID_ARGUMENT
1532 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
1533 * \retval #PSA_ERROR_INVALID_ARGUMENT
1534 * \p id is invalid for the specified lifetime.
1535 * \retval #PSA_ERROR_NOT_SUPPORTED
1536 * \p lifetime is not supported.
1537 * \retval #PSA_ERROR_NOT_PERMITTED
1538 * \p lifetime is valid, but the application does not have the
1539 * permission to create a key there.
1540 */
1541psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
1542 psa_key_id_t id,
1543 psa_key_type_t type,
1544 size_t max_bits,
1545 psa_key_handle_t *handle);
1546
1547/** Close a key handle.
1548 *
1549 * If the handle designates a volatile key, destroy the key material and
1550 * free all associated resources, just like psa_destroy_key().
1551 *
1552 * If the handle designates a persistent key, free all resources associated
1553 * with the key in volatile memory. The key slot in persistent storage is
1554 * not affected and can be opened again later with psa_open_key().
1555 *
1556 * \param handle The key handle to close.
1557 *
1558 * \retval #PSA_SUCCESS
1559 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +01001560 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +01001561 */
1562psa_status_t psa_close_key(psa_key_handle_t handle);
1563
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001564/**@}*/
1565
1566/** \defgroup import_export Key import and export
1567 * @{
1568 */
1569
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001570/**
1571 * \brief Import a key in binary format.
1572 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +01001573 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +01001574 * documentation of psa_export_public_key() for the format of public keys
1575 * and to the documentation of psa_export_key() for the format for
1576 * other key types.
1577 *
1578 * This specification supports a single format for each key type.
1579 * Implementations may support other formats as long as the standard
1580 * format is supported. Implementations that support other formats
1581 * should ensure that the formats are clearly unambiguous so as to
1582 * minimize the risk that an invalid input is accidentally interpreted
1583 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001584 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001585 * \param handle Handle to the slot where the key will be stored.
1586 * This must be a valid slot for a key of the chosen
1587 * type: it must have been obtained by calling
1588 * psa_allocate_key() or psa_create_key() with the
1589 * correct \p type and with a maximum size that is
1590 * compatible with \p data.
Gilles Peskinef7933932018-10-31 14:07:52 +01001591 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
1592 * import, the key slot will contain a key of this type.
1593 * \param[in] data Buffer containing the key data. The content of this
1594 * buffer is interpreted according to \p type. It must
1595 * contain the format described in the documentation
1596 * of psa_export_key() or psa_export_public_key() for
1597 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001598 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001599 *
Gilles Peskine28538492018-07-11 17:34:00 +02001600 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001601 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001602 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001603 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001604 * The key type or key size is not supported, either by the
1605 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001606 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +01001607 * The key slot is invalid,
1608 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +02001609 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001610 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001611 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1612 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1613 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +01001614 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02001615 * \retval #PSA_ERROR_HARDWARE_FAILURE
1616 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001617 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001618 * The library has not been previously initialized by psa_crypto_init().
1619 * It is implementation-dependent whether a failure to initialize
1620 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001621 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001622psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001623 psa_key_type_t type,
1624 const uint8_t *data,
1625 size_t data_length);
1626
1627/**
Gilles Peskineae32aac2018-11-30 14:39:32 +01001628 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +02001629 *
1630 * This function destroys the content of the key slot from both volatile
1631 * memory and, if applicable, non-volatile storage. Implementations shall
1632 * make a best effort to ensure that any previous content of the slot is
1633 * unrecoverable.
1634 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001635 * This function also erases any metadata such as policies and frees all
1636 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +02001637 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001638 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001639 *
Gilles Peskine28538492018-07-11 17:34:00 +02001640 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +02001641 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +02001642 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001643 * The slot holds content and cannot be erased because it is
1644 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001645 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001646 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001647 * There was an failure in communication with the cryptoprocessor.
1648 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +02001649 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001650 * The storage is corrupted. Implementations shall make a best effort
1651 * to erase key material even in this stage, however applications
1652 * should be aware that it may be impossible to guarantee that the
1653 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +02001654 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001655 * An unexpected condition which is not a storage corruption or
1656 * a communication failure occurred. The cryptoprocessor may have
1657 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001658 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001659 * The library has not been previously initialized by psa_crypto_init().
1660 * It is implementation-dependent whether a failure to initialize
1661 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001662 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001663psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001664
1665/**
1666 * \brief Get basic metadata about a key.
1667 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001668 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001669 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001670 * This may be a null pointer, in which case the key type
1671 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001672 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +01001673 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +01001674 * is not written.
1675 *
Gilles Peskine28538492018-07-11 17:34:00 +02001676 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +01001677 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001678 * \retval #PSA_ERROR_EMPTY_SLOT
Gilles Peskineae32aac2018-11-30 14:39:32 +01001679 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +02001680 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1681 * \retval #PSA_ERROR_HARDWARE_FAILURE
1682 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001683 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001684 * The library has not been previously initialized by psa_crypto_init().
1685 * It is implementation-dependent whether a failure to initialize
1686 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001687 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001688psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001689 psa_key_type_t *type,
1690 size_t *bits);
1691
1692/**
1693 * \brief Export a key in binary format.
1694 *
1695 * The output of this function can be passed to psa_import_key() to
1696 * create an equivalent object.
1697 *
Gilles Peskinef7933932018-10-31 14:07:52 +01001698 * If the implementation of psa_import_key() supports other formats
1699 * beyond the format specified here, the output from psa_export_key()
1700 * must use the representation specified here, not the original
1701 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001702 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001703 * For standard key types, the output format is as follows:
1704 *
1705 * - For symmetric keys (including MAC keys), the format is the
1706 * raw bytes of the key.
1707 * - For DES, the key data consists of 8 bytes. The parity bits must be
1708 * correct.
1709 * - For Triple-DES, the format is the concatenation of the
1710 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +01001711 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001712 * is the non-encrypted DER encoding of the representation defined by
1713 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
1714 * ```
1715 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001716 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001717 * modulus INTEGER, -- n
1718 * publicExponent INTEGER, -- e
1719 * privateExponent INTEGER, -- d
1720 * prime1 INTEGER, -- p
1721 * prime2 INTEGER, -- q
1722 * exponent1 INTEGER, -- d mod (p-1)
1723 * exponent2 INTEGER, -- d mod (q-1)
1724 * coefficient INTEGER, -- (inverse of q) mod p
1725 * }
1726 * ```
1727 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
1728 * is the non-encrypted DER encoding of the representation used by
Gilles Peskinec6290c02018-08-13 17:24:59 +02001729 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001730 * ```
1731 * DSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001732 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001733 * prime INTEGER, -- p
1734 * subprime INTEGER, -- q
1735 * generator INTEGER, -- g
1736 * public INTEGER, -- y
1737 * private INTEGER, -- x
1738 * }
1739 * ```
1740 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +01001741 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +01001742 * a representation of the private value as a `ceiling(m/8)`-byte string
1743 * where `m` is the bit size associated with the curve, i.e. the bit size
1744 * of the order of the curve's coordinate field. This byte string is
1745 * in little-endian order for Montgomery curves (curve types
1746 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
1747 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
1748 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +01001749 * This is the content of the `privateKey` field of the `ECPrivateKey`
1750 * format defined by RFC 5915.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001751 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
1752 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001753 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001754 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001755 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001756 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001757 * \param[out] data_length On success, the number of bytes
1758 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001759 *
Gilles Peskine28538492018-07-11 17:34:00 +02001760 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +01001761 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001762 * \retval #PSA_ERROR_EMPTY_SLOT
1763 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +01001764 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +02001765 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1766 * The size of the \p data buffer is too small. You can determine a
1767 * sufficient buffer size by calling
1768 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
1769 * where \c type is the key type
1770 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +02001771 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1772 * \retval #PSA_ERROR_HARDWARE_FAILURE
1773 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001774 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001775 * The library has not been previously initialized by psa_crypto_init().
1776 * It is implementation-dependent whether a failure to initialize
1777 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001778 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001779psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001780 uint8_t *data,
1781 size_t data_size,
1782 size_t *data_length);
1783
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001784/**
1785 * \brief Export a public key or the public part of a key pair in binary format.
1786 *
1787 * The output of this function can be passed to psa_import_key() to
1788 * create an object that is equivalent to the public key.
1789 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001790 * The format is the DER representation defined by RFC 5280 as
1791 * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
1792 * specified below.
1793 * ```
1794 * SubjectPublicKeyInfo ::= SEQUENCE {
1795 * algorithm AlgorithmIdentifier,
1796 * subjectPublicKey BIT STRING }
1797 * AlgorithmIdentifier ::= SEQUENCE {
1798 * algorithm OBJECT IDENTIFIER,
1799 * parameters ANY DEFINED BY algorithm OPTIONAL }
1800 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001801 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001802 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY),
1803 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.1 as
1804 * `RSAPublicKey`,
1805 * with the OID `rsaEncryption`,
1806 * and with the parameters `NULL`.
1807 * ```
1808 * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
1809 * rsadsi(113549) pkcs(1) 1 }
1810 * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 }
1811 *
1812 * RSAPublicKey ::= SEQUENCE {
1813 * modulus INTEGER, -- n
1814 * publicExponent INTEGER } -- e
1815 * ```
1816 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
1817 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
1818 * `DSAPublicKey`,
1819 * with the OID `id-dsa`,
1820 * and with the parameters `DSS-Parms`.
1821 * ```
1822 * id-dsa OBJECT IDENTIFIER ::= {
1823 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
1824 *
1825 * Dss-Parms ::= SEQUENCE {
1826 * p INTEGER,
1827 * q INTEGER,
1828 * g INTEGER }
1829 * DSAPublicKey ::= INTEGER -- public key, Y
1830 * ```
1831 * - For elliptic curve public keys (key types for which
1832 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true),
1833 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.5 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001834 * `ECPoint`, which contains the uncompressed
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001835 * representation defined by SEC1 &sect;2.3.3.
1836 * The OID is `id-ecPublicKey`,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001837 * and the parameters must be given as a `namedCurve` OID as specified in
Gilles Peskinec6290c02018-08-13 17:24:59 +02001838 * RFC 5480 &sect;2.1.1.1 or other applicable standards.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001839 * ```
1840 * ansi-X9-62 OBJECT IDENTIFIER ::=
1841 * { iso(1) member-body(2) us(840) 10045 }
1842 * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 }
1843 * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 }
1844 *
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001845 * ECPoint ::= ...
1846 * -- first 8 bits: 0x04;
Gilles Peskine6c6a0232018-11-15 17:44:43 +01001847 * -- then x_P as a `ceiling(m/8)`-byte string, big endian;
1848 * -- then y_P as a `ceiling(m/8)`-byte string, big endian;
1849 * -- where `m` is the bit size associated with the curve,
Gilles Peskine7b5b4a02018-11-14 21:05:10 +01001850 * -- i.e. the bit size of `q` for a curve over `F_q`.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001851 *
1852 * EcpkParameters ::= CHOICE { -- other choices are not allowed
1853 * namedCurve OBJECT IDENTIFIER }
1854 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001855 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001856 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001857 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001858 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001859 * \param[out] data_length On success, the number of bytes
1860 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001861 *
Gilles Peskine28538492018-07-11 17:34:00 +02001862 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +01001863 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001864 * \retval #PSA_ERROR_EMPTY_SLOT
1865 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +02001866 * The key is neither a public key nor a key pair.
1867 * \retval #PSA_ERROR_NOT_SUPPORTED
1868 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1869 * The size of the \p data buffer is too small. You can determine a
1870 * sufficient buffer size by calling
1871 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
1872 * where \c type is the key type
1873 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +02001874 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1875 * \retval #PSA_ERROR_HARDWARE_FAILURE
1876 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001877 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001878 * The library has not been previously initialized by psa_crypto_init().
1879 * It is implementation-dependent whether a failure to initialize
1880 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001881 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001882psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001883 uint8_t *data,
1884 size_t data_size,
1885 size_t *data_length);
1886
1887/**@}*/
1888
1889/** \defgroup policy Key policies
1890 * @{
1891 */
1892
1893/** \brief Encoding of permitted usage on a key. */
1894typedef uint32_t psa_key_usage_t;
1895
Gilles Peskine7e198532018-03-08 07:50:30 +01001896/** Whether the key may be exported.
1897 *
1898 * A public key or the public part of a key pair may always be exported
1899 * regardless of the value of this permission flag.
1900 *
1901 * If a key does not have export permission, implementations shall not
1902 * allow the key to be exported in plain form from the cryptoprocessor,
1903 * whether through psa_export_key() or through a proprietary interface.
1904 * The key may however be exportable in a wrapped form, i.e. in a form
1905 * where it is encrypted by another key.
1906 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001907#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1908
Gilles Peskine7e198532018-03-08 07:50:30 +01001909/** Whether the key may be used to encrypt a message.
1910 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001911 * This flag allows the key to be used for a symmetric encryption operation,
1912 * for an AEAD encryption-and-authentication operation,
1913 * or for an asymmetric encryption operation,
1914 * if otherwise permitted by the key's type and policy.
1915 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001916 * For a key pair, this concerns the public key.
1917 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001918#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +01001919
1920/** Whether the key may be used to decrypt a message.
1921 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001922 * This flag allows the key to be used for a symmetric decryption operation,
1923 * for an AEAD decryption-and-verification operation,
1924 * or for an asymmetric decryption operation,
1925 * if otherwise permitted by the key's type and policy.
1926 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001927 * For a key pair, this concerns the private key.
1928 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001929#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +01001930
1931/** Whether the key may be used to sign a message.
1932 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001933 * This flag allows the key to be used for a MAC calculation operation
1934 * or for an asymmetric signature operation,
1935 * if otherwise permitted by the key's type and policy.
1936 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001937 * For a key pair, this concerns the private key.
1938 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001939#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +01001940
1941/** Whether the key may be used to verify a message signature.
1942 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001943 * This flag allows the key to be used for a MAC verification operation
1944 * or for an asymmetric signature verification operation,
1945 * if otherwise permitted by by the key's type and policy.
1946 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001947 * For a key pair, this concerns the public key.
1948 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001949#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1950
Gilles Peskineea0fb492018-07-12 17:17:20 +02001951/** Whether the key may be used to derive other keys.
1952 */
1953#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1954
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001955/** The type of the key policy data structure.
1956 *
1957 * This is an implementation-defined \c struct. Applications should not
1958 * make any assumptions about the content of this structure except
1959 * as directed by the documentation of a specific implementation. */
1960typedef struct psa_key_policy_s psa_key_policy_t;
1961
1962/** \brief Initialize a key policy structure to a default that forbids all
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001963 * usage of the key.
1964 *
1965 * \param[out] policy The policy object to initialize.
1966 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001967void psa_key_policy_init(psa_key_policy_t *policy);
1968
Gilles Peskine7e198532018-03-08 07:50:30 +01001969/** \brief Set the standard fields of a policy structure.
1970 *
1971 * Note that this function does not make any consistency check of the
1972 * parameters. The values are only checked when applying the policy to
1973 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001974 *
1975 * \param[out] policy The policy object to modify.
1976 * \param usage The permitted uses for the key.
1977 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +01001978 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001979void psa_key_policy_set_usage(psa_key_policy_t *policy,
1980 psa_key_usage_t usage,
1981 psa_algorithm_t alg);
1982
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001983/** \brief Retrieve the usage field of a policy structure.
1984 *
1985 * \param[in] policy The policy object to query.
1986 *
1987 * \return The permitted uses for a key with this policy.
1988 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001989psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001990
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001991/** \brief Retrieve the algorithm field of a policy structure.
1992 *
1993 * \param[in] policy The policy object to query.
1994 *
1995 * \return The permitted algorithm for a key with this policy.
1996 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001997psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001998
1999/** \brief Set the usage policy on a key slot.
2000 *
2001 * This function must be called on an empty key slot, before importing,
2002 * generating or creating a key in the slot. Changing the policy of an
2003 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +01002004 *
2005 * Implementations may set restrictions on supported key policies
2006 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002007 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002008 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002009 * \param[in] policy The policy object to query.
2010 *
2011 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +01002012 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002013 * \retval #PSA_ERROR_OCCUPIED_SLOT
2014 * \retval #PSA_ERROR_NOT_SUPPORTED
2015 * \retval #PSA_ERROR_INVALID_ARGUMENT
2016 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2017 * \retval #PSA_ERROR_HARDWARE_FAILURE
2018 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002019 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002020 * The library has not been previously initialized by psa_crypto_init().
2021 * It is implementation-dependent whether a failure to initialize
2022 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01002023 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002024psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +01002025 const psa_key_policy_t *policy);
2026
Gilles Peskine7e198532018-03-08 07:50:30 +01002027/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002028 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002029 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002030 * \param[out] policy On success, the key's policy.
2031 *
2032 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +01002033 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002034 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2035 * \retval #PSA_ERROR_HARDWARE_FAILURE
2036 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002037 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002038 * The library has not been previously initialized by psa_crypto_init().
2039 * It is implementation-dependent whether a failure to initialize
2040 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +01002041 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002042psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +01002043 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +01002044
2045/**@}*/
2046
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002047/** \defgroup hash Message digests
2048 * @{
2049 */
2050
Gilles Peskine308b91d2018-02-08 09:47:44 +01002051/** The type of the state data structure for multipart hash operations.
2052 *
Gilles Peskine92b30732018-03-03 21:29:30 +01002053 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01002054 * make any assumptions about the content of this structure except
2055 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002056typedef struct psa_hash_operation_s psa_hash_operation_t;
2057
Gilles Peskine308b91d2018-02-08 09:47:44 +01002058/** The size of the output of psa_hash_finish(), in bytes.
2059 *
2060 * This is also the hash size that psa_hash_verify() expects.
2061 *
2062 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002063 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
Gilles Peskinebe42f312018-07-13 14:38:15 +02002064 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
Gilles Peskine35855962018-04-19 08:39:16 +02002065 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +01002066 *
2067 * \return The hash size for the specified hash algorithm.
2068 * If the hash algorithm is not recognized, return 0.
2069 * An implementation may return either 0 or the correct size
2070 * for a hash algorithm that it recognizes, but does not support.
2071 */
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002072#define PSA_HASH_SIZE(alg) \
2073 ( \
Gilles Peskine00709fa2018-08-22 18:25:41 +02002074 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
2075 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
2076 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
2077 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
2078 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
2079 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
2080 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
2081 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
2082 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
2083 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
2084 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
2085 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
2086 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
2087 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
2088 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002089 0)
2090
Gilles Peskine308b91d2018-02-08 09:47:44 +01002091/** Start a multipart hash operation.
2092 *
2093 * The sequence of operations to calculate a hash (message digest)
2094 * is as follows:
2095 * -# Allocate an operation object which will be passed to all the functions
2096 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002097 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002098 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01002099 * of the message each time. The hash that is calculated is the hash
2100 * of the concatenation of these messages in order.
2101 * -# To calculate the hash, call psa_hash_finish().
2102 * To compare the hash with an expected value, call psa_hash_verify().
2103 *
2104 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002105 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01002106 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002107 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002108 * eventually terminate the operation. The following events terminate an
2109 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01002110 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002111 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01002112 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002113 * \param[out] operation The operation object to use.
2114 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
2115 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01002116 *
Gilles Peskine28538492018-07-11 17:34:00 +02002117 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002118 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002119 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002120 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002121 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2122 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2123 * \retval #PSA_ERROR_HARDWARE_FAILURE
2124 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01002125 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002126psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002127 psa_algorithm_t alg);
2128
Gilles Peskine308b91d2018-02-08 09:47:44 +01002129/** Add a message fragment to a multipart hash operation.
2130 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002131 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002132 *
2133 * If this function returns an error status, the operation becomes inactive.
2134 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002135 * \param[in,out] operation Active hash operation.
2136 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002137 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002138 *
Gilles Peskine28538492018-07-11 17:34:00 +02002139 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002140 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002141 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002142 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02002143 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2144 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2145 * \retval #PSA_ERROR_HARDWARE_FAILURE
2146 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01002147 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002148psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2149 const uint8_t *input,
2150 size_t input_length);
2151
Gilles Peskine308b91d2018-02-08 09:47:44 +01002152/** Finish the calculation of the hash of a message.
2153 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002154 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002155 * This function calculates the hash of the message formed by concatenating
2156 * the inputs passed to preceding calls to psa_hash_update().
2157 *
2158 * When this function returns, the operation becomes inactive.
2159 *
2160 * \warning Applications should not call this function if they expect
2161 * a specific value for the hash. Call psa_hash_verify() instead.
2162 * Beware that comparing integrity or authenticity data such as
2163 * hash values with a function such as \c memcmp is risky
2164 * because the time taken by the comparison may leak information
2165 * about the hashed data which could allow an attacker to guess
2166 * a valid hash and thereby bypass security controls.
2167 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002168 * \param[in,out] operation Active hash operation.
2169 * \param[out] hash Buffer where the hash is to be written.
2170 * \param hash_size Size of the \p hash buffer in bytes.
2171 * \param[out] hash_length On success, the number of bytes
2172 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02002173 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02002174 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002175 *
Gilles Peskine28538492018-07-11 17:34:00 +02002176 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002177 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002178 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002179 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02002180 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002181 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002182 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002183 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02002184 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2185 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2186 * \retval #PSA_ERROR_HARDWARE_FAILURE
2187 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01002188 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002189psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2190 uint8_t *hash,
2191 size_t hash_size,
2192 size_t *hash_length);
2193
Gilles Peskine308b91d2018-02-08 09:47:44 +01002194/** Finish the calculation of the hash of a message and compare it with
2195 * an expected value.
2196 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002197 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002198 * This function calculates the hash of the message formed by concatenating
2199 * the inputs passed to preceding calls to psa_hash_update(). It then
2200 * compares the calculated hash with the expected hash passed as a
2201 * parameter to this function.
2202 *
2203 * When this function returns, the operation becomes inactive.
2204 *
Gilles Peskine19067982018-03-20 17:54:53 +01002205 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01002206 * comparison between the actual hash and the expected hash is performed
2207 * in constant time.
2208 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002209 * \param[in,out] operation Active hash operation.
2210 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002211 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002212 *
Gilles Peskine28538492018-07-11 17:34:00 +02002213 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002214 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02002215 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002216 * The hash of the message was calculated successfully, but it
2217 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02002218 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002219 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02002220 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2221 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2222 * \retval #PSA_ERROR_HARDWARE_FAILURE
2223 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01002224 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002225psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2226 const uint8_t *hash,
2227 size_t hash_length);
2228
Gilles Peskine308b91d2018-02-08 09:47:44 +01002229/** Abort a hash operation.
2230 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01002231 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002232 * \p operation structure itself. Once aborted, the operation object
2233 * can be reused for another operation by calling
2234 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002235 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002236 * You may call this function any time after the operation object has
2237 * been initialized by any of the following methods:
2238 * - A call to psa_hash_setup(), whether it succeeds or not.
2239 * - Initializing the \c struct to all-bits-zero.
2240 * - Initializing the \c struct to logical zeros, e.g.
2241 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002242 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002243 * In particular, calling psa_hash_abort() after the operation has been
2244 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
2245 * psa_hash_verify() is safe and has no effect.
2246 *
2247 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002248 *
Gilles Peskine28538492018-07-11 17:34:00 +02002249 * \retval #PSA_SUCCESS
2250 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002251 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02002252 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2253 * \retval #PSA_ERROR_HARDWARE_FAILURE
2254 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01002255 */
2256psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257
2258/**@}*/
2259
Gilles Peskine8c9def32018-02-08 10:02:12 +01002260/** \defgroup MAC Message authentication codes
2261 * @{
2262 */
2263
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002264/** The type of the state data structure for multipart MAC operations.
2265 *
Gilles Peskine92b30732018-03-03 21:29:30 +01002266 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002267 * make any assumptions about the content of this structure except
2268 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002269typedef struct psa_mac_operation_s psa_mac_operation_t;
2270
Gilles Peskine89167cb2018-07-08 20:12:23 +02002271/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002272 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02002273 * This function sets up the calculation of the MAC
2274 * (message authentication code) of a byte string.
2275 * To verify the MAC of a message against an
2276 * expected value, use psa_mac_verify_setup() instead.
2277 *
2278 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002279 * -# Allocate an operation object which will be passed to all the functions
2280 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +02002281 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002282 * The key remains associated with the operation even if the content
2283 * of the key slot changes.
2284 * -# Call psa_mac_update() zero, one or more times, passing a fragment
2285 * of the message each time. The MAC that is calculated is the MAC
2286 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02002287 * -# At the end of the message, call psa_mac_sign_finish() to finish
2288 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002289 *
2290 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +02002291 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002292 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02002293 * After a successful call to psa_mac_sign_setup(), the application must
2294 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002295 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02002296 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002297 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002298 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002299 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002300 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
2301 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002302 *
Gilles Peskine28538492018-07-11 17:34:00 +02002303 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002304 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002305 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002306 * \retval #PSA_ERROR_EMPTY_SLOT
2307 * \retval #PSA_ERROR_NOT_PERMITTED
2308 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002309 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002310 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002311 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002312 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2313 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2314 * \retval #PSA_ERROR_HARDWARE_FAILURE
2315 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002316 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002317 * The library has not been previously initialized by psa_crypto_init().
2318 * It is implementation-dependent whether a failure to initialize
2319 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002320 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02002321psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002322 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002323 psa_algorithm_t alg);
2324
2325/** Start a multipart MAC verification operation.
2326 *
2327 * This function sets up the verification of the MAC
2328 * (message authentication code) of a byte string against an expected value.
2329 *
2330 * The sequence of operations to verify a MAC is as follows:
2331 * -# Allocate an operation object which will be passed to all the functions
2332 * listed here.
2333 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
2334 * The key remains associated with the operation even if the content
2335 * of the key slot changes.
2336 * -# Call psa_mac_update() zero, one or more times, passing a fragment
2337 * of the message each time. The MAC that is calculated is the MAC
2338 * of the concatenation of these messages in order.
2339 * -# At the end of the message, call psa_mac_verify_finish() to finish
2340 * calculating the actual MAC of the message and verify it against
2341 * the expected value.
2342 *
2343 * The application may call psa_mac_abort() at any time after the operation
2344 * has been initialized with psa_mac_verify_setup().
2345 *
2346 * After a successful call to psa_mac_verify_setup(), the application must
2347 * eventually terminate the operation through one of the following methods:
2348 * - A failed call to psa_mac_update().
2349 * - A call to psa_mac_verify_finish() or psa_mac_abort().
2350 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002351 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002352 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002353 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
2354 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02002355 *
Gilles Peskine28538492018-07-11 17:34:00 +02002356 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02002357 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002358 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002359 * \retval #PSA_ERROR_EMPTY_SLOT
2360 * \retval #PSA_ERROR_NOT_PERMITTED
2361 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02002362 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002363 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02002364 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002365 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2366 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2367 * \retval #PSA_ERROR_HARDWARE_FAILURE
2368 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002369 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002370 * The library has not been previously initialized by psa_crypto_init().
2371 * It is implementation-dependent whether a failure to initialize
2372 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02002373 */
2374psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002375 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002376 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002377
Gilles Peskinedcd14942018-07-12 00:30:52 +02002378/** Add a message fragment to a multipart MAC operation.
2379 *
2380 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
2381 * before calling this function.
2382 *
2383 * If this function returns an error status, the operation becomes inactive.
2384 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002385 * \param[in,out] operation Active MAC operation.
2386 * \param[in] input Buffer containing the message fragment to add to
2387 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002388 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002389 *
2390 * \retval #PSA_SUCCESS
2391 * Success.
2392 * \retval #PSA_ERROR_BAD_STATE
2393 * The operation state is not valid (not started, or already completed).
2394 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2395 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2396 * \retval #PSA_ERROR_HARDWARE_FAILURE
2397 * \retval #PSA_ERROR_TAMPERING_DETECTED
2398 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002399psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2400 const uint8_t *input,
2401 size_t input_length);
2402
Gilles Peskinedcd14942018-07-12 00:30:52 +02002403/** Finish the calculation of the MAC of a message.
2404 *
2405 * The application must call psa_mac_sign_setup() before calling this function.
2406 * This function calculates the MAC of the message formed by concatenating
2407 * the inputs passed to preceding calls to psa_mac_update().
2408 *
2409 * When this function returns, the operation becomes inactive.
2410 *
2411 * \warning Applications should not call this function if they expect
2412 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
2413 * Beware that comparing integrity or authenticity data such as
2414 * MAC values with a function such as \c memcmp is risky
2415 * because the time taken by the comparison may leak information
2416 * about the MAC value which could allow an attacker to guess
2417 * a valid MAC and thereby bypass security controls.
2418 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002419 * \param[in,out] operation Active MAC operation.
2420 * \param[out] mac Buffer where the MAC value is to be written.
2421 * \param mac_size Size of the \p mac buffer in bytes.
2422 * \param[out] mac_length On success, the number of bytes
2423 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002424 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02002425 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002426 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02002427 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002428 *
2429 * \retval #PSA_SUCCESS
2430 * Success.
2431 * \retval #PSA_ERROR_BAD_STATE
2432 * The operation state is not valid (not started, or already completed).
2433 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002434 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02002435 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
2436 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2437 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2438 * \retval #PSA_ERROR_HARDWARE_FAILURE
2439 * \retval #PSA_ERROR_TAMPERING_DETECTED
2440 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02002441psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2442 uint8_t *mac,
2443 size_t mac_size,
2444 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002445
Gilles Peskinedcd14942018-07-12 00:30:52 +02002446/** Finish the calculation of the MAC of a message and compare it with
2447 * an expected value.
2448 *
2449 * The application must call psa_mac_verify_setup() before calling this function.
2450 * This function calculates the MAC of the message formed by concatenating
2451 * the inputs passed to preceding calls to psa_mac_update(). It then
2452 * compares the calculated MAC with the expected MAC passed as a
2453 * parameter to this function.
2454 *
2455 * When this function returns, the operation becomes inactive.
2456 *
2457 * \note Implementations shall make the best effort to ensure that the
2458 * comparison between the actual MAC and the expected MAC is performed
2459 * in constant time.
2460 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002461 * \param[in,out] operation Active MAC operation.
2462 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002463 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002464 *
2465 * \retval #PSA_SUCCESS
2466 * The expected MAC is identical to the actual MAC of the message.
2467 * \retval #PSA_ERROR_INVALID_SIGNATURE
2468 * The MAC of the message was calculated successfully, but it
2469 * differs from the expected MAC.
2470 * \retval #PSA_ERROR_BAD_STATE
2471 * The operation state is not valid (not started, or already completed).
2472 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2473 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2474 * \retval #PSA_ERROR_HARDWARE_FAILURE
2475 * \retval #PSA_ERROR_TAMPERING_DETECTED
2476 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02002477psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2478 const uint8_t *mac,
2479 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002480
Gilles Peskinedcd14942018-07-12 00:30:52 +02002481/** Abort a MAC operation.
2482 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002483 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002484 * \p operation structure itself. Once aborted, the operation object
2485 * can be reused for another operation by calling
2486 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002487 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002488 * You may call this function any time after the operation object has
2489 * been initialized by any of the following methods:
2490 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
2491 * it succeeds or not.
2492 * - Initializing the \c struct to all-bits-zero.
2493 * - Initializing the \c struct to logical zeros, e.g.
2494 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002495 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002496 * In particular, calling psa_mac_abort() after the operation has been
2497 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
2498 * psa_mac_verify_finish() is safe and has no effect.
2499 *
2500 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002501 *
2502 * \retval #PSA_SUCCESS
2503 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002504 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002505 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2506 * \retval #PSA_ERROR_HARDWARE_FAILURE
2507 * \retval #PSA_ERROR_TAMPERING_DETECTED
2508 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002509psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
2510
2511/**@}*/
2512
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002513/** \defgroup cipher Symmetric ciphers
2514 * @{
2515 */
2516
2517/** The type of the state data structure for multipart cipher operations.
2518 *
2519 * This is an implementation-defined \c struct. Applications should not
2520 * make any assumptions about the content of this structure except
2521 * as directed by the documentation of a specific implementation. */
2522typedef struct psa_cipher_operation_s psa_cipher_operation_t;
2523
2524/** Set the key for a multipart symmetric encryption operation.
2525 *
2526 * The sequence of operations to encrypt a message with a symmetric cipher
2527 * is as follows:
2528 * -# Allocate an operation object which will be passed to all the functions
2529 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02002530 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002531 * The key remains associated with the operation even if the content
2532 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03002533 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002534 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03002535 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002536 * requires a specific IV value.
2537 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
2538 * of the message each time.
2539 * -# Call psa_cipher_finish().
2540 *
2541 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02002542 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002543 *
Gilles Peskinefe119512018-07-08 21:39:34 +02002544 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002545 * eventually terminate the operation. The following events terminate an
2546 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03002547 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002548 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002549 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002550 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002551 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002552 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002553 * \param alg The cipher algorithm to compute
2554 * (\c PSA_ALG_XXX value such that
2555 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002556 *
Gilles Peskine28538492018-07-11 17:34:00 +02002557 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002558 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002559 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002560 * \retval #PSA_ERROR_EMPTY_SLOT
2561 * \retval #PSA_ERROR_NOT_PERMITTED
2562 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002563 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002564 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002565 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002566 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2567 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2568 * \retval #PSA_ERROR_HARDWARE_FAILURE
2569 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002570 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002571 * The library has not been previously initialized by psa_crypto_init().
2572 * It is implementation-dependent whether a failure to initialize
2573 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002574 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002575psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002576 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002577 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002578
2579/** Set the key for a multipart symmetric decryption operation.
2580 *
2581 * The sequence of operations to decrypt a message with a symmetric cipher
2582 * is as follows:
2583 * -# Allocate an operation object which will be passed to all the functions
2584 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02002585 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002586 * The key remains associated with the operation even if the content
2587 * of the key slot changes.
2588 * -# Call psa_cipher_update() with the IV (initialization vector) for the
2589 * decryption. If the IV is prepended to the ciphertext, you can call
2590 * psa_cipher_update() on a buffer containing the IV followed by the
2591 * beginning of the message.
2592 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
2593 * of the message each time.
2594 * -# Call psa_cipher_finish().
2595 *
2596 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02002597 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002598 *
Gilles Peskinefe119512018-07-08 21:39:34 +02002599 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002600 * eventually terminate the operation. The following events terminate an
2601 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002602 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002603 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002604 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002605 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002606 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002607 * \param alg The cipher algorithm to compute
2608 * (\c PSA_ALG_XXX value such that
2609 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002610 *
Gilles Peskine28538492018-07-11 17:34:00 +02002611 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002612 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002613 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002614 * \retval #PSA_ERROR_EMPTY_SLOT
2615 * \retval #PSA_ERROR_NOT_PERMITTED
2616 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002617 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002618 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002619 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002620 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2621 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2622 * \retval #PSA_ERROR_HARDWARE_FAILURE
2623 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002624 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002625 * The library has not been previously initialized by psa_crypto_init().
2626 * It is implementation-dependent whether a failure to initialize
2627 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002628 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002629psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002630 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002631 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002632
Gilles Peskinedcd14942018-07-12 00:30:52 +02002633/** Generate an IV for a symmetric encryption operation.
2634 *
2635 * This function generates a random IV (initialization vector), nonce
2636 * or initial counter value for the encryption operation as appropriate
2637 * for the chosen algorithm, key type and key size.
2638 *
2639 * The application must call psa_cipher_encrypt_setup() before
2640 * calling this function.
2641 *
2642 * If this function returns an error status, the operation becomes inactive.
2643 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002644 * \param[in,out] operation Active cipher operation.
2645 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002646 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002647 * \param[out] iv_length On success, the number of bytes of the
2648 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002649 *
2650 * \retval #PSA_SUCCESS
2651 * Success.
2652 * \retval #PSA_ERROR_BAD_STATE
2653 * The operation state is not valid (not started, or IV already set).
2654 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002655 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002656 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2657 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2658 * \retval #PSA_ERROR_HARDWARE_FAILURE
2659 * \retval #PSA_ERROR_TAMPERING_DETECTED
2660 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002661psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
2662 unsigned char *iv,
2663 size_t iv_size,
2664 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002665
Gilles Peskinedcd14942018-07-12 00:30:52 +02002666/** Set the IV for a symmetric encryption or decryption operation.
2667 *
2668 * This function sets the random IV (initialization vector), nonce
2669 * or initial counter value for the encryption or decryption operation.
2670 *
2671 * The application must call psa_cipher_encrypt_setup() before
2672 * calling this function.
2673 *
2674 * If this function returns an error status, the operation becomes inactive.
2675 *
2676 * \note When encrypting, applications should use psa_cipher_generate_iv()
2677 * instead of this function, unless implementing a protocol that requires
2678 * a non-random IV.
2679 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002680 * \param[in,out] operation Active cipher operation.
2681 * \param[in] iv Buffer containing the IV to use.
2682 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002683 *
2684 * \retval #PSA_SUCCESS
2685 * Success.
2686 * \retval #PSA_ERROR_BAD_STATE
2687 * The operation state is not valid (not started, or IV already set).
2688 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002689 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02002690 * or the chosen algorithm does not use an IV.
2691 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2692 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2693 * \retval #PSA_ERROR_HARDWARE_FAILURE
2694 * \retval #PSA_ERROR_TAMPERING_DETECTED
2695 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002696psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
2697 const unsigned char *iv,
2698 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002699
Gilles Peskinedcd14942018-07-12 00:30:52 +02002700/** Encrypt or decrypt a message fragment in an active cipher operation.
2701 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02002702 * Before calling this function, you must:
2703 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
2704 * The choice of setup function determines whether this function
2705 * encrypts or decrypts its input.
2706 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
2707 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002708 *
2709 * If this function returns an error status, the operation becomes inactive.
2710 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002711 * \param[in,out] operation Active cipher operation.
2712 * \param[in] input Buffer containing the message fragment to
2713 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002714 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002715 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002716 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002717 * \param[out] output_length On success, the number of bytes
2718 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002719 *
2720 * \retval #PSA_SUCCESS
2721 * Success.
2722 * \retval #PSA_ERROR_BAD_STATE
2723 * The operation state is not valid (not started, IV required but
2724 * not set, or already completed).
2725 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2726 * The size of the \p output buffer is too small.
2727 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2728 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2729 * \retval #PSA_ERROR_HARDWARE_FAILURE
2730 * \retval #PSA_ERROR_TAMPERING_DETECTED
2731 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002732psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2733 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002734 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002735 unsigned char *output,
2736 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002737 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002738
Gilles Peskinedcd14942018-07-12 00:30:52 +02002739/** Finish encrypting or decrypting a message in a cipher operation.
2740 *
2741 * The application must call psa_cipher_encrypt_setup() or
2742 * psa_cipher_decrypt_setup() before calling this function. The choice
2743 * of setup function determines whether this function encrypts or
2744 * decrypts its input.
2745 *
2746 * This function finishes the encryption or decryption of the message
2747 * formed by concatenating the inputs passed to preceding calls to
2748 * psa_cipher_update().
2749 *
2750 * When this function returns, the operation becomes inactive.
2751 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002752 * \param[in,out] operation Active cipher operation.
2753 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002754 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002755 * \param[out] output_length On success, the number of bytes
2756 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002757 *
2758 * \retval #PSA_SUCCESS
2759 * Success.
2760 * \retval #PSA_ERROR_BAD_STATE
2761 * The operation state is not valid (not started, IV required but
2762 * not set, or already completed).
2763 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2764 * The size of the \p output buffer is too small.
2765 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2766 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2767 * \retval #PSA_ERROR_HARDWARE_FAILURE
2768 * \retval #PSA_ERROR_TAMPERING_DETECTED
2769 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002770psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002771 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002772 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002773 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002774
Gilles Peskinedcd14942018-07-12 00:30:52 +02002775/** Abort a cipher operation.
2776 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002777 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002778 * \p operation structure itself. Once aborted, the operation object
2779 * can be reused for another operation by calling
2780 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002781 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002782 * You may call this function any time after the operation object has
2783 * been initialized by any of the following methods:
2784 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2785 * whether it succeeds or not.
2786 * - Initializing the \c struct to all-bits-zero.
2787 * - Initializing the \c struct to logical zeros, e.g.
2788 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002789 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002790 * In particular, calling psa_cipher_abort() after the operation has been
2791 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2792 * is safe and has no effect.
2793 *
2794 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002795 *
2796 * \retval #PSA_SUCCESS
2797 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002798 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002799 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2800 * \retval #PSA_ERROR_HARDWARE_FAILURE
2801 * \retval #PSA_ERROR_TAMPERING_DETECTED
2802 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002803psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2804
2805/**@}*/
2806
Gilles Peskine3b555712018-03-03 21:27:57 +01002807/** \defgroup aead Authenticated encryption with associated data (AEAD)
2808 * @{
2809 */
2810
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002811/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01002812 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002813 * \param alg An AEAD algorithm
2814 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002815 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002816 *
2817 * \return The tag size for the specified algorithm.
2818 * If the AEAD algorithm does not have an identified
2819 * tag that can be distinguished from the rest of
2820 * the ciphertext, return 0.
2821 * If the AEAD algorithm is not recognized, return 0.
2822 * An implementation may return either 0 or a
2823 * correct size for an AEAD algorithm that it
2824 * recognizes, but does not support.
2825 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002826#define PSA_AEAD_TAG_LENGTH(alg) \
2827 (PSA_ALG_IS_AEAD(alg) ? \
2828 (((alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> PSA_AEAD_TAG_LENGTH_OFFSET) : \
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002829 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01002830
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002831/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002832 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002833 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002834 * \param alg The AEAD algorithm to compute
2835 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002836 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002837 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002838 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002839 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002840 * but not encrypted.
2841 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002842 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002843 * encrypted.
2844 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002845 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002846 * encrypted data. The additional data is not
2847 * part of this output. For algorithms where the
2848 * encrypted data and the authentication tag
2849 * are defined as separate outputs, the
2850 * authentication tag is appended to the
2851 * encrypted data.
2852 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2853 * This must be at least
2854 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2855 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002856 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002857 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002858 *
Gilles Peskine28538492018-07-11 17:34:00 +02002859 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002860 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002861 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002862 * \retval #PSA_ERROR_EMPTY_SLOT
2863 * \retval #PSA_ERROR_NOT_PERMITTED
2864 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002865 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002866 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002867 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002868 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2869 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2870 * \retval #PSA_ERROR_HARDWARE_FAILURE
2871 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002872 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002873 * The library has not been previously initialized by psa_crypto_init().
2874 * It is implementation-dependent whether a failure to initialize
2875 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002876 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002877psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002878 psa_algorithm_t alg,
2879 const uint8_t *nonce,
2880 size_t nonce_length,
2881 const uint8_t *additional_data,
2882 size_t additional_data_length,
2883 const uint8_t *plaintext,
2884 size_t plaintext_length,
2885 uint8_t *ciphertext,
2886 size_t ciphertext_size,
2887 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002888
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002889/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002890 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002891 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002892 * \param alg The AEAD algorithm to compute
2893 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002894 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002895 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002896 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002897 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002898 * but not encrypted.
2899 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002900 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002901 * encrypted. For algorithms where the
2902 * encrypted data and the authentication tag
2903 * are defined as separate inputs, the buffer
2904 * must contain the encrypted data followed
2905 * by the authentication tag.
2906 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002907 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002908 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2909 * This must be at least
2910 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2911 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002912 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03002913 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002914 *
Gilles Peskine28538492018-07-11 17:34:00 +02002915 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002916 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002917 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002918 * \retval #PSA_ERROR_EMPTY_SLOT
2919 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002920 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002921 * \retval #PSA_ERROR_NOT_PERMITTED
2922 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002923 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002924 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002925 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002926 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2927 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2928 * \retval #PSA_ERROR_HARDWARE_FAILURE
2929 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002930 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002931 * The library has not been previously initialized by psa_crypto_init().
2932 * It is implementation-dependent whether a failure to initialize
2933 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002934 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002935psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002936 psa_algorithm_t alg,
2937 const uint8_t *nonce,
2938 size_t nonce_length,
2939 const uint8_t *additional_data,
2940 size_t additional_data_length,
2941 const uint8_t *ciphertext,
2942 size_t ciphertext_length,
2943 uint8_t *plaintext,
2944 size_t plaintext_size,
2945 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002946
2947/**@}*/
2948
Gilles Peskine20035e32018-02-03 22:44:14 +01002949/** \defgroup asymmetric Asymmetric cryptography
2950 * @{
2951 */
2952
2953/**
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002954 * \brief ECDSA signature size for a given curve bit size
Gilles Peskine0189e752018-02-03 23:57:22 +01002955 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002956 * \param curve_bits Curve size in bits.
2957 * \return Signature size in bytes.
Gilles Peskine0189e752018-02-03 23:57:22 +01002958 *
2959 * \note This macro returns a compile-time constant if its argument is one.
Gilles Peskine0189e752018-02-03 23:57:22 +01002960 */
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002961#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
2962 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskine0189e752018-02-03 23:57:22 +01002963
Gilles Peskine0189e752018-02-03 23:57:22 +01002964/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002965 * \brief Sign a hash or short message with a private key.
2966 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002967 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002968 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002969 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2970 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2971 * to determine the hash algorithm to use.
2972 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002973 * \param handle Handle to the key to use for the operation.
2974 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002975 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002976 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002977 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002978 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002979 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002980 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002981 * \param[out] signature_length On success, the number of bytes
2982 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002983 *
Gilles Peskine28538492018-07-11 17:34:00 +02002984 * \retval #PSA_SUCCESS
2985 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002986 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002987 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002988 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002989 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002990 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002991 * \retval #PSA_ERROR_NOT_SUPPORTED
2992 * \retval #PSA_ERROR_INVALID_ARGUMENT
2993 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2994 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2995 * \retval #PSA_ERROR_HARDWARE_FAILURE
2996 * \retval #PSA_ERROR_TAMPERING_DETECTED
2997 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002998 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002999 * The library has not been previously initialized by psa_crypto_init().
3000 * It is implementation-dependent whether a failure to initialize
3001 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003002 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003003psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01003004 psa_algorithm_t alg,
3005 const uint8_t *hash,
3006 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01003007 uint8_t *signature,
3008 size_t signature_size,
3009 size_t *signature_length);
3010
3011/**
3012 * \brief Verify the signature a hash or short message using a public key.
3013 *
Gilles Peskine08bac712018-06-26 16:14:46 +02003014 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02003015 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02003016 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
3017 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3018 * to determine the hash algorithm to use.
3019 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003020 * \param handle Handle to the key to use for the operation.
3021 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003022 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003023 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003024 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02003025 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003026 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003027 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003028 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003029 *
Gilles Peskine28538492018-07-11 17:34:00 +02003030 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01003031 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02003032 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01003033 * The calculation was perfomed successfully, but the passed
3034 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02003035 * \retval #PSA_ERROR_NOT_SUPPORTED
3036 * \retval #PSA_ERROR_INVALID_ARGUMENT
3037 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3038 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3039 * \retval #PSA_ERROR_HARDWARE_FAILURE
3040 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003041 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003042 * The library has not been previously initialized by psa_crypto_init().
3043 * It is implementation-dependent whether a failure to initialize
3044 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003045 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003046psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01003047 psa_algorithm_t alg,
3048 const uint8_t *hash,
3049 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003050 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003051 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003052
Gilles Peskine723feff2018-05-31 20:08:13 +02003053#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
Gilles Peskine072ac562018-06-30 00:21:29 +02003054 (PSA_ALG_IS_RSA_OAEP(alg) ? \
3055 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskine723feff2018-05-31 20:08:13 +02003056 11 /*PKCS#1v1.5*/)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003057
3058/**
3059 * \brief Encrypt a short message with a public key.
3060 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003061 * \param handle Handle to the key to use for the operation.
3062 * It must be a public key or an asymmetric
3063 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003064 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003065 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003066 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003067 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003068 * \param[in] salt A salt or label, if supported by the
3069 * encryption algorithm.
3070 * If the algorithm does not support a
3071 * salt, pass \c NULL.
3072 * If the algorithm supports an optional
3073 * salt and you do not want to pass a salt,
3074 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003075 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003076 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3077 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003078 * \param salt_length Size of the \p salt buffer in bytes.
3079 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003080 * \param[out] output Buffer where the encrypted message is to
3081 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003082 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003083 * \param[out] output_length On success, the number of bytes
3084 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003085 *
Gilles Peskine28538492018-07-11 17:34:00 +02003086 * \retval #PSA_SUCCESS
3087 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003088 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003089 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02003090 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003091 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003092 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003093 * \retval #PSA_ERROR_NOT_SUPPORTED
3094 * \retval #PSA_ERROR_INVALID_ARGUMENT
3095 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3096 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3097 * \retval #PSA_ERROR_HARDWARE_FAILURE
3098 * \retval #PSA_ERROR_TAMPERING_DETECTED
3099 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03003100 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003101 * The library has not been previously initialized by psa_crypto_init().
3102 * It is implementation-dependent whether a failure to initialize
3103 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003104 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003105psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003106 psa_algorithm_t alg,
3107 const uint8_t *input,
3108 size_t input_length,
3109 const uint8_t *salt,
3110 size_t salt_length,
3111 uint8_t *output,
3112 size_t output_size,
3113 size_t *output_length);
3114
3115/**
3116 * \brief Decrypt a short message with a private key.
3117 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003118 * \param handle Handle to the key to use for the operation.
3119 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003120 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003121 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003122 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003123 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003124 * \param[in] salt A salt or label, if supported by the
3125 * encryption algorithm.
3126 * If the algorithm does not support a
3127 * salt, pass \c NULL.
3128 * If the algorithm supports an optional
3129 * salt and you do not want to pass a salt,
3130 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003131 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003132 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3133 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003134 * \param salt_length Size of the \p salt buffer in bytes.
3135 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003136 * \param[out] output Buffer where the decrypted message is to
3137 * be written.
3138 * \param output_size Size of the \c output buffer in bytes.
3139 * \param[out] output_length On success, the number of bytes
3140 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003141 *
Gilles Peskine28538492018-07-11 17:34:00 +02003142 * \retval #PSA_SUCCESS
3143 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003144 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003145 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003146 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003147 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003148 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003149 * \retval #PSA_ERROR_NOT_SUPPORTED
3150 * \retval #PSA_ERROR_INVALID_ARGUMENT
3151 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3152 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3153 * \retval #PSA_ERROR_HARDWARE_FAILURE
3154 * \retval #PSA_ERROR_TAMPERING_DETECTED
3155 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3156 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003157 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003158 * The library has not been previously initialized by psa_crypto_init().
3159 * It is implementation-dependent whether a failure to initialize
3160 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003161 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003162psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003163 psa_algorithm_t alg,
3164 const uint8_t *input,
3165 size_t input_length,
3166 const uint8_t *salt,
3167 size_t salt_length,
3168 uint8_t *output,
3169 size_t output_size,
3170 size_t *output_length);
3171
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003172/**@}*/
3173
Gilles Peskineedd76872018-07-20 17:42:05 +02003174/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02003175 * @{
3176 */
3177
3178/** The type of the state data structure for generators.
3179 *
3180 * Before calling any function on a generator, the application must
3181 * initialize it by any of the following means:
3182 * - Set the structure to all-bits-zero, for example:
3183 * \code
3184 * psa_crypto_generator_t generator;
3185 * memset(&generator, 0, sizeof(generator));
3186 * \endcode
3187 * - Initialize the structure to logical zero values, for example:
3188 * \code
3189 * psa_crypto_generator_t generator = {0};
3190 * \endcode
3191 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
3192 * for example:
3193 * \code
3194 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3195 * \endcode
3196 * - Assign the result of the function psa_crypto_generator_init()
3197 * to the structure, for example:
3198 * \code
3199 * psa_crypto_generator_t generator;
3200 * generator = psa_crypto_generator_init();
3201 * \endcode
3202 *
3203 * This is an implementation-defined \c struct. Applications should not
3204 * make any assumptions about the content of this structure except
3205 * as directed by the documentation of a specific implementation.
3206 */
3207typedef struct psa_crypto_generator_s psa_crypto_generator_t;
3208
3209/** \def PSA_CRYPTO_GENERATOR_INIT
3210 *
3211 * This macro returns a suitable initializer for a generator object
3212 * of type #psa_crypto_generator_t.
3213 */
3214#ifdef __DOXYGEN_ONLY__
3215/* This is an example definition for documentation purposes.
3216 * Implementations should define a suitable value in `crypto_struct.h`.
3217 */
3218#define PSA_CRYPTO_GENERATOR_INIT {0}
3219#endif
3220
3221/** Return an initial value for a generator object.
3222 */
3223static psa_crypto_generator_t psa_crypto_generator_init(void);
3224
3225/** Retrieve the current capacity of a generator.
3226 *
3227 * The capacity of a generator is the maximum number of bytes that it can
3228 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
3229 *
3230 * \param[in] generator The generator to query.
3231 * \param[out] capacity On success, the capacity of the generator.
3232 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003233 * \retval #PSA_SUCCESS
3234 * \retval #PSA_ERROR_BAD_STATE
3235 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02003236 */
3237psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3238 size_t *capacity);
3239
3240/** Read some data from a generator.
3241 *
3242 * This function reads and returns a sequence of bytes from a generator.
3243 * The data that is read is discarded from the generator. The generator's
3244 * capacity is decreased by the number of bytes read.
3245 *
3246 * \param[in,out] generator The generator object to read from.
3247 * \param[out] output Buffer where the generator output will be
3248 * written.
3249 * \param output_length Number of bytes to output.
3250 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003251 * \retval #PSA_SUCCESS
3252 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02003253 * There were fewer than \p output_length bytes
3254 * in the generator. Note that in this case, no
3255 * output is written to the output buffer.
3256 * The generator's capacity is set to 0, thus
3257 * subsequent calls to this function will not
3258 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003259 * \retval #PSA_ERROR_BAD_STATE
3260 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3261 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3262 * \retval #PSA_ERROR_HARDWARE_FAILURE
3263 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003264 */
3265psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
3266 uint8_t *output,
3267 size_t output_length);
3268
3269/** Create a symmetric key from data read from a generator.
3270 *
3271 * This function reads a sequence of bytes from a generator and imports
3272 * these bytes as a key.
3273 * The data that is read is discarded from the generator. The generator's
3274 * capacity is decreased by the number of bytes read.
3275 *
3276 * This function is equivalent to calling #psa_generator_read and
3277 * passing the resulting output to #psa_import_key, but
3278 * if the implementation provides an isolation boundary then
3279 * the key material is not exposed outside the isolation boundary.
3280 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003281 * \param handle Handle to the slot where the key will be stored.
3282 * This must be a valid slot for a key of the chosen
3283 * type: it must have been obtained by calling
3284 * psa_allocate_key() or psa_create_key() with the
3285 * correct \p type and with a maximum size that is
3286 * compatible with \p bits.
3287 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003288 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
3289 * This must be a symmetric key type.
3290 * \param bits Key size in bits.
3291 * \param[in,out] generator The generator object to read from.
3292 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003293 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003294 * Success.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003295 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02003296 * There were fewer than \p output_length bytes
3297 * in the generator. Note that in this case, no
3298 * output is written to the output buffer.
3299 * The generator's capacity is set to 0, thus
3300 * subsequent calls to this function will not
3301 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003302 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003303 * The key type or key size is not supported, either by the
3304 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003305 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01003306 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003307 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003308 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003309 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3310 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3311 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3312 * \retval #PSA_ERROR_HARDWARE_FAILURE
3313 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003314 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003315 * The library has not been previously initialized by psa_crypto_init().
3316 * It is implementation-dependent whether a failure to initialize
3317 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003318 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003319psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003320 psa_key_type_t type,
3321 size_t bits,
3322 psa_crypto_generator_t *generator);
3323
3324/** Abort a generator.
3325 *
3326 * Once a generator has been aborted, its capacity is zero.
3327 * Aborting a generator frees all associated resources except for the
3328 * \c generator structure itself.
3329 *
3330 * This function may be called at any time as long as the generator
3331 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3332 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3333 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3334 * on a generator that has not been set up.
3335 *
3336 * Once aborted, the generator object may be called.
3337 *
3338 * \param[in,out] generator The generator to abort.
3339 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003340 * \retval #PSA_SUCCESS
3341 * \retval #PSA_ERROR_BAD_STATE
3342 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3343 * \retval #PSA_ERROR_HARDWARE_FAILURE
3344 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003345 */
3346psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3347
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003348/** Use the maximum possible capacity for a generator.
3349 *
3350 * Use this value as the capacity argument when setting up a generator
3351 * to indicate that the generator should have the maximum possible capacity.
3352 * The value of the maximum possible capacity depends on the generator
3353 * algorithm.
3354 */
3355#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3356
Gilles Peskineeab56e42018-07-12 17:12:33 +02003357/**@}*/
3358
Gilles Peskineea0fb492018-07-12 17:17:20 +02003359/** \defgroup derivation Key derivation
3360 * @{
3361 */
3362
3363/** Set up a key derivation operation.
3364 *
3365 * A key derivation algorithm takes three inputs: a secret input \p key and
3366 * two non-secret inputs \p label and p salt.
3367 * The result of this function is a byte generator which can
3368 * be used to produce keys and other cryptographic material.
3369 *
3370 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02003371 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
3372 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003373 *
3374 * \param[in,out] generator The generator object to set up. It must
Gilles Peskine92587db2018-09-18 12:12:42 +02003375 * have been initialized to all-bits-zero,
3376 * a logical zero (`{0}`),
3377 * \c PSA_CRYPTO_GENERATOR_INIT or
3378 * psa_crypto_generator_init().
Gilles Peskineae32aac2018-11-30 14:39:32 +01003379 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003380 * \param alg The key derivation algorithm to compute
3381 * (\c PSA_ALG_XXX value such that
3382 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3383 * \param[in] salt Salt to use.
3384 * \param salt_length Size of the \p salt buffer in bytes.
3385 * \param[in] label Label to use.
3386 * \param label_length Size of the \p label buffer in bytes.
3387 * \param capacity The maximum number of bytes that the
3388 * generator will be able to provide.
3389 *
3390 * \retval #PSA_SUCCESS
3391 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003392 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02003393 * \retval #PSA_ERROR_EMPTY_SLOT
3394 * \retval #PSA_ERROR_NOT_PERMITTED
3395 * \retval #PSA_ERROR_INVALID_ARGUMENT
3396 * \c key is not compatible with \c alg,
3397 * or \p capacity is too large for the specified algorithm and key.
3398 * \retval #PSA_ERROR_NOT_SUPPORTED
3399 * \c alg is not supported or is not a key derivation algorithm.
3400 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3401 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3402 * \retval #PSA_ERROR_HARDWARE_FAILURE
3403 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003404 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003405 * The library has not been previously initialized by psa_crypto_init().
3406 * It is implementation-dependent whether a failure to initialize
3407 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003408 */
3409psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003410 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003411 psa_algorithm_t alg,
3412 const uint8_t *salt,
3413 size_t salt_length,
3414 const uint8_t *label,
3415 size_t label_length,
3416 size_t capacity);
3417
Gilles Peskine01d718c2018-09-18 12:01:02 +02003418/** Set up a key agreement operation.
3419 *
3420 * A key agreement algorithm takes two inputs: a private key \p private_key
3421 * a public key \p peer_key.
3422 * The result of this function is a byte generator which can
3423 * be used to produce keys and other cryptographic material.
3424 *
Gilles Peskine211a4362018-10-25 22:22:31 +02003425 * The resulting generator always has the maximum capacity permitted by
3426 * the algorithm.
3427 *
Gilles Peskine01d718c2018-09-18 12:01:02 +02003428 * \param[in,out] generator The generator object to set up. It must
3429 * have been initialized to all-bits-zero,
3430 * a logical zero (`{0}`),
3431 * \c PSA_CRYPTO_GENERATOR_INIT or
3432 * psa_crypto_generator_init().
Gilles Peskineae32aac2018-11-30 14:39:32 +01003433 * \param private_key Handle to the private key to use.
Gilles Peskined171e782018-11-15 17:46:21 +01003434 * \param[in] peer_key Public key of the peer. It must be
3435 * in the same format that psa_import_key()
3436 * accepts. The standard formats for public
3437 * keys are documented in the documentation
3438 * of psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003439 * \param peer_key_length Size of \p peer_key in bytes.
3440 * \param alg The key agreement algorithm to compute
3441 * (\c PSA_ALG_XXX value such that
3442 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
3443 *
3444 * \retval #PSA_SUCCESS
3445 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003446 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02003447 * \retval #PSA_ERROR_EMPTY_SLOT
3448 * \retval #PSA_ERROR_NOT_PERMITTED
3449 * \retval #PSA_ERROR_INVALID_ARGUMENT
3450 * \c private_key is not compatible with \c alg,
3451 * or \p peer_key is not valid for \c alg or not compatible with
3452 * \c private_key.
3453 * \retval #PSA_ERROR_NOT_SUPPORTED
3454 * \c alg is not supported or is not a key derivation algorithm.
3455 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3456 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3457 * \retval #PSA_ERROR_HARDWARE_FAILURE
3458 * \retval #PSA_ERROR_TAMPERING_DETECTED
3459 */
3460psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003461 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003462 const uint8_t *peer_key,
3463 size_t peer_key_length,
3464 psa_algorithm_t alg);
3465
Gilles Peskineea0fb492018-07-12 17:17:20 +02003466/**@}*/
3467
Gilles Peskineedd76872018-07-20 17:42:05 +02003468/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003469 * @{
3470 */
3471
3472/**
3473 * \brief Generate random bytes.
3474 *
3475 * \warning This function **can** fail! Callers MUST check the return status
3476 * and MUST NOT use the content of the output buffer if the return
3477 * status is not #PSA_SUCCESS.
3478 *
3479 * \note To generate a key, use psa_generate_key() instead.
3480 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003481 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003482 * \param output_size Number of bytes to generate and output.
3483 *
Gilles Peskine28538492018-07-11 17:34:00 +02003484 * \retval #PSA_SUCCESS
3485 * \retval #PSA_ERROR_NOT_SUPPORTED
3486 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3487 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3488 * \retval #PSA_ERROR_HARDWARE_FAILURE
3489 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003490 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003491 * The library has not been previously initialized by psa_crypto_init().
3492 * It is implementation-dependent whether a failure to initialize
3493 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003494 */
3495psa_status_t psa_generate_random(uint8_t *output,
3496 size_t output_size);
3497
Gilles Peskine4c317f42018-07-12 01:24:09 +02003498/** Extra parameters for RSA key generation.
3499 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02003500 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02003501 * parameter to psa_generate_key().
3502 */
3503typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02003504 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02003505} psa_generate_key_extra_rsa;
3506
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003507/**
3508 * \brief Generate a key or key pair.
3509 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003510 * \param handle Handle to the slot where the key will be stored.
3511 * This must be a valid slot for a key of the chosen
3512 * type: it must have been obtained by calling
3513 * psa_allocate_key() or psa_create_key() with the
3514 * correct \p type and with a maximum size that is
3515 * compatible with \p bits.
3516 * It must not contain any key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003517 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
3518 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02003519 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003520 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003521 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003522 * default parameters. Implementation that support
3523 * the generation of vendor-specific key types
3524 * that allow extra parameters shall document
3525 * the format of these extra parameters and
3526 * the default values. For standard parameters,
3527 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003528 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003529 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
3530 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003531 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003532 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
3533 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003534 * - For an RSA key (\p type is
3535 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3536 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003537 * specifying the public exponent. The
3538 * default public exponent used when \p extra
3539 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02003540 * \param extra_size Size of the buffer that \p extra
3541 * points to, in bytes. Note that if \p extra is
3542 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003543 *
Gilles Peskine28538492018-07-11 17:34:00 +02003544 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +01003545 * \retval #PSA_ERROR_INVALID_HANDLE
3546 * \retval #PSA_ERROR_OCCUPIED_SLOT
3547 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02003548 * \retval #PSA_ERROR_NOT_SUPPORTED
3549 * \retval #PSA_ERROR_INVALID_ARGUMENT
3550 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3551 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3552 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3553 * \retval #PSA_ERROR_HARDWARE_FAILURE
3554 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003555 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003556 * The library has not been previously initialized by psa_crypto_init().
3557 * It is implementation-dependent whether a failure to initialize
3558 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003559 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003560psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003561 psa_key_type_t type,
3562 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003563 const void *extra,
3564 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003565
3566/**@}*/
3567
Gilles Peskinee59236f2018-01-27 23:32:46 +01003568#ifdef __cplusplus
3569}
3570#endif
3571
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003572/* The file "crypto_sizes.h" contains definitions for size calculation
3573 * macros whose definitions are implementation-specific. */
3574#include "crypto_sizes.h"
3575
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003576/* The file "crypto_struct.h" contains definitions for
3577 * implementation-specific structs that are declared above. */
3578#include "crypto_struct.h"
3579
3580/* The file "crypto_extra.h" contains vendor-specific definitions. This
3581 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003582#include "crypto_extra.h"
3583
3584#endif /* PSA_CRYPTO_H */