blob: 5be1b515fbd7396a996ad90dda2e8b48f1feac93 [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 Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned 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
60/**
61 * \brief Function return status.
62 *
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020063 * This is either #PSA_SUCCESS (which is zero), indicating success,
64 * or a nonzero value indicating that an error occurred. Errors are
65 * encoded as one of the \c PSA_ERROR_xxx values defined here.
Gilles Peskinee59236f2018-01-27 23:32:46 +010066 */
itayzafrirc2a79762018-06-18 16:20:16 +030067typedef int32_t psa_status_t;
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020068
Gilles Peskine0344d812018-12-20 20:09:04 +010069#if !defined(PSA_SUCCESS)
70/* If PSA_SUCCESS is defined, assume that PSA crypto is being used
71 * together with PSA IPC, which also defines the identifier
72 * PSA_SUCCESS. We must not define PSA_SUCCESS ourselves in that case;
73 * the other error code names don't clash. This is a temporary hack
74 * until we unify error reporting in PSA IPC and PSA crypto.
75 *
76 * Note that psa_defs.h must be included before this header!
77 */
itayzafrirc2a79762018-06-18 16:20:16 +030078/** The action was completed successfully. */
79#define PSA_SUCCESS ((psa_status_t)0)
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020080#endif /* !defined(PSA_SUCCESS) */
itayzafrirc2a79762018-06-18 16:20:16 +030081
itayzafrirf26dbfc2018-08-01 16:09:08 +030082/** An error occurred that does not correspond to any defined
83 * failure cause.
84 *
85 * Implementations may use this error code if none of the other standard
86 * error codes are applicable. */
87#define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)1)
88
itayzafrirc2a79762018-06-18 16:20:16 +030089/** The requested operation or a parameter is not supported
90 * by this implementation.
91 *
92 * Implementations should return this error code when an enumeration
93 * parameter such as a key type, algorithm, etc. is not recognized.
94 * If a combination of parameters is recognized and identified as
95 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +030096#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)2)
itayzafrirc2a79762018-06-18 16:20:16 +030097
98/** The requested action is denied by a policy.
99 *
100 * Implementations should return this error code when the parameters
101 * are recognized as valid and supported, and a policy explicitly
102 * denies the requested operation.
103 *
104 * If a subset of the parameters of a function call identify a
105 * forbidden operation, and another subset of the parameters are
106 * not valid or not supported, it is unspecified whether the function
107 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
108 * #PSA_ERROR_INVALID_ARGUMENT. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300109#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)3)
itayzafrirc2a79762018-06-18 16:20:16 +0300110
111/** An output buffer is too small.
112 *
Gilles Peskinebe42f312018-07-13 14:38:15 +0200113 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
itayzafrirc2a79762018-06-18 16:20:16 +0300114 * description to determine a sufficient buffer size.
115 *
116 * Implementations should preferably return this error code only
117 * in cases when performing the operation with a larger output
118 * buffer would succeed. However implementations may return this
119 * error if a function has invalid or unsupported parameters in addition
120 * to the parameters that determine the necessary output buffer size. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300121#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)4)
itayzafrirc2a79762018-06-18 16:20:16 +0300122
123/** A slot is occupied, but must be empty to carry out the
124 * requested action.
125 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100126 * If a handle is invalid, it does not designate an occupied slot.
127 * The error for an invalid handle is #PSA_ERROR_INVALID_HANDLE.
128 */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300129#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)5)
itayzafrirc2a79762018-06-18 16:20:16 +0300130
131/** A slot is empty, but must be occupied to carry out the
132 * requested action.
133 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100134 * If a handle is invalid, it does not designate an empty slot.
135 * The error for an invalid handle is #PSA_ERROR_INVALID_HANDLE.
136 */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300137#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)6)
itayzafrirc2a79762018-06-18 16:20:16 +0300138
139/** The requested action cannot be performed in the current state.
140 *
141 * Multipart operations return this error when one of the
142 * functions is called out of sequence. Refer to the function
143 * descriptions for permitted sequencing of functions.
144 *
145 * Implementations shall not return this error code to indicate
146 * that a key slot is occupied when it needs to be free or vice versa,
147 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
148 * as applicable. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300149#define PSA_ERROR_BAD_STATE ((psa_status_t)7)
itayzafrirc2a79762018-06-18 16:20:16 +0300150
151/** The parameters passed to the function are invalid.
152 *
153 * Implementations may return this error any time a parameter or
154 * combination of parameters are recognized as invalid.
155 *
156 * Implementations shall not return this error code to indicate
157 * that a key slot is occupied when it needs to be free or vice versa,
158 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100159 * as applicable.
160 *
161 * Implementation shall not return this error code to indicate that a
162 * key handle is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
163 * instead.
164 */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300165#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)8)
itayzafrirc2a79762018-06-18 16:20:16 +0300166
167/** There is not enough runtime memory.
168 *
169 * If the action is carried out across multiple security realms, this
170 * error can refer to available memory in any of the security realms. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300171#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)9)
itayzafrirc2a79762018-06-18 16:20:16 +0300172
173/** There is not enough persistent storage.
174 *
175 * Functions that modify the key storage return this error code if
176 * there is insufficient storage space on the host media. In addition,
177 * many functions that do not otherwise access storage may return this
178 * error code if the implementation requires a mandatory log entry for
179 * the requested action and the log storage space is full. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300180#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)10)
itayzafrirc2a79762018-06-18 16:20:16 +0300181
182/** There was a communication failure inside the implementation.
183 *
184 * This can indicate a communication failure between the application
185 * and an external cryptoprocessor or between the cryptoprocessor and
186 * an external volatile or persistent memory. A communication failure
187 * may be transient or permanent depending on the cause.
188 *
189 * \warning If a function returns this error, it is undetermined
190 * whether the requested action has completed or not. Implementations
191 * should return #PSA_SUCCESS on successful completion whenver
192 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
193 * if the requested action was completed successfully in an external
194 * cryptoprocessor but there was a breakdown of communication before
195 * the cryptoprocessor could report the status to the application.
196 */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300197#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)11)
itayzafrirc2a79762018-06-18 16:20:16 +0300198
199/** There was a storage failure that may have led to data loss.
200 *
201 * This error indicates that some persistent storage is corrupted.
202 * It should not be used for a corruption of volatile memory
203 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
204 * between the cryptoprocessor and its external storage (use
205 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
206 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
207 *
208 * Note that a storage failure does not indicate that any data that was
209 * previously read is invalid. However this previously read data may no
210 * longer be readable from storage.
211 *
212 * When a storage failure occurs, it is no longer possible to ensure
213 * the global integrity of the keystore. Depending on the global
214 * integrity guarantees offered by the implementation, access to other
215 * data may or may not fail even if the data is still readable but
216 * its integrity canont be guaranteed.
217 *
218 * Implementations should only use this error code to report a
219 * permanent storage corruption. However application writers should
220 * keep in mind that transient errors while reading the storage may be
221 * reported using this error code. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300222#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)12)
itayzafrirc2a79762018-06-18 16:20:16 +0300223
224/** A hardware failure was detected.
225 *
226 * A hardware failure may be transient or permanent depending on the
227 * cause. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300228#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)13)
itayzafrirc2a79762018-06-18 16:20:16 +0300229
230/** A tampering attempt was detected.
231 *
232 * If an application receives this error code, there is no guarantee
233 * that previously accessed or computed data was correct and remains
234 * confidential. Applications should not perform any security function
235 * and should enter a safe failure state.
236 *
237 * Implementations may return this error code if they detect an invalid
238 * state that cannot happen during normal operation and that indicates
239 * that the implementation's security guarantees no longer hold. Depending
240 * on the implementation architecture and on its security and safety goals,
241 * the implementation may forcibly terminate the application.
242 *
243 * This error code is intended as a last resort when a security breach
244 * is detected and it is unsure whether the keystore data is still
245 * protected. Implementations shall only return this error code
246 * to report an alarm from a tampering detector, to indicate that
247 * the confidentiality of stored data can no longer be guaranteed,
248 * or to indicate that the integrity of previously returned data is now
249 * considered compromised. Implementations shall not use this error code
250 * to indicate a hardware failure that merely makes it impossible to
251 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
252 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
253 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
254 * instead).
255 *
256 * This error indicates an attack against the application. Implementations
257 * shall not return this error code as a consequence of the behavior of
258 * the application itself. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300259#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)14)
itayzafrirc2a79762018-06-18 16:20:16 +0300260
261/** There is not enough entropy to generate random data needed
262 * for the requested action.
263 *
264 * This error indicates a failure of a hardware random generator.
265 * Application writers should note that this error can be returned not
266 * only by functions whose purpose is to generate random data, such
267 * as key, IV or nonce generation, but also by functions that execute
268 * an algorithm with a randomized result, as well as functions that
269 * use randomization of intermediate computations as a countermeasure
270 * to certain attacks.
271 *
272 * Implementations should avoid returning this error after psa_crypto_init()
273 * has succeeded. Implementations should generate sufficient
274 * entropy during initialization and subsequently use a cryptographically
275 * secure pseudorandom generator (PRNG). However implementations may return
276 * this error at any time if a policy requires the PRNG to be reseeded
277 * during normal operation. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300278#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)15)
itayzafrirc2a79762018-06-18 16:20:16 +0300279
280/** The signature, MAC or hash is incorrect.
281 *
282 * Verification functions return this error if the verification
283 * calculations completed successfully, and the value to be verified
284 * was determined to be incorrect.
285 *
286 * If the value to verify has an invalid size, implementations may return
287 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300288#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)16)
itayzafrirc2a79762018-06-18 16:20:16 +0300289
290/** The decrypted padding is incorrect.
291 *
292 * \warning In some protocols, when decrypting data, it is essential that
293 * the behavior of the application does not depend on whether the padding
294 * is correct, down to precise timing. Applications should prefer
295 * protocols that use authenticated encryption rather than plain
296 * encryption. If the application must perform a decryption of
297 * unauthenticated data, the application writer should take care not
298 * to reveal whether the padding is invalid.
299 *
300 * Implementations should strive to make valid and invalid padding
301 * as close as possible to indistinguishable to an external observer.
302 * In particular, the timing of a decryption operation should not
303 * depend on the validity of the padding. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300304#define PSA_ERROR_INVALID_PADDING ((psa_status_t)17)
itayzafrirc2a79762018-06-18 16:20:16 +0300305
Gilles Peskineeab56e42018-07-12 17:12:33 +0200306/** The generator has insufficient capacity left.
307 *
308 * Once a function returns this error, attempts to read from the
309 * generator will always return this error. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300310#define PSA_ERROR_INSUFFICIENT_CAPACITY ((psa_status_t)18)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100311
Gilles Peskinef535eb22018-11-30 14:08:36 +0100312/** The key handle is not valid.
313 */
314#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)19)
315
Gilles Peskinee59236f2018-01-27 23:32:46 +0100316/**
317 * \brief Library initialization.
318 *
319 * Applications must call this function before calling any other
320 * function in this module.
321 *
322 * Applications may call this function more than once. Once a call
323 * succeeds, subsequent calls are guaranteed to succeed.
324 *
itayzafrir18617092018-09-16 12:22:41 +0300325 * If the application calls other functions before calling psa_crypto_init(),
326 * the behavior is undefined. Implementations are encouraged to either perform
327 * the operation as if the library had been initialized or to return
328 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
329 * implementations should not return a success status if the lack of
330 * initialization may have security implications, for example due to improper
331 * seeding of the random number generator.
332 *
Gilles Peskine28538492018-07-11 17:34:00 +0200333 * \retval #PSA_SUCCESS
334 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
335 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
336 * \retval #PSA_ERROR_HARDWARE_FAILURE
337 * \retval #PSA_ERROR_TAMPERING_DETECTED
338 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100339 */
340psa_status_t psa_crypto_init(void);
341
Gilles Peskine2905a7a2018-03-07 16:39:31 +0100342#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
343#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Gilles Peskine0189e752018-02-03 23:57:22 +0100344
Gilles Peskinee59236f2018-01-27 23:32:46 +0100345/**@}*/
346
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100347/** \defgroup crypto_types Key and algorithm types
348 * @{
349 */
350
Gilles Peskine308b91d2018-02-08 09:47:44 +0100351/** \brief Encoding of a key type.
352 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100353typedef uint32_t psa_key_type_t;
354
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100355/** An invalid key type value.
356 *
357 * Zero is not the encoding of any key type.
358 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100359#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100360
361/** Vendor-defined flag
362 *
363 * Key types defined by this standard will never have the
364 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
365 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
366 * respect the bitwise structure used by standard encodings whenever practical.
367 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100368#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100369
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200370#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x70000000)
371#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x40000000)
372#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x50000000)
373#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x60000000)
374#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x70000000)
375
376#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x10000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200377
Gilles Peskinee8779742018-08-10 16:10:56 +0200378/** Whether a key type is vendor-defined. */
379#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
380 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
381
382/** Whether a key type is an unstructured array of bytes.
383 *
384 * This encompasses both symmetric keys and non-key data.
385 */
386#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
387 (((type) & PSA_KEY_TYPE_CATEGORY_MASK & ~(psa_key_type_t)0x10000000) == \
388 PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
389
390/** Whether a key type is asymmetric: either a key pair or a public key. */
391#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
392 (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
393 & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
394 PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
395/** Whether a key type is the public part of a key pair. */
396#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
397 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
398/** Whether a key type is a key pair containing a private part and a public
399 * part. */
400#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
401 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
402/** The key pair type corresponding to a public key type.
403 *
404 * You may also pass a key pair type as \p type, it will be left unchanged.
405 *
406 * \param type A public key type or key pair type.
407 *
408 * \return The corresponding key pair type.
409 * If \p type is not a public key or a key pair,
410 * the return value is undefined.
411 */
412#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
413 ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
414/** The public key type corresponding to a key pair type.
415 *
416 * You may also pass a key pair type as \p type, it will be left unchanged.
417 *
418 * \param type A public key type or key pair type.
419 *
420 * \return The corresponding public key type.
421 * If \p type is not a public key or a key pair,
422 * the return value is undefined.
423 */
424#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
425 ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
Gilles Peskinee8779742018-08-10 16:10:56 +0200426
Gilles Peskine35855962018-04-19 08:39:16 +0200427/** Raw data.
428 *
429 * A "key" of this type cannot be used for any cryptographic operation.
430 * Applications may use this type to store arbitrary data in the keystore. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200431#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x50000001)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100432
Gilles Peskine35855962018-04-19 08:39:16 +0200433/** HMAC key.
434 *
435 * The key policy determines which underlying hash algorithm the key can be
436 * used for.
437 *
438 * HMAC keys should generally have the same size as the underlying hash.
Gilles Peskinebe42f312018-07-13 14:38:15 +0200439 * This size can be calculated with #PSA_HASH_SIZE(\c alg) where
440 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200441#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x51000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200442
Gilles Peskineea0fb492018-07-12 17:17:20 +0200443/** A secret for key derivation.
444 *
445 * The key policy determines which key derivation algorithm the key
446 * can be used for.
447 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200448#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x52000000)
Gilles Peskineea0fb492018-07-12 17:17:20 +0200449
Gilles Peskine35855962018-04-19 08:39:16 +0200450/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
451 *
452 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
453 * 32 bytes (AES-256).
454 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200455#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x40000001)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200456
Gilles Peskine35855962018-04-19 08:39:16 +0200457/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
458 *
459 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
460 * 24 bytes (3-key 3DES).
461 *
462 * Note that single DES and 2-key 3DES are weak and strongly
463 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
464 * is weak and deprecated and should only be used in legacy protocols.
465 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200466#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x40000002)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200467
Gilles Peskine35855962018-04-19 08:39:16 +0200468/** Key for an cipher, AEAD or MAC algorithm based on the
469 * Camellia block cipher. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200470#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x40000003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200471
Gilles Peskine35855962018-04-19 08:39:16 +0200472/** Key for the RC4 stream cipher.
473 *
474 * Note that RC4 is weak and deprecated and should only be used in
475 * legacy protocols. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200476#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x40000004)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100477
Gilles Peskine308b91d2018-02-08 09:47:44 +0100478/** RSA public key. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200479#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x60010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100480/** RSA key pair (private and public key). */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200481#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x70010000)
Gilles Peskine583b55d2018-08-22 18:21:32 +0200482/** Whether a key type is an RSA key (pair or public-only). */
483#define PSA_KEY_TYPE_IS_RSA(type) \
484 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200485
Gilles Peskine06dc2632018-03-08 07:47:25 +0100486/** DSA public key. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200487#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x60020000)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100488/** DSA key pair (private and public key). */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200489#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x70020000)
Gilles Peskine583b55d2018-08-22 18:21:32 +0200490/** Whether a key type is an DSA key (pair or public-only). */
491#define PSA_KEY_TYPE_IS_DSA(type) \
492 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200493
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200494#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x60030000)
495#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x70030000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100496#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200497/** Elliptic curve key pair. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100498#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
499 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200500/** Elliptic curve public key. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100501#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
502 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100503
Gilles Peskined8008d62018-06-29 19:51:51 +0200504/** Whether a key type is an elliptic curve key (pair or public-only). */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100505#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100506 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
507 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine55728b02018-07-16 23:08:16 +0200508#define PSA_KEY_TYPE_IS_ECC_KEYPAIR(type) \
509 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
510 PSA_KEY_TYPE_ECC_KEYPAIR_BASE)
511#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
512 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
513 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100514
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200515/** The type of PSA elliptic curve identifiers. */
516typedef uint16_t psa_ecc_curve_t;
517/** Extract the curve from an elliptic curve key type. */
518#define PSA_KEY_TYPE_GET_CURVE(type) \
519 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
520 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
521 0))
522
523/* The encoding of curve identifiers is currently aligned with the
524 * TLS Supported Groups Registry (formerly known as the
525 * TLS EC Named Curve Registry)
526 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
Gilles Peskine70ce2c62018-08-22 18:21:57 +0200527 * The values are defined by RFC 8422 and RFC 7027. */
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200528#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
529#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
530#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
531#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
532#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
533#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
534#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
535#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
536#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
537#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
538#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
539#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
540#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
541#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
542#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
543#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
544#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
545#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
546#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
547#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
548#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
549#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
550#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
551#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
552#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
553#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
554#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
555#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
556#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
557#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200558
Gilles Peskine7e198532018-03-08 07:50:30 +0100559/** The block size of a block cipher.
560 *
561 * \param type A cipher key type (value of type #psa_key_type_t).
562 *
563 * \return The block size for a block cipher, or 1 for a stream cipher.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200564 * The return value is undefined if \p type is not a supported
Gilles Peskine35855962018-04-19 08:39:16 +0200565 * cipher key type.
566 *
567 * \note It is possible to build stream cipher algorithms on top of a block
568 * cipher, for example CTR mode (#PSA_ALG_CTR).
569 * This macro only takes the key type into account, so it cannot be
570 * used to determine the size of the data that #psa_cipher_update()
571 * might buffer for future processing in general.
Gilles Peskine7e198532018-03-08 07:50:30 +0100572 *
573 * \note This macro returns a compile-time constant if its argument is one.
574 *
575 * \warning This macro may evaluate its argument multiple times.
576 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100577#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100578 ( \
579 (type) == PSA_KEY_TYPE_AES ? 16 : \
580 (type) == PSA_KEY_TYPE_DES ? 8 : \
581 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100582 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100583 0)
584
Gilles Peskine308b91d2018-02-08 09:47:44 +0100585/** \brief Encoding of a cryptographic algorithm.
586 *
587 * For algorithms that can be applied to multiple key types, this type
588 * does not encode the key type. For example, for symmetric ciphers
589 * based on a block cipher, #psa_algorithm_t encodes the block cipher
590 * mode and the padding mode while the block cipher itself is encoded
591 * via #psa_key_type_t.
592 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100593typedef uint32_t psa_algorithm_t;
594
Gilles Peskine98f0a242018-02-06 18:57:29 +0100595#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
596#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
597#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
598#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
599#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
600#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
601#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
602#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
603#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
604#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskinee8f0e3d2018-09-18 11:52:10 +0200605#define PSA_ALG_CATEGORY_KEY_SELECTION ((psa_algorithm_t)0x31000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100606
Gilles Peskine98f0a242018-02-06 18:57:29 +0100607#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
608 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200609
Gilles Peskine308b91d2018-02-08 09:47:44 +0100610/** Whether the specified algorithm is a hash algorithm.
611 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100612 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100613 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200614 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
615 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskine7e198532018-03-08 07:50:30 +0100616 * algorithm identifier.
617 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100618#define PSA_ALG_IS_HASH(alg) \
619 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200620
621/** Whether the specified algorithm is a MAC algorithm.
622 *
623 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
624 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200625 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
626 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200627 * algorithm identifier.
628 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100629#define PSA_ALG_IS_MAC(alg) \
630 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200631
632/** Whether the specified algorithm is a symmetric cipher algorithm.
633 *
634 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
635 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200636 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
637 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200638 * algorithm identifier.
639 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100640#define PSA_ALG_IS_CIPHER(alg) \
641 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200642
643/** Whether the specified algorithm is an authenticated encryption
644 * with associated data (AEAD) algorithm.
645 *
646 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
647 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200648 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
649 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200650 * algorithm identifier.
651 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100652#define PSA_ALG_IS_AEAD(alg) \
653 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200654
655/** Whether the specified algorithm is a public-key signature algorithm.
656 *
657 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
658 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200659 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
660 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200661 * algorithm identifier.
662 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100663#define PSA_ALG_IS_SIGN(alg) \
664 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200665
666/** Whether the specified algorithm is a public-key encryption algorithm.
667 *
668 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
669 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200670 * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
671 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200672 * algorithm identifier.
673 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100674#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
675 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200676
Gilles Peskinee8f0e3d2018-09-18 11:52:10 +0200677#define PSA_ALG_KEY_SELECTION_FLAG ((psa_algorithm_t)0x01000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200678/** Whether the specified algorithm is a key agreement algorithm.
679 *
680 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
681 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200682 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
683 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200684 * algorithm identifier.
685 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100686#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
Gilles Peskinee8f0e3d2018-09-18 11:52:10 +0200687 (((alg) & PSA_ALG_CATEGORY_MASK & ~PSA_ALG_KEY_SELECTION_FLAG) == \
688 PSA_ALG_CATEGORY_KEY_AGREEMENT)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200689
690/** Whether the specified algorithm is a key derivation algorithm.
691 *
692 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
693 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200694 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
695 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200696 * algorithm identifier.
697 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100698#define PSA_ALG_IS_KEY_DERIVATION(alg) \
699 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
700
Gilles Peskinee8f0e3d2018-09-18 11:52:10 +0200701/** Whether the specified algorithm is a key selection algorithm.
702 *
703 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
704 *
705 * \return 1 if \p alg is a key selection algorithm, 0 otherwise.
706 * This macro may return either 0 or 1 if \p alg is not a supported
707 * algorithm identifier.
708 */
709#define PSA_ALG_IS_KEY_SELECTION(alg) \
710 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_SELECTION)
711
Gilles Peskine98f0a242018-02-06 18:57:29 +0100712#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
713#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
714#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
715#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100716#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
717#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskineedd76872018-07-20 17:42:05 +0200718/** SHA2-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100719#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
Gilles Peskineedd76872018-07-20 17:42:05 +0200720/** SHA2-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100721#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
Gilles Peskineedd76872018-07-20 17:42:05 +0200722/** SHA2-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100723#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
Gilles Peskineedd76872018-07-20 17:42:05 +0200724/** SHA2-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100725#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
Gilles Peskineedd76872018-07-20 17:42:05 +0200726/** SHA2-512/224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100727#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
Gilles Peskineedd76872018-07-20 17:42:05 +0200728/** SHA2-512/256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100729#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
Gilles Peskineedd76872018-07-20 17:42:05 +0200730/** SHA3-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100731#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
Gilles Peskineedd76872018-07-20 17:42:05 +0200732/** SHA3-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100733#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
Gilles Peskineedd76872018-07-20 17:42:05 +0200734/** SHA3-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100735#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
Gilles Peskineedd76872018-07-20 17:42:05 +0200736/** SHA3-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100737#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
738
Gilles Peskine8c9def32018-02-08 10:02:12 +0100739#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100740#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200741/** Macro to build an HMAC algorithm.
742 *
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200743 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
Gilles Peskine35855962018-04-19 08:39:16 +0200744 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200745 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200746 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine35855962018-04-19 08:39:16 +0200747 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200748 * \return The corresponding HMAC algorithm.
749 * \return Unspecified if \p alg is not a supported
750 * hash algorithm.
Gilles Peskine35855962018-04-19 08:39:16 +0200751 */
752#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100753 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200754
Gilles Peskine00709fa2018-08-22 18:25:41 +0200755#define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100756 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200757
758/** Whether the specified algorithm is an HMAC algorithm.
759 *
760 * HMAC is a family of MAC algorithms that are based on a hash function.
761 *
762 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
763 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200764 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
765 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200766 * algorithm identifier.
767 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100768#define PSA_ALG_IS_HMAC(alg) \
769 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
770 PSA_ALG_HMAC_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200771
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200772/* In the encoding of a MAC algorithm, the bits corresponding to
773 * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
774 * truncated. As an exception, the value 0 means the untruncated algorithm,
775 * whatever its length is. The length is encoded in 6 bits, so it can
776 * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
777 * to full length is correctly encoded as 0 and any non-trivial truncation
778 * is correctly encoded as a value between 1 and 63. */
Gilles Peskined911eb72018-08-14 15:18:45 +0200779#define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x00003f00)
780#define PSA_MAC_TRUNCATION_OFFSET 8
781
782/** Macro to build a truncated MAC algorithm.
783 *
784 * A truncated MAC algorithm is identical to the corresponding MAC
785 * algorithm except that the MAC value for the truncated algorithm
786 * consists of only the first \p mac_length bytes of the MAC value
787 * for the untruncated algorithm.
788 *
789 * \note This macro may allow constructing algorithm identifiers that
790 * are not valid, either because the specified length is larger
791 * than the untruncated MAC or because the specified length is
792 * smaller than permitted by the implementation.
793 *
794 * \note It is implementation-defined whether a truncated MAC that
795 * is truncated to the same length as the MAC of the untruncated
796 * algorithm is considered identical to the untruncated algorithm
797 * for policy comparison purposes.
798 *
799 * \param alg A MAC algorithm identifier (value of type
800 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
801 * is true). This may be a truncated or untruncated
802 * MAC algorithm.
803 * \param mac_length Desired length of the truncated MAC in bytes.
Gilles Peskine6d72ff92018-08-21 14:55:08 +0200804 * This must be at most the full length of the MAC
805 * and must be at least an implementation-specified
806 * minimum. The implementation-specified minimum
807 * shall not be zero.
Gilles Peskined911eb72018-08-14 15:18:45 +0200808 *
809 * \return The corresponding MAC algorithm with the specified
810 * length.
811 * \return Unspecified if \p alg is not a supported
812 * MAC algorithm or if \p mac_length is too small or
813 * too large for the specified MAC algorithm.
814 */
815#define PSA_ALG_TRUNCATED_MAC(alg, mac_length) \
816 (((alg) & ~PSA_ALG_MAC_TRUNCATION_MASK) | \
817 ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
818
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +0200819/** Macro to build the base MAC algorithm corresponding to a truncated
820 * MAC algorithm.
821 *
822 * \param alg A MAC algorithm identifier (value of type
823 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
824 * is true). This may be a truncated or untruncated
825 * MAC algorithm.
826 *
827 * \return The corresponding base MAC algorithm.
828 * \return Unspecified if \p alg is not a supported
829 * MAC algorithm.
830 */
831#define PSA_ALG_FULL_LENGTH_MAC(alg) \
832 ((alg) & ~PSA_ALG_MAC_TRUNCATION_MASK)
833
Gilles Peskined911eb72018-08-14 15:18:45 +0200834/** Length to which a MAC algorithm is truncated.
835 *
836 * \param alg A MAC algorithm identifier (value of type
837 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
838 * is true).
839 *
840 * \return Length of the truncated MAC in bytes.
841 * \return 0 if \p alg is a non-truncated MAC algorithm.
842 * \return Unspecified if \p alg is not a supported
843 * MAC algorithm.
844 */
845#define PSA_MAC_TRUNCATED_LENGTH(alg) \
846 (((alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
847
Gilles Peskine8c9def32018-02-08 10:02:12 +0100848#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
849#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
850#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
851#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200852
853/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
854 *
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200855 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
856 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200857 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
858 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200859 * algorithm identifier.
860 */
Gilles Peskine9df2dc82018-08-22 18:24:17 +0200861#define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100862 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
863 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100864
Gilles Peskinedaea26f2018-08-21 14:02:45 +0200865#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
866#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100867
Gilles Peskinedcd14942018-07-12 00:30:52 +0200868/** Whether the specified algorithm is a stream cipher.
869 *
870 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
871 * by applying a bitwise-xor with a stream of bytes that is generated
872 * from a key.
873 *
874 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
875 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200876 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
877 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200878 * algorithm identifier or if it is not a symmetric cipher algorithm.
879 */
Moran Pekerbed71a22018-04-22 20:19:20 +0300880#define PSA_ALG_IS_STREAM_CIPHER(alg) \
Gilles Peskinedaea26f2018-08-21 14:02:45 +0200881 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
882 (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
883
884/** The ARC4 stream cipher algorithm.
885 */
886#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800001)
887
888/** The CTR stream cipher mode.
889 *
890 * CTR is a stream cipher which is built from a block cipher.
891 * The underlying block cipher is determined by the key type.
892 * For example, to use AES-128-CTR, use this algorithm with
893 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
894 */
895#define PSA_ALG_CTR ((psa_algorithm_t)0x04c00001)
896
897#define PSA_ALG_CFB ((psa_algorithm_t)0x04c00002)
898
899#define PSA_ALG_OFB ((psa_algorithm_t)0x04c00003)
900
901/** The XTS cipher mode.
902 *
903 * XTS is a cipher mode which is built from a block cipher. It requires at
904 * least one full block of input, but beyond this minimum the input
905 * does not need to be a whole number of blocks.
906 */
907#define PSA_ALG_XTS ((psa_algorithm_t)0x044000ff)
908
909/** The CBC block cipher chaining mode, with no padding.
910 *
911 * The underlying block cipher is determined by the key type.
912 *
913 * This symmetric cipher mode can only be used with messages whose lengths
914 * are whole number of blocks for the chosen block cipher.
915 */
916#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04600100)
917
918/** The CBC block cipher chaining mode with PKCS#7 padding.
919 *
920 * The underlying block cipher is determined by the key type.
921 *
922 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
923 */
924#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04600101)
Moran Pekerbed71a22018-04-22 20:19:20 +0300925
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200926#define PSA_ALG_CCM ((psa_algorithm_t)0x06001001)
927#define PSA_ALG_GCM ((psa_algorithm_t)0x06001002)
928
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200929/* In the encoding of a AEAD algorithm, the bits corresponding to
930 * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
931 * The constants for default lengths follow this encoding.
932 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200933#define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x00003f00)
934#define PSA_AEAD_TAG_LENGTH_OFFSET 8
935
936/** Macro to build a shortened AEAD algorithm.
937 *
938 * A shortened AEAD algorithm is similar to the corresponding AEAD
939 * algorithm, but has an authentication tag that consists of fewer bytes.
940 * Depending on the algorithm, the tag length may affect the calculation
941 * of the ciphertext.
942 *
943 * \param alg A AEAD algorithm identifier (value of type
944 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p alg)
945 * is true).
Gilles Peskine31119812018-08-21 14:47:48 +0200946 * \param tag_length Desired length of the authentication tag in bytes.
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200947 *
948 * \return The corresponding AEAD algorithm with the specified
949 * length.
950 * \return Unspecified if \p alg is not a supported
951 * AEAD algorithm or if \p tag_length is not valid
952 * for the specified AEAD algorithm.
953 */
954#define PSA_ALG_AEAD_WITH_TAG_LENGTH(alg, tag_length) \
955 (((alg) & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) | \
956 ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
957 PSA_ALG_AEAD_TAG_LENGTH_MASK))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100958
Gilles Peskine70f46e12018-08-20 15:07:53 +0200959/** Calculate the corresponding AEAD algorithm with the default tag length.
960 *
961 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
962 * #PSA_ALG_IS_AEAD(\p alg) is true).
963 *
964 * \return The corresponding AEAD algorithm with the default tag length
965 * for that algorithm.
966 */
967#define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg) \
968 ( \
969 PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, PSA_ALG_CCM) \
970 PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, PSA_ALG_GCM) \
971 0)
972#define PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, ref) \
973 PSA_ALG_AEAD_WITH_TAG_LENGTH(alg, 0) == \
974 PSA_ALG_AEAD_WITH_TAG_LENGTH(ref, 0) ? \
975 ref :
976
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200977#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
978/** RSA PKCS#1 v1.5 signature with hashing.
979 *
980 * This is the signature scheme defined by RFC 8017
981 * (PKCS#1: RSA Cryptography Specifications) under the name
982 * RSASSA-PKCS1-v1_5.
983 *
984 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200985 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200986 *
987 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
988 * \return Unspecified if \p alg is not a supported
989 * hash algorithm.
990 */
Gilles Peskinea5926232018-03-28 14:16:50 +0200991#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200992 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
993/** Raw PKCS#1 v1.5 signature.
994 *
995 * The input to this algorithm is the DigestInfo structure used by
996 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
997 * steps 3&ndash;6.
998 */
999#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
Gilles Peskinea5926232018-03-28 14:16:50 +02001000#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001001 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001002
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001003#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
1004/** RSA PSS signature with hashing.
1005 *
1006 * This is the signature scheme defined by RFC 8017
1007 * (PKCS#1: RSA Cryptography Specifications) under the name
Gilles Peskinea4d20bd2018-06-29 23:35:02 +02001008 * RSASSA-PSS, with the message generation function MGF1, and with
1009 * a salt length equal to the length of the hash. The specified
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001010 * hash algorithm is used to hash the input message, to create the
1011 * salted hash, and for the mask generation.
1012 *
1013 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001014 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001015 *
1016 * \return The corresponding RSA PSS signature algorithm.
1017 * \return Unspecified if \p alg is not a supported
1018 * hash algorithm.
1019 */
1020#define PSA_ALG_RSA_PSS(hash_alg) \
1021 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1022#define PSA_ALG_IS_RSA_PSS(alg) \
1023 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
1024
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001025#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
1026/** DSA signature with hashing.
1027 *
1028 * This is the signature scheme defined by FIPS 186-4,
1029 * with a random per-message secret number (*k*).
1030 *
1031 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001032 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001033 *
1034 * \return The corresponding DSA signature algorithm.
1035 * \return Unspecified if \p alg is not a supported
1036 * hash algorithm.
1037 */
1038#define PSA_ALG_DSA(hash_alg) \
1039 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1040#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
1041#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
1042#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
1043 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1044#define PSA_ALG_IS_DSA(alg) \
1045 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
1046 PSA_ALG_DSA_BASE)
1047#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
1048 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +02001049#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
1050 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
1051#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
1052 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001053
1054#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
1055/** ECDSA signature with hashing.
1056 *
1057 * This is the ECDSA signature scheme defined by ANSI X9.62,
1058 * with a random per-message secret number (*k*).
1059 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001060 * The representation of the signature as a byte string consists of
1061 * the concatentation of the signature values *r* and *s*. Each of
1062 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1063 * of the base point of the curve in octets. Each value is represented
1064 * in big-endian order (most significant octet first).
1065 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001066 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001067 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001068 *
1069 * \return The corresponding ECDSA signature algorithm.
1070 * \return Unspecified if \p alg is not a supported
1071 * hash algorithm.
1072 */
1073#define PSA_ALG_ECDSA(hash_alg) \
1074 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1075/** ECDSA signature without hashing.
1076 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001077 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001078 * without specifying a hash algorithm. This algorithm may only be
1079 * used to sign or verify a sequence of bytes that should be an
1080 * already-calculated hash. Note that the input is padded with
1081 * zeros on the left or truncated on the left as required to fit
1082 * the curve size.
1083 */
1084#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
1085#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
1086/** Deterministic ECDSA signature with hashing.
1087 *
1088 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1089 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001090 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1091 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001092 * Note that when this algorithm is used for verification, signatures
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001093 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001094 * same private key are accepted. In other words,
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001095 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1096 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001097 *
1098 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001099 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001100 *
1101 * \return The corresponding deterministic ECDSA signature
1102 * algorithm.
1103 * \return Unspecified if \p alg is not a supported
1104 * hash algorithm.
1105 */
1106#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1107 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1108#define PSA_ALG_IS_ECDSA(alg) \
1109 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
1110 PSA_ALG_ECDSA_BASE)
1111#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
1112 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +02001113#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1114 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1115#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1116 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001117
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001118/** Get the hash used by a hash-and-sign signature algorithm.
1119 *
1120 * A hash-and-sign algorithm is a signature algorithm which is
1121 * composed of two phases: first a hashing phase which does not use
1122 * the key and produces a hash of the input message, then a signing
1123 * phase which only uses the hash and the key and not the message
1124 * itself.
1125 *
1126 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001127 * #PSA_ALG_IS_SIGN(\p alg) is true).
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001128 *
1129 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1130 * algorithm.
1131 * \return 0 if \p alg is a signature algorithm that does not
1132 * follow the hash-and-sign structure.
1133 * \return Unspecified if \p alg is not a signature algorithm or
1134 * if it is not supported by the implementation.
1135 */
1136#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001137 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
1138 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
Gilles Peskine54622ae2018-06-29 22:24:24 +02001139 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001140 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1141 0)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001142
Gilles Peskinedcd14942018-07-12 00:30:52 +02001143/** RSA PKCS#1 v1.5 encryption.
1144 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001145#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001146
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001147#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001148/** RSA OAEP encryption.
1149 *
1150 * This is the encryption scheme defined by RFC 8017
1151 * (PKCS#1: RSA Cryptography Specifications) under the name
1152 * RSAES-OAEP, with the message generation function MGF1.
1153 *
1154 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1155 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1156 * for MGF1.
1157 *
1158 * \return The corresponding RSA OAEP signature algorithm.
1159 * \return Unspecified if \p alg is not a supported
1160 * hash algorithm.
1161 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001162#define PSA_ALG_RSA_OAEP(hash_alg) \
1163 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1164#define PSA_ALG_IS_RSA_OAEP(alg) \
1165 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
Gilles Peskine072ac562018-06-30 00:21:29 +02001166#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1167 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1168 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1169 0)
Gilles Peskined1e8e412018-06-07 09:49:39 +02001170
Gilles Peskinebef7f142018-07-12 17:22:21 +02001171#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100)
1172/** Macro to build an HKDF algorithm.
1173 *
1174 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1175 *
1176 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1177 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1178 *
1179 * \return The corresponding HKDF algorithm.
1180 * \return Unspecified if \p alg is not a supported
1181 * hash algorithm.
1182 */
1183#define PSA_ALG_HKDF(hash_alg) \
1184 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1185/** Whether the specified algorithm is an HKDF algorithm.
1186 *
1187 * HKDF is a family of key derivation algorithms that are based on a hash
1188 * function and the HMAC construction.
1189 *
1190 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1191 *
1192 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1193 * This macro may return either 0 or 1 if \c alg is not a supported
1194 * key derivation algorithm identifier.
1195 */
1196#define PSA_ALG_IS_HKDF(alg) \
1197 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1198#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1199 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1200
Hanno Becker79250c22018-10-09 17:32:46 +01001201#define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t)0x30000200)
1202/** Macro to build a TLS-1.2 PRF algorithm.
1203 *
Hanno Becker2255a362018-11-16 16:05:13 +00001204 * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1205 * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1206 * used with either SHA-256 or SHA-384.
1207 *
1208 * For the application to TLS-1.2, the salt and label arguments passed
1209 * to psa_key_derivation() are what's called 'seed' and 'label' in RFC 5246,
1210 * respectively. For example, for TLS key expansion, the salt is the
1211 * concatenation of ServerHello.Random + ClientHello.Random,
1212 * while the label is "key expansion".
1213 *
Hanno Becker79250c22018-10-09 17:32:46 +01001214 * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the
1215 * TLS 1.2 PRF using HMAC-SHA-256.
1216 *
1217 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1218 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1219 *
1220 * \return The corresponding TLS-1.2 PRF algorithm.
1221 * \return Unspecified if \p alg is not a supported
1222 * hash algorithm.
1223 */
1224#define PSA_ALG_TLS12_PRF(hash_alg) \
1225 (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1226
1227/** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1228 *
Hanno Becker79250c22018-10-09 17:32:46 +01001229 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1230 *
1231 * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1232 * This macro may return either 0 or 1 if \c alg is not a supported
1233 * key derivation algorithm identifier.
1234 */
1235#define PSA_ALG_IS_TLS12_PRF(alg) \
1236 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1237#define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1238 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1239
Hanno Becker8dbfca42018-10-12 11:56:55 +01001240#define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t)0x30000300)
1241/** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1242 *
Hanno Becker2255a362018-11-16 16:05:13 +00001243 * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1244 * from the PreSharedKey (PSK) through the application of padding
1245 * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1246 * The latter is based on HMAC and can be used with either SHA-256
1247 * or SHA-384.
1248 *
1249 * For the application to TLS-1.2, the salt passed to psa_key_derivation()
1250 * (and forwarded to the TLS-1.2 PRF) is the concatenation of the
1251 * ClientHello.Random + ServerHello.Random, while the label is "master secret"
1252 * or "extended master secret".
1253 *
Hanno Becker8dbfca42018-10-12 11:56:55 +01001254 * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the
1255 * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
1256 *
1257 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1258 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1259 *
1260 * \return The corresponding TLS-1.2 PSK to MS algorithm.
1261 * \return Unspecified if \p alg is not a supported
1262 * hash algorithm.
1263 */
1264#define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
1265 (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1266
1267/** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
1268 *
Hanno Becker8dbfca42018-10-12 11:56:55 +01001269 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1270 *
1271 * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
1272 * This macro may return either 0 or 1 if \c alg is not a supported
1273 * key derivation algorithm identifier.
1274 */
1275#define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
1276 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
1277#define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
1278 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1279
Gilles Peskinee8f0e3d2018-09-18 11:52:10 +02001280#define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t)0x010fffff)
1281
1282/** Use a shared secret as is.
1283 *
1284 * Specify this algorithm as the selection component of a key agreement
1285 * to use the raw result of the key agreement as key material.
1286 *
1287 * \warning The raw result of a key agreement algorithm such as finite-field
1288 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
1289 * not be used directly as key material. It can however be used as the secret
1290 * input in a key derivation algorithm.
1291 */
1292#define PSA_ALG_SELECT_RAW ((psa_algorithm_t)0x31000001)
1293
1294#define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
1295 (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
1296
1297#define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
1298 ((alg) & ~PSA_ALG_KEY_DERIVATION_MASK)
Gilles Peskine93098fd2018-09-18 11:54:43 +02001299
1300#define PSA_ALG_FFDH_BASE ((psa_algorithm_t)0x22100000)
1301/** The Diffie-Hellman key agreement algorithm.
1302 *
Gilles Peskine2607bca2018-10-25 22:21:03 +02001303 * This algorithm combines the finite-field Diffie-Hellman (DH) key
1304 * agreement, also known as Diffie-Hellman-Merkle (DHM) key agreement,
1305 * to produce a shared secret from a private key and the peer's
Gilles Peskine93098fd2018-09-18 11:54:43 +02001306 * public key, with a key selection or key derivation algorithm to produce
1307 * one or more shared keys and other shared cryptographic material.
1308 *
Gilles Peskine99d02592018-11-15 17:47:25 +01001309 * The shared secret produced by key agreement and passed as input to the
1310 * derivation or selection algorithm \p kdf_alg is the shared secret
1311 * `g^{ab}` in big-endian format.
1312 * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
1313 * in bits.
Gilles Peskine79dd6222018-10-25 22:22:11 +02001314 *
Gilles Peskine93098fd2018-09-18 11:54:43 +02001315 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1316 * that #PSA_ALG_IS_KEY_DERIVATION(\p hash_alg) is true)
1317 * or a key selection algorithm (\c PSA_ALG_XXX value such
Gilles Peskine19643c52018-11-16 16:45:02 +01001318 * that #PSA_ALG_IS_KEY_SELECTION(\p hash_alg) is true).
Gilles Peskine93098fd2018-09-18 11:54:43 +02001319 *
1320 * \return The Diffie-Hellman algorithm with the specified
1321 * selection or derivation algorithm.
1322 */
1323#define PSA_ALG_FFDH(kdf_alg) \
1324 (PSA_ALG_FFDH_BASE | ((kdf_alg) & PSA_ALG_KEY_DERIVATION_MASK))
1325/** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
1326 *
1327 * This includes every supported key selection or key agreement algorithm
1328 * for the output of the Diffie-Hellman calculation.
1329 *
1330 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1331 *
1332 * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
1333 * This macro may return either 0 or 1 if \c alg is not a supported
1334 * key agreement algorithm identifier.
1335 */
1336#define PSA_ALG_IS_FFDH(alg) \
1337 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH_BASE)
1338
1339#define PSA_ALG_ECDH_BASE ((psa_algorithm_t)0x22200000)
Gilles Peskine2607bca2018-10-25 22:21:03 +02001340/** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
Gilles Peskine93098fd2018-09-18 11:54:43 +02001341 *
1342 * This algorithm combines the elliptic curve Diffie-Hellman key
1343 * agreement to produce a shared secret from a private key and the peer's
1344 * public key, with a key selection or key derivation algorithm to produce
1345 * one or more shared keys and other shared cryptographic material.
1346 *
Gilles Peskine7b5b4a02018-11-14 21:05:10 +01001347 * The shared secret produced by key agreement and passed as input to the
1348 * derivation or selection algorithm \p kdf_alg is the x-coordinate of
Gilles Peskine6c6a0232018-11-15 17:44:43 +01001349 * the shared secret point. It is always `ceiling(m / 8)` bytes long where
1350 * `m` is the bit size associated with the curve, i.e. the bit size of the
1351 * order of the curve's coordinate field. When `m` is not a multiple of 8,
Gilles Peskine7b5b4a02018-11-14 21:05:10 +01001352 * the byte containing the most significant bit of the shared secret
1353 * is padded with zero bits. The byte order is either little-endian
1354 * or big-endian depending on the curve type.
1355 *
1356 * - For Montgomery curves (curve types `PSA_ECC_CURVE_CURVEXXX`),
1357 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1358 * in little-endian byte order.
1359 * The bit size is 448 for Curve448 and 255 for Curve25519.
1360 * - For Weierstrass curves over prime fields (curve types
1361 * `PSA_ECC_CURVE_SECPXXX` and `PSA_ECC_CURVE_BRAINPOOL_PXXX`),
1362 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1363 * in big-endian byte order.
Gilles Peskine6c6a0232018-11-15 17:44:43 +01001364 * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
Gilles Peskine7b5b4a02018-11-14 21:05:10 +01001365 * - For Weierstrass curves over binary fields (curve types
1366 * `PSA_ECC_CURVE_SECTXXX`),
1367 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1368 * in big-endian byte order.
Gilles Peskine6c6a0232018-11-15 17:44:43 +01001369 * The bit size is `m` for the field `F_{2^m}`.
Gilles Peskine79dd6222018-10-25 22:22:11 +02001370 *
Gilles Peskine93098fd2018-09-18 11:54:43 +02001371 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1372 * that #PSA_ALG_IS_KEY_DERIVATION(\p hash_alg) is true)
1373 * or a selection algorithm (\c PSA_ALG_XXX value such
1374 * that #PSA_ALG_IS_KEY_SELECTION(\p hash_alg) is true).
1375 *
1376 * \return The Diffie-Hellman algorithm with the specified
1377 * selection or derivation algorithm.
1378 */
1379#define PSA_ALG_ECDH(kdf_alg) \
1380 (PSA_ALG_ECDH_BASE | ((kdf_alg) & PSA_ALG_KEY_DERIVATION_MASK))
1381/** Whether the specified algorithm is an elliptic curve Diffie-Hellman
1382 * algorithm.
1383 *
1384 * This includes every supported key selection or key agreement algorithm
1385 * for the output of the Diffie-Hellman calculation.
1386 *
1387 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1388 *
1389 * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
1390 * 0 otherwise.
1391 * This macro may return either 0 or 1 if \c alg is not a supported
1392 * key agreement algorithm identifier.
1393 */
1394#define PSA_ALG_IS_ECDH(alg) \
1395 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH_BASE)
1396
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001397/**@}*/
1398
1399/** \defgroup key_management Key management
1400 * @{
1401 */
1402
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001403/** Encoding of key lifetimes.
1404 */
1405typedef uint32_t psa_key_lifetime_t;
1406
1407/** Encoding of identifiers of persistent keys.
1408 */
1409typedef uint32_t psa_key_id_t;
1410
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01001411/** A volatile key only exists as long as the handle to it is not closed.
1412 * The key material is guaranteed to be erased on a power reset.
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001413 */
1414#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1415
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01001416/** The default storage area for persistent keys.
1417 *
1418 * A persistent key remains in storage until it is explicitly destroyed or
1419 * until the corresponding storage area is wiped. This specification does
1420 * not define any mechanism to wipe a storage area, but implementations may
1421 * provide their own mechanism (for example to perform a factory reset,
1422 * to prepare for device refurbishment, or to uninstall an application).
1423 *
1424 * This lifetime value is the default storage area for the calling
1425 * application. Implementations may offer other storage areas designated
1426 * by other lifetime values as implementation-specific extensions.
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001427 */
1428#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1429
Gilles Peskineae32aac2018-11-30 14:39:32 +01001430/** \brief Retrieve the lifetime of an open key.
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001431 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001432 * \param handle Handle to query.
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001433 * \param[out] lifetime On success, the lifetime value.
1434 *
1435 * \retval #PSA_SUCCESS
1436 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001437 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001438 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1439 * \retval #PSA_ERROR_HARDWARE_FAILURE
1440 * \retval #PSA_ERROR_TAMPERING_DETECTED
1441 * \retval #PSA_ERROR_BAD_STATE
1442 * The library has not been previously initialized by psa_crypto_init().
1443 * It is implementation-dependent whether a failure to initialize
1444 * results in this error code.
1445 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001446psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001447 psa_key_lifetime_t *lifetime);
1448
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001449
Gilles Peskinef535eb22018-11-30 14:08:36 +01001450/** Allocate a key slot for a transient key, i.e. a key which is only stored
1451 * in volatile memory.
1452 *
1453 * The allocated key slot and its handle remain valid until the
1454 * application calls psa_close_key() or psa_destroy_key() or until the
1455 * application terminates.
1456 *
1457 * This function takes a key type and maximum size as arguments so that
1458 * the implementation can reserve a corresponding amount of memory.
1459 * Implementations are not required to enforce this limit: if the application
1460 * later tries to create a larger key or a key of a different type, it
1461 * is implementation-defined whether this may succeed.
1462 *
1463 * \param type The type of key that the slot will contain.
1464 * \param max_bits The maximum key size that the slot will contain.
1465 * \param[out] handle On success, a handle to a volatile key slot.
1466 *
1467 * \retval #PSA_SUCCESS
1468 * Success. The application can now use the value of `*handle`
1469 * to access the newly allocated key slot.
1470 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1471 * There was not enough memory, or the maximum number of key slots
1472 * has been reached.
1473 * \retval #PSA_ERROR_INVALID_ARGUMENT
1474 * This implementation does not support this key type.
1475 */
1476
1477psa_status_t psa_allocate_key(psa_key_type_t type,
1478 size_t max_bits,
1479 psa_key_handle_t *handle);
1480
1481/** Open a handle to an existing persistent key.
1482 *
1483 * Open a handle to a key which was previously created with psa_create_key().
1484 *
1485 * \param lifetime The lifetime of the key. This designates a storage
1486 * area where the key material is stored. This must not
1487 * be #PSA_KEY_LIFETIME_VOLATILE.
1488 * \param id The persistent identifier of the key.
1489 * \param[out] handle On success, a handle to a key slot which contains
1490 * the data and metadata loaded from the specified
1491 * persistent location.
1492 *
1493 * \retval #PSA_SUCCESS
1494 * Success. The application can now use the value of `*handle`
1495 * to access the newly allocated key slot.
1496 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1497 * \retval #PSA_ERROR_EMPTY_SLOT
1498 * \retval #PSA_ERROR_INVALID_ARGUMENT
1499 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
1500 * \retval #PSA_ERROR_INVALID_ARGUMENT
1501 * \p id is invalid for the specified lifetime.
1502 * \retval #PSA_ERROR_NOT_SUPPORTED
1503 * \p lifetime is not supported.
1504 * \retval #PSA_ERROR_NOT_PERMITTED
1505 * The specified key exists, but the application does not have the
1506 * permission to access it. Note that this specification does not
1507 * define any way to create such a key, but it may be possible
1508 * through implementation-specific means.
1509 */
1510psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
1511 psa_key_id_t id,
1512 psa_key_handle_t *handle);
1513
1514/** Create a new persistent key slot.
1515 *
1516 * Create a new persistent key slot and return a handle to it. The handle
1517 * remains valid until the application calls psa_close_key() or terminates.
1518 * The application can open the key again with psa_open_key() until it
1519 * removes the key by calling psa_destroy_key().
1520 *
1521 * \param lifetime The lifetime of the key. This designates a storage
1522 * area where the key material is stored. This must not
1523 * be #PSA_KEY_LIFETIME_VOLATILE.
1524 * \param id The persistent identifier of the key.
1525 * \param type The type of key that the slot will contain.
1526 * \param max_bits The maximum key size that the slot will contain.
1527 * \param[out] handle On success, a handle to the newly created key slot.
1528 * When key material is later created in this key slot,
1529 * it will be saved to the specified persistent location.
1530 *
1531 * \retval #PSA_SUCCESS
1532 * Success. The application can now use the value of `*handle`
1533 * to access the newly allocated key slot.
1534 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1535 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1536 * \retval #PSA_ERROR_OCCUPIED_SLOT
1537 * There is already a key with the identifier \p id in the storage
1538 * area designated by \p lifetime.
1539 * \retval #PSA_ERROR_INVALID_ARGUMENT
1540 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
1541 * \retval #PSA_ERROR_INVALID_ARGUMENT
1542 * \p id is invalid for the specified lifetime.
1543 * \retval #PSA_ERROR_NOT_SUPPORTED
1544 * \p lifetime is not supported.
1545 * \retval #PSA_ERROR_NOT_PERMITTED
1546 * \p lifetime is valid, but the application does not have the
1547 * permission to create a key there.
1548 */
1549psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
1550 psa_key_id_t id,
1551 psa_key_type_t type,
1552 size_t max_bits,
1553 psa_key_handle_t *handle);
1554
1555/** Close a key handle.
1556 *
1557 * If the handle designates a volatile key, destroy the key material and
1558 * free all associated resources, just like psa_destroy_key().
1559 *
1560 * If the handle designates a persistent key, free all resources associated
1561 * with the key in volatile memory. The key slot in persistent storage is
1562 * not affected and can be opened again later with psa_open_key().
1563 *
1564 * \param handle The key handle to close.
1565 *
1566 * \retval #PSA_SUCCESS
1567 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +01001568 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +01001569 */
1570psa_status_t psa_close_key(psa_key_handle_t handle);
1571
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001572/**@}*/
1573
1574/** \defgroup import_export Key import and export
1575 * @{
1576 */
1577
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001578/**
1579 * \brief Import a key in binary format.
1580 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +01001581 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +01001582 * documentation of psa_export_public_key() for the format of public keys
1583 * and to the documentation of psa_export_key() for the format for
1584 * other key types.
1585 *
1586 * This specification supports a single format for each key type.
1587 * Implementations may support other formats as long as the standard
1588 * format is supported. Implementations that support other formats
1589 * should ensure that the formats are clearly unambiguous so as to
1590 * minimize the risk that an invalid input is accidentally interpreted
1591 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001592 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001593 * \param handle Handle to the slot where the key will be stored.
1594 * This must be a valid slot for a key of the chosen
1595 * type: it must have been obtained by calling
1596 * psa_allocate_key() or psa_create_key() with the
1597 * correct \p type and with a maximum size that is
1598 * compatible with \p data.
Gilles Peskinef7933932018-10-31 14:07:52 +01001599 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
1600 * import, the key slot will contain a key of this type.
1601 * \param[in] data Buffer containing the key data. The content of this
1602 * buffer is interpreted according to \p type. It must
1603 * contain the format described in the documentation
1604 * of psa_export_key() or psa_export_public_key() for
1605 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001606 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001607 *
Gilles Peskine28538492018-07-11 17:34:00 +02001608 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001609 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01001610 * If the key is persistent, the key material and the key's metadata
1611 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001612 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001613 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001614 * The key type or key size is not supported, either by the
1615 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001616 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +01001617 * The key slot is invalid,
1618 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +02001619 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001620 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001621 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1622 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1623 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +01001624 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02001625 * \retval #PSA_ERROR_HARDWARE_FAILURE
1626 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001627 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001628 * The library has not been previously initialized by psa_crypto_init().
1629 * It is implementation-dependent whether a failure to initialize
1630 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001631 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001632psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001633 psa_key_type_t type,
1634 const uint8_t *data,
1635 size_t data_length);
1636
1637/**
Gilles Peskineae32aac2018-11-30 14:39:32 +01001638 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +02001639 *
1640 * This function destroys the content of the key slot from both volatile
1641 * memory and, if applicable, non-volatile storage. Implementations shall
1642 * make a best effort to ensure that any previous content of the slot is
1643 * unrecoverable.
1644 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001645 * This function also erases any metadata such as policies and frees all
1646 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +02001647 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001648 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001649 *
Gilles Peskine28538492018-07-11 17:34:00 +02001650 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +02001651 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +02001652 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001653 * The slot holds content and cannot be erased because it is
1654 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001655 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001656 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001657 * There was an failure in communication with the cryptoprocessor.
1658 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +02001659 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001660 * The storage is corrupted. Implementations shall make a best effort
1661 * to erase key material even in this stage, however applications
1662 * should be aware that it may be impossible to guarantee that the
1663 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +02001664 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001665 * An unexpected condition which is not a storage corruption or
1666 * a communication failure occurred. The cryptoprocessor may have
1667 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001668 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001669 * The library has not been previously initialized by psa_crypto_init().
1670 * It is implementation-dependent whether a failure to initialize
1671 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001672 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001673psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001674
1675/**
1676 * \brief Get basic metadata about a key.
1677 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001678 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001679 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001680 * This may be a null pointer, in which case the key type
1681 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001682 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +01001683 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +01001684 * is not written.
1685 *
Gilles Peskine28538492018-07-11 17:34:00 +02001686 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +01001687 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001688 * \retval #PSA_ERROR_EMPTY_SLOT
Gilles Peskineae32aac2018-11-30 14:39:32 +01001689 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +02001690 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1691 * \retval #PSA_ERROR_HARDWARE_FAILURE
1692 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001693 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001694 * The library has not been previously initialized by psa_crypto_init().
1695 * It is implementation-dependent whether a failure to initialize
1696 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001697 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001698psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001699 psa_key_type_t *type,
1700 size_t *bits);
1701
1702/**
1703 * \brief Export a key in binary format.
1704 *
1705 * The output of this function can be passed to psa_import_key() to
1706 * create an equivalent object.
1707 *
Gilles Peskinef7933932018-10-31 14:07:52 +01001708 * If the implementation of psa_import_key() supports other formats
1709 * beyond the format specified here, the output from psa_export_key()
1710 * must use the representation specified here, not the original
1711 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001712 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001713 * For standard key types, the output format is as follows:
1714 *
1715 * - For symmetric keys (including MAC keys), the format is the
1716 * raw bytes of the key.
1717 * - For DES, the key data consists of 8 bytes. The parity bits must be
1718 * correct.
1719 * - For Triple-DES, the format is the concatenation of the
1720 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +01001721 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001722 * is the non-encrypted DER encoding of the representation defined by
1723 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
1724 * ```
1725 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001726 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001727 * modulus INTEGER, -- n
1728 * publicExponent INTEGER, -- e
1729 * privateExponent INTEGER, -- d
1730 * prime1 INTEGER, -- p
1731 * prime2 INTEGER, -- q
1732 * exponent1 INTEGER, -- d mod (p-1)
1733 * exponent2 INTEGER, -- d mod (q-1)
1734 * coefficient INTEGER, -- (inverse of q) mod p
1735 * }
1736 * ```
1737 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
1738 * is the non-encrypted DER encoding of the representation used by
Gilles Peskinec6290c02018-08-13 17:24:59 +02001739 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001740 * ```
1741 * DSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001742 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001743 * prime INTEGER, -- p
1744 * subprime INTEGER, -- q
1745 * generator INTEGER, -- g
1746 * public INTEGER, -- y
1747 * private INTEGER, -- x
1748 * }
1749 * ```
1750 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +01001751 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +01001752 * a representation of the private value as a `ceiling(m/8)`-byte string
1753 * where `m` is the bit size associated with the curve, i.e. the bit size
1754 * of the order of the curve's coordinate field. This byte string is
1755 * in little-endian order for Montgomery curves (curve types
1756 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
1757 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
1758 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +01001759 * This is the content of the `privateKey` field of the `ECPrivateKey`
1760 * format defined by RFC 5915.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001761 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
1762 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001763 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001764 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001765 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001766 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001767 * \param[out] data_length On success, the number of bytes
1768 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001769 *
Gilles Peskine28538492018-07-11 17:34:00 +02001770 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +01001771 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001772 * \retval #PSA_ERROR_EMPTY_SLOT
1773 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +01001774 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +02001775 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1776 * The size of the \p data buffer is too small. You can determine a
1777 * sufficient buffer size by calling
1778 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
1779 * where \c type is the key type
1780 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +02001781 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1782 * \retval #PSA_ERROR_HARDWARE_FAILURE
1783 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001784 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001785 * The library has not been previously initialized by psa_crypto_init().
1786 * It is implementation-dependent whether a failure to initialize
1787 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001788 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001789psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001790 uint8_t *data,
1791 size_t data_size,
1792 size_t *data_length);
1793
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001794/**
1795 * \brief Export a public key or the public part of a key pair in binary format.
1796 *
1797 * The output of this function can be passed to psa_import_key() to
1798 * create an object that is equivalent to the public key.
1799 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001800 * The format is the DER representation defined by RFC 5280 as
1801 * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
1802 * specified below.
1803 * ```
1804 * SubjectPublicKeyInfo ::= SEQUENCE {
1805 * algorithm AlgorithmIdentifier,
1806 * subjectPublicKey BIT STRING }
1807 * AlgorithmIdentifier ::= SEQUENCE {
1808 * algorithm OBJECT IDENTIFIER,
1809 * parameters ANY DEFINED BY algorithm OPTIONAL }
1810 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001811 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001812 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY),
1813 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.1 as
1814 * `RSAPublicKey`,
1815 * with the OID `rsaEncryption`,
1816 * and with the parameters `NULL`.
1817 * ```
1818 * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
1819 * rsadsi(113549) pkcs(1) 1 }
1820 * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 }
1821 *
1822 * RSAPublicKey ::= SEQUENCE {
1823 * modulus INTEGER, -- n
1824 * publicExponent INTEGER } -- e
1825 * ```
1826 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
1827 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
1828 * `DSAPublicKey`,
1829 * with the OID `id-dsa`,
1830 * and with the parameters `DSS-Parms`.
1831 * ```
1832 * id-dsa OBJECT IDENTIFIER ::= {
1833 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
1834 *
1835 * Dss-Parms ::= SEQUENCE {
1836 * p INTEGER,
1837 * q INTEGER,
1838 * g INTEGER }
1839 * DSAPublicKey ::= INTEGER -- public key, Y
1840 * ```
1841 * - For elliptic curve public keys (key types for which
1842 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true),
1843 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.5 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001844 * `ECPoint`, which contains the uncompressed
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001845 * representation defined by SEC1 &sect;2.3.3.
1846 * The OID is `id-ecPublicKey`,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001847 * and the parameters must be given as a `namedCurve` OID as specified in
Gilles Peskinec6290c02018-08-13 17:24:59 +02001848 * RFC 5480 &sect;2.1.1.1 or other applicable standards.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001849 * ```
1850 * ansi-X9-62 OBJECT IDENTIFIER ::=
1851 * { iso(1) member-body(2) us(840) 10045 }
1852 * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 }
1853 * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 }
1854 *
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001855 * ECPoint ::= ...
1856 * -- first 8 bits: 0x04;
Gilles Peskine6c6a0232018-11-15 17:44:43 +01001857 * -- then x_P as a `ceiling(m/8)`-byte string, big endian;
1858 * -- then y_P as a `ceiling(m/8)`-byte string, big endian;
1859 * -- where `m` is the bit size associated with the curve,
Gilles Peskine7b5b4a02018-11-14 21:05:10 +01001860 * -- i.e. the bit size of `q` for a curve over `F_q`.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001861 *
1862 * EcpkParameters ::= CHOICE { -- other choices are not allowed
1863 * namedCurve OBJECT IDENTIFIER }
1864 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001865 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001866 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001867 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001868 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001869 * \param[out] data_length On success, the number of bytes
1870 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001871 *
Gilles Peskine28538492018-07-11 17:34:00 +02001872 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +01001873 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001874 * \retval #PSA_ERROR_EMPTY_SLOT
1875 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +02001876 * The key is neither a public key nor a key pair.
1877 * \retval #PSA_ERROR_NOT_SUPPORTED
1878 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1879 * The size of the \p data buffer is too small. You can determine a
1880 * sufficient buffer size by calling
1881 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
1882 * where \c type is the key type
1883 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +02001884 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1885 * \retval #PSA_ERROR_HARDWARE_FAILURE
1886 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001887 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001888 * The library has not been previously initialized by psa_crypto_init().
1889 * It is implementation-dependent whether a failure to initialize
1890 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001891 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001892psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001893 uint8_t *data,
1894 size_t data_size,
1895 size_t *data_length);
1896
1897/**@}*/
1898
1899/** \defgroup policy Key policies
1900 * @{
1901 */
1902
1903/** \brief Encoding of permitted usage on a key. */
1904typedef uint32_t psa_key_usage_t;
1905
Gilles Peskine7e198532018-03-08 07:50:30 +01001906/** Whether the key may be exported.
1907 *
1908 * A public key or the public part of a key pair may always be exported
1909 * regardless of the value of this permission flag.
1910 *
1911 * If a key does not have export permission, implementations shall not
1912 * allow the key to be exported in plain form from the cryptoprocessor,
1913 * whether through psa_export_key() or through a proprietary interface.
1914 * The key may however be exportable in a wrapped form, i.e. in a form
1915 * where it is encrypted by another key.
1916 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001917#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1918
Gilles Peskine7e198532018-03-08 07:50:30 +01001919/** Whether the key may be used to encrypt a message.
1920 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001921 * This flag allows the key to be used for a symmetric encryption operation,
1922 * for an AEAD encryption-and-authentication operation,
1923 * or for an asymmetric encryption operation,
1924 * if otherwise permitted by the key's type and policy.
1925 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001926 * For a key pair, this concerns the public key.
1927 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001928#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +01001929
1930/** Whether the key may be used to decrypt a message.
1931 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001932 * This flag allows the key to be used for a symmetric decryption operation,
1933 * for an AEAD decryption-and-verification operation,
1934 * or for an asymmetric decryption 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_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +01001940
1941/** Whether the key may be used to sign a message.
1942 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001943 * This flag allows the key to be used for a MAC calculation operation
1944 * or for an asymmetric signature operation,
1945 * if otherwise permitted by the key's type and policy.
1946 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001947 * For a key pair, this concerns the private key.
1948 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001949#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +01001950
1951/** Whether the key may be used to verify a message signature.
1952 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001953 * This flag allows the key to be used for a MAC verification operation
1954 * or for an asymmetric signature verification operation,
1955 * if otherwise permitted by by the key's type and policy.
1956 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001957 * For a key pair, this concerns the public key.
1958 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001959#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1960
Gilles Peskineea0fb492018-07-12 17:17:20 +02001961/** Whether the key may be used to derive other keys.
1962 */
1963#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1964
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001965/** The type of the key policy data structure.
1966 *
1967 * This is an implementation-defined \c struct. Applications should not
1968 * make any assumptions about the content of this structure except
1969 * as directed by the documentation of a specific implementation. */
1970typedef struct psa_key_policy_s psa_key_policy_t;
1971
1972/** \brief Initialize a key policy structure to a default that forbids all
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001973 * usage of the key.
1974 *
1975 * \param[out] policy The policy object to initialize.
1976 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001977void psa_key_policy_init(psa_key_policy_t *policy);
1978
Gilles Peskine7e198532018-03-08 07:50:30 +01001979/** \brief Set the standard fields of a policy structure.
1980 *
1981 * Note that this function does not make any consistency check of the
1982 * parameters. The values are only checked when applying the policy to
1983 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001984 *
1985 * \param[out] policy The policy object to modify.
1986 * \param usage The permitted uses for the key.
1987 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +01001988 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001989void psa_key_policy_set_usage(psa_key_policy_t *policy,
1990 psa_key_usage_t usage,
1991 psa_algorithm_t alg);
1992
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001993/** \brief Retrieve the usage field of a policy structure.
1994 *
1995 * \param[in] policy The policy object to query.
1996 *
1997 * \return The permitted uses for a key with this policy.
1998 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02001999psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01002000
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002001/** \brief Retrieve the algorithm field of a policy structure.
2002 *
2003 * \param[in] policy The policy object to query.
2004 *
2005 * \return The permitted algorithm for a key with this policy.
2006 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002007psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01002008
2009/** \brief Set the usage policy on a key slot.
2010 *
2011 * This function must be called on an empty key slot, before importing,
2012 * generating or creating a key in the slot. Changing the policy of an
2013 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +01002014 *
2015 * Implementations may set restrictions on supported key policies
2016 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002017 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002018 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002019 * \param[in] policy The policy object to query.
2020 *
2021 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002022 * Success.
2023 * If the key is persistent, it is implementation-defined whether
2024 * the policy has been saved to persistent storage. Implementations
2025 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002026 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002027 * \retval #PSA_ERROR_OCCUPIED_SLOT
2028 * \retval #PSA_ERROR_NOT_SUPPORTED
2029 * \retval #PSA_ERROR_INVALID_ARGUMENT
2030 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2031 * \retval #PSA_ERROR_HARDWARE_FAILURE
2032 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002033 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002034 * The library has not been previously initialized by psa_crypto_init().
2035 * It is implementation-dependent whether a failure to initialize
2036 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01002037 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002038psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +01002039 const psa_key_policy_t *policy);
2040
Gilles Peskine7e198532018-03-08 07:50:30 +01002041/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002042 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002043 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002044 * \param[out] policy On success, the key's policy.
2045 *
2046 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +01002047 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002048 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2049 * \retval #PSA_ERROR_HARDWARE_FAILURE
2050 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002051 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002052 * The library has not been previously initialized by psa_crypto_init().
2053 * It is implementation-dependent whether a failure to initialize
2054 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +01002055 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002056psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +01002057 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +01002058
2059/**@}*/
2060
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002061/** \defgroup hash Message digests
2062 * @{
2063 */
2064
Gilles Peskine308b91d2018-02-08 09:47:44 +01002065/** The type of the state data structure for multipart hash operations.
2066 *
Gilles Peskine92b30732018-03-03 21:29:30 +01002067 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01002068 * make any assumptions about the content of this structure except
2069 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002070typedef struct psa_hash_operation_s psa_hash_operation_t;
2071
Gilles Peskine308b91d2018-02-08 09:47:44 +01002072/** The size of the output of psa_hash_finish(), in bytes.
2073 *
2074 * This is also the hash size that psa_hash_verify() expects.
2075 *
2076 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002077 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
Gilles Peskinebe42f312018-07-13 14:38:15 +02002078 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
Gilles Peskine35855962018-04-19 08:39:16 +02002079 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +01002080 *
2081 * \return The hash size for the specified hash algorithm.
2082 * If the hash algorithm is not recognized, return 0.
2083 * An implementation may return either 0 or the correct size
2084 * for a hash algorithm that it recognizes, but does not support.
2085 */
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002086#define PSA_HASH_SIZE(alg) \
2087 ( \
Gilles Peskine00709fa2018-08-22 18:25:41 +02002088 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
2089 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
2090 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
2091 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
2092 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
2093 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
2094 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
2095 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
2096 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
2097 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
2098 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
2099 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
2100 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
2101 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
2102 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002103 0)
2104
Gilles Peskine308b91d2018-02-08 09:47:44 +01002105/** Start a multipart hash operation.
2106 *
2107 * The sequence of operations to calculate a hash (message digest)
2108 * is as follows:
2109 * -# Allocate an operation object which will be passed to all the functions
2110 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002111 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002112 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01002113 * of the message each time. The hash that is calculated is the hash
2114 * of the concatenation of these messages in order.
2115 * -# To calculate the hash, call psa_hash_finish().
2116 * To compare the hash with an expected value, call psa_hash_verify().
2117 *
2118 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002119 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01002120 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002121 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002122 * eventually terminate the operation. The following events terminate an
2123 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01002124 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002125 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01002126 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002127 * \param[out] operation The operation object to use.
2128 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
2129 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01002130 *
Gilles Peskine28538492018-07-11 17:34:00 +02002131 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002132 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002133 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002134 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002135 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2136 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2137 * \retval #PSA_ERROR_HARDWARE_FAILURE
2138 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01002139 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002140psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002141 psa_algorithm_t alg);
2142
Gilles Peskine308b91d2018-02-08 09:47:44 +01002143/** Add a message fragment to a multipart hash operation.
2144 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002145 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002146 *
2147 * If this function returns an error status, the operation becomes inactive.
2148 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002149 * \param[in,out] operation Active hash operation.
2150 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002151 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002152 *
Gilles Peskine28538492018-07-11 17:34:00 +02002153 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002154 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002155 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002156 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02002157 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2158 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2159 * \retval #PSA_ERROR_HARDWARE_FAILURE
2160 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01002161 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002162psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2163 const uint8_t *input,
2164 size_t input_length);
2165
Gilles Peskine308b91d2018-02-08 09:47:44 +01002166/** Finish the calculation of the hash of a message.
2167 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002168 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002169 * This function calculates the hash of the message formed by concatenating
2170 * the inputs passed to preceding calls to psa_hash_update().
2171 *
2172 * When this function returns, the operation becomes inactive.
2173 *
2174 * \warning Applications should not call this function if they expect
2175 * a specific value for the hash. Call psa_hash_verify() instead.
2176 * Beware that comparing integrity or authenticity data such as
2177 * hash values with a function such as \c memcmp is risky
2178 * because the time taken by the comparison may leak information
2179 * about the hashed data which could allow an attacker to guess
2180 * a valid hash and thereby bypass security controls.
2181 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002182 * \param[in,out] operation Active hash operation.
2183 * \param[out] hash Buffer where the hash is to be written.
2184 * \param hash_size Size of the \p hash buffer in bytes.
2185 * \param[out] hash_length On success, the number of bytes
2186 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02002187 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02002188 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002189 *
Gilles Peskine28538492018-07-11 17:34:00 +02002190 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002191 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002192 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002193 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02002194 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002195 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002196 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002197 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02002198 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2199 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2200 * \retval #PSA_ERROR_HARDWARE_FAILURE
2201 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01002202 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002203psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2204 uint8_t *hash,
2205 size_t hash_size,
2206 size_t *hash_length);
2207
Gilles Peskine308b91d2018-02-08 09:47:44 +01002208/** Finish the calculation of the hash of a message and compare it with
2209 * an expected value.
2210 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002211 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002212 * This function calculates the hash of the message formed by concatenating
2213 * the inputs passed to preceding calls to psa_hash_update(). It then
2214 * compares the calculated hash with the expected hash passed as a
2215 * parameter to this function.
2216 *
2217 * When this function returns, the operation becomes inactive.
2218 *
Gilles Peskine19067982018-03-20 17:54:53 +01002219 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01002220 * comparison between the actual hash and the expected hash is performed
2221 * in constant time.
2222 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002223 * \param[in,out] operation Active hash operation.
2224 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002225 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002226 *
Gilles Peskine28538492018-07-11 17:34:00 +02002227 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002228 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02002229 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002230 * The hash of the message was calculated successfully, but it
2231 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02002232 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002233 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02002234 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2235 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2236 * \retval #PSA_ERROR_HARDWARE_FAILURE
2237 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01002238 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2240 const uint8_t *hash,
2241 size_t hash_length);
2242
Gilles Peskine308b91d2018-02-08 09:47:44 +01002243/** Abort a hash operation.
2244 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01002245 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002246 * \p operation structure itself. Once aborted, the operation object
2247 * can be reused for another operation by calling
2248 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002249 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002250 * You may call this function any time after the operation object has
2251 * been initialized by any of the following methods:
2252 * - A call to psa_hash_setup(), whether it succeeds or not.
2253 * - Initializing the \c struct to all-bits-zero.
2254 * - Initializing the \c struct to logical zeros, e.g.
2255 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002256 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002257 * In particular, calling psa_hash_abort() after the operation has been
2258 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
2259 * psa_hash_verify() is safe and has no effect.
2260 *
2261 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002262 *
Gilles Peskine28538492018-07-11 17:34:00 +02002263 * \retval #PSA_SUCCESS
2264 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002265 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02002266 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2267 * \retval #PSA_ERROR_HARDWARE_FAILURE
2268 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01002269 */
2270psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002271
2272/**@}*/
2273
Gilles Peskine8c9def32018-02-08 10:02:12 +01002274/** \defgroup MAC Message authentication codes
2275 * @{
2276 */
2277
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002278/** The type of the state data structure for multipart MAC operations.
2279 *
Gilles Peskine92b30732018-03-03 21:29:30 +01002280 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002281 * make any assumptions about the content of this structure except
2282 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002283typedef struct psa_mac_operation_s psa_mac_operation_t;
2284
Gilles Peskine89167cb2018-07-08 20:12:23 +02002285/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002286 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02002287 * This function sets up the calculation of the MAC
2288 * (message authentication code) of a byte string.
2289 * To verify the MAC of a message against an
2290 * expected value, use psa_mac_verify_setup() instead.
2291 *
2292 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002293 * -# Allocate an operation object which will be passed to all the functions
2294 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +02002295 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002296 * The key remains associated with the operation even if the content
2297 * of the key slot changes.
2298 * -# Call psa_mac_update() zero, one or more times, passing a fragment
2299 * of the message each time. The MAC that is calculated is the MAC
2300 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02002301 * -# At the end of the message, call psa_mac_sign_finish() to finish
2302 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002303 *
2304 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +02002305 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002306 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02002307 * After a successful call to psa_mac_sign_setup(), the application must
2308 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002309 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02002310 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002311 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002312 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002313 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002314 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
2315 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002316 *
Gilles Peskine28538492018-07-11 17:34:00 +02002317 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002318 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002319 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002320 * \retval #PSA_ERROR_EMPTY_SLOT
2321 * \retval #PSA_ERROR_NOT_PERMITTED
2322 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002323 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002324 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002325 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002326 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2327 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2328 * \retval #PSA_ERROR_HARDWARE_FAILURE
2329 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002330 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002331 * The library has not been previously initialized by psa_crypto_init().
2332 * It is implementation-dependent whether a failure to initialize
2333 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002334 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02002335psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002336 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002337 psa_algorithm_t alg);
2338
2339/** Start a multipart MAC verification operation.
2340 *
2341 * This function sets up the verification of the MAC
2342 * (message authentication code) of a byte string against an expected value.
2343 *
2344 * The sequence of operations to verify a MAC is as follows:
2345 * -# Allocate an operation object which will be passed to all the functions
2346 * listed here.
2347 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
2348 * The key remains associated with the operation even if the content
2349 * of the key slot changes.
2350 * -# Call psa_mac_update() zero, one or more times, passing a fragment
2351 * of the message each time. The MAC that is calculated is the MAC
2352 * of the concatenation of these messages in order.
2353 * -# At the end of the message, call psa_mac_verify_finish() to finish
2354 * calculating the actual MAC of the message and verify it against
2355 * the expected value.
2356 *
2357 * The application may call psa_mac_abort() at any time after the operation
2358 * has been initialized with psa_mac_verify_setup().
2359 *
2360 * After a successful call to psa_mac_verify_setup(), the application must
2361 * eventually terminate the operation through one of the following methods:
2362 * - A failed call to psa_mac_update().
2363 * - A call to psa_mac_verify_finish() or psa_mac_abort().
2364 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002365 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002366 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002367 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
2368 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02002369 *
Gilles Peskine28538492018-07-11 17:34:00 +02002370 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02002371 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002372 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002373 * \retval #PSA_ERROR_EMPTY_SLOT
2374 * \retval #PSA_ERROR_NOT_PERMITTED
2375 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02002376 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002377 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02002378 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002379 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2380 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2381 * \retval #PSA_ERROR_HARDWARE_FAILURE
2382 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002383 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002384 * The library has not been previously initialized by psa_crypto_init().
2385 * It is implementation-dependent whether a failure to initialize
2386 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02002387 */
2388psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002389 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002390 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002391
Gilles Peskinedcd14942018-07-12 00:30:52 +02002392/** Add a message fragment to a multipart MAC operation.
2393 *
2394 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
2395 * before calling this function.
2396 *
2397 * If this function returns an error status, the operation becomes inactive.
2398 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002399 * \param[in,out] operation Active MAC operation.
2400 * \param[in] input Buffer containing the message fragment to add to
2401 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002402 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002403 *
2404 * \retval #PSA_SUCCESS
2405 * Success.
2406 * \retval #PSA_ERROR_BAD_STATE
2407 * The operation state is not valid (not started, or already completed).
2408 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2409 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2410 * \retval #PSA_ERROR_HARDWARE_FAILURE
2411 * \retval #PSA_ERROR_TAMPERING_DETECTED
2412 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002413psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2414 const uint8_t *input,
2415 size_t input_length);
2416
Gilles Peskinedcd14942018-07-12 00:30:52 +02002417/** Finish the calculation of the MAC of a message.
2418 *
2419 * The application must call psa_mac_sign_setup() before calling this function.
2420 * This function calculates the MAC of the message formed by concatenating
2421 * the inputs passed to preceding calls to psa_mac_update().
2422 *
2423 * When this function returns, the operation becomes inactive.
2424 *
2425 * \warning Applications should not call this function if they expect
2426 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
2427 * Beware that comparing integrity or authenticity data such as
2428 * MAC values with a function such as \c memcmp is risky
2429 * because the time taken by the comparison may leak information
2430 * about the MAC value which could allow an attacker to guess
2431 * a valid MAC and thereby bypass security controls.
2432 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002433 * \param[in,out] operation Active MAC operation.
2434 * \param[out] mac Buffer where the MAC value is to be written.
2435 * \param mac_size Size of the \p mac buffer in bytes.
2436 * \param[out] mac_length On success, the number of bytes
2437 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002438 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02002439 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002440 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02002441 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002442 *
2443 * \retval #PSA_SUCCESS
2444 * Success.
2445 * \retval #PSA_ERROR_BAD_STATE
2446 * The operation state is not valid (not started, or already completed).
2447 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002448 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02002449 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
2450 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2451 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2452 * \retval #PSA_ERROR_HARDWARE_FAILURE
2453 * \retval #PSA_ERROR_TAMPERING_DETECTED
2454 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02002455psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2456 uint8_t *mac,
2457 size_t mac_size,
2458 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002459
Gilles Peskinedcd14942018-07-12 00:30:52 +02002460/** Finish the calculation of the MAC of a message and compare it with
2461 * an expected value.
2462 *
2463 * The application must call psa_mac_verify_setup() before calling this function.
2464 * This function calculates the MAC of the message formed by concatenating
2465 * the inputs passed to preceding calls to psa_mac_update(). It then
2466 * compares the calculated MAC with the expected MAC passed as a
2467 * parameter to this function.
2468 *
2469 * When this function returns, the operation becomes inactive.
2470 *
2471 * \note Implementations shall make the best effort to ensure that the
2472 * comparison between the actual MAC and the expected MAC is performed
2473 * in constant time.
2474 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002475 * \param[in,out] operation Active MAC operation.
2476 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002477 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002478 *
2479 * \retval #PSA_SUCCESS
2480 * The expected MAC is identical to the actual MAC of the message.
2481 * \retval #PSA_ERROR_INVALID_SIGNATURE
2482 * The MAC of the message was calculated successfully, but it
2483 * differs from the expected MAC.
2484 * \retval #PSA_ERROR_BAD_STATE
2485 * The operation state is not valid (not started, or already completed).
2486 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2487 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2488 * \retval #PSA_ERROR_HARDWARE_FAILURE
2489 * \retval #PSA_ERROR_TAMPERING_DETECTED
2490 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02002491psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2492 const uint8_t *mac,
2493 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494
Gilles Peskinedcd14942018-07-12 00:30:52 +02002495/** Abort a MAC operation.
2496 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002497 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002498 * \p operation structure itself. Once aborted, the operation object
2499 * can be reused for another operation by calling
2500 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002501 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002502 * You may call this function any time after the operation object has
2503 * been initialized by any of the following methods:
2504 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
2505 * it succeeds or not.
2506 * - Initializing the \c struct to all-bits-zero.
2507 * - Initializing the \c struct to logical zeros, e.g.
2508 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002509 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002510 * In particular, calling psa_mac_abort() after the operation has been
2511 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
2512 * psa_mac_verify_finish() is safe and has no effect.
2513 *
2514 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002515 *
2516 * \retval #PSA_SUCCESS
2517 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002518 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002519 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2520 * \retval #PSA_ERROR_HARDWARE_FAILURE
2521 * \retval #PSA_ERROR_TAMPERING_DETECTED
2522 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002523psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
2524
2525/**@}*/
2526
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002527/** \defgroup cipher Symmetric ciphers
2528 * @{
2529 */
2530
2531/** The type of the state data structure for multipart cipher operations.
2532 *
2533 * This is an implementation-defined \c struct. Applications should not
2534 * make any assumptions about the content of this structure except
2535 * as directed by the documentation of a specific implementation. */
2536typedef struct psa_cipher_operation_s psa_cipher_operation_t;
2537
2538/** Set the key for a multipart symmetric encryption operation.
2539 *
2540 * The sequence of operations to encrypt a message with a symmetric cipher
2541 * is as follows:
2542 * -# Allocate an operation object which will be passed to all the functions
2543 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02002544 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002545 * The key remains associated with the operation even if the content
2546 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03002547 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002548 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03002549 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002550 * requires a specific IV value.
2551 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
2552 * of the message each time.
2553 * -# Call psa_cipher_finish().
2554 *
2555 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02002556 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002557 *
Gilles Peskinefe119512018-07-08 21:39:34 +02002558 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002559 * eventually terminate the operation. The following events terminate an
2560 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03002561 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002562 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002563 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002564 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002565 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002566 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002567 * \param alg The cipher algorithm to compute
2568 * (\c PSA_ALG_XXX value such that
2569 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002570 *
Gilles Peskine28538492018-07-11 17:34:00 +02002571 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002572 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002573 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002574 * \retval #PSA_ERROR_EMPTY_SLOT
2575 * \retval #PSA_ERROR_NOT_PERMITTED
2576 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002577 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002578 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002579 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002580 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2581 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2582 * \retval #PSA_ERROR_HARDWARE_FAILURE
2583 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002584 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002585 * The library has not been previously initialized by psa_crypto_init().
2586 * It is implementation-dependent whether a failure to initialize
2587 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002588 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002589psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002590 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002591 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002592
2593/** Set the key for a multipart symmetric decryption operation.
2594 *
2595 * The sequence of operations to decrypt a message with a symmetric cipher
2596 * is as follows:
2597 * -# Allocate an operation object which will be passed to all the functions
2598 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02002599 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002600 * The key remains associated with the operation even if the content
2601 * of the key slot changes.
2602 * -# Call psa_cipher_update() with the IV (initialization vector) for the
2603 * decryption. If the IV is prepended to the ciphertext, you can call
2604 * psa_cipher_update() on a buffer containing the IV followed by the
2605 * beginning of the message.
2606 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
2607 * of the message each time.
2608 * -# Call psa_cipher_finish().
2609 *
2610 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02002611 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002612 *
Gilles Peskinefe119512018-07-08 21:39:34 +02002613 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002614 * eventually terminate the operation. The following events terminate an
2615 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002616 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002617 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002618 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002619 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002620 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002621 * \param alg The cipher algorithm to compute
2622 * (\c PSA_ALG_XXX value such that
2623 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002624 *
Gilles Peskine28538492018-07-11 17:34:00 +02002625 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002626 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002627 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002628 * \retval #PSA_ERROR_EMPTY_SLOT
2629 * \retval #PSA_ERROR_NOT_PERMITTED
2630 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002631 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002632 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002633 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002634 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2635 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2636 * \retval #PSA_ERROR_HARDWARE_FAILURE
2637 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002638 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002639 * The library has not been previously initialized by psa_crypto_init().
2640 * It is implementation-dependent whether a failure to initialize
2641 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002642 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002643psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002644 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002645 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002646
Gilles Peskinedcd14942018-07-12 00:30:52 +02002647/** Generate an IV for a symmetric encryption operation.
2648 *
2649 * This function generates a random IV (initialization vector), nonce
2650 * or initial counter value for the encryption operation as appropriate
2651 * for the chosen algorithm, key type and key size.
2652 *
2653 * The application must call psa_cipher_encrypt_setup() before
2654 * calling this function.
2655 *
2656 * If this function returns an error status, the operation becomes inactive.
2657 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002658 * \param[in,out] operation Active cipher operation.
2659 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002660 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002661 * \param[out] iv_length On success, the number of bytes of the
2662 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002663 *
2664 * \retval #PSA_SUCCESS
2665 * Success.
2666 * \retval #PSA_ERROR_BAD_STATE
2667 * The operation state is not valid (not started, or IV already set).
2668 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002669 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002670 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2671 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2672 * \retval #PSA_ERROR_HARDWARE_FAILURE
2673 * \retval #PSA_ERROR_TAMPERING_DETECTED
2674 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002675psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
2676 unsigned char *iv,
2677 size_t iv_size,
2678 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002679
Gilles Peskinedcd14942018-07-12 00:30:52 +02002680/** Set the IV for a symmetric encryption or decryption operation.
2681 *
2682 * This function sets the random IV (initialization vector), nonce
2683 * or initial counter value for the encryption or decryption operation.
2684 *
2685 * The application must call psa_cipher_encrypt_setup() before
2686 * calling this function.
2687 *
2688 * If this function returns an error status, the operation becomes inactive.
2689 *
2690 * \note When encrypting, applications should use psa_cipher_generate_iv()
2691 * instead of this function, unless implementing a protocol that requires
2692 * a non-random IV.
2693 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002694 * \param[in,out] operation Active cipher operation.
2695 * \param[in] iv Buffer containing the IV to use.
2696 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002697 *
2698 * \retval #PSA_SUCCESS
2699 * Success.
2700 * \retval #PSA_ERROR_BAD_STATE
2701 * The operation state is not valid (not started, or IV already set).
2702 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002703 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02002704 * or the chosen algorithm does not use an IV.
2705 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2706 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2707 * \retval #PSA_ERROR_HARDWARE_FAILURE
2708 * \retval #PSA_ERROR_TAMPERING_DETECTED
2709 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002710psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
2711 const unsigned char *iv,
2712 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002713
Gilles Peskinedcd14942018-07-12 00:30:52 +02002714/** Encrypt or decrypt a message fragment in an active cipher operation.
2715 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02002716 * Before calling this function, you must:
2717 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
2718 * The choice of setup function determines whether this function
2719 * encrypts or decrypts its input.
2720 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
2721 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002722 *
2723 * If this function returns an error status, the operation becomes inactive.
2724 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002725 * \param[in,out] operation Active cipher operation.
2726 * \param[in] input Buffer containing the message fragment to
2727 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002728 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002729 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002730 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002731 * \param[out] output_length On success, the number of bytes
2732 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002733 *
2734 * \retval #PSA_SUCCESS
2735 * Success.
2736 * \retval #PSA_ERROR_BAD_STATE
2737 * The operation state is not valid (not started, IV required but
2738 * not set, or already completed).
2739 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2740 * The size of the \p output buffer is too small.
2741 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2742 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2743 * \retval #PSA_ERROR_HARDWARE_FAILURE
2744 * \retval #PSA_ERROR_TAMPERING_DETECTED
2745 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002746psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2747 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002748 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002749 unsigned char *output,
2750 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002751 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002752
Gilles Peskinedcd14942018-07-12 00:30:52 +02002753/** Finish encrypting or decrypting a message in a cipher operation.
2754 *
2755 * The application must call psa_cipher_encrypt_setup() or
2756 * psa_cipher_decrypt_setup() before calling this function. The choice
2757 * of setup function determines whether this function encrypts or
2758 * decrypts its input.
2759 *
2760 * This function finishes the encryption or decryption of the message
2761 * formed by concatenating the inputs passed to preceding calls to
2762 * psa_cipher_update().
2763 *
2764 * When this function returns, the operation becomes inactive.
2765 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002766 * \param[in,out] operation Active cipher operation.
2767 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002768 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002769 * \param[out] output_length On success, the number of bytes
2770 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002771 *
2772 * \retval #PSA_SUCCESS
2773 * Success.
2774 * \retval #PSA_ERROR_BAD_STATE
2775 * The operation state is not valid (not started, IV required but
2776 * not set, or already completed).
2777 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2778 * The size of the \p output buffer is too small.
2779 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2780 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2781 * \retval #PSA_ERROR_HARDWARE_FAILURE
2782 * \retval #PSA_ERROR_TAMPERING_DETECTED
2783 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002784psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002785 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002786 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002787 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002788
Gilles Peskinedcd14942018-07-12 00:30:52 +02002789/** Abort a cipher operation.
2790 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002791 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002792 * \p operation structure itself. Once aborted, the operation object
2793 * can be reused for another operation by calling
2794 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002795 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002796 * You may call this function any time after the operation object has
2797 * been initialized by any of the following methods:
2798 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2799 * whether it succeeds or not.
2800 * - Initializing the \c struct to all-bits-zero.
2801 * - Initializing the \c struct to logical zeros, e.g.
2802 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002803 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002804 * In particular, calling psa_cipher_abort() after the operation has been
2805 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2806 * is safe and has no effect.
2807 *
2808 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002809 *
2810 * \retval #PSA_SUCCESS
2811 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002812 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002813 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2814 * \retval #PSA_ERROR_HARDWARE_FAILURE
2815 * \retval #PSA_ERROR_TAMPERING_DETECTED
2816 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002817psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2818
2819/**@}*/
2820
Gilles Peskine3b555712018-03-03 21:27:57 +01002821/** \defgroup aead Authenticated encryption with associated data (AEAD)
2822 * @{
2823 */
2824
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002825/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01002826 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002827 * \param alg An AEAD algorithm
2828 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002829 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002830 *
2831 * \return The tag size for the specified algorithm.
2832 * If the AEAD algorithm does not have an identified
2833 * tag that can be distinguished from the rest of
2834 * the ciphertext, return 0.
2835 * If the AEAD algorithm is not recognized, return 0.
2836 * An implementation may return either 0 or a
2837 * correct size for an AEAD algorithm that it
2838 * recognizes, but does not support.
2839 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002840#define PSA_AEAD_TAG_LENGTH(alg) \
2841 (PSA_ALG_IS_AEAD(alg) ? \
2842 (((alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> PSA_AEAD_TAG_LENGTH_OFFSET) : \
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002843 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01002844
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002845/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002846 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002847 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002848 * \param alg The AEAD algorithm to compute
2849 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002850 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002851 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002852 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002853 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002854 * but not encrypted.
2855 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002856 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002857 * encrypted.
2858 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002859 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002860 * encrypted data. The additional data is not
2861 * part of this output. For algorithms where the
2862 * encrypted data and the authentication tag
2863 * are defined as separate outputs, the
2864 * authentication tag is appended to the
2865 * encrypted data.
2866 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2867 * This must be at least
2868 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2869 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002870 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002871 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002872 *
Gilles Peskine28538492018-07-11 17:34:00 +02002873 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002874 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002875 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002876 * \retval #PSA_ERROR_EMPTY_SLOT
2877 * \retval #PSA_ERROR_NOT_PERMITTED
2878 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002879 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002880 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002881 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002882 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2883 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2884 * \retval #PSA_ERROR_HARDWARE_FAILURE
2885 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002886 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002887 * The library has not been previously initialized by psa_crypto_init().
2888 * It is implementation-dependent whether a failure to initialize
2889 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002890 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002891psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002892 psa_algorithm_t alg,
2893 const uint8_t *nonce,
2894 size_t nonce_length,
2895 const uint8_t *additional_data,
2896 size_t additional_data_length,
2897 const uint8_t *plaintext,
2898 size_t plaintext_length,
2899 uint8_t *ciphertext,
2900 size_t ciphertext_size,
2901 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002902
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002903/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002904 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002905 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002906 * \param alg The AEAD algorithm to compute
2907 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002908 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002909 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002910 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002911 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002912 * but not encrypted.
2913 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002914 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002915 * encrypted. For algorithms where the
2916 * encrypted data and the authentication tag
2917 * are defined as separate inputs, the buffer
2918 * must contain the encrypted data followed
2919 * by the authentication tag.
2920 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002921 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002922 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2923 * This must be at least
2924 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2925 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002926 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03002927 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002928 *
Gilles Peskine28538492018-07-11 17:34:00 +02002929 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002930 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002931 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002932 * \retval #PSA_ERROR_EMPTY_SLOT
2933 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002934 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002935 * \retval #PSA_ERROR_NOT_PERMITTED
2936 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002937 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002938 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002939 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002940 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2941 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2942 * \retval #PSA_ERROR_HARDWARE_FAILURE
2943 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002944 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002945 * The library has not been previously initialized by psa_crypto_init().
2946 * It is implementation-dependent whether a failure to initialize
2947 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002948 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002949psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002950 psa_algorithm_t alg,
2951 const uint8_t *nonce,
2952 size_t nonce_length,
2953 const uint8_t *additional_data,
2954 size_t additional_data_length,
2955 const uint8_t *ciphertext,
2956 size_t ciphertext_length,
2957 uint8_t *plaintext,
2958 size_t plaintext_size,
2959 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002960
2961/**@}*/
2962
Gilles Peskine20035e32018-02-03 22:44:14 +01002963/** \defgroup asymmetric Asymmetric cryptography
2964 * @{
2965 */
2966
2967/**
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002968 * \brief ECDSA signature size for a given curve bit size
Gilles Peskine0189e752018-02-03 23:57:22 +01002969 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002970 * \param curve_bits Curve size in bits.
2971 * \return Signature size in bytes.
Gilles Peskine0189e752018-02-03 23:57:22 +01002972 *
2973 * \note This macro returns a compile-time constant if its argument is one.
Gilles Peskine0189e752018-02-03 23:57:22 +01002974 */
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002975#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
2976 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskine0189e752018-02-03 23:57:22 +01002977
Gilles Peskine0189e752018-02-03 23:57:22 +01002978/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002979 * \brief Sign a hash or short message with a private key.
2980 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002981 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002982 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002983 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2984 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2985 * to determine the hash algorithm to use.
2986 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002987 * \param handle Handle to the key to use for the operation.
2988 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002989 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002990 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002991 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002992 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002993 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002994 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002995 * \param[out] signature_length On success, the number of bytes
2996 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002997 *
Gilles Peskine28538492018-07-11 17:34:00 +02002998 * \retval #PSA_SUCCESS
2999 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003000 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01003001 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02003002 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01003003 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003004 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003005 * \retval #PSA_ERROR_NOT_SUPPORTED
3006 * \retval #PSA_ERROR_INVALID_ARGUMENT
3007 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3008 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3009 * \retval #PSA_ERROR_HARDWARE_FAILURE
3010 * \retval #PSA_ERROR_TAMPERING_DETECTED
3011 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03003012 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003013 * The library has not been previously initialized by psa_crypto_init().
3014 * It is implementation-dependent whether a failure to initialize
3015 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003016 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003017psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01003018 psa_algorithm_t alg,
3019 const uint8_t *hash,
3020 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01003021 uint8_t *signature,
3022 size_t signature_size,
3023 size_t *signature_length);
3024
3025/**
3026 * \brief Verify the signature a hash or short message using a public key.
3027 *
Gilles Peskine08bac712018-06-26 16:14:46 +02003028 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02003029 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02003030 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
3031 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3032 * to determine the hash algorithm to use.
3033 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003034 * \param handle Handle to the key to use for the operation.
3035 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003036 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003037 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003038 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02003039 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003040 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003041 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003042 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003043 *
Gilles Peskine28538492018-07-11 17:34:00 +02003044 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01003045 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02003046 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01003047 * The calculation was perfomed successfully, but the passed
3048 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02003049 * \retval #PSA_ERROR_NOT_SUPPORTED
3050 * \retval #PSA_ERROR_INVALID_ARGUMENT
3051 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3052 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3053 * \retval #PSA_ERROR_HARDWARE_FAILURE
3054 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003055 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003056 * The library has not been previously initialized by psa_crypto_init().
3057 * It is implementation-dependent whether a failure to initialize
3058 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003059 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003060psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01003061 psa_algorithm_t alg,
3062 const uint8_t *hash,
3063 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003064 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003065 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003066
Gilles Peskine723feff2018-05-31 20:08:13 +02003067#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
Gilles Peskine072ac562018-06-30 00:21:29 +02003068 (PSA_ALG_IS_RSA_OAEP(alg) ? \
3069 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskine723feff2018-05-31 20:08:13 +02003070 11 /*PKCS#1v1.5*/)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003071
3072/**
3073 * \brief Encrypt a short message with a public key.
3074 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003075 * \param handle Handle to the key to use for the operation.
3076 * It must be a public key or an asymmetric
3077 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003078 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003079 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003080 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003081 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003082 * \param[in] salt A salt or label, if supported by the
3083 * encryption algorithm.
3084 * If the algorithm does not support a
3085 * salt, pass \c NULL.
3086 * If the algorithm supports an optional
3087 * salt and you do not want to pass a salt,
3088 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003089 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003090 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3091 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003092 * \param salt_length Size of the \p salt buffer in bytes.
3093 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003094 * \param[out] output Buffer where the encrypted message is to
3095 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003096 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003097 * \param[out] output_length On success, the number of bytes
3098 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003099 *
Gilles Peskine28538492018-07-11 17:34:00 +02003100 * \retval #PSA_SUCCESS
3101 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003102 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003103 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02003104 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003105 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003106 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003107 * \retval #PSA_ERROR_NOT_SUPPORTED
3108 * \retval #PSA_ERROR_INVALID_ARGUMENT
3109 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3110 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3111 * \retval #PSA_ERROR_HARDWARE_FAILURE
3112 * \retval #PSA_ERROR_TAMPERING_DETECTED
3113 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03003114 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003115 * The library has not been previously initialized by psa_crypto_init().
3116 * It is implementation-dependent whether a failure to initialize
3117 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003118 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003119psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003120 psa_algorithm_t alg,
3121 const uint8_t *input,
3122 size_t input_length,
3123 const uint8_t *salt,
3124 size_t salt_length,
3125 uint8_t *output,
3126 size_t output_size,
3127 size_t *output_length);
3128
3129/**
3130 * \brief Decrypt a short message with a private key.
3131 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003132 * \param handle Handle to the key to use for the operation.
3133 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003134 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003135 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003136 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003137 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003138 * \param[in] salt A salt or label, if supported by the
3139 * encryption algorithm.
3140 * If the algorithm does not support a
3141 * salt, pass \c NULL.
3142 * If the algorithm supports an optional
3143 * salt and you do not want to pass a salt,
3144 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003145 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003146 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3147 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003148 * \param salt_length Size of the \p salt buffer in bytes.
3149 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003150 * \param[out] output Buffer where the decrypted message is to
3151 * be written.
3152 * \param output_size Size of the \c output buffer in bytes.
3153 * \param[out] output_length On success, the number of bytes
3154 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003155 *
Gilles Peskine28538492018-07-11 17:34:00 +02003156 * \retval #PSA_SUCCESS
3157 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003158 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003159 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003160 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003161 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003162 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003163 * \retval #PSA_ERROR_NOT_SUPPORTED
3164 * \retval #PSA_ERROR_INVALID_ARGUMENT
3165 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3166 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3167 * \retval #PSA_ERROR_HARDWARE_FAILURE
3168 * \retval #PSA_ERROR_TAMPERING_DETECTED
3169 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3170 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003171 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003172 * The library has not been previously initialized by psa_crypto_init().
3173 * It is implementation-dependent whether a failure to initialize
3174 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003175 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003176psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003177 psa_algorithm_t alg,
3178 const uint8_t *input,
3179 size_t input_length,
3180 const uint8_t *salt,
3181 size_t salt_length,
3182 uint8_t *output,
3183 size_t output_size,
3184 size_t *output_length);
3185
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003186/**@}*/
3187
Gilles Peskineedd76872018-07-20 17:42:05 +02003188/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02003189 * @{
3190 */
3191
3192/** The type of the state data structure for generators.
3193 *
3194 * Before calling any function on a generator, the application must
3195 * initialize it by any of the following means:
3196 * - Set the structure to all-bits-zero, for example:
3197 * \code
3198 * psa_crypto_generator_t generator;
3199 * memset(&generator, 0, sizeof(generator));
3200 * \endcode
3201 * - Initialize the structure to logical zero values, for example:
3202 * \code
3203 * psa_crypto_generator_t generator = {0};
3204 * \endcode
3205 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
3206 * for example:
3207 * \code
3208 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3209 * \endcode
3210 * - Assign the result of the function psa_crypto_generator_init()
3211 * to the structure, for example:
3212 * \code
3213 * psa_crypto_generator_t generator;
3214 * generator = psa_crypto_generator_init();
3215 * \endcode
3216 *
3217 * This is an implementation-defined \c struct. Applications should not
3218 * make any assumptions about the content of this structure except
3219 * as directed by the documentation of a specific implementation.
3220 */
3221typedef struct psa_crypto_generator_s psa_crypto_generator_t;
3222
3223/** \def PSA_CRYPTO_GENERATOR_INIT
3224 *
3225 * This macro returns a suitable initializer for a generator object
3226 * of type #psa_crypto_generator_t.
3227 */
3228#ifdef __DOXYGEN_ONLY__
3229/* This is an example definition for documentation purposes.
3230 * Implementations should define a suitable value in `crypto_struct.h`.
3231 */
3232#define PSA_CRYPTO_GENERATOR_INIT {0}
3233#endif
3234
3235/** Return an initial value for a generator object.
3236 */
3237static psa_crypto_generator_t psa_crypto_generator_init(void);
3238
3239/** Retrieve the current capacity of a generator.
3240 *
3241 * The capacity of a generator is the maximum number of bytes that it can
3242 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
3243 *
3244 * \param[in] generator The generator to query.
3245 * \param[out] capacity On success, the capacity of the generator.
3246 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003247 * \retval #PSA_SUCCESS
3248 * \retval #PSA_ERROR_BAD_STATE
3249 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02003250 */
3251psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3252 size_t *capacity);
3253
3254/** Read some data from a generator.
3255 *
3256 * This function reads and returns a sequence of bytes from a generator.
3257 * The data that is read is discarded from the generator. The generator's
3258 * capacity is decreased by the number of bytes read.
3259 *
3260 * \param[in,out] generator The generator object to read from.
3261 * \param[out] output Buffer where the generator output will be
3262 * written.
3263 * \param output_length Number of bytes to output.
3264 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003265 * \retval #PSA_SUCCESS
3266 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02003267 * There were fewer than \p output_length bytes
3268 * in the generator. Note that in this case, no
3269 * output is written to the output buffer.
3270 * The generator's capacity is set to 0, thus
3271 * subsequent calls to this function will not
3272 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003273 * \retval #PSA_ERROR_BAD_STATE
3274 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3275 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3276 * \retval #PSA_ERROR_HARDWARE_FAILURE
3277 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003278 */
3279psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
3280 uint8_t *output,
3281 size_t output_length);
3282
3283/** Create a symmetric key from data read from a generator.
3284 *
3285 * This function reads a sequence of bytes from a generator and imports
3286 * these bytes as a key.
3287 * The data that is read is discarded from the generator. The generator's
3288 * capacity is decreased by the number of bytes read.
3289 *
3290 * This function is equivalent to calling #psa_generator_read and
3291 * passing the resulting output to #psa_import_key, but
3292 * if the implementation provides an isolation boundary then
3293 * the key material is not exposed outside the isolation boundary.
3294 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003295 * \param handle Handle to the slot where the key will be stored.
3296 * This must be a valid slot for a key of the chosen
3297 * type: it must have been obtained by calling
3298 * psa_allocate_key() or psa_create_key() with the
3299 * correct \p type and with a maximum size that is
3300 * compatible with \p bits.
3301 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003302 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
3303 * This must be a symmetric key type.
3304 * \param bits Key size in bits.
3305 * \param[in,out] generator The generator object to read from.
3306 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003307 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003308 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003309 * If the key is persistent, the key material and the key's metadata
3310 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003311 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02003312 * There were fewer than \p output_length bytes
3313 * in the generator. Note that in this case, no
3314 * output is written to the output buffer.
3315 * The generator's capacity is set to 0, thus
3316 * subsequent calls to this function will not
3317 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003318 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003319 * The key type or key size is not supported, either by the
3320 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003321 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01003322 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003323 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003324 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003325 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3326 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3327 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3328 * \retval #PSA_ERROR_HARDWARE_FAILURE
3329 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003330 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003331 * The library has not been previously initialized by psa_crypto_init().
3332 * It is implementation-dependent whether a failure to initialize
3333 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003334 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003335psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003336 psa_key_type_t type,
3337 size_t bits,
3338 psa_crypto_generator_t *generator);
3339
3340/** Abort a generator.
3341 *
3342 * Once a generator has been aborted, its capacity is zero.
3343 * Aborting a generator frees all associated resources except for the
3344 * \c generator structure itself.
3345 *
3346 * This function may be called at any time as long as the generator
3347 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3348 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3349 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3350 * on a generator that has not been set up.
3351 *
3352 * Once aborted, the generator object may be called.
3353 *
3354 * \param[in,out] generator The generator to abort.
3355 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003356 * \retval #PSA_SUCCESS
3357 * \retval #PSA_ERROR_BAD_STATE
3358 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3359 * \retval #PSA_ERROR_HARDWARE_FAILURE
3360 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003361 */
3362psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3363
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003364/** Use the maximum possible capacity for a generator.
3365 *
3366 * Use this value as the capacity argument when setting up a generator
3367 * to indicate that the generator should have the maximum possible capacity.
3368 * The value of the maximum possible capacity depends on the generator
3369 * algorithm.
3370 */
3371#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3372
Gilles Peskineeab56e42018-07-12 17:12:33 +02003373/**@}*/
3374
Gilles Peskineea0fb492018-07-12 17:17:20 +02003375/** \defgroup derivation Key derivation
3376 * @{
3377 */
3378
3379/** Set up a key derivation operation.
3380 *
3381 * A key derivation algorithm takes three inputs: a secret input \p key and
3382 * two non-secret inputs \p label and p salt.
3383 * The result of this function is a byte generator which can
3384 * be used to produce keys and other cryptographic material.
3385 *
3386 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02003387 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
3388 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003389 *
3390 * \param[in,out] generator The generator object to set up. It must
Gilles Peskine92587db2018-09-18 12:12:42 +02003391 * have been initialized to all-bits-zero,
3392 * a logical zero (`{0}`),
3393 * \c PSA_CRYPTO_GENERATOR_INIT or
3394 * psa_crypto_generator_init().
Gilles Peskineae32aac2018-11-30 14:39:32 +01003395 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003396 * \param alg The key derivation algorithm to compute
3397 * (\c PSA_ALG_XXX value such that
3398 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3399 * \param[in] salt Salt to use.
3400 * \param salt_length Size of the \p salt buffer in bytes.
3401 * \param[in] label Label to use.
3402 * \param label_length Size of the \p label buffer in bytes.
3403 * \param capacity The maximum number of bytes that the
3404 * generator will be able to provide.
3405 *
3406 * \retval #PSA_SUCCESS
3407 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003408 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02003409 * \retval #PSA_ERROR_EMPTY_SLOT
3410 * \retval #PSA_ERROR_NOT_PERMITTED
3411 * \retval #PSA_ERROR_INVALID_ARGUMENT
3412 * \c key is not compatible with \c alg,
3413 * or \p capacity is too large for the specified algorithm and key.
3414 * \retval #PSA_ERROR_NOT_SUPPORTED
3415 * \c alg is not supported or is not a key derivation algorithm.
3416 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3417 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3418 * \retval #PSA_ERROR_HARDWARE_FAILURE
3419 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003420 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003421 * The library has not been previously initialized by psa_crypto_init().
3422 * It is implementation-dependent whether a failure to initialize
3423 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003424 */
3425psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003426 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003427 psa_algorithm_t alg,
3428 const uint8_t *salt,
3429 size_t salt_length,
3430 const uint8_t *label,
3431 size_t label_length,
3432 size_t capacity);
3433
Gilles Peskine01d718c2018-09-18 12:01:02 +02003434/** Set up a key agreement operation.
3435 *
3436 * A key agreement algorithm takes two inputs: a private key \p private_key
3437 * a public key \p peer_key.
3438 * The result of this function is a byte generator which can
3439 * be used to produce keys and other cryptographic material.
3440 *
Gilles Peskine211a4362018-10-25 22:22:31 +02003441 * The resulting generator always has the maximum capacity permitted by
3442 * the algorithm.
3443 *
Gilles Peskine01d718c2018-09-18 12:01:02 +02003444 * \param[in,out] generator The generator object to set up. It must
3445 * have been initialized to all-bits-zero,
3446 * a logical zero (`{0}`),
3447 * \c PSA_CRYPTO_GENERATOR_INIT or
3448 * psa_crypto_generator_init().
Gilles Peskineae32aac2018-11-30 14:39:32 +01003449 * \param private_key Handle to the private key to use.
Gilles Peskined171e782018-11-15 17:46:21 +01003450 * \param[in] peer_key Public key of the peer. It must be
3451 * in the same format that psa_import_key()
3452 * accepts. The standard formats for public
3453 * keys are documented in the documentation
3454 * of psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003455 * \param peer_key_length Size of \p peer_key in bytes.
3456 * \param alg The key agreement algorithm to compute
3457 * (\c PSA_ALG_XXX value such that
3458 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
3459 *
3460 * \retval #PSA_SUCCESS
3461 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003462 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02003463 * \retval #PSA_ERROR_EMPTY_SLOT
3464 * \retval #PSA_ERROR_NOT_PERMITTED
3465 * \retval #PSA_ERROR_INVALID_ARGUMENT
3466 * \c private_key is not compatible with \c alg,
3467 * or \p peer_key is not valid for \c alg or not compatible with
3468 * \c private_key.
3469 * \retval #PSA_ERROR_NOT_SUPPORTED
3470 * \c alg is not supported or is not a key derivation algorithm.
3471 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3472 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3473 * \retval #PSA_ERROR_HARDWARE_FAILURE
3474 * \retval #PSA_ERROR_TAMPERING_DETECTED
3475 */
3476psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003477 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003478 const uint8_t *peer_key,
3479 size_t peer_key_length,
3480 psa_algorithm_t alg);
3481
Gilles Peskineea0fb492018-07-12 17:17:20 +02003482/**@}*/
3483
Gilles Peskineedd76872018-07-20 17:42:05 +02003484/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003485 * @{
3486 */
3487
3488/**
3489 * \brief Generate random bytes.
3490 *
3491 * \warning This function **can** fail! Callers MUST check the return status
3492 * and MUST NOT use the content of the output buffer if the return
3493 * status is not #PSA_SUCCESS.
3494 *
3495 * \note To generate a key, use psa_generate_key() instead.
3496 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003497 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003498 * \param output_size Number of bytes to generate and output.
3499 *
Gilles Peskine28538492018-07-11 17:34:00 +02003500 * \retval #PSA_SUCCESS
3501 * \retval #PSA_ERROR_NOT_SUPPORTED
3502 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3503 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3504 * \retval #PSA_ERROR_HARDWARE_FAILURE
3505 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003506 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003507 * The library has not been previously initialized by psa_crypto_init().
3508 * It is implementation-dependent whether a failure to initialize
3509 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003510 */
3511psa_status_t psa_generate_random(uint8_t *output,
3512 size_t output_size);
3513
Gilles Peskine4c317f42018-07-12 01:24:09 +02003514/** Extra parameters for RSA key generation.
3515 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02003516 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02003517 * parameter to psa_generate_key().
3518 */
3519typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02003520 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02003521} psa_generate_key_extra_rsa;
3522
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003523/**
3524 * \brief Generate a key or key pair.
3525 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003526 * \param handle Handle to the slot where the key will be stored.
3527 * This must be a valid slot for a key of the chosen
3528 * type: it must have been obtained by calling
3529 * psa_allocate_key() or psa_create_key() with the
3530 * correct \p type and with a maximum size that is
3531 * compatible with \p bits.
3532 * It must not contain any key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003533 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
3534 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02003535 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003536 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003537 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003538 * default parameters. Implementation that support
3539 * the generation of vendor-specific key types
3540 * that allow extra parameters shall document
3541 * the format of these extra parameters and
3542 * the default values. For standard parameters,
3543 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003544 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003545 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
3546 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003547 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003548 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
3549 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003550 * - For an RSA key (\p type is
3551 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3552 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003553 * specifying the public exponent. The
3554 * default public exponent used when \p extra
3555 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02003556 * \param extra_size Size of the buffer that \p extra
3557 * points to, in bytes. Note that if \p extra is
3558 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003559 *
Gilles Peskine28538492018-07-11 17:34:00 +02003560 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003561 * Success.
3562 * If the key is persistent, the key material and the key's metadata
3563 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003564 * \retval #PSA_ERROR_INVALID_HANDLE
3565 * \retval #PSA_ERROR_OCCUPIED_SLOT
3566 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02003567 * \retval #PSA_ERROR_NOT_SUPPORTED
3568 * \retval #PSA_ERROR_INVALID_ARGUMENT
3569 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3570 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3571 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3572 * \retval #PSA_ERROR_HARDWARE_FAILURE
3573 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003574 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003575 * The library has not been previously initialized by psa_crypto_init().
3576 * It is implementation-dependent whether a failure to initialize
3577 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003578 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003579psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003580 psa_key_type_t type,
3581 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003582 const void *extra,
3583 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003584
3585/**@}*/
3586
Gilles Peskinee59236f2018-01-27 23:32:46 +01003587#ifdef __cplusplus
3588}
3589#endif
3590
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003591/* The file "crypto_sizes.h" contains definitions for size calculation
3592 * macros whose definitions are implementation-specific. */
3593#include "crypto_sizes.h"
3594
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003595/* The file "crypto_struct.h" contains definitions for
3596 * implementation-specific structs that are declared above. */
3597#include "crypto_struct.h"
3598
3599/* The file "crypto_extra.h" contains vendor-specific definitions. This
3600 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003601#include "crypto_extra.h"
3602
3603#endif /* PSA_CRYPTO_H */